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(fastify): Use plugin name for middleware span name #1680

Merged
merged 3 commits into from
Sep 26, 2023
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 @@ -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