-
-
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.
feat(node): Allow to pass instrumentation config to
httpIntegration
(…
…#12761) Today, it is not (easily) possible to use custom config for the OTEL HttpInstrumentation. We depend on our own integration for various things, so overwriting this will lead to lots of problems. With this PR, you can now pass some config through directly, in an "allowlisted" way, + there is an escape hatch to add arbitrary other config: ```js Sentry.init({ integrations: [ Sentry.httpIntegration({ instrumentation: { // these three are "vetted" requestHook: (span, req) => span.setAttribute('custom', req.method), responseHook: (span, res) => span.setAttribute('custom', res.method), applyCustomAttributesOnSpan: (span, req, res) => span.setAttribute('custom', req.method), // escape hatch: Can add arbitrary other config that is passed through _experimentalConfig: { serverName: 'xxx' } }) ] }); ``` Closes #12672
- Loading branch information
Showing
7 changed files
with
208 additions
and
29 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
dev-packages/node-integration-tests/suites/express/tracing/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
38 changes: 38 additions & 0 deletions
38
dev-packages/node-integration-tests/suites/tracing/httpIntegration/server-experimental.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,38 @@ | ||
const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
release: '1.0', | ||
// disable attaching headers to /test/* endpoints | ||
tracePropagationTargets: [/^(?!.*test).*$/], | ||
tracesSampleRate: 1.0, | ||
transport: loggingTransport, | ||
|
||
integrations: [ | ||
Sentry.httpIntegration({ | ||
instrumentation: { | ||
_experimentalConfig: { | ||
serverName: 'sentry-test-server-name', | ||
}, | ||
}, | ||
}), | ||
], | ||
}); | ||
|
||
// 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', (_req, res) => { | ||
res.send({ response: 'response 1' }); | ||
}); | ||
|
||
Sentry.setupExpressErrorHandler(app); | ||
|
||
startExpressServerAndSendPortToRunner(app); |
58 changes: 58 additions & 0 deletions
58
dev-packages/node-integration-tests/suites/tracing/httpIntegration/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,58 @@ | ||
const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
release: '1.0', | ||
// disable attaching headers to /test/* endpoints | ||
tracePropagationTargets: [/^(?!.*test).*$/], | ||
tracesSampleRate: 1.0, | ||
transport: loggingTransport, | ||
|
||
integrations: [ | ||
Sentry.httpIntegration({ | ||
instrumentation: { | ||
requestHook: (span, req) => { | ||
span.setAttribute('attr1', 'yes'); | ||
Sentry.setExtra('requestHookCalled', { | ||
url: req.url, | ||
method: req.method, | ||
}); | ||
}, | ||
responseHook: (span, res) => { | ||
span.setAttribute('attr2', 'yes'); | ||
Sentry.setExtra('responseHookCalled', { | ||
url: res.req.url, | ||
method: res.req.method, | ||
}); | ||
}, | ||
applyCustomAttributesOnSpan: (span, req, res) => { | ||
span.setAttribute('attr3', 'yes'); | ||
Sentry.setExtra('applyCustomAttributesOnSpanCalled', { | ||
reqUrl: req.url, | ||
reqMethod: req.method, | ||
resUrl: res.req.url, | ||
resMethod: res.req.method, | ||
}); | ||
}, | ||
}, | ||
}), | ||
], | ||
}); | ||
|
||
// 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', (_req, res) => { | ||
res.send({ response: 'response 1' }); | ||
}); | ||
|
||
Sentry.setupExpressErrorHandler(app); | ||
|
||
startExpressServerAndSendPortToRunner(app); |
80 changes: 80 additions & 0 deletions
80
dev-packages/node-integration-tests/suites/tracing/httpIntegration/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,80 @@ | ||
import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; | ||
|
||
describe('httpIntegration', () => { | ||
afterAll(() => { | ||
cleanupChildProcesses(); | ||
}); | ||
|
||
test('allows to pass instrumentation options to integration', done => { | ||
// response shape seems different on Node 14, so we skip this there | ||
const nodeMajorVersion = Number(process.versions.node.split('.')[0]); | ||
if (nodeMajorVersion <= 14) { | ||
done(); | ||
return; | ||
} | ||
|
||
createRunner(__dirname, 'server.js') | ||
.ignore('session', 'sessions') | ||
.expect({ | ||
transaction: { | ||
contexts: { | ||
trace: { | ||
span_id: expect.any(String), | ||
trace_id: expect.any(String), | ||
data: { | ||
url: expect.stringMatching(/\/test$/), | ||
'http.response.status_code': 200, | ||
attr1: 'yes', | ||
attr2: 'yes', | ||
attr3: 'yes', | ||
}, | ||
op: 'http.server', | ||
status: 'ok', | ||
}, | ||
}, | ||
extra: { | ||
requestHookCalled: { | ||
url: expect.stringMatching(/\/test$/), | ||
method: 'GET', | ||
}, | ||
responseHookCalled: { | ||
url: expect.stringMatching(/\/test$/), | ||
method: 'GET', | ||
}, | ||
applyCustomAttributesOnSpanCalled: { | ||
reqUrl: expect.stringMatching(/\/test$/), | ||
reqMethod: 'GET', | ||
resUrl: expect.stringMatching(/\/test$/), | ||
resMethod: 'GET', | ||
}, | ||
}, | ||
}, | ||
}) | ||
.start(done) | ||
.makeRequest('get', '/test'); | ||
}); | ||
|
||
test('allows to pass experimental config through to integration', done => { | ||
createRunner(__dirname, 'server-experimental.js') | ||
.ignore('session', 'sessions') | ||
.expect({ | ||
transaction: { | ||
contexts: { | ||
trace: { | ||
span_id: expect.any(String), | ||
trace_id: expect.any(String), | ||
data: { | ||
url: expect.stringMatching(/\/test$/), | ||
'http.response.status_code': 200, | ||
'http.server_name': 'sentry-test-server-name', | ||
}, | ||
op: 'http.server', | ||
status: 'ok', | ||
}, | ||
}, | ||
}, | ||
}) | ||
.start(done) | ||
.makeRequest('get', '/test'); | ||
}); | ||
}); |
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