-
-
Notifications
You must be signed in to change notification settings - Fork 223
/
tracing_client.dart
78 lines (66 loc) · 2.24 KB
/
tracing_client.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import 'package:http/http.dart';
import '../hub.dart';
import '../hub_adapter.dart';
import '../protocol.dart';
import '../sentry_trace_origins.dart';
import '../tracing.dart';
import '../utils/tracing_utils.dart';
import '../utils/http_sanitizer.dart';
/// A [http](https://pub.dev/packages/http)-package compatible HTTP client
/// which adds support to Sentry Performance feature.
/// https://develop.sentry.dev/sdk/performance
class TracingClient extends BaseClient {
TracingClient({Client? client, Hub? hub})
: _hub = hub ?? HubAdapter(),
_client = client ?? Client();
final Client _client;
final Hub _hub;
@override
Future<StreamedResponse> send(BaseRequest request) async {
// see https://develop.sentry.dev/sdk/performance/#header-sentry-trace
final urlDetails = HttpSanitizer.sanitizeUrl(request.url.toString());
var description = request.method;
if (urlDetails != null) {
description += ' ${urlDetails.urlOrFallback}';
}
final currentSpan = _hub.getSpan();
var span = currentSpan?.startChild(
'http.client',
description: description,
);
span?.origin = SentryTraceOrigins.autoHttpHttp;
// if the span is NoOp, we don't want to attach headers
if (span is NoOpSentrySpan) {
span = null;
}
span?.setData('http.method', request.method);
urlDetails?.applyToSpan(span);
StreamedResponse? response;
try {
if (span != null) {
if (containsTargetOrMatchesRegExp(
_hub.options.tracePropagationTargets, request.url.toString())) {
addSentryTraceHeader(span, request.headers);
addBaggageHeader(
span,
request.headers,
logger: _hub.options.logger,
);
}
}
response = await _client.send(request);
span?.setData('http.response.status_code', response.statusCode);
span?.setData('http.response_content_length', response.contentLength);
span?.status = SpanStatus.fromHttpStatusCode(response.statusCode);
} catch (exception) {
span?.throwable = exception;
span?.status = SpanStatus.internalError();
rethrow;
} finally {
await span?.finish();
}
return response;
}
@override
void close() => _client.close();
}