Skip to content

Commit

Permalink
feat(hapi): Skip update HTTP's span name and update RpcMetadata's rou…
Browse files Browse the repository at this point in the history
…te instead (#1570)

Co-authored-by: Gerhard Stöbich <deb2001-github@yahoo.de>
Co-authored-by: Amir Blum <amirgiraffe@gmail.com>
  • Loading branch information
3 people authored Aug 12, 2023
1 parent ad94c5c commit 10bdbf7
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 30 deletions.
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
16 changes: 4 additions & 12 deletions plugins/node/opentelemetry-instrumentation-hapi/test/hapi.test.ts
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

0 comments on commit 10bdbf7

Please sign in to comment.