From ffd936396ce1a04aa2e73e2f295cea095342af41 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Fri, 18 Nov 2022 09:57:39 +0100 Subject: [PATCH 1/2] Do not attach headers if Span is NoOp --- CHANGELOG.md | 6 ++++++ dart/lib/src/http_client/tracing_client.dart | 8 +++++++- dart/test/http_client/tracing_client_test.dart | 14 ++++++++++++++ dio/lib/src/tracing_client_adapter.dart | 7 ++++++- dio/test/tracing_client_adapter_test.dart | 13 +++++++++++++ 5 files changed, 46 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 199a6fcaeb..dc67004ea2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Do not attach headers if Span is NoOp ([#1112](https://github.com/getsentry/sentry-dart/pull/1112)) + ## 6.16.0 ### Features diff --git a/dart/lib/src/http_client/tracing_client.dart b/dart/lib/src/http_client/tracing_client.dart index 336286eba1..322fab9419 100644 --- a/dart/lib/src/http_client/tracing_client.dart +++ b/dart/lib/src/http_client/tracing_client.dart @@ -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 @@ -19,11 +20,16 @@ class TracingClient extends BaseClient { Future 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) { diff --git a/dart/test/http_client/tracing_client_test.dart b/dart/test/http_client/tracing_client_test.dart index 5a356e5139..65927cfb99 100644 --- a/dart/test/http_client/tracing_client_test.dart +++ b/dart/test/http_client/tracing_client_test.dart @@ -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( diff --git a/dio/lib/src/tracing_client_adapter.dart b/dio/lib/src/tracing_client_adapter.dart index 6a9162e1da..c24d26be5d 100644 --- a/dio/lib/src/tracing_client_adapter.dart +++ b/dio/lib/src/tracing_client_adapter.dart @@ -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) { diff --git a/dio/test/tracing_client_adapter_test.dart b/dio/test/tracing_client_adapter_test.dart index 5d27224a24..4251786c8b 100644 --- a/dio/test/tracing_client_adapter_test.dart +++ b/dio/test/tracing_client_adapter_test.dart @@ -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(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'), From ab7c651805d8f36bab7b6fecfa93f521f6628a8a Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Fri, 18 Nov 2022 09:58:29 +0100 Subject: [PATCH 2/2] fix --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc67004ea2..8ffc0ee53c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Fixes -- Do not attach headers if Span is NoOp ([#1112](https://github.com/getsentry/sentry-dart/pull/1112)) +- Do not attach headers if Span is NoOp ([#1143](https://github.com/getsentry/sentry-dart/pull/1143)) ## 6.16.0