Skip to content

Commit

Permalink
Do not attach headers if Span is NoOp (#1143)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Nov 18, 2022
1 parent ccc09e4 commit 6d7a391
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Do not attach headers if Span is NoOp ([#1143](https://github.com/getsentry/sentry-dart/pull/1143))

## 6.16.0

### Features
Expand Down
8 changes: 7 additions & 1 deletion dart/lib/src/http_client/tracing_client.dart
Expand Up @@ -2,6 +2,7 @@ import 'package:http/http.dart';
import '../hub.dart';
import '../hub_adapter.dart';
import '../protocol.dart';
import '../tracing.dart';
import '../utils/tracing_utils.dart';

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

// if the span is NoOp, we dont want to attach headers
if (span is NoOpSentrySpan) {
span = null;
}

StreamedResponse? response;
try {
if (span != null) {
Expand Down
14 changes: 14 additions & 0 deletions dart/test/http_client/tracing_client_test.dart
Expand Up @@ -135,6 +135,20 @@ void main() {
expect(response.request!.headers[sentryTrace.name], sentryTrace.value);
});

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

await fixture._hub
.configureScope((scope) => scope.span = NoOpSentrySpan());

final response = await sut.get(requestUri);

expect(response.request!.headers['baggage'], null);
expect(response.request!.headers['sentry-trace'], null);
});

test('captured span do not add headers if origins not set', () async {
final sut = fixture.getSut(
client: fixture.getClient(
Expand Down
7 changes: 6 additions & 1 deletion dio/lib/src/tracing_client_adapter.dart
Expand Up @@ -25,11 +25,16 @@ class TracingClientAdapter extends HttpClientAdapter {
) async {
// see https://develop.sentry.dev/sdk/performance/#header-sentry-trace
final currentSpan = _hub.getSpan();
final span = currentSpan?.startChild(
var span = currentSpan?.startChild(
'http.client',
description: '${options.method} ${options.uri}',
);

// if the span is NoOp, we dont want to attach headers
if (span is NoOpSentrySpan) {
span = null;
}

ResponseBody? response;
try {
if (span != null) {
Expand Down
13 changes: 13 additions & 0 deletions dio/test/tracing_client_adapter_test.dart
Expand Up @@ -121,6 +121,19 @@ void main() {
);
});

test('captured span do not add headers if NoOp', () async {
final sut = fixture.getSut(
client: fixture.getClient(statusCode: 200, reason: 'OK'),
);
await fixture._hub
.configureScope((scope) => scope.span = NoOpSentrySpan());

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

expect(response.headers['baggage'], null);
expect(response.headers['sentry-trace'], null);
});

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

0 comments on commit 6d7a391

Please sign in to comment.