-
-
Notifications
You must be signed in to change notification settings - Fork 220
/
tracing_client_test.dart
162 lines (130 loc) · 3.87 KB
/
tracing_client_test.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import 'package:http/http.dart';
import 'package:http/testing.dart';
import 'package:mockito/mockito.dart';
import 'package:sentry/sentry.dart';
import 'package:sentry/src/http_client/tracing_client.dart';
import 'package:sentry/src/sentry_tracer.dart';
import 'package:test/test.dart';
import '../mocks.dart';
import '../mocks/mock_transport.dart';
final requestUri = Uri.parse('https://example.com?foo=bar');
void main() {
group(TracingClient, () {
late Fixture fixture;
setUp(() {
fixture = Fixture();
});
test('captured span if successful request', () async {
final sut = fixture.getSut(
client: fixture.getClient(statusCode: 200, reason: 'OK'),
);
final tr = fixture._hub.startTransaction(
'name',
'op',
bindToScope: true,
);
await sut.get(requestUri);
await tr.finish();
final tracer = (tr as SentryTracer);
final span = tracer.children.first;
expect(span.status, SpanStatus.ok());
expect(span.context.operation, 'http.client');
expect(span.context.description, 'GET https://example.com?foo=bar');
});
test('finish span if errored request', () async {
final sut = fixture.getSut(
client: createThrowingClient(),
);
final tr = fixture._hub.startTransaction(
'name',
'op',
bindToScope: true,
);
try {
await sut.get(requestUri);
} catch (_) {
// ignore
}
await tr.finish();
final tracer = (tr as SentryTracer);
final span = tracer.children.first;
expect(span.finished, isTrue);
});
test('associate exception to span if errored request', () async {
final sut = fixture.getSut(
client: createThrowingClient(),
);
final tr = fixture._hub.startTransaction(
'name',
'op',
bindToScope: true,
);
dynamic exception;
try {
await sut.get(requestUri);
} catch (error) {
exception = error;
}
await tr.finish();
final tracer = (tr as SentryTracer);
final span = tracer.children.first;
expect(span.status, SpanStatus.internalError());
expect(span.throwable, exception);
});
test('captured span adds sentry-trace header to the request', () async {
final sut = fixture.getSut(
client: fixture.getClient(statusCode: 200, reason: 'OK'),
);
final tr = fixture._hub.startTransaction(
'name',
'op',
bindToScope: true,
);
final response = await sut.get(requestUri);
await tr.finish();
final tracer = (tr as SentryTracer);
final span = tracer.children.first;
expect(response.request!.headers['sentry-trace'],
span.toSentryTrace().value);
});
test('do not throw if no span bound to the scope', () async {
final sut = fixture.getSut(
client: fixture.getClient(statusCode: 200, reason: 'OK'),
);
await sut.get(requestUri);
});
});
}
MockClient createThrowingClient() {
return MockClient(
(request) async {
expect(request.url, requestUri);
throw TestException();
},
);
}
class CloseableMockClient extends Mock implements BaseClient {}
class Fixture {
final _options = SentryOptions(dsn: fakeDsn);
late Hub _hub;
final transport = MockTransport();
Fixture() {
_options.transport = transport;
_options.tracesSampleRate = 1.0;
_hub = Hub(_options);
}
TracingClient getSut({MockClient? client}) {
final mc = client ?? getClient();
return TracingClient(
client: mc,
hub: _hub,
);
}
MockClient getClient({int statusCode = 200, String? reason}) {
return MockClient((request) async {
expect(request.url, requestUri);
return Response('', statusCode, reasonPhrase: reason, request: request);
});
}
}
class TestException implements Exception {}