Skip to content

Commit 53a5d35

Browse files
committed
feat: tracingHandler for APM
1 parent a0e1f14 commit 53a5d35

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

packages/node/src/handlers.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,39 @@ import { flush } from './sdk';
1212

1313
const DEFAULT_SHUTDOWN_TIMEOUT = 2000;
1414

15+
/**
16+
* Express compatible tracing handler.
17+
* @see Exposed as `Handlers.tracingHandler`
18+
*/
19+
export function tracingHandler(): (
20+
req: http.IncomingMessage,
21+
res: http.ServerResponse,
22+
next: (error?: any) => void,
23+
) => void {
24+
return function sentryTracingMiddleware(
25+
req: http.IncomingMessage,
26+
res: http.ServerResponse,
27+
next: (error?: any) => void,
28+
): void {
29+
// TODO: At this point req.route.path we use in `extractTransaction` is not available
30+
// but `req.path` or `req.url` should do the job as well. We could unify this here.
31+
const reqMethod = (req.method || '').toUpperCase();
32+
const reqUrl = req.url;
33+
const hub = getCurrentHub();
34+
const transaction = hub.startSpan({
35+
transaction: `${reqMethod}|${reqUrl}`,
36+
});
37+
hub.configureScope(scope => {
38+
scope.setSpan(transaction);
39+
});
40+
res
41+
.once('response', () => transaction.setSuccess())
42+
.once('error', () => transaction.setFailure())
43+
.once('finish', () => transaction.finish());
44+
next();
45+
};
46+
}
47+
1548
type TransactionTypes = 'path' | 'methodPath' | 'handler';
1649

1750
/** JSDoc */

0 commit comments

Comments
 (0)