Skip to content

Commit 6d7a391

Browse files
authored
Do not attach headers if Span is NoOp (#1143)
1 parent ccc09e4 commit 6d7a391

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Fixes
6+
7+
- Do not attach headers if Span is NoOp ([#1143](https://github.com/getsentry/sentry-dart/pull/1143))
8+
39
## 6.16.0
410

511
### Features

dart/lib/src/http_client/tracing_client.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:http/http.dart';
22
import '../hub.dart';
33
import '../hub_adapter.dart';
44
import '../protocol.dart';
5+
import '../tracing.dart';
56
import '../utils/tracing_utils.dart';
67

78
/// A [http](https://pub.dev/packages/http)-package compatible HTTP client
@@ -19,11 +20,16 @@ class TracingClient extends BaseClient {
1920
Future<StreamedResponse> send(BaseRequest request) async {
2021
// see https://develop.sentry.dev/sdk/performance/#header-sentry-trace
2122
final currentSpan = _hub.getSpan();
22-
final span = currentSpan?.startChild(
23+
var span = currentSpan?.startChild(
2324
'http.client',
2425
description: '${request.method} ${request.url}',
2526
);
2627

28+
// if the span is NoOp, we dont want to attach headers
29+
if (span is NoOpSentrySpan) {
30+
span = null;
31+
}
32+
2733
StreamedResponse? response;
2834
try {
2935
if (span != null) {

dart/test/http_client/tracing_client_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ void main() {
135135
expect(response.request!.headers[sentryTrace.name], sentryTrace.value);
136136
});
137137

138+
test('captured span do not add headers if NoOp', () async {
139+
final sut = fixture.getSut(
140+
client: fixture.getClient(statusCode: 200, reason: 'OK'),
141+
);
142+
143+
await fixture._hub
144+
.configureScope((scope) => scope.span = NoOpSentrySpan());
145+
146+
final response = await sut.get(requestUri);
147+
148+
expect(response.request!.headers['baggage'], null);
149+
expect(response.request!.headers['sentry-trace'], null);
150+
});
151+
138152
test('captured span do not add headers if origins not set', () async {
139153
final sut = fixture.getSut(
140154
client: fixture.getClient(

dio/lib/src/tracing_client_adapter.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ class TracingClientAdapter extends HttpClientAdapter {
2525
) async {
2626
// see https://develop.sentry.dev/sdk/performance/#header-sentry-trace
2727
final currentSpan = _hub.getSpan();
28-
final span = currentSpan?.startChild(
28+
var span = currentSpan?.startChild(
2929
'http.client',
3030
description: '${options.method} ${options.uri}',
3131
);
3232

33+
// if the span is NoOp, we dont want to attach headers
34+
if (span is NoOpSentrySpan) {
35+
span = null;
36+
}
37+
3338
ResponseBody? response;
3439
try {
3540
if (span != null) {

dio/test/tracing_client_adapter_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ void main() {
121121
);
122122
});
123123

124+
test('captured span do not add headers if NoOp', () async {
125+
final sut = fixture.getSut(
126+
client: fixture.getClient(statusCode: 200, reason: 'OK'),
127+
);
128+
await fixture._hub
129+
.configureScope((scope) => scope.span = NoOpSentrySpan());
130+
131+
final response = await sut.get<dynamic>(requestOptions);
132+
133+
expect(response.headers['baggage'], null);
134+
expect(response.headers['sentry-trace'], null);
135+
});
136+
124137
test('do not throw if no span bound to the scope', () async {
125138
final sut = fixture.getSut(
126139
client: fixture.getClient(statusCode: 200, reason: 'OK'),

0 commit comments

Comments
 (0)