|
1 |
| -import { RequestOptions } from 'http'; |
2 |
| - |
3 |
| -import { extractUrl } from './../../src/integrations/http'; |
4 |
| - |
5 |
| -describe('extractUrl()', () => { |
6 |
| - const urlString = 'http://dogs.are.great:1231/yay/'; |
7 |
| - const urlParts: RequestOptions = { |
8 |
| - protocol: 'http:', |
9 |
| - host: 'dogs.are.great', |
10 |
| - method: 'GET', |
11 |
| - path: '/yay/', |
12 |
| - port: 1231, |
13 |
| - }; |
14 |
| - |
15 |
| - it('accepts a url string', () => { |
16 |
| - expect(extractUrl(urlString)).toBe(urlString); |
| 1 | +import * as sentryCore from '@sentry/core'; |
| 2 | +import { Hub } from '@sentry/hub'; |
| 3 | +import * as hubModule from '@sentry/hub'; |
| 4 | +import { addExtensionMethods, Span, TRACEPARENT_REGEXP, Transaction } from '@sentry/tracing'; |
| 5 | +import * as http from 'http'; |
| 6 | +import * as nock from 'nock'; |
| 7 | + |
| 8 | +import { NodeClient } from '../../src/client'; |
| 9 | +import { Http as HttpIntegration } from '../../src/integrations/http'; |
| 10 | + |
| 11 | +describe('tracing', () => { |
| 12 | + function createTransactionOnScope() { |
| 13 | + const hub = new Hub( |
| 14 | + new NodeClient({ |
| 15 | + dsn: 'https://[email protected]/12312012', |
| 16 | + tracesSampleRate: 1.0, |
| 17 | + integrations: [new HttpIntegration({ tracing: true })], |
| 18 | + }), |
| 19 | + ); |
| 20 | + addExtensionMethods(); |
| 21 | + |
| 22 | + // we need to mock both of these because the tracing handler relies on `@sentry/core` while the sampler relies on |
| 23 | + // `@sentry/hub`, and mocking breaks the link between the two |
| 24 | + jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub); |
| 25 | + jest.spyOn(hubModule, 'getCurrentHub').mockReturnValue(hub); |
| 26 | + |
| 27 | + const transaction = hub.startTransaction({ name: 'dogpark' }); |
| 28 | + hub.getScope()?.setSpan(transaction); |
| 29 | + |
| 30 | + return transaction; |
| 31 | + } |
| 32 | + |
| 33 | + it("creates a span for each outgoing non-sentry request when there's a transaction on the scope", () => { |
| 34 | + nock('http://dogs.are.great') |
| 35 | + .get('/') |
| 36 | + .reply(200); |
| 37 | + |
| 38 | + const transaction = createTransactionOnScope(); |
| 39 | + const spans = (transaction as Span).spanRecorder?.spans as Span[]; |
| 40 | + |
| 41 | + http.get('http://dogs.are.great/'); |
| 42 | + |
| 43 | + expect(spans.length).toEqual(2); |
| 44 | + expect(spans[1].description).toEqual('GET http://dogs.are.great/'); |
| 45 | + expect(spans[1].op).toEqual('request'); |
| 46 | + }); |
| 47 | + |
| 48 | + it("doesn't create a span for outgoing sentry requests", () => { |
| 49 | + nock('http://squirrelchasers.ingest.sentry.io') |
| 50 | + .get('/api/12312012/store/') |
| 51 | + .reply(200); |
| 52 | + |
| 53 | + const transaction = createTransactionOnScope(); |
| 54 | + const spans = (transaction as Span).spanRecorder?.spans as Span[]; |
| 55 | + |
| 56 | + http.get('http://squirrelchasers.ingest.sentry.io/api/12312012/store/'); |
| 57 | + |
| 58 | + // only the transaction itself should be there |
| 59 | + expect(spans.length).toEqual(1); |
| 60 | + expect((spans[0] as Transaction).name).toEqual('dogpark'); |
17 | 61 | });
|
18 | 62 |
|
19 |
| - it('accepts a http.RequestOptions object and returns a string with everything in the right place', () => { |
20 |
| - expect(extractUrl(urlParts)).toBe(urlString); |
| 63 | + it('attaches the sentry-trace header to outgoing non-sentry requests', async () => { |
| 64 | + nock('http://dogs.are.great') |
| 65 | + .get('/') |
| 66 | + .reply(200); |
| 67 | + |
| 68 | + createTransactionOnScope(); |
| 69 | + |
| 70 | + const request = http.get('http://dogs.are.great/'); |
| 71 | + const sentryTraceHeader = request.getHeader('sentry-trace') as string; |
| 72 | + |
| 73 | + expect(sentryTraceHeader).toBeDefined(); |
| 74 | + expect(TRACEPARENT_REGEXP.test(sentryTraceHeader)).toBe(true); |
| 75 | + }); |
| 76 | + |
| 77 | + it("doesn't attach the sentry-trace header to outgoing sentry requests", () => { |
| 78 | + nock('http://squirrelchasers.ingest.sentry.io') |
| 79 | + .get('/api/12312012/store/') |
| 80 | + .reply(200); |
| 81 | + |
| 82 | + createTransactionOnScope(); |
| 83 | + |
| 84 | + const request = http.get('http://squirrelchasers.ingest.sentry.io/api/12312012/store/'); |
| 85 | + const sentryTraceHeader = request.getHeader('sentry-trace'); |
| 86 | + |
| 87 | + expect(sentryTraceHeader).not.toBeDefined(); |
21 | 88 | });
|
22 | 89 | });
|
0 commit comments