Skip to content

Commit e9cf19b

Browse files
Adding ignored routes and descriptive operations names
1 parent f63c88c commit e9cf19b

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

src/telemetry/instrumentations.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import type { Instrumentation } from '@opentelemetry/instrumentation';
22
import { DnsInstrumentation } from '@opentelemetry/instrumentation-dns';
3-
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
3+
import { ExpressInstrumentation, ExpressInstrumentationConfig } from '@opentelemetry/instrumentation-express';
44
import { UndiciInstrumentation } from '@opentelemetry/instrumentation-undici';
55
import { GenericPoolInstrumentation } from '@opentelemetry/instrumentation-generic-pool';
6-
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
6+
import { HttpInstrumentation, HttpInstrumentationConfig } from '@opentelemetry/instrumentation-http';
77
import { IORedisInstrumentation } from '@opentelemetry/instrumentation-ioredis';
88
import { NetInstrumentation } from '@opentelemetry/instrumentation-net';
99
import { PgInstrumentation } from '@opentelemetry/instrumentation-pg';
1010
import { PinoInstrumentation } from '@opentelemetry/instrumentation-pino';
11+
import { Span } from '@opentelemetry/api';
12+
import { IncomingMessage, ClientRequest } from 'http';
13+
import { Request } from 'express';
1114

1215
const InstrumentationMap = {
1316
'@opentelemetry/instrumentation-http': HttpInstrumentation,
@@ -25,17 +28,56 @@ const InstrumentationMap = {
2528
type ConfigArg<T> = T extends new (...args: infer U) => unknown ? U[0] : never;
2629
export type InstrumentationConfigMap = {
2730
[Name in keyof typeof InstrumentationMap]?: ConfigArg<(typeof InstrumentationMap)[Name]>;
31+
} & {
32+
// Add optional global ignore patterns
33+
ignoreIncomingPaths?: string[];
2834
};
2935

36+
// Add default ignored endpoints
37+
const DEFAULT_IGNORED_ENDPOINTS = ['/health', '/metrics'];
38+
39+
// Add helper function to create span names
40+
function createSpanName(method: string, route: string): string {
41+
return `${method.toUpperCase()} ${route}`;
42+
}
43+
3044
export function getAutoInstrumentations(
3145
inputConfigs: InstrumentationConfigMap = {},
3246
): Instrumentation[] {
3347
const keys = Object.keys(InstrumentationMap) as Array<keyof typeof InstrumentationMap>;
48+
const ignorePaths = [...DEFAULT_IGNORED_ENDPOINTS, ...(inputConfigs.ignoreIncomingPaths || [])];
49+
3450
return keys
3551
.map((name) => {
3652
const Instance = InstrumentationMap[name];
37-
// Defaults are defined by the instrumentation itself
38-
const userConfig = inputConfigs[name] ?? {};
53+
// Create a base config from user input or empty object
54+
const userConfig = { ...(inputConfigs[name] || {}) };
55+
56+
// Configure HTTP instrumentation
57+
if (name === '@opentelemetry/instrumentation-http') {
58+
const httpConfig = userConfig as HttpInstrumentationConfig;
59+
httpConfig.ignoreIncomingRequestHook = (request) => {
60+
const path = request.url?.split('?')[0] || '/';
61+
return ignorePaths.includes(path);
62+
};
63+
httpConfig.requestHook = (span, request) => {
64+
const method = request.method || 'UNKNOWN';
65+
const path = (request instanceof IncomingMessage ? request.url : request.path)?.split('?')[0] || '/';
66+
span.updateName(createSpanName(method, path));
67+
};
68+
}
69+
70+
// Configure Express instrumentation
71+
if (name === '@opentelemetry/instrumentation-express') {
72+
const expressConfig = userConfig as ExpressInstrumentationConfig;
73+
expressConfig.ignoreLayers = ignorePaths;
74+
expressConfig.requestHook = (span, req) => {
75+
const method = req.request?.method || 'UNKNOWN';
76+
// Use the matched route path instead of raw URL if available
77+
const route = (req.request as any).route?.path || req.request?.url?.split('?')[0] || '/';
78+
span.updateName(createSpanName(method, route));
79+
};
80+
}
3981

4082
try {
4183
return new Instance(userConfig);

0 commit comments

Comments
 (0)