Skip to content

Commit

Permalink
Add Sampling Decision to Trace Envelope Header (#1639)
Browse files Browse the repository at this point in the history
* add sampled flag

* fmt

* update changelog

* redo fmt

* fix tests
  • Loading branch information
buenaflor authored Sep 13, 2023
1 parent d53c6fa commit bdd1a23
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Enhancements

- Add Sampling Decision to Trace Envelope Header ([#1639](https://github.com/getsentry/sentry-dart/pull/1639))
- Add http.request.method attribute to http spans data ([#1633](https://github.com/getsentry/sentry-dart/pull/1633))
- Add db.system and db.name attributes to db spans data ([#1629](https://github.com/getsentry/sentry-dart/pull/1629))
- Log SDK errors to the console if the log level is `fatal` even if `debug` is disabled ([#1635](https://github.com/getsentry/sentry-dart/pull/1635))
Expand Down
4 changes: 4 additions & 0 deletions dart/lib/src/sentry_baggage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ class SentryBaggage {
set(_sampleRateKeyName, value);
}

void setSampled(String value) {
set('sentry-sampled', value);
}

double? getSampleRate() {
final sampleRate = get(_sampleRateKeyName);
if (sampleRate == null) {
Expand Down
8 changes: 7 additions & 1 deletion dart/lib/src/sentry_trace_context_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class SentryTraceContextHeader {
this.userSegment,
this.transaction,
this.sampleRate,
this.sampled,
});

final SentryId traceId;
Expand All @@ -22,6 +23,7 @@ class SentryTraceContextHeader {
final String? userSegment;
final String? transaction;
final String? sampleRate;
final String? sampled;

/// Deserializes a [SentryTraceContextHeader] from JSON [Map].
factory SentryTraceContextHeader.fromJson(Map<String, dynamic> json) {
Expand All @@ -34,6 +36,7 @@ class SentryTraceContextHeader {
userSegment: json['user_segment'],
transaction: json['transaction'],
sampleRate: json['sample_rate'],
sampled: json['sampled'],
);
}

Expand All @@ -48,6 +51,7 @@ class SentryTraceContextHeader {
if (userSegment != null) 'user_segment': userSegment,
if (transaction != null) 'transaction': transaction,
if (sampleRate != null) 'sample_rate': sampleRate,
if (sampled != null) 'sampled': sampled,
};
}

Expand Down Expand Up @@ -76,7 +80,9 @@ class SentryTraceContextHeader {
if (sampleRate != null) {
baggage.setSampleRate(sampleRate!);
}

if (sampled != null) {
baggage.setSampled(sampled!);
}
return baggage;
}

Expand Down
1 change: 1 addition & 0 deletions dart/lib/src/sentry_tracer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ class SentryTracer extends ISentrySpan {
transaction:
_isHighQualityTransactionName(transactionNameSource) ? name : null,
sampleRate: _sampleRateToString(_rootSpan.samplingDecision?.sampleRate),
sampled: _rootSpan.samplingDecision?.sampled.toString(),
);

return _sentryTraceContextHeader;
Expand Down
3 changes: 2 additions & 1 deletion dart/test/protocol/sentry_baggage_header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ void main() {
baggage.setUserSegment('userSegment');
baggage.setTransaction('transaction');
baggage.setSampleRate('1.0');
baggage.setSampled('false');

final baggageHeader = SentryBaggageHeader.fromBaggage(baggage);

expect(baggageHeader.value,
'sentry-trace_id=$id,sentry-public_key=publicKey,sentry-release=release,sentry-environment=environment,sentry-user_id=userId,sentry-user_segment=userSegment,sentry-transaction=transaction,sentry-sample_rate=1.0');
'sentry-trace_id=$id,sentry-public_key=publicKey,sentry-release=release,sentry-environment=environment,sentry-user_id=userId,sentry-user_segment=userSegment,sentry-transaction=transaction,sentry-sample_rate=1.0,sentry-sampled=false');
});
});
}
4 changes: 3 additions & 1 deletion dart/test/sentry_trace_context_header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void main() {
'user_segment': 'user_segment',
'transaction': 'transaction',
'sample_rate': '1.0',
'sampled': 'false'
};
final context = SentryTraceContextHeader.fromJson(mapJson);

Expand All @@ -26,6 +27,7 @@ void main() {
expect(context.userSegment, 'user_segment');
expect(context.transaction, 'transaction');
expect(context.sampleRate, '1.0');
expect(context.sampled, 'false');
});

test('toJson', () {
Expand All @@ -38,7 +40,7 @@ void main() {
final baggage = context.toBaggage();

expect(baggage.toHeaderString(),
'sentry-trace_id=${id.toString()},sentry-public_key=123,sentry-release=release,sentry-environment=environment,sentry-user_id=user_id,sentry-user_segment=user_segment,sentry-transaction=transaction,sentry-sample_rate=1.0');
'sentry-trace_id=${id.toString()},sentry-public_key=123,sentry-release=release,sentry-environment=environment,sentry-user_id=user_id,sentry-user_segment=user_segment,sentry-transaction=transaction,sentry-sample_rate=1.0,sentry-sampled=false');
});
});
}
2 changes: 2 additions & 0 deletions dart/test/sentry_tracer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ void main() {
expect(newBaggage.get('sentry-user_segment'), 'segment');
expect(newBaggage.get('sentry-transaction'), 'name');
expect(newBaggage.get('sentry-sample_rate'), '1');
expect(newBaggage.get('sentry-sampled'), 'true');
});

test('skip transaction name if low cardinality', () {
Expand Down Expand Up @@ -562,6 +563,7 @@ void main() {
expect(context.userSegment, 'segment');
expect(context.transaction, 'name');
expect(context.sampleRate, '1');
expect(context.sampled, 'true');
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion dart/test/utils/tracing_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void main() {
addBaggageHeaderFromSpan(sut, headers);

expect(headers[baggage!.name],
'other-vendor-value=foo,sentry-trace_id=${sut.context.traceId},sentry-public_key=abc,sentry-release=release,sentry-environment=environment,sentry-user_segment=segment,sentry-transaction=name,sentry-sample_rate=1');
'other-vendor-value=foo,sentry-trace_id=${sut.context.traceId},sentry-public_key=abc,sentry-release=release,sentry-environment=environment,sentry-user_segment=segment,sentry-transaction=name,sentry-sample_rate=1,sentry-sampled=true');
});
});

Expand Down

0 comments on commit bdd1a23

Please sign in to comment.