Skip to content

Commit

Permalink
fix(fastify): Use plugin name for middleware span name
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Sep 13, 2023
1 parent b10224b commit 061da0c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ export class FastifyInstrumentation extends InstrumentationBase {
return original.apply(this, args);
}

const spanName = `${FastifyNames.MIDDLEWARE} - ${
original.name || ANONYMOUS_NAME
}`;
const name = original.name || pluginName || ANONYMOUS_NAME;
const spanName = `${FastifyNames.MIDDLEWARE} - ${name}`;

const reply = args[1] as PluginFastifyReply;

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

const spanAttributes: SpanAttributes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
import { Span } from '@opentelemetry/api';
import * as http from 'http';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { ANONYMOUS_NAME } from '../src/instrumentation';
import { AttributeNames, FastifyInstrumentation } from '../src';
import { FastifyRequestInfo } from '../src/types';

Expand Down Expand Up @@ -152,7 +151,10 @@ describe('fastify', () => {
'plugin.name': 'fastify -> @fastify/express',
[SemanticAttributes.HTTP_ROUTE]: '/test',
});
assert.strictEqual(span.name, `request handler - ${ANONYMOUS_NAME}`);
assert.strictEqual(
span.name,
'request handler - fastify -> @fastify/express'
);
const baseSpan = spans[1];
assert.strictEqual(span.parentSpanId, baseSpan.spanContext().spanId);
});
Expand Down Expand Up @@ -290,7 +292,7 @@ describe('fastify', () => {
assert.strictEqual(spans.length, 6);
const baseSpan = spans[1];
const span = spans[2];
assert.strictEqual(span.name, `middleware - ${ANONYMOUS_NAME}`);
assert.strictEqual(span.name, 'middleware - subsystem');
assert.deepStrictEqual(span.attributes, {
'fastify.type': 'middleware',
'plugin.name': 'subsystem',
Expand All @@ -307,7 +309,7 @@ describe('fastify', () => {

assert.strictEqual(spans.length, 6);
const span = spans[3];
assert.strictEqual(span.name, 'request handler - anonymous');
assert.strictEqual(span.name, 'request handler - subsystem');
assert.deepStrictEqual(span.status, {
code: SpanStatusCode.ERROR,
message: 'foo',
Expand Down

0 comments on commit 061da0c

Please sign in to comment.