-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(node): Ensure fetch/http breadcrumbs are created correctly (#12137)
This PR ensures that (node) fetch and http requests generate breadcrumbs correctly, even if tracing is disabled. It also adds tests for this, and ensures we pass the request/response to the hint correctly. Fixes #12132
- Loading branch information
Showing
6 changed files
with
351 additions
and
37 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
dev-packages/node-integration-tests/suites/tracing/requests/fetch-breadcrumbs/scenario.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
import * as Sentry from '@sentry/node'; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
release: '1.0', | ||
tracePropagationTargets: [/\/v0/, 'v1'], | ||
integrations: [], | ||
transport: loggingTransport, | ||
// Ensure this gets a correct hint | ||
beforeBreadcrumb(breadcrumb, hint) { | ||
breadcrumb.data = breadcrumb.data || {}; | ||
const req = hint?.request as { path?: string }; | ||
breadcrumb.data.ADDED_PATH = req?.path; | ||
return breadcrumb; | ||
}, | ||
}); | ||
|
||
async function run(): Promise<void> { | ||
Sentry.addBreadcrumb({ message: 'manual breadcrumb' }); | ||
|
||
// Since fetch is lazy loaded, we need to wait a bit until it's fully instrumented | ||
await new Promise(resolve => setTimeout(resolve, 100)); | ||
await fetch(`${process.env.SERVER_URL}/api/v0`).then(res => res.text()); | ||
await fetch(`${process.env.SERVER_URL}/api/v1`).then(res => res.text()); | ||
await fetch(`${process.env.SERVER_URL}/api/v2`).then(res => res.text()); | ||
await fetch(`${process.env.SERVER_URL}/api/v3`).then(res => res.text()); | ||
|
||
Sentry.captureException(new Error('foo')); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
run(); |
79 changes: 79 additions & 0 deletions
79
dev-packages/node-integration-tests/suites/tracing/requests/fetch-breadcrumbs/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { conditionalTest } from '../../../../utils'; | ||
import { createRunner } from '../../../../utils/runner'; | ||
import { createTestServer } from '../../../../utils/server'; | ||
|
||
conditionalTest({ min: 18 })('outgoing fetch', () => { | ||
test('outgoing fetch requests create breadcrumbs', done => { | ||
createTestServer(done) | ||
.start() | ||
.then(SERVER_URL => { | ||
createRunner(__dirname, 'scenario.ts') | ||
.withEnv({ SERVER_URL }) | ||
.ensureNoErrorOutput() | ||
.ignore('session', 'sessions') | ||
.expect({ | ||
event: { | ||
breadcrumbs: [ | ||
{ | ||
message: 'manual breadcrumb', | ||
timestamp: expect.any(Number), | ||
}, | ||
{ | ||
category: 'http', | ||
data: { | ||
'http.method': 'GET', | ||
url: `${SERVER_URL}/api/v0`, | ||
status_code: 404, | ||
ADDED_PATH: '/api/v0', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
{ | ||
category: 'http', | ||
data: { | ||
'http.method': 'GET', | ||
url: `${SERVER_URL}/api/v1`, | ||
status_code: 404, | ||
ADDED_PATH: '/api/v1', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
{ | ||
category: 'http', | ||
data: { | ||
'http.method': 'GET', | ||
url: `${SERVER_URL}/api/v2`, | ||
status_code: 404, | ||
ADDED_PATH: '/api/v2', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
{ | ||
category: 'http', | ||
data: { | ||
'http.method': 'GET', | ||
url: `${SERVER_URL}/api/v3`, | ||
status_code: 404, | ||
ADDED_PATH: '/api/v3', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
], | ||
exception: { | ||
values: [ | ||
{ | ||
type: 'Error', | ||
value: 'foo', | ||
}, | ||
], | ||
}, | ||
}, | ||
}) | ||
.start(done); | ||
}); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
dev-packages/node-integration-tests/suites/tracing/requests/http-breadcrumbs/scenario.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
import * as Sentry from '@sentry/node'; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
release: '1.0', | ||
tracePropagationTargets: [/\/v0/, 'v1'], | ||
integrations: [], | ||
transport: loggingTransport, | ||
// Ensure this gets a correct hint | ||
beforeBreadcrumb(breadcrumb, hint) { | ||
breadcrumb.data = breadcrumb.data || {}; | ||
const req = hint?.request as { path?: string }; | ||
breadcrumb.data.ADDED_PATH = req?.path; | ||
return breadcrumb; | ||
}, | ||
}); | ||
|
||
import * as http from 'http'; | ||
|
||
async function run(): Promise<void> { | ||
Sentry.addBreadcrumb({ message: 'manual breadcrumb' }); | ||
|
||
await makeHttpRequest(`${process.env.SERVER_URL}/api/v0`); | ||
await makeHttpRequest(`${process.env.SERVER_URL}/api/v1`); | ||
await makeHttpRequest(`${process.env.SERVER_URL}/api/v2`); | ||
await makeHttpRequest(`${process.env.SERVER_URL}/api/v3`); | ||
|
||
Sentry.captureException(new Error('foo')); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
run(); | ||
|
||
function makeHttpRequest(url: string): Promise<void> { | ||
return new Promise<void>(resolve => { | ||
http | ||
.request(url, httpRes => { | ||
httpRes.on('data', () => { | ||
// we don't care about data | ||
}); | ||
httpRes.on('end', () => { | ||
resolve(); | ||
}); | ||
}) | ||
.end(); | ||
}); | ||
} |
76 changes: 76 additions & 0 deletions
76
dev-packages/node-integration-tests/suites/tracing/requests/http-breadcrumbs/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { createRunner } from '../../../../utils/runner'; | ||
import { createTestServer } from '../../../../utils/server'; | ||
|
||
test('outgoing http requests create breadcrumbs', done => { | ||
createTestServer(done) | ||
.start() | ||
.then(SERVER_URL => { | ||
createRunner(__dirname, 'scenario.ts') | ||
.withEnv({ SERVER_URL }) | ||
.ensureNoErrorOutput() | ||
.ignore('session', 'sessions') | ||
.expect({ | ||
event: { | ||
breadcrumbs: [ | ||
{ | ||
message: 'manual breadcrumb', | ||
timestamp: expect.any(Number), | ||
}, | ||
{ | ||
category: 'http', | ||
data: { | ||
'http.method': 'GET', | ||
url: `${SERVER_URL}/api/v0`, | ||
status_code: 404, | ||
ADDED_PATH: '/api/v0', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
{ | ||
category: 'http', | ||
data: { | ||
'http.method': 'GET', | ||
url: `${SERVER_URL}/api/v1`, | ||
status_code: 404, | ||
ADDED_PATH: '/api/v1', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
{ | ||
category: 'http', | ||
data: { | ||
'http.method': 'GET', | ||
url: `${SERVER_URL}/api/v2`, | ||
status_code: 404, | ||
ADDED_PATH: '/api/v2', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
{ | ||
category: 'http', | ||
data: { | ||
'http.method': 'GET', | ||
url: `${SERVER_URL}/api/v3`, | ||
status_code: 404, | ||
ADDED_PATH: '/api/v3', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 'http', | ||
}, | ||
], | ||
exception: { | ||
values: [ | ||
{ | ||
type: 'Error', | ||
value: 'foo', | ||
}, | ||
], | ||
}, | ||
}, | ||
}) | ||
.start(done); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.