Skip to content

Commit

Permalink
Deprecate extra in favor of contexts (#1435)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored May 15, 2023
1 parent 1314516 commit 62de927
Show file tree
Hide file tree
Showing 20 changed files with 55 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### Breaking Changes

- Android `minSdkVersion` is now 19 (Flutter already defines 19-20 as best effort)
- Deprecate `extra` in favor of `contexts` ([#1435](https://github.com/getsentry/sentry-dart/pull/1435))

## 7.5.2

Expand Down
2 changes: 2 additions & 0 deletions dart/example/bin/event_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ final event = SentryEvent(
environment: 'Test',
message: SentryMessage('This is an example Dart event.'),
tags: const <String, String>{'project-id': '7371'},
// ignore: deprecated_member_use
extra: const <String, String>{'section': '1'},
// fingerprint: const <String>['example-dart'], fingerprint forces events to group together
user: SentryUser(
id: '800',
username: 'first-user',
email: '[email protected]',
// ipAddress: '127.0.0.1', sendDefaultPii feature is enabled
// ignore: deprecated_member_use
extras: <String, String>{'first-sign-in': '2020-01-01'},
),
breadcrumbs: [
Expand Down
1 change: 1 addition & 0 deletions dart/example/bin/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Future<void> runApp() async {
username: 'first-user',
email: '[email protected]',
// ipAddress: '127.0.0.1', sendDefaultPii feature is enabled
// ignore: deprecated_member_use
extras: <String, String>{'first-sign-in': '2020-01-01'},
));
scope
Expand Down
1 change: 1 addition & 0 deletions dart/example_web/web/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ final event = SentryEvent(
environment: 'Test',
message: SentryMessage('This is an example Dart event.'),
tags: const <String, String>{'project-id': '7371'},
// ignore: deprecated_member_use, deprecated_member_use_from_same_package
extra: const <String, String>{'section': '1'},
// fingerprint: const <String>['example-dart'],
user: SentryUser(
Expand Down
12 changes: 10 additions & 2 deletions dart/lib/src/protocol/sentry_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class SentryEvent with SentryEventLike<SentryEvent> {
DateTime? timestamp,
Map<String, String>? modules,
Map<String, String>? tags,
Map<String, dynamic>? extra,
@Deprecated('Additional Data is deprecated in favor of structured [Contexts] and should be avoided when possible')
Map<String, dynamic>? extra,
List<String>? fingerprint,
List<Breadcrumb>? breadcrumbs,
List<SentryException>? exceptions,
Expand All @@ -40,6 +41,7 @@ class SentryEvent with SentryEventLike<SentryEvent> {
contexts = contexts ?? Contexts(),
modules = modules != null ? Map.from(modules) : null,
tags = tags != null ? Map.from(tags) : null,
// ignore: deprecated_member_use_from_same_package
extra = extra != null ? Map.from(extra) : null,
fingerprint = fingerprint != null ? List.from(fingerprint) : null,
breadcrumbs = breadcrumbs != null ? List.from(breadcrumbs) : null,
Expand Down Expand Up @@ -128,6 +130,8 @@ class SentryEvent with SentryEventLike<SentryEvent> {
///
/// Sentry.io docs do not talk about restrictions on the values, other than
/// they must be JSON-serializable.
@Deprecated(
'Additional Data is deprecated in favor of structured [Contexts] and should be avoided when possible')
final Map<String, dynamic>? extra;

/// List of breadcrumbs for this event.
Expand Down Expand Up @@ -201,7 +205,8 @@ class SentryEvent with SentryEventLike<SentryEvent> {
SentryLevel? level,
String? culprit,
Map<String, String>? tags,
Map<String, dynamic>? extra,
@Deprecated('Additional Data is deprecated in favor of structured [Contexts] and should be avoided when possible')
Map<String, dynamic>? extra,
List<String>? fingerprint,
SentryUser? user,
Contexts? contexts,
Expand Down Expand Up @@ -229,6 +234,7 @@ class SentryEvent with SentryEventLike<SentryEvent> {
level: level ?? this.level,
culprit: culprit ?? this.culprit,
tags: (tags != null ? Map.from(tags) : null) ?? this.tags,
// ignore: deprecated_member_use_from_same_package
extra: (extra != null ? Map.from(extra) : null) ?? this.extra,
fingerprint: (fingerprint != null ? List.from(fingerprint) : null) ??
this.fingerprint,
Expand Down Expand Up @@ -286,6 +292,7 @@ class SentryEvent with SentryEventLike<SentryEvent> {
timestampJson != null ? DateTime.tryParse(timestampJson) : null,
modules: modules,
tags: tags,
// ignore: deprecated_member_use_from_same_package
extra: extra,
fingerprint:
fingerprintJson?.map((e) => e as String).toList(growable: false),
Expand Down Expand Up @@ -374,6 +381,7 @@ class SentryEvent with SentryEventLike<SentryEvent> {
if (level != null) 'level': level!.name,
if (culprit != null) 'culprit': culprit,
if (tags?.isNotEmpty ?? false) 'tags': tags,
// ignore: deprecated_member_use_from_same_package
if (extra?.isNotEmpty ?? false) 'extra': extra,
if (type != null) 'type': type,
if (fingerprint?.isNotEmpty ?? false) 'fingerprint': fingerprint,
Expand Down
8 changes: 6 additions & 2 deletions dart/lib/src/protocol/sentry_transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class SentryTransaction extends SentryEvent {
String? transaction,
dynamic throwable,
Map<String, String>? tags,
Map<String, dynamic>? extra,
@Deprecated('Additional Data is deprecated in favor of structured [Contexts] and should be avoided when possible')
Map<String, dynamic>? extra,
SentryUser? user,
Contexts? contexts,
List<Breadcrumb>? breadcrumbs,
Expand All @@ -46,6 +47,7 @@ class SentryTransaction extends SentryEvent {
transaction: transaction ?? _tracer.name,
throwable: throwable ?? _tracer.throwable,
tags: tags ?? _tracer.tags,
// ignore: deprecated_member_use_from_same_package
extra: extra ?? _tracer.data,
user: user,
contexts: contexts,
Expand Down Expand Up @@ -116,7 +118,8 @@ class SentryTransaction extends SentryEvent {
SentryLevel? level,
String? culprit,
Map<String, String>? tags,
Map<String, dynamic>? extra,
@Deprecated('Additional Data is deprecated in favor of structured [Contexts] and should be avoided when possible')
Map<String, dynamic>? extra,
List<String>? fingerprint,
SentryUser? user,
Contexts? contexts,
Expand All @@ -142,6 +145,7 @@ class SentryTransaction extends SentryEvent {
transaction: transaction ?? this.transaction,
throwable: throwable ?? this.throwable,
tags: (tags != null ? Map.from(tags) : null) ?? this.tags,
// ignore: deprecated_member_use_from_same_package
extra: (extra != null ? Map.from(extra) : null) ?? this.extra,
user: user ?? this.user,
contexts: contexts ?? this.contexts,
Expand Down
2 changes: 2 additions & 0 deletions dart/lib/src/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ class Scope {
? event.breadcrumbs
: List.from(_breadcrumbs),
tags: tags.isNotEmpty ? _mergeEventTags(event) : event.tags,
// ignore: deprecated_member_use_from_same_package
extra: extra.isNotEmpty ? _mergeEventExtra(event) : event.extra,
);

Expand Down Expand Up @@ -362,6 +363,7 @@ class Scope {
/// the event extra will be kept.
Map<String, dynamic> _mergeEventExtra(SentryEvent event) =>
extra.map((key, value) => MapEntry(key, value))
// ignore: deprecated_member_use_from_same_package
..addAll(event.extra ?? {});

/// If scope and event have a user, the user of the event takes
Expand Down
1 change: 1 addition & 0 deletions dart/test/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ final fakeEvent = SentryEvent(
transaction: '/example/app',
level: SentryLevel.warning,
tags: const <String, String>{'project-id': '7371'},
// ignore: deprecated_member_use_from_same_package
extra: const <String, String>{'company-name': 'Dart Inc'},
fingerprint: const <String>['example-dart'],
modules: const {'module1': 'factory'},
Expand Down
5 changes: 4 additions & 1 deletion dart/test/scope_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ void main() {
test('apply context to event', () async {
final event = SentryEvent(
tags: const {'etag': '987'},
// ignore: deprecated_member_use_from_same_package
extra: const {'e-infos': 'abc'},
);
final scope = Scope(SentryOptions(dsn: fakeDsn))
Expand All @@ -413,7 +414,9 @@ void main() {
expect(updatedEvent?.tags,
{'etag': '987', 'build': '579', 'page-locale': 'en-us'});
expect(
updatedEvent?.extra, {'e-infos': 'abc', 'company-name': 'Dart Inc'});
// ignore: deprecated_member_use_from_same_package
updatedEvent?.extra,
{'e-infos': 'abc', 'company-name': 'Dart Inc'});
expect(updatedEvent?.contexts['theme'], {'value': 'material'});
});

Expand Down
7 changes: 7 additions & 0 deletions dart/test/sentry_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ void main() {

final event = SentryEvent(
tags: const {eventTagKey: eventTagValue},
// ignore: deprecated_member_use_from_same_package
extra: const {eventExtraKey: eventExtraValue},
modules: const {eventExtraKey: eventExtraValue},
level: SentryLevel.warning,
Expand Down Expand Up @@ -709,6 +710,7 @@ void main() {
scopeTagKey: scopeTagValue,
eventTagKey: eventTagValue,
});
// ignore: deprecated_member_use_from_same_package
expect(capturedEvent.extra, {
scopeExtraKey: scopeExtraValue,
eventExtraKey: eventExtraValue,
Expand Down Expand Up @@ -1018,6 +1020,7 @@ void main() {
final event = await eventFromEnvelope(capturedEnvelope);

expect(event.tags!.containsKey('theme'), true);
// ignore: deprecated_member_use_from_same_package
expect(event.extra!.containsKey('host'), true);
expect(event.modules!.containsKey('core'), true);
expect(event.sdk!.integrations.contains('testIntegration'), true);
Expand Down Expand Up @@ -1057,6 +1060,7 @@ void main() {
fixture.options.addEventProcessor(FunctionEventProcessor(
(event, {hint}) => event
..tags!.addAll({'theme': 'material'})
// ignore: deprecated_member_use_from_same_package
..extra!['host'] = '0.0.0.1'
..modules!.addAll({'core': '1.0'})
..breadcrumbs!.add(Breadcrumb(message: 'processor crumb'))
Expand All @@ -1074,6 +1078,7 @@ void main() {
final event = await eventFromEnvelope(capturedEnvelope);

expect(event.tags!.containsKey('theme'), true);
// ignore: deprecated_member_use_from_same_package
expect(event.extra!.containsKey('host'), true);
expect(event.modules!.containsKey('core'), true);
expect(event.sdk!.integrations.contains('testIntegration'), true);
Expand Down Expand Up @@ -1620,6 +1625,7 @@ Future<SentryTransaction?> asyncBeforeSendTransactionCallbackDropEvent(
SentryEvent? beforeSendCallback(SentryEvent event, {Hint? hint}) {
return event
..tags!.addAll({'theme': 'material'})
// ignore: deprecated_member_use_from_same_package
..extra!['host'] = '0.0.0.1'
..modules!.addAll({'core': '1.0'})
..breadcrumbs!.add(Breadcrumb(message: 'processor crumb'))
Expand All @@ -1632,6 +1638,7 @@ SentryTransaction? beforeSendTransactionCallback(
SentryTransaction transaction) {
return transaction
..tags!.addAll({'theme': 'material'})
// ignore: deprecated_member_use_from_same_package
..extra!['host'] = '0.0.0.1'
..sdk!.addIntegration('testIntegration')
..sdk!.addPackage('test-pkg', '1.0')
Expand Down
1 change: 1 addition & 0 deletions dart/test/sentry_envelope_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ void main() {
// This test passes if no exceptions are thrown, thus no asserts.
// This is a test for https://github.com/getsentry/sentry-dart/issues/523
test('serialize with non-serializable class', () async {
// ignore: deprecated_member_use_from_same_package
final event = SentryEvent(extra: {'non-ecodable': NonEncodable()});
final sut = SentryEnvelope.fromEvent(
event,
Expand Down
3 changes: 3 additions & 0 deletions dart/test/sentry_event_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ void main() {
expect(sentryEvent.level, isNull);
expect(sentryEvent.culprit, isNull);
expect(sentryEvent.tags, isNull);
// ignore: deprecated_member_use_from_same_package
expect(sentryEvent.extra, isNull);
expect(sentryEvent.breadcrumbs, isNull);
expect(sentryEvent.user, isNull);
Expand Down Expand Up @@ -199,6 +200,7 @@ void main() {
'a': 'b',
'c': 'd',
},
// ignore: deprecated_member_use_from_same_package
extra: const <String, dynamic>{
'e': 'f',
'g': 2,
Expand Down Expand Up @@ -441,6 +443,7 @@ void main() {
exceptions: [SentryException(type: null, value: null)],
threads: [SentryThread(stacktrace: SentryStackTrace(frames: []))],
tags: {},
// ignore: deprecated_member_use_from_same_package
extra: {},
contexts: Contexts(),
fingerprint: [],
Expand Down
3 changes: 3 additions & 0 deletions dart/test/sentry_tracer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ void main() {

final tr = fixture.hub.captureTransactionCalls.first;

// ignore: deprecated_member_use_from_same_package
expect(tr.transaction.extra?['test'], 'test');
});

Expand All @@ -114,6 +115,7 @@ void main() {

final tr = fixture.hub.captureTransactionCalls.first;

// ignore: deprecated_member_use_from_same_package
expect(tr.transaction.extra?['test'], isNull);
});

Expand All @@ -126,6 +128,7 @@ void main() {

final tr = fixture.hub.captureTransactionCalls.first;

// ignore: deprecated_member_use_from_same_package
expect(tr.transaction.extra?['test'], {'key': 'value'});
});

Expand Down
1 change: 1 addition & 0 deletions dio/test/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ final fakeEvent = SentryEvent(
transaction: '/example/app',
level: SentryLevel.warning,
tags: const <String, String>{'project-id': '7371'},
// ignore: deprecated_member_use
extra: const <String, String>{'company-name': 'Dart Inc'},
fingerprint: const <String>['example-dart'],
modules: const {'module1': 'factory'},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class FlutterEnricherEventProcessor implements EventProcessor {
final defaultRouteName = _window?.defaultRouteName;
// A FlutterEngine has no renderViewElement if it was started or is
// accessed from an isolate different to the main isolate.

// to keep compatibility with older versions
// ignore: deprecated_member_use
final hasRenderView = _widgetsBinding?.renderViewElement != null;

Expand Down
3 changes: 3 additions & 0 deletions flutter/lib/src/integrations/load_contexts_integration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class _LoadContextsIntegrationEventProcessor implements EventProcessor {

final extraMap = infos['extra'] as Map?;
if (extraMap != null && extraMap.isNotEmpty) {
// ignore: deprecated_member_use
final extras = event.extra ?? {};
final newExtras = Map<String, dynamic>.from(extraMap);

Expand All @@ -108,6 +109,8 @@ class _LoadContextsIntegrationEventProcessor implements EventProcessor {
extras[extra.key] = extra.value;
}
}

// ignore: deprecated_member_use
event = event.copyWith(extra: extras);
}

Expand Down
1 change: 1 addition & 0 deletions flutter/lib/src/view_hierarchy/sentry_tree_walker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ class _TreeWalker {
}

SentryViewHierarchy? walkWidgetTree(WidgetsBinding instance) {
// to keep compatibility with older versions
// ignore: deprecated_member_use
final rootElement = instance.renderViewElement;
if (rootElement == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void main() {
packages: packages,
),
tags: tags,
// ignore: deprecated_member_use
extra: extra,
user: user,
dist: dist,
Expand Down Expand Up @@ -312,8 +313,11 @@ void main() {
final e = getEvent(extra: {'key': 'flutter', 'key-a': 'flutter'});
final event = await fixture.options.eventProcessors.first.apply(e);

// ignore: deprecated_member_use
expect(event?.extra?['key'], 'flutter');
// ignore: deprecated_member_use
expect(event?.extra?['key-a'], 'flutter');
// ignore: deprecated_member_use
expect(event?.extra?['key-b'], 'native');
});

Expand Down
1 change: 1 addition & 0 deletions logging/lib/src/extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extension LogRecordX on LogRecord {
level: level.toSentryLevel(),
message: SentryMessage(message),
throwable: error,
// ignore: deprecated_member_use
extra: <String, Object>{
if (object != null) 'LogRecord.object': object!,
'LogRecord.sequenceNumber': sequenceNumber,
Expand Down
1 change: 1 addition & 0 deletions logging/test/logging_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ void main() {
expect(event.level, SentryLevel.warning);
expect(event.logger, 'FooBarLogger');
expect(event.throwable, exception);
// ignore: deprecated_member_use
expect(event.extra?['LogRecord.sequenceNumber'], isNotNull);
expect(fixture.hub.events.first.stackTrace, stackTrace);
});
Expand Down

0 comments on commit 62de927

Please sign in to comment.