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(instrumentation-hapi): change root span name to route name #571

Merged
merged 3 commits into from Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -61,6 +61,7 @@
"typescript": "4.3.5"
},
"dependencies": {
"@opentelemetry/core": "^0.23.0",
"@opentelemetry/instrumentation": "^0.23.0",
"@opentelemetry/semantic-conventions": "^0.23.0",
"@types/hapi__hapi": "20.0.8"
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/

import * as api from '@opentelemetry/api';
import { getRPCMetadata, RPCType } from '@opentelemetry/core';
import {
InstrumentationBase,
InstrumentationConfig,
Expand Down Expand Up @@ -43,6 +44,7 @@ import {
getExtMetadata,
isDirectExtInput,
isPatchableExtMethod,
getRootSpanMetadata,
} from './utils';

/** Hapi instrumentation for OpenTelemetry */
Expand Down Expand Up @@ -374,6 +376,12 @@ export class HapiInstrumentation extends InstrumentationBase {
if (api.trace.getSpan(api.context.active()) === undefined) {
return await oldHandler(request, h, err);
}
const rpcMetadata = getRPCMetadata(api.context.active());
if (rpcMetadata?.type === RPCType.HTTP) {
const rootSpanMetadata = getRootSpanMetadata(route);
rpcMetadata.span.updateName(rootSpanMetadata.name);
rpcMetadata.span.setAttributes(rootSpanMetadata.attributes);
}
const metadata = getRouteMetadata(route, pluginName);
const span = instrumentation.tracer.startSpan(metadata.name, {
attributes: metadata.attributes,
Expand Down
14 changes: 14 additions & 0 deletions plugins/node/opentelemetry-instrumentation-hapi/src/utils.ts
Expand Up @@ -94,6 +94,20 @@ export const getRouteMetadata = (
};
};

export const getRootSpanMetadata = (
route: Hapi.ServerRoute
): {
attributes: SpanAttributes;
name: string;
} => {
return {
attributes: {
[SemanticAttributes.HTTP_ROUTE]: route.path,
},
name: `${route.method} ${route.path}`,
dyladan marked this conversation as resolved.
Show resolved Hide resolved
obecny marked this conversation as resolved.
Show resolved Hide resolved
};
};

export const getExtMetadata = (
extPoint: Hapi.ServerRequestExtType,
pluginName?: string
Expand Down
Expand Up @@ -15,6 +15,8 @@
*/

import { context, trace } from '@opentelemetry/api';
import { RPCType, setRPCMetadata } from '@opentelemetry/core';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { NodeTracerProvider } from '@opentelemetry/node';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import {
Expand Down Expand Up @@ -325,6 +327,43 @@ describe('Hapi Instrumentation - Core Tests', () => {
assert.strictEqual(res.statusCode, 200);
assert.deepStrictEqual(memoryExporter.getFinishedSpans().length, 0);
});

it('should rename root span with route information', async () => {
const rootSpan = tracer.startSpan('rootSpan', {});
server.route({
method: 'GET',
path: '/users/{userId}',
handler: (request, h) => {
return `Hello ${request.params.userId}`;
},
});

await server.start();
assert.strictEqual(memoryExporter.getFinishedSpans().length, 0);
const rpcMetadata = { type: RPCType.HTTP, span: rootSpan };
await context.with(
setRPCMetadata(trace.setSpan(context.active(), rootSpan), rpcMetadata),
async () => {
const res = await server.inject({
method: 'GET',
url: '/users/1',
});
assert.strictEqual(res.statusCode, 200);

rootSpan.end();
assert.deepStrictEqual(memoryExporter.getFinishedSpans().length, 2);

const exportedRootSpan = memoryExporter
.getFinishedSpans()
.find(span => span.name === 'GET /users/{userId}');
assert.notStrictEqual(exportedRootSpan, undefined);
assert.strictEqual(
exportedRootSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
'/users/{userId}'
);
}
);
});
});

describe('Disabling Hapi instrumentation', () => {
Expand Down