Skip to content

Commit 4503d3e

Browse files
mydeablumamir
andauthored
fix(fastify): Use plugin name for middleware span name (#1680)
Co-authored-by: Amir Blum <[email protected]>
1 parent 47301c0 commit 4503d3e

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,8 @@ export class FastifyInstrumentation extends InstrumentationBase {
116116
return original.apply(this, args);
117117
}
118118

119-
const spanName = `${FastifyNames.MIDDLEWARE} - ${
120-
original.name || ANONYMOUS_NAME
121-
}`;
119+
const name = original.name || pluginName || ANONYMOUS_NAME;
120+
const spanName = `${FastifyNames.MIDDLEWARE} - ${name}`;
122121

123122
const reply = args[1] as PluginFastifyReply;
124123

@@ -263,7 +262,7 @@ export class FastifyInstrumentation extends InstrumentationBase {
263262
const requestContext = (request as any).context || {};
264263
const handlerName = (requestContext.handler?.name || '').substr(6);
265264
const spanName = `${FastifyNames.REQUEST_HANDLER} - ${
266-
handlerName || ANONYMOUS_NAME
265+
handlerName || this.pluginName || ANONYMOUS_NAME
267266
}`;
268267

269268
const spanAttributes: SpanAttributes = {

plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {
2727
import { Span } from '@opentelemetry/api';
2828
import * as http from 'http';
2929
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
30-
import { ANONYMOUS_NAME } from '../src/instrumentation';
3130
import { AttributeNames, FastifyInstrumentation } from '../src';
3231
import { FastifyRequestInfo } from '../src/types';
3332

@@ -152,7 +151,10 @@ describe('fastify', () => {
152151
'plugin.name': 'fastify -> @fastify/express',
153152
[SemanticAttributes.HTTP_ROUTE]: '/test',
154153
});
155-
assert.strictEqual(span.name, `request handler - ${ANONYMOUS_NAME}`);
154+
assert.strictEqual(
155+
span.name,
156+
'request handler - fastify -> @fastify/express'
157+
);
156158
const baseSpan = spans[1];
157159
assert.strictEqual(span.parentSpanId, baseSpan.spanContext().spanId);
158160
});
@@ -290,7 +292,7 @@ describe('fastify', () => {
290292
assert.strictEqual(spans.length, 6);
291293
const baseSpan = spans[1];
292294
const span = spans[2];
293-
assert.strictEqual(span.name, `middleware - ${ANONYMOUS_NAME}`);
295+
assert.strictEqual(span.name, 'middleware - subsystem');
294296
assert.deepStrictEqual(span.attributes, {
295297
'fastify.type': 'middleware',
296298
'plugin.name': 'subsystem',
@@ -307,7 +309,7 @@ describe('fastify', () => {
307309

308310
assert.strictEqual(spans.length, 6);
309311
const span = spans[3];
310-
assert.strictEqual(span.name, 'request handler - anonymous');
312+
assert.strictEqual(span.name, 'request handler - subsystem');
311313
assert.deepStrictEqual(span.status, {
312314
code: SpanStatusCode.ERROR,
313315
message: 'foo',

0 commit comments

Comments
 (0)