-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathexpress.ts
More file actions
82 lines (74 loc) · 2.64 KB
/
express.ts
File metadata and controls
82 lines (74 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Automatic istrumentation for Express using OTel
import type { InstrumentationConfig } from '@opentelemetry/instrumentation';
import { InstrumentationBase, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation';
import { context } from '@opentelemetry/api';
import { getRPCMetadata, RPCType } from '@opentelemetry/core';
import { ensureIsWrapped, generateInstrumentOnce } from '@sentry/node-core';
import {
type ExpressIntegrationOptions,
type IntegrationFn,
debug,
patchExpressModule,
SDK_VERSION,
defineIntegration,
setupExpressErrorHandler as coreSetupExpressErrorHandler,
type ExpressHandlerOptions,
} from '@sentry/core';
export { expressErrorHandler } from '@sentry/core';
import { DEBUG_BUILD } from '../../debug-build';
const INTEGRATION_NAME = 'Express';
const SUPPORTED_VERSIONS = ['>=4.0.0 <6'];
export function setupExpressErrorHandler(
//oxlint-disable-next-line no-explicit-any
app: { use: (middleware: any) => unknown },
options?: ExpressHandlerOptions,
): void {
coreSetupExpressErrorHandler(app, options);
ensureIsWrapped(app.use, 'express');
}
export type ExpressInstrumentationConfig = InstrumentationConfig &
Omit<ExpressIntegrationOptions, 'express' | 'onRouteResolved'>;
export const instrumentExpress = generateInstrumentOnce(
INTEGRATION_NAME,
(options?: ExpressInstrumentationConfig) => new ExpressInstrumentation(options),
);
export class ExpressInstrumentation extends InstrumentationBase<ExpressInstrumentationConfig> {
public constructor(config: ExpressInstrumentationConfig = {}) {
super('sentry-express', SDK_VERSION, config);
}
public init(): InstrumentationNodeModuleDefinition {
const module = new InstrumentationNodeModuleDefinition(
'express',
SUPPORTED_VERSIONS,
express => {
try {
patchExpressModule({
...this.getConfig(),
express,
onRouteResolved(route) {
const rpcMetadata = getRPCMetadata(context.active());
if (route && rpcMetadata?.type === RPCType.HTTP) {
rpcMetadata.route = route;
}
},
});
} catch (e) {
DEBUG_BUILD && debug.error('Failed to patch express module:', e);
}
return express;
},
// we do not ever actually unpatch in our SDKs
express => express,
);
return module;
}
}
const _expressInstrumentation = ((options?: ExpressInstrumentationConfig) => {
return {
name: INTEGRATION_NAME,
setupOnce() {
instrumentExpress(options);
},
};
}) satisfies IntegrationFn;
export const expressIntegration = defineIntegration(_expressInstrumentation);