From 6435c7453f163a4df7dd365e73341ebae0ce8651 Mon Sep 17 00:00:00 2001 From: GIancarlo Buenaflor Date: Thu, 12 Sep 2024 14:48:51 +0200 Subject: [PATCH 1/4] add --- dart/lib/src/protocol/sentry_trace_context.dart | 6 ++++++ dart/lib/src/protocol/sentry_transaction.dart | 1 + dart/lib/src/sentry_span_context.dart | 2 ++ dart/test/sentry_trace_context_test.dart | 6 +++++- dart/test/sentry_transaction_test.dart | 12 ++++++++++++ 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/dart/lib/src/protocol/sentry_trace_context.dart b/dart/lib/src/protocol/sentry_trace_context.dart index e44eede721..004a2f0387 100644 --- a/dart/lib/src/protocol/sentry_trace_context.dart +++ b/dart/lib/src/protocol/sentry_trace_context.dart @@ -41,6 +41,8 @@ class SentryTraceContext { /// @see final String? origin; + final Map? data; + @internal final Map? unknown; @@ -62,6 +64,7 @@ class SentryTraceContext { : SpanStatus.fromString(json['status'] as String), sampled: true, origin: json['origin'] == null ? null : json['origin'] as String?, + data: json['data'] == null ? null : json['data'] as Map, unknown: json.notAccessed(), ); } @@ -78,6 +81,7 @@ class SentryTraceContext { if (description != null) 'description': description, if (status != null) 'status': status!.toString(), if (origin != null) 'origin': origin, + if (data != null) 'data': data, }; } @@ -92,6 +96,7 @@ class SentryTraceContext { origin: origin, unknown: unknown, replayId: replayId, + data: data, ); SentryTraceContext({ @@ -105,6 +110,7 @@ class SentryTraceContext { this.origin, this.unknown, this.replayId, + this.data, }) : traceId = traceId ?? SentryId.newId(), spanId = spanId ?? SpanId.newId(); diff --git a/dart/lib/src/protocol/sentry_transaction.dart b/dart/lib/src/protocol/sentry_transaction.dart index eea319aa41..e7f904f487 100644 --- a/dart/lib/src/protocol/sentry_transaction.dart +++ b/dart/lib/src/protocol/sentry_transaction.dart @@ -60,6 +60,7 @@ class SentryTransaction extends SentryEvent { contexts.trace = spanContext.toTraceContext( sampled: tracer.samplingDecision?.sampled, status: tracer.status, + data: extra ?? tracer.data, ); this.transactionInfo = transactionInfo ?? diff --git a/dart/lib/src/sentry_span_context.dart b/dart/lib/src/sentry_span_context.dart index 1b1274c4d0..26c4078910 100644 --- a/dart/lib/src/sentry_span_context.dart +++ b/dart/lib/src/sentry_span_context.dart @@ -53,6 +53,7 @@ class SentrySpanContext { SentryTraceContext toTraceContext({ bool? sampled, SpanStatus? status, + Map? data, }) { return SentryTraceContext( operation: operation, @@ -63,6 +64,7 @@ class SentrySpanContext { sampled: sampled, status: status, origin: origin, + data: data, ); } } diff --git a/dart/test/sentry_trace_context_test.dart b/dart/test/sentry_trace_context_test.dart index ab33512a2c..a58b31ac60 100644 --- a/dart/test/sentry_trace_context_test.dart +++ b/dart/test/sentry_trace_context_test.dart @@ -19,6 +19,7 @@ void main() { expect(map['status'], 'aborted'); expect(map['origin'], 'auto.ui'); expect(map['replay_id'], isNotNull); + expect(map['data'], {'key': 'value'}); }); test('fromJson deserializes', () { @@ -30,7 +31,8 @@ void main() { 'description': 'desc', 'status': 'aborted', 'origin': 'auto.ui', - 'replay_id': '00000000000000000000000000000004' + 'replay_id': '00000000000000000000000000000004', + 'data': {'key': 'value'}, }; map.addAll(testUnknown); final traceContext = SentryTraceContext.fromJson(map); @@ -44,6 +46,7 @@ void main() { expect(traceContext.sampled, true); expect( traceContext.replayId.toString(), '00000000000000000000000000000004'); + expect(traceContext.data, {'key': 'value'}); }); } @@ -57,6 +60,7 @@ class Fixture { status: SpanStatus.aborted(), origin: 'auto.ui', replayId: SentryId.newId(), + data: {'key': 'value'}, unknown: testUnknown, ); } diff --git a/dart/test/sentry_transaction_test.dart b/dart/test/sentry_transaction_test.dart index 7de0549a13..290dbae371 100644 --- a/dart/test/sentry_transaction_test.dart +++ b/dart/test/sentry_transaction_test.dart @@ -64,6 +64,18 @@ void main() { expect(sut.sampled, true); }); + test('returns contexts.trace.data if data is set', () async { + final tracer = _createTracer(sampled: true); + tracer.setData('key', 'value'); + final child = tracer.startChild('child'); + await child.finish(); + await tracer.finish(); + + final sut = fixture.getSut(tracer); + + expect(sut.contexts.trace!.data, {'key': 'value'}); + }); + test('returns sampled false if not sampled', () async { final tracer = _createTracer(sampled: false); final child = tracer.startChild('child'); From 13685ff9222f589e3add8281223ec350eed7e8a6 Mon Sep 17 00:00:00 2001 From: GIancarlo Buenaflor Date: Thu, 12 Sep 2024 14:55:14 +0200 Subject: [PATCH 2/4] set data to null if empty --- dart/lib/src/protocol/sentry_transaction.dart | 3 ++- dart/test/sentry_transaction_test.dart | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/dart/lib/src/protocol/sentry_transaction.dart b/dart/lib/src/protocol/sentry_transaction.dart index e7f904f487..8ab9fefa45 100644 --- a/dart/lib/src/protocol/sentry_transaction.dart +++ b/dart/lib/src/protocol/sentry_transaction.dart @@ -57,10 +57,11 @@ class SentryTransaction extends SentryEvent { this.metricSummaries = metricSummaries ?? tracer.localMetricsAggregator?.getSummaries(); + final data = extra ?? tracer.data; contexts.trace = spanContext.toTraceContext( sampled: tracer.samplingDecision?.sampled, status: tracer.status, - data: extra ?? tracer.data, + data: data.isEmpty ? null : data, ); this.transactionInfo = transactionInfo ?? diff --git a/dart/test/sentry_transaction_test.dart b/dart/test/sentry_transaction_test.dart index 290dbae371..7109172a68 100644 --- a/dart/test/sentry_transaction_test.dart +++ b/dart/test/sentry_transaction_test.dart @@ -64,7 +64,7 @@ void main() { expect(sut.sampled, true); }); - test('returns contexts.trace.data if data is set', () async { + test('returns contexts.trace.data if data is set on the tracer', () async { final tracer = _createTracer(sampled: true); tracer.setData('key', 'value'); final child = tracer.startChild('child'); @@ -76,6 +76,17 @@ void main() { expect(sut.contexts.trace!.data, {'key': 'value'}); }); + test('returns null contexts.trace.data if data is not set', () async { + final tracer = _createTracer(sampled: true); + final child = tracer.startChild('child'); + await child.finish(); + await tracer.finish(); + + final sut = fixture.getSut(tracer); + + expect(sut.contexts.trace!.data, isNull); + }); + test('returns sampled false if not sampled', () async { final tracer = _createTracer(sampled: false); final child = tracer.startChild('child'); From d30a3711866d3a9c85c0d16b8f264813b37b521a Mon Sep 17 00:00:00 2001 From: GIancarlo Buenaflor Date: Thu, 12 Sep 2024 14:55:45 +0200 Subject: [PATCH 3/4] update testn ame --- dart/test/sentry_transaction_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dart/test/sentry_transaction_test.dart b/dart/test/sentry_transaction_test.dart index 7109172a68..e22e4feae7 100644 --- a/dart/test/sentry_transaction_test.dart +++ b/dart/test/sentry_transaction_test.dart @@ -64,7 +64,7 @@ void main() { expect(sut.sampled, true); }); - test('returns contexts.trace.data if data is set on the tracer', () async { + test('returns contexts.trace.data if data is set', () async { final tracer = _createTracer(sampled: true); tracer.setData('key', 'value'); final child = tracer.startChild('child'); From d8986030bb7fde605f74695f503b6c4325a33245 Mon Sep 17 00:00:00 2001 From: GIancarlo Buenaflor Date: Thu, 12 Sep 2024 14:56:17 +0200 Subject: [PATCH 4/4] Add CHANGELOG entry --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff734baf10..08e3fc41fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Features + +- Emit `transaction.data` inside `contexts.trace.data` ([#2284](https://github.com/getsentry/sentry-dart/pull/2284)) + ## 8.9.0 ### Features