Skip to content

Commit 8e2f518

Browse files
chigia001Flarna
andauthored
feat(express): Skip update HTTP's span name and update RpcMetadata's route instead (#1557)
* feat(express): Skip update HTTP's span name and update RpcMetadata's route instead * feat(express): simplify condition to when written to rpcMetadata.route * feat(express): remove comment related to layerType being undefined * chore: force build * chore: force cache key * Revert "chore: force cache key" This reverts commit 483c7f1. --------- Co-authored-by: Gerhard Stöbich <[email protected]>
1 parent 774d254 commit 8e2f518

File tree

7 files changed

+118
-161
lines changed

7 files changed

+118
-161
lines changed

plugins/node/opentelemetry-instrumentation-express/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Express instrumentation has few options available to choose from. You can set th
7676

7777
`spanNameHook` is invoked with 2 arguments:
7878

79-
- `info: ExpressRequestInfo` containing the incoming Express.js request, the current route handler creating a span and `ExpressLayerType` - the type of the handling layer or undefined when renaming the root HTTP instrumentation span.
79+
- `info: ExpressRequestInfo` containing the incoming Express.js request, the current route handler creating a span and `ExpressLayerType` - the type of the handling layer.
8080
- `defaultName: string` - original name proposed by the instrumentation.
8181

8282
#### Ignore a whole Express route

plugins/node/opentelemetry-instrumentation-express/src/instrumentation.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { setRPCMetadata, getRPCMetadata, RPCType } from '@opentelemetry/core';
17+
import { getRPCMetadata, RPCType } from '@opentelemetry/core';
1818
import { trace, context, diag, SpanAttributes } from '@opentelemetry/api';
1919
import type * as express from 'express';
2020
import { ExpressInstrumentationConfig, ExpressRequestInfo } from './types';
@@ -198,18 +198,10 @@ export class ExpressInstrumentation extends InstrumentationBase<
198198
// once we reach the request handler
199199
const rpcMetadata = getRPCMetadata(context.active());
200200
if (
201-
metadata.attributes[AttributeNames.EXPRESS_TYPE] ===
202-
ExpressLayerType.REQUEST_HANDLER &&
201+
type === ExpressLayerType.REQUEST_HANDLER &&
203202
rpcMetadata?.type === RPCType.HTTP
204203
) {
205-
const name = instrumentation._getSpanName(
206-
{
207-
request: req,
208-
route,
209-
},
210-
`${req.method} ${route.length > 0 ? route : '/'}`
211-
);
212-
rpcMetadata.span.updateName(name);
204+
rpcMetadata.route = route || '/';
213205
}
214206

215207
// verify against the config if the layer should be ignored
@@ -270,13 +262,6 @@ export class ExpressInstrumentation extends InstrumentationBase<
270262
// verify we have a callback
271263
const args = Array.from(arguments);
272264
const callbackIdx = args.findIndex(arg => typeof arg === 'function');
273-
const newContext =
274-
rpcMetadata?.type === RPCType.HTTP
275-
? setRPCMetadata(
276-
context.active(),
277-
Object.assign(rpcMetadata, { route: route })
278-
)
279-
: context.active();
280265
if (callbackIdx >= 0) {
281266
arguments[callbackIdx] = function () {
282267
if (spanHasEnded === false) {
@@ -288,7 +273,7 @@ export class ExpressInstrumentation extends InstrumentationBase<
288273
(req[_LAYERS_STORE_PROPERTY] as string[]).pop();
289274
}
290275
const callback = args[callbackIdx] as Function;
291-
return context.bind(newContext, callback).apply(this, arguments);
276+
return callback.apply(this, arguments);
292277
};
293278
}
294279
const result = original.apply(this, arguments);

plugins/node/opentelemetry-instrumentation-express/src/types.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ export type IgnoreMatcher = string | RegExp | ((name: string) => boolean);
2424
export type ExpressRequestInfo = {
2525
request: Request;
2626
route: string;
27-
/**
28-
* If layerType is undefined, SpanNameHook is being invoked to rename the original root HTTP span.
29-
*/
30-
layerType?: ExpressLayerType;
27+
layerType: ExpressLayerType;
3128
};
3229

3330
export type SpanNameHook = (

plugins/node/opentelemetry-instrumentation-express/test/custom-config.test.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
} from '@opentelemetry/sdk-trace-base';
2424
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
2525
import * as assert from 'assert';
26-
import { RPCType, setRPCMetadata } from '@opentelemetry/core';
26+
import { RPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core';
2727
import { ExpressLayerType } from '../src/enums/ExpressLayerType';
2828
import { AttributeNames } from '../src/enums/AttributeNames';
2929
import { ExpressInstrumentation, ExpressInstrumentationConfig } from '../src';
@@ -110,8 +110,9 @@ describe('ExpressInstrumentation', () => {
110110
});
111111

112112
it('should not repeat middleware paths in the span name', async () => {
113+
let rpcMetadata: RPCMetadata;
113114
app.use((req, res, next) => {
114-
const rpcMetadata = { type: RPCType.HTTP, span: rootSpan };
115+
rpcMetadata = { type: RPCType.HTTP, span: rootSpan };
115116
return context.with(
116117
setRPCMetadata(
117118
trace.setSpan(context.active(), rootSpan),
@@ -139,8 +140,6 @@ describe('ExpressInstrumentation', () => {
139140
assert.strictEqual(response, 'ok');
140141
rootSpan.end();
141142

142-
const spans = memoryExporter.getFinishedSpans();
143-
144143
const requestHandlerSpan = memoryExporter
145144
.getFinishedSpans()
146145
.find(span => span.name.includes('request handler'));
@@ -154,8 +153,7 @@ describe('ExpressInstrumentation', () => {
154153
requestHandlerSpan?.attributes[AttributeNames.EXPRESS_TYPE],
155154
'request_handler'
156155
);
157-
const exportedRootSpan = spans.find(span => span.name === 'GET /mw');
158-
assert.notStrictEqual(exportedRootSpan, undefined);
156+
assert.strictEqual(rpcMetadata.route, '/mw');
159157
}
160158
);
161159
});
@@ -167,8 +165,9 @@ describe('ExpressInstrumentation', () => {
167165
ExpressLayerType.REQUEST_HANDLER,
168166
],
169167
} as ExpressInstrumentationConfig);
168+
let rpcMetadata: RPCMetadata;
170169
app.use((req, res, next) => {
171-
const rpcMetadata = { type: RPCType.HTTP, span: rootSpan };
170+
rpcMetadata = { type: RPCType.HTTP, span: rootSpan };
172171
return context.with(
173172
setRPCMetadata(
174173
trace.setSpan(context.active(), rootSpan),
@@ -192,8 +191,6 @@ describe('ExpressInstrumentation', () => {
192191
assert.strictEqual(response, 'ok');
193192
rootSpan.end();
194193

195-
const spans = memoryExporter.getFinishedSpans();
196-
197194
const requestHandlerSpan = memoryExporter
198195
.getFinishedSpans()
199196
.find(span => span.name.includes('request handler'));
@@ -207,8 +204,7 @@ describe('ExpressInstrumentation', () => {
207204
requestHandlerSpan?.attributes[AttributeNames.EXPRESS_TYPE],
208205
'request_handler'
209206
);
210-
const exportedRootSpan = spans.find(span => span.name === 'GET /');
211-
assert.notStrictEqual(exportedRootSpan, undefined);
207+
assert.strictEqual(rpcMetadata?.route, '/');
212208
}
213209
);
214210
});

0 commit comments

Comments
 (0)