Skip to content

feat: tracing without performance for dio #1837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- Tracing without performance for Dio integration ([#1837](https://github.com/getsentry/sentry-dart/pull/1837))
- Accept `Map<String, dynamic>` in `Hint` class ([#1807](https://github.com/getsentry/sentry-dart/pull/1807))
- Please check if everything works as expected when using `Hint`
- Factory constructor `Hint.withMap(Map<String, dynamic> map)` now takes `Map<String, dynamic>` instead of `Map<String, Object>`
Expand Down
31 changes: 25 additions & 6 deletions dio/lib/src/tracing_client_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,38 @@ class TracingClientAdapter implements HttpClientAdapter {

ResponseBody? response;
try {
if (span != null) {
if (containsTargetOrMatchesRegExp(
// ignore: invalid_use_of_internal_member
_hub.options.tracePropagationTargets,
options.uri.toString(),
)) {
if (containsTargetOrMatchesRegExp(
// ignore: invalid_use_of_internal_member
_hub.options.tracePropagationTargets,
options.uri.toString(),
)) {
if (span != null) {
addSentryTraceHeaderFromSpan(span, options.headers);
addBaggageHeaderFromSpan(
span,
options.headers,
// ignore: invalid_use_of_internal_member
logger: _hub.options.logger,
);
} else {
// ignore: invalid_use_of_internal_member
final scope = _hub.scope;
// ignore: invalid_use_of_internal_member
final propagationContext = scope.propagationContext;

final traceHeader = propagationContext.toSentryTrace();
addSentryTraceHeader(traceHeader, options.headers);

final baggage = propagationContext.baggage;
if (baggage != null) {
final baggageHeader = SentryBaggageHeader.fromBaggage(baggage);
addBaggageHeader(
baggageHeader,
options.headers,
// ignore: invalid_use_of_internal_member
logger: _hub.options.logger,
);
}
}
}

Expand Down
37 changes: 30 additions & 7 deletions dio/test/tracing_client_adapter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,48 @@ void main() {
);
});

test('captured span do not add headers if NoOp', () async {
test('do not throw if no span bound to the scope', () async {
final sut = fixture.getSut(
client: fixture.getClient(statusCode: 200, reason: 'OK'),
);

await sut.get<dynamic>(requestOptions);
});

test('set headers from propagationContext when tracing is disabled',
() async {
fixture._options.enableTracing = false;
final sut = fixture.getSut(
client: fixture.getClient(statusCode: 200, reason: 'OK'),
);
await fixture._hub
.configureScope((scope) => scope.span = NoOpSentrySpan());

final propagationContext = fixture._hub.scope.propagationContext;
propagationContext.baggage = SentryBaggage({'foo': 'bar'});

final response = await sut.get<dynamic>(requestOptions);

expect(response.headers['baggage'], null);
expect(response.headers['sentry-trace'], null);
expect(
response.headers['sentry-trace'],
[propagationContext.toSentryTrace().value],
);
expect(response.headers['baggage'], ['foo=bar']);
});

test('do not throw if no span bound to the scope', () async {
test('set headers from propagationContext when no transaction', () async {
final sut = fixture.getSut(
client: fixture.getClient(statusCode: 200, reason: 'OK'),
);

await sut.get<dynamic>(requestOptions);
final propagationContext = fixture._hub.scope.propagationContext;
propagationContext.baggage = SentryBaggage({'foo': 'bar'});

final response = await sut.get<dynamic>(requestOptions);

expect(
response.headers['sentry-trace'],
[propagationContext.toSentryTrace().value],
);
expect(response.headers['baggage'], ['foo=bar']);
});
});
}
Expand Down