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

feat(hapi): Skip update HTTP's span name and update RpcMetadata's route instead #1570

Merged
merged 5 commits into from
Aug 12, 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 @@ -45,7 +45,6 @@ import {
getExtMetadata,
isDirectExtInput,
isPatchableExtMethod,
getRootSpanMetadata,
} from './utils';

/** Hapi instrumentation for OpenTelemetry */
Expand Down Expand Up @@ -389,9 +388,7 @@ export class HapiInstrumentation extends InstrumentationBase {
}
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);
rpcMetadata.route = route.path;
}
const metadata = getRouteMetadata(route, pluginName);
const span = instrumentation.tracer.startSpan(metadata.name, {
Expand Down
14 changes: 0 additions & 14 deletions plugins/node/opentelemetry-instrumentation-hapi/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,6 @@ 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}`,
};
};

export const getExtMetadata = (
extPoint: Hapi.ServerRequestExtType,
pluginName?: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
*/

import { context, trace, SpanStatusCode } from '@opentelemetry/api';
import { RPCType, setRPCMetadata } from '@opentelemetry/core';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { RPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import {
Expand Down Expand Up @@ -379,7 +378,7 @@ describe('Hapi Instrumentation - Core Tests', () => {
assert.deepStrictEqual(memoryExporter.getFinishedSpans().length, 0);
});

it('should rename root span with route information', async () => {
it('should update rpcMetadata.route information', async () => {
const rootSpan = tracer.startSpan('rootSpan', {});
server.route({
method: 'GET',
Expand All @@ -391,7 +390,7 @@ describe('Hapi Instrumentation - Core Tests', () => {

await server.start();
assert.strictEqual(memoryExporter.getFinishedSpans().length, 0);
const rpcMetadata = { type: RPCType.HTTP, span: rootSpan };
const rpcMetadata: RPCMetadata = { type: RPCType.HTTP, span: rootSpan };
await context.with(
setRPCMetadata(trace.setSpan(context.active(), rootSpan), rpcMetadata),
async () => {
Expand All @@ -404,14 +403,7 @@ describe('Hapi Instrumentation - Core Tests', () => {
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}'
);
assert.strictEqual(rpcMetadata.route, '/users/{userId}');
}
);
});
Expand Down