-
-
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): Pass inferred name & attributes to
tracesSampler
(#12945)
Previously, we passed the span name directly to the `tracesSampler` as-is. However, this is not ideal because this does not match the name we actually send to sentry later, making this confusing. The reason is that we infer the name from the attributes for some types of spans, but we do that at export-time. This PR adjust this so that we send the inferred name & attributes to the `tracesSampler`. This works reasonably enough. However, there is a big caveat: This still only has access to the initial attributes that a span has at creation time. This means that we'll never have e.g. the `http.route` correctly there, because all `http.server` spans originate from the http instrumentation where they do not have a route yet, and then more specific instrumentation (e.g. express or fastify) add the `http.route` to that span later. So the name will never be parametrized, for example, for `http.server` spans, which is not ideal (as it will still not match the final span exactly) but should still be better than what we had so far (where the name would always just be e.g. `GET`). Fixes #12944
- Loading branch information
Showing
6 changed files
with
131 additions
and
39 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
dev-packages/node-integration-tests/suites/express/tracing/tracesSampler/server.js
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,40 @@ | ||
const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
release: '1.0', | ||
transport: loggingTransport, | ||
tracesSampler: samplingContext => { | ||
// The name we get here is inferred at span creation time | ||
// At this point, we sadly do not have a http.route attribute yet, | ||
// so we infer the name from the unparametrized route instead | ||
return ( | ||
samplingContext.name === 'GET /test/123' && | ||
samplingContext.attributes['sentry.op'] === 'http.server' && | ||
samplingContext.attributes['http.method'] === 'GET' | ||
); | ||
}, | ||
debug: true, | ||
}); | ||
|
||
// express must be required after Sentry is initialized | ||
const express = require('express'); | ||
const cors = require('cors'); | ||
const { startExpressServerAndSendPortToRunner } = require('@sentry-internal/node-integration-tests'); | ||
|
||
const app = express(); | ||
|
||
app.use(cors()); | ||
|
||
app.get('/test/:id', (_req, res) => { | ||
res.send('Success'); | ||
}); | ||
|
||
app.get('/test2', (_req, res) => { | ||
res.send('Success'); | ||
}); | ||
|
||
Sentry.setupExpressErrorHandler(app); | ||
|
||
startExpressServerAndSendPortToRunner(app); |
24 changes: 24 additions & 0 deletions
24
dev-packages/node-integration-tests/suites/express/tracing/tracesSampler/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,24 @@ | ||
import { cleanupChildProcesses, createRunner } from '../../../../utils/runner'; | ||
|
||
describe('express tracesSampler', () => { | ||
afterAll(() => { | ||
cleanupChildProcesses(); | ||
}); | ||
|
||
describe('CJS', () => { | ||
test('correctly samples & passes data to tracesSampler', done => { | ||
const runner = createRunner(__dirname, 'server.js') | ||
.expect({ | ||
transaction: { | ||
transaction: 'GET /test/:id', | ||
}, | ||
}) | ||
.start(done); | ||
|
||
// This is not sampled | ||
runner.makeRequest('get', '/test2?q=1'); | ||
// This is sampled | ||
runner.makeRequest('get', '/test/123?q=1'); | ||
}); | ||
}); | ||
}); |
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
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
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
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