Skip to content

Commit

Permalink
feat(restify): Skip update HTTP's span name and update RpcMetadata's …
Browse files Browse the repository at this point in the history
…route instead (#1571)

* feat(restify): Skip update HTTP's span name and update RpcMetadata's route instead

* feat(restify): remove unused import

---------

Co-authored-by: Amir Blum <[email protected]>
  • Loading branch information
chigia001 and blumamir authored Aug 12, 2023
1 parent 10bdbf7 commit 7d4b13e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
} from '@opentelemetry/instrumentation';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { isPromise, isAsyncFunction } from './utils';
import { getRPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core';
import { getRPCMetadata, RPCType } from '@opentelemetry/core';
import type { RestifyInstrumentationConfig } from './types';

const { diag } = api;
Expand Down Expand Up @@ -176,7 +176,7 @@ export class RestifyInstrumentation extends InstrumentationBase<any> {
// replace HTTP instrumentations name with one that contains a route
const httpMetadata = getRPCMetadata(api.context.active());
if (httpMetadata?.type === RPCType.HTTP) {
httpMetadata.span.updateName(`${req.method} ${route}`);
httpMetadata.route = route;
}

const fnName = handler.name || undefined;
Expand Down Expand Up @@ -237,13 +237,7 @@ export class RestifyInstrumentation extends InstrumentationBase<any> {
});
};

let newContext = api.trace.setSpan(api.context.active(), span);
if (httpMetadata) {
newContext = setRPCMetadata(
newContext,
Object.assign(httpMetadata, { route })
);
}
const newContext = api.trace.setSpan(api.context.active(), span);
return api.context.with(
newContext,
(req: types.Request, res: restify.Response, next: restify.Next) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { context, trace, Span } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { RPCType, setRPCMetadata } from '@opentelemetry/core';
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 @@ -292,14 +292,14 @@ describe('Restify Instrumentation', () => {
);
});

it('should rename HTTP span', async () => {
it('should update rpcMetadata.route', async () => {
const httpSpan: types.InstrumentationSpan = tracer.startSpan('HTTP GET');
const rpcMetadata: RPCMetadata = {
type: RPCType.HTTP,
span: httpSpan,
};

const testLocalServer = await createServer((server: restify.Server) => {
const rpcMetadata = {
type: RPCType.HTTP,
span: httpSpan,
};
server.pre((req, res, next) => {
// to simulate HTTP instrumentation
context.with(
Expand All @@ -320,7 +320,7 @@ describe('Restify Instrumentation', () => {
);
httpSpan.end();
assert.strictEqual(memoryExporter.getFinishedSpans().length, 3);
assert.strictEqual(httpSpan.name, 'GET /route/:param');
assert.strictEqual(rpcMetadata.route, '/route/:param');
assert.strictEqual(res, '{"route":"hello"}');
} finally {
testLocalServer.close();
Expand Down

0 comments on commit 7d4b13e

Please sign in to comment.