Skip to content
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

fix: Removed deprecated properties usage in Fastify instrumentation #1679

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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