Skip to content

Commit

Permalink
fix: Removed deprecated properties usage in Fastify instrumentation (#…
Browse files Browse the repository at this point in the history
…1679)

Co-authored-by: Marc Pichler <[email protected]>
  • Loading branch information
seidelmartin and pichlermarc authored Oct 10, 2023
1 parent 52dd42d commit d3328f8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@opentelemetry/sdk-trace-node": "^1.8.0",
"@types/express": "4.17.18",
"@types/mocha": "7.0.2",
"@types/node": "18.6.5",
"@types/node": "18.15.3",
"fastify": "4.18.0",
"mocha": "7.2.0",
"nyc": "15.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ export class FastifyInstrumentation extends InstrumentationBase {
}
instrumentation._wrap(reply, 'send', instrumentation._patchSend());

const anyRequest = request as any;

const rpcMetadata = getRPCMetadata(context.active());
const routeName = request.routerPath;
const routeName =
anyRequest.routeOptions?.config?.url || request.routerPath;
if (routeName && rpcMetadata?.type === RPCType.HTTP) {
rpcMetadata.route = routeName;
}
Expand Down Expand Up @@ -176,22 +179,22 @@ export class FastifyInstrumentation extends InstrumentationBase {
const handler = args[1] as HandlerOriginal;
const pluginName = this.pluginName;
if (applicationHookNames.includes(name)) {
return original.apply(this, [name as any, handler]);
return original.apply(this, [name, handler] as never);
}

const syncFunctionWithDone =
typeof args[args.length - 1] === 'function' &&
handler.constructor.name !== 'AsyncFunction';

return original.apply(this, [
name as any,
name,
instrumentation._wrapHandler(
pluginName,
name,
handler,
syncFunctionWithDone
),
]);
] as never);
};
};
}
Expand Down Expand Up @@ -259,16 +262,21 @@ export class FastifyInstrumentation extends InstrumentationBase {
if (!instrumentation.isEnabled()) {
return done();
}
const requestContext = (request as any).context || {};
const handlerName = (requestContext.handler?.name || '').substr(6);
const anyRequest = request as any;

const handler =
anyRequest.routeOptions?.handler || anyRequest.context?.handler || {};

const handlerName = handler?.name.substr(6);
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]: request.routerPath,
[SemanticAttributes.HTTP_ROUTE]:
anyRequest.routeOptions?.config?.url || request.routerPath,
};
if (handlerName) {
spanAttributes[AttributeNames.FASTIFY_NAME] = handlerName;
Expand Down
10 changes: 3 additions & 7 deletions plugins/node/opentelemetry-instrumentation-fastify/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@
* limitations under the License.
*/

import {
Span,
SpanAttributes,
SpanStatusCode,
Tracer,
} from '@opentelemetry/api';
import { Attributes, Span, SpanStatusCode, Tracer } from '@opentelemetry/api';
import { spanRequestSymbol } from './constants';

import type { PluginFastifyReply } from './internal-types';
Expand All @@ -35,13 +30,14 @@ export function startSpan(
reply: PluginFastifyReply,
tracer: Tracer,
spanName: string,
spanAttributes: SpanAttributes = {}
spanAttributes: Attributes = {}
) {
const span = tracer.startSpan(spanName, { attributes: spanAttributes });

const spans: Span[] = reply[spanRequestSymbol] || [];
spans.push(span);

// eslint-disable-next-line @typescript-eslint/no-floating-promises
Object.defineProperty(reply, spanRequestSymbol, {
enumerable: false,
configurable: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe('fastify', () => {
async function subsystem(fastify: FastifyInstance) {
fastify.addHook(
'onRequest',
async (
(
req: FastifyRequest,
res: FastifyReply,
next: HookHandlerDoneFunction
Expand Down

0 comments on commit d3328f8

Please sign in to comment.