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

perf(instrumentation-nestjs-core): extract reusable span attributes to outer scope #2087

Merged
merged 3 commits into from Apr 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -148,21 +148,21 @@ function createWrapCreateHandler(tracer: api.Tracer, moduleVersion?: string) {
) {
arguments[1] = createWrapHandler(tracer, moduleVersion, callback);
const handler = original.apply(this, arguments as any);
const callbackName = callback.name;
const instanceName =
instance.constructor && instance.constructor.name
? instance.constructor.name
: 'UnnamedInstance';
const spanName = callbackName
? `${instanceName}.${callbackName}`
: instanceName;

return function (
this: any,
req: any,
res: any,
next: (...args: any[]) => unknown
) {
const callbackName = callback.name;
const instanceName =
instance.constructor && instance.constructor.name
? instance.constructor.name
: 'UnnamedInstance';
const spanName = callbackName
? `${instanceName}.${callbackName}`
: instanceName;

const span = tracer.startSpan(spanName, {
attributes: {
...Instrumentation.COMMON_ATTRIBUTES,
Expand Down Expand Up @@ -197,15 +197,17 @@ function createWrapHandler(
moduleVersion: string | undefined,
handler: Function
) {
const spanName = handler.name || 'anonymous nest handler';
const options = {
attributes: {
...Instrumentation.COMMON_ATTRIBUTES,
[AttributeNames.VERSION]: moduleVersion,
[AttributeNames.TYPE]: NestType.REQUEST_HANDLER,
[AttributeNames.CALLBACK]: handler.name,
},
};
const wrappedHandler = function (this: RouterExecutionContext) {
const span = tracer.startSpan(handler.name || 'anonymous nest handler', {
attributes: {
...Instrumentation.COMMON_ATTRIBUTES,
[AttributeNames.VERSION]: moduleVersion,
[AttributeNames.TYPE]: NestType.REQUEST_HANDLER,
[AttributeNames.CALLBACK]: handler.name,
},
});
const span = tracer.startSpan(spanName, options);
const spanContext = api.trace.setSpan(api.context.active(), span);

return api.context.with(spanContext, async () => {
Expand Down