Skip to content

Commit 1eaa6af

Browse files
committed
add tests
1 parent f8cbb21 commit 1eaa6af

File tree

4 files changed

+137
-20
lines changed

4 files changed

+137
-20
lines changed

packages/node/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"eslint": "7.6.0",
3636
"express": "^4.17.1",
3737
"jest": "^24.7.1",
38+
"nock": "^13.0.5",
3839
"npm-run-all": "^4.1.2",
3940
"prettier": "1.19.0",
4041
"rimraf": "^2.6.3",
Lines changed: 85 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,89 @@
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');
1761
});
1862

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();
2188
});
2289
});

packages/tracing/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ addExtensionMethods();
1515

1616
export { addExtensionMethods };
1717

18-
export { extractTraceparentData, getActiveTransaction, hasTracingEnabled, stripUrlQueryAndFragment } from './utils';
18+
export {
19+
extractTraceparentData,
20+
getActiveTransaction,
21+
hasTracingEnabled,
22+
stripUrlQueryAndFragment,
23+
TRACEPARENT_REGEXP,
24+
} from './utils';

yarn.lock

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3957,11 +3957,32 @@ [email protected]:
39573957
resolved "https://registry.yarnpkg.com/after/-/after-0.8.2.tgz#fedb394f9f0e02aa9768e702bda23b505fae7e1f"
39583958
integrity sha1-/ts5T58OAqqXaOcCvaI7UF+ufh8=
39593959

3960-
agent-base@4, agent-base@5, agent-base@6, agent-base@^4.3.0, agent-base@~4.2.1:
3960+
agent-base@4, agent-base@^4.3.0:
3961+
version "4.3.0"
3962+
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.3.0.tgz#8165f01c436009bccad0b1d122f05ed770efc6ee"
3963+
integrity sha512-salcGninV0nPrwpGNn4VTXBb1SOuXQBiqbrNXoeizJsHrsL6ERFM2Ne3JUSBWRE6aeNJI2ROP/WEEIDUiDe3cg==
3964+
dependencies:
3965+
es6-promisify "^5.0.0"
3966+
3967+
agent-base@5:
39613968
version "5.1.1"
39623969
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-5.1.1.tgz#e8fb3f242959db44d63be665db7a8e739537a32c"
39633970
integrity sha512-TMeqbNl2fMW0nMjTEPOwe3J/PRFP4vqeoNuQMG0HlMrtm5QxKqdvAkZ1pRBQ/ulIyDD5Yq0nJ7YbdD8ey0TO3g==
39643971

3972+
agent-base@6:
3973+
version "6.0.2"
3974+
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77"
3975+
integrity sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==
3976+
dependencies:
3977+
debug "4"
3978+
3979+
agent-base@~4.2.1:
3980+
version "4.2.1"
3981+
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.2.1.tgz#d89e5999f797875674c07d87f260fc41e83e8ca9"
3982+
integrity sha512-JVwXMr9nHYTUXsBFKUqhJwvlcYU/blreOEUkhNR2eXZIvwd+c+o5V4MgDPKWnMS/56awN3TRzIP+KoPn+roQtg==
3983+
dependencies:
3984+
es6-promisify "^5.0.0"
3985+
39653986
agentkeepalive@^3.4.1:
39663987
version "3.5.2"
39673988
resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-3.5.2.tgz#a113924dd3fa24a0bc3b78108c450c2abee00f67"
@@ -8938,6 +8959,18 @@ es6-object-assign@^1.1.0:
89388959
resolved "https://registry.yarnpkg.com/es6-object-assign/-/es6-object-assign-1.1.0.tgz#c2c3582656247c39ea107cb1e6652b6f9f24523c"
89398960
integrity sha1-wsNYJlYkfDnqEHyx5mUrb58kUjw=
89408961

8962+
es6-promise@^4.0.3:
8963+
version "4.2.8"
8964+
resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.2.8.tgz#4eb21594c972bc40553d276e510539143db53e0a"
8965+
integrity sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w==
8966+
8967+
es6-promisify@^5.0.0:
8968+
version "5.0.0"
8969+
resolved "https://registry.yarnpkg.com/es6-promisify/-/es6-promisify-5.0.0.tgz#5109d62f3e56ea967c4b63505aef08291c8a5203"
8970+
integrity sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM=
8971+
dependencies:
8972+
es6-promise "^4.0.3"
8973+
89418974
escalade@^3.0.1:
89428975
version "3.0.2"
89438976
resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.0.2.tgz#6a580d70edb87880f22b4c91d0d56078df6962c4"
@@ -14159,6 +14192,16 @@ nock@^13.0.4:
1415914192
lodash.set "^4.3.2"
1416014193
propagate "^2.0.0"
1416114194

14195+
nock@^13.0.5:
14196+
version "13.0.5"
14197+
resolved "https://registry.yarnpkg.com/nock/-/nock-13.0.5.tgz#a618c6f86372cb79fac04ca9a2d1e4baccdb2414"
14198+
integrity sha512-1ILZl0zfFm2G4TIeJFW0iHknxr2NyA+aGCMTjDVUsBY4CkMRispF1pfIYkTRdAR/3Bg+UzdEuK0B6HczMQZcCg==
14199+
dependencies:
14200+
debug "^4.1.0"
14201+
json-stringify-safe "^5.0.1"
14202+
lodash.set "^4.3.2"
14203+
propagate "^2.0.0"
14204+
1416214205
1416314206
version "1.0.5"
1416414207
resolved "https://registry.yarnpkg.com/node-environment-flags/-/node-environment-flags-1.0.5.tgz#fa930275f5bf5dae188d6192b24b4c8bbac3d76a"

0 commit comments

Comments
 (0)