1+ import { getIsolationScope } from '../currentScopes' ;
12import { defineIntegration } from '../integration' ;
3+ import { SEMANTIC_ATTRIBUTE_USER_IP_ADDRESS } from '../semanticAttributes' ;
24import type { Event } from '../types-hoist/event' ;
35import type { IntegrationFn } from '../types-hoist/integration' ;
4- import type { RequestEventData } from '../types-hoist/request' ;
6+ import type { QueryParams , RequestEventData } from '../types-hoist/request' ;
7+ import type { StreamedSpanJSON } from '../types-hoist/span' ;
58import { parseCookie } from '../utils/cookie' ;
9+ import { httpHeadersToSpanAttributes } from '../utils/request' ;
610import { getClientIPAddress , ipHeaderNames } from '../vendor/getIpAddress' ;
11+ import { safeSetSpanJSONAttributes } from '../tracing/spans/captureSpan' ;
712
813interface RequestDataIncludeOptions {
914 cookies ?: boolean ;
@@ -55,6 +60,22 @@ const _requestDataIntegration = ((options: RequestDataIntegrationOptions = {}) =
5560
5661 return event ;
5762 } ,
63+ processSegmentSpan ( span , client ) {
64+ const { sdkProcessingMetadata = { } } = getIsolationScope ( ) . getScopeData ( ) ;
65+ const { normalizedRequest, ipAddress } = sdkProcessingMetadata ;
66+
67+ if ( ! normalizedRequest ) {
68+ return ;
69+ }
70+
71+ const { sendDefaultPii } = client . getOptions ( ) ;
72+ const includeWithDefaultPiiApplied : RequestDataIncludeOptions = {
73+ ...include ,
74+ ip : include . ip ?? sendDefaultPii ,
75+ } ;
76+
77+ addNormalizedRequestDataToSpan ( span , normalizedRequest , ipAddress , includeWithDefaultPiiApplied , sendDefaultPii ) ;
78+ } ,
5879 } ;
5980} ) satisfies IntegrationFn ;
6081
@@ -91,6 +112,60 @@ function addNormalizedRequestDataToEvent(
91112 }
92113}
93114
115+ function addNormalizedRequestDataToSpan (
116+ span : StreamedSpanJSON ,
117+ normalizedRequest : RequestEventData ,
118+ ipAddress : string | undefined ,
119+ include : RequestDataIncludeOptions ,
120+ sendDefaultPii : boolean | undefined ,
121+ ) : void {
122+ const requestData = extractNormalizedRequestData ( normalizedRequest , include ) ;
123+ const attributes : Record < string , unknown > = { } ;
124+
125+ if ( requestData . url ) {
126+ attributes [ 'url.full' ] = requestData . url ;
127+ }
128+
129+ if ( requestData . method ) {
130+ attributes [ 'http.request.method' ] = requestData . method ;
131+ }
132+
133+ if ( requestData . query_string ) {
134+ attributes [ 'url.query' ] = normalizeQueryString ( requestData . query_string ) ;
135+ }
136+
137+ safeSetSpanJSONAttributes ( span , attributes ) ;
138+
139+ // Process cookies before headers so normalizedRequest.cookies takes precedence
140+ // over the raw cookie header (matching the processEvent path).
141+ if ( requestData . cookies && Object . keys ( requestData . cookies ) . length > 0 ) {
142+ const cookieString = Object . entries ( requestData . cookies )
143+ . map ( ( [ name , value ] ) => `${ name } =${ value } ` )
144+ . join ( '; ' ) ;
145+ const cookieAttributes = httpHeadersToSpanAttributes ( { cookie : cookieString } , sendDefaultPii ?? false , 'request' ) ;
146+ safeSetSpanJSONAttributes ( span , cookieAttributes ) ;
147+ }
148+
149+ if ( requestData . headers ) {
150+ const headerAttributes = httpHeadersToSpanAttributes ( requestData . headers , sendDefaultPii ?? false , 'request' ) ;
151+ safeSetSpanJSONAttributes ( span , headerAttributes ) ;
152+ }
153+
154+ if ( requestData . data != null ) {
155+ const serialized = typeof requestData . data === 'string' ? requestData . data : JSON . stringify ( requestData . data ) ;
156+ if ( serialized ) {
157+ safeSetSpanJSONAttributes ( span , { 'http.request.body.data' : serialized } ) ;
158+ }
159+ }
160+
161+ if ( include . ip ) {
162+ const ip = ( normalizedRequest . headers && getClientIPAddress ( normalizedRequest . headers ) ) || ipAddress || undefined ;
163+ if ( ip ) {
164+ safeSetSpanJSONAttributes ( span , { [ SEMANTIC_ATTRIBUTE_USER_IP_ADDRESS ] : ip } ) ;
165+ }
166+ }
167+ }
168+
94169function extractNormalizedRequestData (
95170 normalizedRequest : RequestEventData ,
96171 include : RequestDataIncludeOptions ,
@@ -101,13 +176,10 @@ function extractNormalizedRequestData(
101176 if ( include . headers ) {
102177 requestData . headers = headers ;
103178
104- // Remove the Cookie header in case cookie data should not be included in the event
105179 if ( ! include . cookies ) {
106180 delete ( headers as { cookie ?: string } ) . cookie ;
107181 }
108182
109- // Remove IP headers in case IP data should not be included in the event.
110- // Match case-insensitively — same as getClientIPAddress — so lowercase keys are stripped too.
111183 if ( ! include . ip ) {
112184 const ipHeaderNamesLower = new Set ( ipHeaderNames . map ( name => name . toLowerCase ( ) ) ) ;
113185 for ( const key of Object . keys ( headers ) ) {
@@ -140,3 +212,14 @@ function extractNormalizedRequestData(
140212
141213 return requestData ;
142214}
215+
216+ function normalizeQueryString ( queryString : QueryParams ) : string | undefined {
217+ if ( typeof queryString === 'string' ) {
218+ return queryString || undefined ;
219+ }
220+
221+ const pairs = Array . isArray ( queryString ) ? queryString : Object . entries ( queryString ) ;
222+ const result = pairs . map ( ( [ key , value ] ) => `${ key } =${ value } ` ) . join ( '&' ) ;
223+
224+ return result || undefined ;
225+ }
0 commit comments