-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(node): Migrate to @fastify/otel
#15542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
onurtemizkan
wants to merge
30
commits into
develop
Choose a base branch
from
onur/fastify-otel-migration
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
471944f
feat(node): Migrate to `@fastify/otel`
onurtemizkan fb1b004
Inline fastify types
onurtemizkan 5e30d0c
Use default `FastifyOtelInstrumentation` export
onurtemizkan 7a042ce
Update types.
onurtemizkan adf3ed9
Auto-register @fastify/otel plugin when instrumented.
onurtemizkan ef959b0
Remove version check.
onurtemizkan d1218cf
Switch using `diagnostics_channel`
onurtemizkan 5cce1ff
Fix formatting.
onurtemizkan c1ce58c
Bump `@fastify/otel` to `0.5.0`
onurtemizkan 1c1ef9a
Add vendor permalinks
onurtemizkan eb8897f
Move fastify into its own folder.
onurtemizkan b775be9
Update e2e tests
onurtemizkan f61ee23
Fix formatting
onurtemizkan 445fd0f
Deduplicate deps
onurtemizkan 73a762a
Reset lockfile
onurtemizkan 3474b46
Use `skipLibCheck` in e2e tests
onurtemizkan 1dda077
Address review comments
onurtemizkan 6cb1aec
Bump `@fastify/otel` to `0.5.2`
onurtemizkan b5d0a1e
Dedupe deps
onurtemizkan 4c3872a
Reset lockfile
onurtemizkan 5b8d851
Add middie spans back to e2e tests
onurtemizkan 4176529
Realign spans between versions
onurtemizkan 0bc53de
Bump Fastify 5 versions to `5.3.1`
onurtemizkan 9183d4e
Set `fastify` as external
onurtemizkan 59211f4
Dedupe deps
onurtemizkan 60fe4e5
Try when `fastify` is imported as a `type` on `@fastify/otel`
onurtemizkan 5a95504
Bump `@fastify/otel` to `0.6.0`
onurtemizkan fc6f51f
Update types of `fastifyOtelInstrumentationInstance`
onurtemizkan 3ede671
Update fastify-3 e2e test proxy name
onurtemizkan 5df4347
Align e2e tests
onurtemizkan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
File renamed without changes.
File renamed without changes.
This file contains hidden or 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
File renamed without changes.
157 changes: 157 additions & 0 deletions
157
dev-packages/e2e-tests/test-applications/node-fastify-3/src/app.ts
This file contains hidden or 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,157 @@ | ||
import type * as S from '@sentry/node'; | ||
const Sentry = require('@sentry/node') as typeof S; | ||
|
||
// We wrap console.warn to find out if a warning is incorrectly logged | ||
console.warn = new Proxy(console.warn, { | ||
apply: function (target, thisArg, argumentsList) { | ||
const msg = argumentsList[0]; | ||
if (typeof msg === 'string' && msg.startsWith('[Sentry]')) { | ||
console.error(`Sentry warning was triggered: ${msg}`); | ||
process.exit(1); | ||
} | ||
|
||
return target.apply(thisArg, argumentsList); | ||
}, | ||
}); | ||
|
||
Sentry.init({ | ||
environment: 'qa', // dynamic sampling bias to keep transactions | ||
dsn: process.env.E2E_TEST_DSN, | ||
integrations: [], | ||
tracesSampleRate: 1, | ||
tunnel: 'http://localhost:3031/', // proxy server | ||
tracePropagationTargets: ['http://localhost:3030', '/external-allowed'], | ||
}); | ||
|
||
import type * as H from 'http'; | ||
import type * as F from 'fastify'; | ||
|
||
// Make sure fastify is imported after Sentry is initialized | ||
const { fastify } = require('fastify') as typeof F; | ||
const http = require('http') as typeof H; | ||
|
||
const app = fastify(); | ||
const port = 3030; | ||
const port2 = 3040; | ||
|
||
Sentry.setupFastifyErrorHandler(app); | ||
|
||
app.get('/test-success', function (_req, res) { | ||
res.send({ version: 'v1' }); | ||
}); | ||
|
||
app.get<{ Params: { param: string } }>('/test-param/:param', function (req, res) { | ||
res.send({ paramWas: req.params.param }); | ||
}); | ||
|
||
app.get<{ Params: { id: string } }>('/test-inbound-headers/:id', function (req, res) { | ||
const headers = req.headers; | ||
|
||
res.send({ headers, id: req.params.id }); | ||
}); | ||
|
||
app.get<{ Params: { id: string } }>('/test-outgoing-http/:id', async function (req, res) { | ||
const id = req.params.id; | ||
const data = await makeHttpRequest(`http://localhost:3030/test-inbound-headers/${id}`); | ||
|
||
res.send(data); | ||
}); | ||
|
||
app.get<{ Params: { id: string } }>('/test-outgoing-fetch/:id', async function (req, res) { | ||
const id = req.params.id; | ||
const response = await fetch(`http://localhost:3030/test-inbound-headers/${id}`); | ||
const data = await response.json(); | ||
|
||
res.send(data); | ||
}); | ||
|
||
app.get('/test-transaction', async function (req, res) { | ||
Sentry.startSpan({ name: 'test-span' }, () => { | ||
Sentry.startSpan({ name: 'child-span' }, () => {}); | ||
}); | ||
|
||
res.send({}); | ||
}); | ||
|
||
app.get('/test-error', async function (req, res) { | ||
const exceptionId = Sentry.captureException(new Error('This is an error')); | ||
|
||
await Sentry.flush(2000); | ||
|
||
res.send({ exceptionId }); | ||
}); | ||
|
||
app.get<{ Params: { id: string } }>('/test-exception/:id', async function (req, res) { | ||
throw new Error(`This is an exception with id ${req.params.id}`); | ||
}); | ||
|
||
app.get('/test-outgoing-fetch-external-allowed', async function (req, res) { | ||
const fetchResponse = await fetch(`http://localhost:${port2}/external-allowed`); | ||
const data = await fetchResponse.json(); | ||
|
||
res.send(data); | ||
}); | ||
|
||
app.get('/test-outgoing-fetch-external-disallowed', async function (req, res) { | ||
const fetchResponse = await fetch(`http://localhost:${port2}/external-disallowed`); | ||
const data = await fetchResponse.json(); | ||
|
||
res.send(data); | ||
}); | ||
|
||
app.get('/test-outgoing-http-external-allowed', async function (req, res) { | ||
const data = await makeHttpRequest(`http://localhost:${port2}/external-allowed`); | ||
res.send(data); | ||
}); | ||
|
||
app.get('/test-outgoing-http-external-disallowed', async function (req, res) { | ||
const data = await makeHttpRequest(`http://localhost:${port2}/external-disallowed`); | ||
res.send(data); | ||
}); | ||
|
||
app.post('/test-post', function (req, res) { | ||
res.send({ status: 'ok', body: req.body }); | ||
}); | ||
|
||
app.listen({ port: port }); | ||
|
||
// A second app so we can test header propagation between external URLs | ||
const app2 = fastify(); | ||
app2.get('/external-allowed', function (req, res) { | ||
const headers = req.headers; | ||
|
||
res.send({ headers, route: '/external-allowed' }); | ||
}); | ||
|
||
app2.get('/external-disallowed', function (req, res) { | ||
const headers = req.headers; | ||
|
||
res.send({ headers, route: '/external-disallowed' }); | ||
}); | ||
|
||
app2.listen({ port: port2 }); | ||
|
||
function makeHttpRequest(url: string) { | ||
return new Promise(resolve => { | ||
const data: any[] = []; | ||
|
||
http | ||
.request(url, httpRes => { | ||
httpRes.on('data', chunk => { | ||
data.push(chunk); | ||
}); | ||
httpRes.on('error', error => { | ||
resolve({ error: error.message, url }); | ||
}); | ||
httpRes.on('end', () => { | ||
try { | ||
const json = JSON.parse(Buffer.concat(data).toString()); | ||
resolve(json); | ||
} catch { | ||
resolve({ data: Buffer.concat(data).toString(), url }); | ||
} | ||
}); | ||
}) | ||
.end(); | ||
}); | ||
} |
This file contains hidden or 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
30 changes: 30 additions & 0 deletions
30
dev-packages/e2e-tests/test-applications/node-fastify-3/tests/errors.test.ts
This file contains hidden or 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,30 @@ | ||
import { expect, test } from '@playwright/test'; | ||
import { waitForError } from '@sentry-internal/test-utils'; | ||
|
||
test('Sends correct error event', async ({ baseURL }) => { | ||
const errorEventPromise = waitForError('node-fastify-3', event => { | ||
return !event.type && event.exception?.values?.[0]?.value === 'This is an exception with id 123'; | ||
}); | ||
|
||
await fetch(`${baseURL}/test-exception/123`); | ||
|
||
const errorEvent = await errorEventPromise; | ||
|
||
expect(errorEvent.exception?.values).toHaveLength(1); | ||
expect(errorEvent.exception?.values?.[0]?.value).toBe('This is an exception with id 123'); | ||
|
||
expect(errorEvent.request).toEqual({ | ||
method: 'GET', | ||
cookies: {}, | ||
headers: expect.any(Object), | ||
url: 'http://localhost:3030/test-exception/123', | ||
}); | ||
|
||
expect(errorEvent.transaction).toEqual('GET /test-exception/:id'); | ||
|
||
expect(errorEvent.contexts?.trace).toEqual({ | ||
trace_id: expect.stringMatching(/[a-f0-9]{32}/), | ||
span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/), | ||
}); | ||
}); |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we have no more plugin/middleware spans like this anymore? Is this "OK"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked again after the updates. We still have the
middie
spans, I added them back to the tests. But the problem is now that we don't have access to therequestHook
anymore, the spans are not formatted correctly in NestJS applications (whensetupFastifyErrorHandler
is not used). We attempt usingaddFastifySpanAttributes
insidesetupFastifyErrorHandler
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm now trying to use
diagnosticsChannel
for it. Maybe we can reach the context from itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we try to PR something like the requestHook to the otel instrumentation, maybe? Or is this something they do not want?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mydea, they released a way to reach the spans: https://github.com/fastify/otel/releases/tag/v0.6.0
The problem is that with this release, they removed the vendored types we added previously. Adding
fastify
here as adevDependency
works for TypeScript 5, but not in previous versions. Should we make this an exception for TS-3.8 support/tests?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you mean, can we not replicate this anymore? 😢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, yes: https://github.com/fastify/otel/pull/46/files#diff-7aa4473ede4abd9ec099e87fec67fd57afafaf39e05d493ab4533acc38547eb8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mydea - Looks like they have overridden the types of
fastify
. So, it works now without addingfastify
as a dependency. Please bear with me when I generate sample events and post links.