1
1
import type { FastifyInstance , FastifyPluginAsync , FastifyRequest } from 'fastify' ;
2
2
import fp from 'fastify-plugin' ;
3
3
import * as Sentry from '@sentry/node' ;
4
- import '@sentry/tracing' ;
5
- import { Transaction } from '@sentry/tracing' ;
6
- import type { ExtractedNodeRequestData , TraceparentData } from '@sentry/types' ;
4
+ import type { ExtractedNodeRequestData , TraceparentData , Transaction } from '@sentry/types' ;
7
5
import { extractTraceparentData , normalize } from '@sentry/utils' ;
8
6
import { cleanRequestId } from './helpers' ;
9
7
10
8
const plugin : FastifyPluginAsync = async server => {
11
- server . decorateRequest ( 'sentryTransaction' , null ) ;
9
+ server . decorateReply ( 'sentryTransaction' , null ) ;
12
10
13
11
function shouldIgnore ( request : FastifyRequest ) {
14
12
if (
@@ -22,7 +20,7 @@ const plugin: FastifyPluginAsync = async server => {
22
20
return false ;
23
21
}
24
22
25
- server . addHook ( 'onRequest' , async request => {
23
+ server . addHook ( 'onRequest' , async ( request , reply ) => {
26
24
if ( shouldIgnore ( request ) ) {
27
25
return ;
28
26
}
@@ -53,11 +51,10 @@ const plugin: FastifyPluginAsync = async server => {
53
51
} ,
54
52
{ request : extractedRequestData } ,
55
53
) ;
56
- ( request as any ) . sentryTransaction = transaction ;
54
+ ( reply as any ) . sentryTransaction = transaction ;
55
+ transaction . sampled = true ;
57
56
58
- Sentry . getCurrentHub ( ) . configureScope ( scope => {
59
- scope . setSpan ( transaction ) ;
60
- } ) ;
57
+ Sentry . configureScope ( scope => scope . setSpan ( transaction ) ) ;
61
58
62
59
return ;
63
60
} ) ;
@@ -68,7 +65,7 @@ const plugin: FastifyPluginAsync = async server => {
68
65
}
69
66
70
67
setImmediate ( ( ) => {
71
- const transaction : Transaction = ( request as any ) . sentryTransaction ;
68
+ const transaction : Transaction = ( reply as any ) . sentryTransaction ;
72
69
73
70
transaction . setData ( 'url' , request . url ) ;
74
71
transaction . setData ( 'query' , request . query ) ;
0 commit comments