-
Notifications
You must be signed in to change notification settings - Fork 615
fix(instrumentation-fastify): fix span attributes and avoid FSTDEP017 FastifyDeprecation warning for 404 request #1763
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
22793d3
fix(fastify): fix span attributes and avoid FSTDEP017 FastifyDeprecat…
trentm 94553e4
need to limit to fastify 4.18.0, because later versions require a new…
trentm bd91541
Merge branch 'main' into tm-fastify-404
trentm 03a26ac
hopefully helpful comment on when routeOptions.url became available
trentm 839838f
add some more versions to fastify test-all-versions tests
trentm b009011
Merge branch 'main' into tm-fastify-404
trentm 18832b4
fix compile
trentm 4dffa45
poke to re-run tests to see if some failures are spurious
trentm 342d976
Merge branch 'main' into tm-fastify-404
trentm 6094158
Merge branch 'main' into tm-fastify-404
pichlermarc 99de149
Merge branch 'main' into tm-fastify-404
trentm ab85a10
Merge branch 'main' into tm-fastify-404
trentm b0b74e0
sync package-lock file. However this is also undoing part of recent #…
trentm d8ff26e
add an ESM test
trentm d957004
Merge branch 'main' into tm-fastify-404
trentm bda679b
bump to latest contrib-test-utils ver
trentm 48b6471
Merge branch 'main' into tm-fastify-404
trentm 9ed84de
update to latest contrib-test-utils release, sync package-lock
trentm fd51b5e
Merge branch 'main' into tm-fastify-404
trentm 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| "fastify": | ||
| - versions: "4.23.2" | ||
| # Sanity check the first 4.x release, instead of all releases, plus recent | ||
| # releases. | ||
| - versions: "4.0.0 || >=4.24.3 <5" | ||
| commands: npm run test | ||
|
|
||
| # Fastify versions after 4.18.0 require a typescript greater than 4.4.4. | ||
| "typescript": | ||
| - versions: "4.7.4" |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,8 +96,9 @@ export class FastifyInstrumentation extends InstrumentationBase { | |
| const anyRequest = request as any; | ||
|
|
||
| const rpcMetadata = getRPCMetadata(context.active()); | ||
| const routeName = | ||
| anyRequest.routeOptions?.config?.url || request.routerPath; | ||
| const routeName = anyRequest.routeOptions | ||
| ? anyRequest.routeOptions.url // since [email protected] | ||
| : request.routerPath; | ||
| if (routeName && rpcMetadata?.type === RPCType.HTTP) { | ||
| rpcMetadata.route = routeName; | ||
| } | ||
|
|
@@ -265,18 +266,21 @@ export class FastifyInstrumentation extends InstrumentationBase { | |
| const anyRequest = request as any; | ||
|
|
||
| const handler = | ||
| anyRequest.routeOptions?.handler || anyRequest.context?.handler || {}; | ||
| anyRequest.routeOptions?.handler || anyRequest.context?.handler; | ||
|
|
||
| const handlerName = handler?.name.substr(6); | ||
| const handlerName = handler?.name.startsWith('bound ') | ||
| ? handler.name.substr(6) | ||
| : handler?.name; | ||
| const spanName = `${FastifyNames.REQUEST_HANDLER} - ${ | ||
| handlerName || this.pluginName || ANONYMOUS_NAME | ||
| }`; | ||
|
|
||
| const spanAttributes: SpanAttributes = { | ||
| [AttributeNames.PLUGIN_NAME]: this.pluginName, | ||
| [AttributeNames.FASTIFY_TYPE]: FastifyTypes.REQUEST_HANDLER, | ||
| [SemanticAttributes.HTTP_ROUTE]: | ||
| anyRequest.routeOptions?.config?.url || request.routerPath, | ||
| [SemanticAttributes.HTTP_ROUTE]: anyRequest.routeOptions | ||
| ? anyRequest.routeOptions.url // since [email protected] | ||
| : request.routerPath, | ||
| }; | ||
| if (handlerName) { | ||
| spanAttributes[AttributeNames.FASTIFY_NAME] = handlerName; | ||
|
|
||
54 changes: 54 additions & 0 deletions
54
plugins/node/opentelemetry-instrumentation-fastify/test/fixtures/use-fastify.mjs
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,54 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // Use fastify from an ES module: | ||
| // node --experimental-loader=@opentelemetry/instrumentation/hook.mjs use-fastify.mjs | ||
|
|
||
| import { trace } from '@opentelemetry/api'; | ||
| import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils'; | ||
|
|
||
| import { FastifyInstrumentation } from '../../build/src/index.js'; | ||
|
|
||
| const sdk = createTestNodeSdk({ | ||
| serviceName: 'use-fastify', | ||
| instrumentations: [ | ||
| new FastifyInstrumentation() | ||
| ] | ||
| }) | ||
| sdk.start(); | ||
|
|
||
| import Fastify from 'fastify'; | ||
| import http from 'http'; | ||
|
|
||
| // Start a fastify server. | ||
| const app = Fastify(); | ||
| app.get('/a-route', function aRoute(_request, reply) { | ||
| reply.send({ hello: 'world' }); | ||
| }) | ||
| const addr = await app.listen({ port: 0 }); | ||
|
|
||
| // Make a single request to it. | ||
| await new Promise(resolve => { | ||
| http.get(addr + '/a-route', (res) => { | ||
| res.resume(); | ||
| res.on('end', () => { | ||
| resolve(); | ||
| }); | ||
| }) | ||
| }) | ||
|
|
||
| await app.close(); | ||
| await sdk.shutdown(); |
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 |
|---|---|---|
|
|
@@ -25,13 +25,21 @@ import { | |
| SimpleSpanProcessor, | ||
| } from '@opentelemetry/sdk-trace-base'; | ||
| import { Span } from '@opentelemetry/api'; | ||
| import { | ||
| getPackageVersion, | ||
| runTestFixture, | ||
| TestCollector, | ||
| } from '@opentelemetry/contrib-test-utils'; | ||
| import * as semver from 'semver'; | ||
| import * as http from 'http'; | ||
| import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; | ||
| import { AttributeNames, FastifyInstrumentation } from '../src'; | ||
| import { FastifyRequestInfo } from '../src/types'; | ||
|
|
||
| const URL = require('url').URL; | ||
|
|
||
| const fastifyVersion = getPackageVersion('fastify'); | ||
|
|
||
| const httpRequest = { | ||
| get: (options: http.ClientRequestArgs | string) => { | ||
| return new Promise((resolve, reject) => { | ||
|
|
@@ -183,6 +191,23 @@ describe('fastify', () => { | |
| assert.strictEqual(span.parentSpanId, baseSpan.spanContext().spanId); | ||
| }); | ||
|
|
||
| it('should generate span for 404 request', async () => { | ||
| await startServer(); | ||
| await httpRequest.get(`http://localhost:${PORT}/no-such-route`); | ||
|
|
||
| const spans = memoryExporter.getFinishedSpans(); | ||
| assert.strictEqual(spans.length, 5); | ||
| const span = spans[2]; | ||
| assert.deepStrictEqual(span.attributes, { | ||
| 'fastify.name': 'basic404', | ||
| 'fastify.type': 'request_handler', | ||
| 'plugin.name': 'fastify -> @fastify/express', | ||
| }); | ||
| assert.strictEqual(span.name, 'request handler - basic404'); | ||
| const baseSpan = spans[1]; | ||
| assert.strictEqual(span.parentSpanId, baseSpan.spanContext().spanId); | ||
| }); | ||
|
|
||
| describe('when subsystem is registered', () => { | ||
| beforeEach(async () => { | ||
| httpInstrumentation.enable(); | ||
|
|
@@ -424,12 +449,17 @@ describe('fastify', () => { | |
| await startServer(); | ||
| }); | ||
|
|
||
| it('preClose is not instrumented', async () => { | ||
| app.addHook('preClose', () => { | ||
| assertRootContextActive(); | ||
| }); | ||
| it('preClose is not instrumented', async function () { | ||
| // 'preClose' was added in [email protected]. | ||
| if (semver.lt(fastifyVersion, '4.16.0')) { | ||
| this.skip(); | ||
| } else { | ||
| app.addHook('preClose', () => { | ||
| assertRootContextActive(); | ||
| }); | ||
|
|
||
| await startServer(); | ||
| await startServer(); | ||
| } | ||
| }); | ||
|
|
||
| it('onClose is not instrumented', async () => { | ||
|
|
@@ -507,4 +537,30 @@ describe('fastify', () => { | |
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it('should work with ESM usage', async () => { | ||
| await runTestFixture({ | ||
| cwd: __dirname, | ||
| argv: ['fixtures/use-fastify.mjs'], | ||
| env: { | ||
| NODE_OPTIONS: | ||
| '--experimental-loader=@opentelemetry/instrumentation/hook.mjs', | ||
| NODE_NO_WARNINGS: '1', | ||
| }, | ||
| checkResult: (err, stdout, stderr) => { | ||
| assert.ifError(err); | ||
| }, | ||
| checkCollector: (collector: TestCollector) => { | ||
| const spans = collector.sortedSpans; | ||
| assert.strictEqual(spans.length, 1); | ||
| assert.strictEqual(spans[0].name, 'request handler - aRoute'); | ||
| assert.strictEqual( | ||
| spans[0].attributes.filter(a => a.key === 'plugin.name')[0]?.value | ||
| ?.stringValue, | ||
| 'fastify', | ||
| 'attribute plugin.name' | ||
| ); | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Regarding the codecov warning on this line and below: TAV tests could cover line 101, but currently they won't because it doesn't go back to an early-enough fastify version. Shall I add an
// istanbul ignore nextdirective here?Update: Actually, perhaps an
istanbuldirective won't help, because that is whatnycis using. I don't know what codecov is using. When I run the tests locally, I get different lines that are uncovered: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.
Interesting; AFAIK codecov uses the reports generated by istanbul, so adding the directive should help. Looks like the uncovered lines above are actually not tested at the moment (see full file on the codecov report - not sure if you have access). We could also add an earlier fastify version to TAV if that fixes the issue.
I've just recently added TAV to this package, having more versions tested would likely be a good idea anyway.
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've added testing of [email protected] plus the current latest and any subsequent fastify 4.x releases to the .tav.yml.
I don't know if coverage data from TAV runs get included in the codecov summary.
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.
Ah right, it looks like reports from TAV are not included.
IMO just having the version tested is good enough for now, we may look into adding coverage from TAV in the future, but we've been having problems with codecov for a while now, so I think this will likely be a larger task.