11import {
2+ type RequestEventData ,
3+ type SpanAttributes ,
24 captureException ,
35 debug ,
46 flushIfServerless ,
7+ getClient ,
58 getDefaultIsolationScope ,
69 getIsolationScope ,
10+ httpHeadersToSpanAttributes ,
11+ httpRequestToRequestData ,
712 SEMANTIC_ATTRIBUTE_SENTRY_OP ,
813 SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ,
914 SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ,
@@ -22,40 +27,40 @@ import type { EventHandler, EventHandlerRequest, H3Event } from 'h3';
2227 */
2328export function wrapMiddlewareHandler ( handler : EventHandler , fileName : string ) {
2429 return async ( event : H3Event < EventHandlerRequest > ) => {
25- const middlewarePath = event ?. path || event ?. node ?. req ?. url || 'unknown' ;
30+ debug . log ( `Sentry middleware: ${ fileName } handling ${ event . path } ` ) ;
2631
27- debug . log ( `Sentry middleware: ${ fileName } handling ${ middlewarePath } ` ) ;
28-
29- const origin = 'auto.http.nuxt' ;
3032 const isolationScope = getIsolationScope ( ) ;
3133 const newIsolationScope = isolationScope === getDefaultIsolationScope ( ) ? isolationScope . clone ( ) : isolationScope ;
34+ const normalizedRequest = createNormalizedRequestData ( event ) ;
35+ newIsolationScope . setSDKProcessingMetadata ( {
36+ normalizedRequest,
37+ } ) ;
38+
39+ const attributes = getSpanAttributes ( event ) ;
3240
3341 return withIsolationScope ( newIsolationScope , async ( ) => {
3442 return startSpan (
3543 {
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,
4246 } ,
4347 async span => {
4448 try {
4549 const result = await handler ( event ) ;
4650 span . setStatus ( { code : SPAN_STATUS_OK } ) ;
51+
4752 return result ;
4853 } catch ( error ) {
4954 span . setStatus ( { code : SPAN_STATUS_ERROR , message : 'internal_error' } ) ;
5055 span . recordException ( error ) ;
5156 captureException ( error , {
5257 mechanism : {
5358 handled : false ,
54- type : origin ,
59+ type : attributes [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] ,
5560 } ,
5661 } ) ;
57-
5862 span . end ( ) ;
63+
5964 // Re-throw the error to be handled by the caller
6065 throw error ;
6166 } finally {
@@ -66,3 +71,51 @@ export function wrapMiddlewareHandler(handler: EventHandler, fileName: string) {
6671 } ) ;
6772 } ;
6873}
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