Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not attach headers if Span is NoOp #1143

Merged
merged 3 commits into from Nov 18, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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