-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
http.test.ts
264 lines (207 loc) · 9.65 KB
/
http.test.ts
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import * as sentryCore from '@sentry/core';
import * as hubModule from '@sentry/hub';
import { Hub } from '@sentry/hub';
import { addExtensionMethods, Span, TRACEPARENT_REGEXP, Transaction } from '@sentry/tracing';
import { parseSemver } from '@sentry/utils';
import * as http from 'http';
import * as https from 'https';
import * as HttpsProxyAgent from 'https-proxy-agent';
import * as nock from 'nock';
import { Breadcrumb } from '../../src';
import { NodeClient } from '../../src/client';
import { Http as HttpIntegration } from '../../src/integrations/http';
import { getDefaultNodeClientOptions } from '../helper/node-client-options';
const NODE_VERSION = parseSemver(process.versions.node);
describe('tracing', () => {
function createTransactionOnScope(sendDefaultPii: boolean = true) {
const options = getDefaultNodeClientOptions({
dsn: 'https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012',
tracesSampleRate: 1.0,
integrations: [new HttpIntegration({ tracing: true })],
release: '1.0.0',
environment: 'production',
sendDefaultPii,
});
const hub = new Hub(new NodeClient(options));
addExtensionMethods();
hub.configureScope(scope =>
scope.setUser({
id: 'uid123',
segment: 'segmentA',
}),
);
// we need to mock both of these because the tracing handler relies on `@sentry/core` while the sampler relies on
// `@sentry/hub`, and mocking breaks the link between the two
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
jest.spyOn(hubModule, 'getCurrentHub').mockReturnValue(hub);
const transaction = hub.startTransaction({
name: 'dogpark',
traceId: '12312012123120121231201212312012',
});
hub.getScope()?.setSpan(transaction);
return transaction;
}
it("creates a span for each outgoing non-sentry request when there's a transaction on the scope", () => {
nock('http://dogs.are.great').get('/').reply(200);
const transaction = createTransactionOnScope();
const spans = (transaction as unknown as Span).spanRecorder?.spans as Span[];
http.get('http://dogs.are.great/');
expect(spans.length).toEqual(2);
// our span is at index 1 because the transaction itself is at index 0
expect(spans[1].description).toEqual('GET http://dogs.are.great/');
expect(spans[1].op).toEqual('http.client');
});
it("doesn't create a span for outgoing sentry requests", () => {
nock('http://squirrelchasers.ingest.sentry.io').get('/api/12312012/store/').reply(200);
const transaction = createTransactionOnScope();
const spans = (transaction as unknown as Span).spanRecorder?.spans as Span[];
http.get('http://squirrelchasers.ingest.sentry.io/api/12312012/store/');
// only the transaction itself should be there
expect(spans.length).toEqual(1);
expect((spans[0] as Transaction).name).toEqual('dogpark');
});
it('attaches the sentry-trace header to outgoing non-sentry requests', async () => {
nock('http://dogs.are.great').get('/').reply(200);
createTransactionOnScope();
const request = http.get('http://dogs.are.great/');
const sentryTraceHeader = request.getHeader('sentry-trace') as string;
expect(sentryTraceHeader).toBeDefined();
expect(TRACEPARENT_REGEXP.test(sentryTraceHeader)).toBe(true);
});
it("doesn't attach the sentry-trace header to outgoing sentry requests", () => {
nock('http://squirrelchasers.ingest.sentry.io').get('/api/12312012/store/').reply(200);
createTransactionOnScope();
const request = http.get('http://squirrelchasers.ingest.sentry.io/api/12312012/store/');
const sentryTraceHeader = request.getHeader('sentry-trace');
expect(sentryTraceHeader).not.toBeDefined();
});
it('attaches the baggage header to outgoing non-sentry requests', async () => {
nock('http://dogs.are.great').get('/').reply(200);
createTransactionOnScope();
const request = http.get('http://dogs.are.great/');
const baggageHeader = request.getHeader('baggage') as string;
expect(baggageHeader).toBeDefined();
expect(typeof baggageHeader).toEqual('string');
expect(baggageHeader).toEqual(
'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=dogpark,sentry-user_id=uid123,' +
'sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,' +
'sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1',
);
});
it('propagates 3rd party baggage header data to outgoing non-sentry requests', async () => {
nock('http://dogs.are.great').get('/').reply(200);
createTransactionOnScope();
const request = http.get({ host: 'http://dogs.are.great/', headers: { baggage: 'dog=great' } });
const baggageHeader = request.getHeader('baggage') as string;
expect(baggageHeader).toBeDefined();
expect(typeof baggageHeader).toEqual('string');
expect(baggageHeader).toEqual(
'dog=great,sentry-environment=production,sentry-release=1.0.0,sentry-transaction=dogpark,' +
'sentry-user_id=uid123,sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,' +
'sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1',
);
});
it('does not add the user_id to the baggage header if sendDefaultPii is set to false', async () => {
nock('http://dogs.are.great').get('/').reply(200);
createTransactionOnScope(false);
const request = http.get({ host: 'http://dogs.are.great/', headers: { baggage: 'dog=great' } });
const baggageHeader = request.getHeader('baggage') as string;
expect(baggageHeader).toBeDefined();
expect(typeof baggageHeader).toEqual('string');
expect(baggageHeader).toEqual(
'dog=great,sentry-environment=production,sentry-release=1.0.0,sentry-transaction=dogpark,' +
'sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,' +
'sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1',
);
});
it("doesn't attach the sentry-trace header to outgoing sentry requests", () => {
nock('http://squirrelchasers.ingest.sentry.io').get('/api/12312012/store/').reply(200);
createTransactionOnScope();
const request = http.get('http://squirrelchasers.ingest.sentry.io/api/12312012/store/');
const baggage = request.getHeader('baggage');
expect(baggage).not.toBeDefined();
});
});
describe('default protocols', () => {
function captureBreadcrumb(key: string): Promise<Breadcrumb> {
const hub = new Hub();
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
let resolve: (value: Breadcrumb | PromiseLike<Breadcrumb>) => void;
const p = new Promise<Breadcrumb>(r => {
resolve = r;
});
const options = getDefaultNodeClientOptions({
dsn: 'https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012',
integrations: [new HttpIntegration({ breadcrumbs: true })],
beforeBreadcrumb: (b: Breadcrumb) => {
if ((b.data?.url as string).includes(key)) {
resolve(b);
}
return b;
},
});
hub.bindClient(new NodeClient(options));
return p;
}
it('http module', async () => {
const key = 'catrunners';
const p = captureBreadcrumb(key);
nock(`http://${key}.ingest.sentry.io`).get('/api/123122332/store/').reply(200);
http.get({
host: `${key}.ingest.sentry.io`,
path: '/api/123122332/store/',
});
const b = await p;
expect(b.data?.url).toEqual(expect.stringContaining('http://'));
});
it('https module', async () => {
const key = 'catcatchers';
const p = captureBreadcrumb(key);
let nockProtocol = 'https';
// NOTE: Prior to Node 9, `https` used internals of `http` module, so
// the integration doesn't patch the `https` module. However this then
// causes issues with nock, because nock will patch the `https` module
// regardless (if it asked to mock a https:// url), preventing the
// request from reaching the integrations patch of the `http` module.
// The result is test failures in Node v8 and lower.
//
// We can work around this by telling giving nock a http:// url, so it
// only patches the `http` module, then Nodes usage of the `http` module
// in the `https` module results in both nock's and the integrations
// patch being called. All this relies on nock not properly checking
// the agent passed to `http.get` / `http.request`, thus resulting in it
// intercepting a https:// request with http:// mock. It's a safe bet
// because the latest versions of nock no longer support Node v8 and lower,
// so won't bother dealing with this old Node edge case.
if (NODE_VERSION.major && NODE_VERSION.major < 9) {
nockProtocol = 'http';
}
nock(`${nockProtocol}://${key}.ingest.sentry.io`).get('/api/123122332/store/').reply(200);
https.get({
host: `${key}.ingest.sentry.io`,
path: '/api/123122332/store/',
timeout: 300,
});
const b = await p;
expect(b.data?.url).toEqual(expect.stringContaining('https://'));
});
it('makes https request over http proxy', async () => {
const key = 'catcatchers';
const p = captureBreadcrumb(key);
let nockProtocol = 'https';
const proxy = 'http://<PROXY_URL>:3128';
const agent = HttpsProxyAgent(proxy);
if (NODE_VERSION.major && NODE_VERSION.major < 9) {
nockProtocol = 'http';
}
nock(`${nockProtocol}://${key}.ingest.sentry.io`).get('/api/123122332/store/').reply(200);
https.get({
host: `${key}.ingest.sentry.io`,
path: '/api/123122332/store/',
timeout: 300,
agent,
});
const b = await p;
expect(b.data?.url).toEqual(expect.stringContaining('https://'));
});
});