@@ -7,6 +7,24 @@ import type { Handle, ResolveOptions } from '@sveltejs/kit';
7
7
8
8
import { getTracePropagationData } from './utils' ;
9
9
10
+ export type SentryHandleOptions = {
11
+ /**
12
+ * Controls whether the SDK should capture errors and traces in requests that don't belong to a
13
+ * route defined in your SvelteKit application.
14
+ *
15
+ * By default, this option is set to `false` to reduce noise (e.g. bots sending random requests to your server).
16
+ *
17
+ * Set this option to `true` if you want to monitor requests events without a route. This might be useful in certain
18
+ * scenarios, for instance if you registered other handlers that handle these requests.
19
+ * If you set this option, you might want adjust the the transaction name in the `beforeSendTransaction`
20
+ * callback of your server-side `Sentry.init` options. You can also use `beforeSendTransaction` to filter out
21
+ * transactions that you still don't want to be sent to Sentry.
22
+ *
23
+ * @default false
24
+ */
25
+ handleUnknownRoutes ?: boolean ;
26
+ } ;
27
+
10
28
function sendErrorToSentry ( e : unknown ) : unknown {
11
29
// In case we have a primitive, wrap it in the equivalent wrapper class (string -> String, etc.) so that we can
12
30
// store a seen flag on it.
@@ -59,33 +77,42 @@ export const transformPageChunk: NonNullable<ResolveOptions['transformPageChunk'
59
77
* // export const handle = sequence(sentryHandle(), yourCustomHandler);
60
78
* ```
61
79
*/
62
- export function sentryHandle ( ) : Handle {
80
+ export function sentryHandle ( handlerOptions ?: SentryHandleOptions ) : Handle {
81
+ const options = {
82
+ handleUnknownRoutes : false ,
83
+ ...handlerOptions ,
84
+ } ;
85
+
86
+ const sentryRequestHandler : Handle = input => {
87
+ // if there is an active transaction, we know that this handle call is nested and hence
88
+ // we don't create a new domain for it. If we created one, nested server calls would
89
+ // create new transactions instead of adding a child span to the currently active span.
90
+ if ( getCurrentHub ( ) . getScope ( ) . getSpan ( ) ) {
91
+ return instrumentHandle ( input , options ) ;
92
+ }
93
+ return runWithAsyncContext ( ( ) => {
94
+ return instrumentHandle ( input , options ) ;
95
+ } ) ;
96
+ } ;
97
+
63
98
return sentryRequestHandler ;
64
99
}
65
100
66
- const sentryRequestHandler : Handle = input => {
67
- // if there is an active transaction, we know that this handle call is nested and hence
68
- // we don't create a new domain for it. If we created one, nested server calls would
69
- // create new transactions instead of adding a child span to the currently active span.
70
- if ( getCurrentHub ( ) . getScope ( ) . getSpan ( ) ) {
71
- return instrumentHandle ( input ) ;
101
+ function instrumentHandle ( { event, resolve } : Parameters < Handle > [ 0 ] , options : SentryHandleOptions ) : ReturnType < Handle > {
102
+ if ( ! event . route ?. id && ! options . handleUnknownRoutes ) {
103
+ return resolve ( event ) ;
72
104
}
73
- return runWithAsyncContext ( ( ) => {
74
- return instrumentHandle ( input ) ;
75
- } ) ;
76
- } ;
77
105
78
- function instrumentHandle ( { event, resolve } : Parameters < Handle > [ 0 ] ) : ReturnType < Handle > {
79
106
const { traceparentData, dynamicSamplingContext } = getTracePropagationData ( event ) ;
80
107
81
108
return trace (
82
109
{
83
110
op : 'http.server' ,
84
- name : `${ event . request . method } ${ event . route . id } ` ,
111
+ name : `${ event . request . method } ${ event . route ? .id || event . url . pathname } ` ,
85
112
status : 'ok' ,
86
113
...traceparentData ,
87
114
metadata : {
88
- source : 'route' ,
115
+ source : event . route . id ? 'route' : 'url ',
89
116
dynamicSamplingContext : traceparentData && ! dynamicSamplingContext ? { } : dynamicSamplingContext ,
90
117
} ,
91
118
} ,
0 commit comments