1
1
import {
2
+ type RequestEventData ,
3
+ type SpanAttributes ,
2
4
captureException ,
3
5
debug ,
4
6
flushIfServerless ,
7
+ getClient ,
5
8
getDefaultIsolationScope ,
6
9
getIsolationScope ,
10
+ httpHeadersToSpanAttributes ,
11
+ httpRequestToRequestData ,
7
12
SEMANTIC_ATTRIBUTE_SENTRY_OP ,
8
13
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ,
9
14
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ,
@@ -22,40 +27,40 @@ import type { EventHandler, EventHandlerRequest, H3Event } from 'h3';
22
27
*/
23
28
export function wrapMiddlewareHandler ( handler : EventHandler , fileName : string ) {
24
29
return async ( event : H3Event < EventHandlerRequest > ) => {
25
- const middlewarePath = event ?. path || event ?. node ?. req ?. url || 'unknown' ;
30
+ debug . log ( `Sentry middleware: ${ fileName } handling ${ event . path } ` ) ;
26
31
27
- debug . log ( `Sentry middleware: ${ fileName } handling ${ middlewarePath } ` ) ;
28
-
29
- const origin = 'auto.http.nuxt' ;
30
32
const isolationScope = getIsolationScope ( ) ;
31
33
const newIsolationScope = isolationScope === getDefaultIsolationScope ( ) ? isolationScope . clone ( ) : isolationScope ;
34
+ const normalizedRequest = createNormalizedRequestData ( event ) ;
35
+ newIsolationScope . setSDKProcessingMetadata ( {
36
+ normalizedRequest,
37
+ } ) ;
38
+
39
+ const attributes = getSpanAttributes ( event ) ;
32
40
33
41
return withIsolationScope ( newIsolationScope , async ( ) => {
34
42
return startSpan (
35
43
{
36
- name : `${ fileName } ` ,
37
- attributes : {
38
- [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'http.server.middleware' ,
39
- [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'route' ,
40
- [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : origin ,
41
- } ,
44
+ name : fileName ,
45
+ attributes,
42
46
} ,
43
47
async span => {
44
48
try {
45
49
const result = await handler ( event ) ;
46
50
span . setStatus ( { code : SPAN_STATUS_OK } ) ;
51
+
47
52
return result ;
48
53
} catch ( error ) {
49
54
span . setStatus ( { code : SPAN_STATUS_ERROR , message : 'internal_error' } ) ;
50
55
span . recordException ( error ) ;
51
56
captureException ( error , {
52
57
mechanism : {
53
58
handled : false ,
54
- type : origin ,
59
+ type : attributes [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] ,
55
60
} ,
56
61
} ) ;
57
-
58
62
span . end ( ) ;
63
+
59
64
// Re-throw the error to be handled by the caller
60
65
throw error ;
61
66
} finally {
@@ -66,3 +71,51 @@ export function wrapMiddlewareHandler(handler: EventHandler, fileName: string) {
66
71
} ) ;
67
72
} ;
68
73
}
74
+
75
+ /**
76
+ * Creates the normalized request data for the middleware handler based on the event.
77
+ */
78
+ function createNormalizedRequestData ( event : H3Event < EventHandlerRequest > ) : RequestEventData {
79
+ // Extract headers from the Node.js request object
80
+ const headers = event . node ?. req ?. headers || { } ;
81
+
82
+ return httpRequestToRequestData ( {
83
+ method : event . method ,
84
+ url : event . path || event . node ?. req ?. url ,
85
+ headers,
86
+ } ) ;
87
+ }
88
+
89
+ /**
90
+ * Gets the span attributes for the middleware handler based on the event.
91
+ */
92
+ function getSpanAttributes ( event : H3Event < EventHandlerRequest > ) : SpanAttributes {
93
+ const attributes : SpanAttributes = {
94
+ [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'http.server.middleware' ,
95
+ [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'custom' ,
96
+ [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.http.nuxt' ,
97
+ } ;
98
+
99
+ // Add HTTP method
100
+ if ( event . method ) {
101
+ attributes [ 'http.request.method' ] = event . method ;
102
+ }
103
+
104
+ // Add route information
105
+ if ( event . path ) {
106
+ attributes [ 'http.route' ] = event . path ;
107
+ }
108
+
109
+ // Extract and add HTTP headers as span attributes
110
+ const client = getClient ( ) ;
111
+ const sendDefaultPii = client ?. getOptions ( ) . sendDefaultPii ?? false ;
112
+
113
+ // Get headers from the Node.js request object
114
+ const headers = event . node ?. req ?. headers || { } ;
115
+ const headerAttributes = httpHeadersToSpanAttributes ( headers , sendDefaultPii ) ;
116
+
117
+ // Merge header attributes with existing attributes
118
+ Object . assign ( attributes , headerAttributes ) ;
119
+
120
+ return attributes ;
121
+ }
0 commit comments