@@ -152,15 +152,22 @@ const SENSITIVE_HEADER_SNIPPETS = [
152152const PII_HEADER_SNIPPETS = [ 'x-forwarded-' , '-user' ] ;
153153
154154/**
155- * Converts incoming HTTP request headers to OpenTelemetry span attributes following semantic conventions.
156- * Header names are converted to the format: http.request.header.<key>
155+ * Converts incoming HTTP request or response headers to OpenTelemetry span attributes following semantic conventions.
156+ * Header names are converted to the format: http.< request|response> .header.<key>
157157 * where <key> is the header name in lowercase with dashes converted to underscores.
158158 *
159+ * @param lifecycle - The lifecycle of the headers, either 'request' or 'response'
160+ *
159161 * @see https://opentelemetry.io/docs/specs/semconv/registry/attributes/http/#http-request-header
162+ * @see https://opentelemetry.io/docs/specs/semconv/registry/attributes/http/#http-response-header
163+ *
164+ * @see https://getsentry.github.io/sentry-conventions/attributes/http/#http-request-header-key
165+ * @see https://getsentry.github.io/sentry-conventions/attributes/http/#http-response-header-key
160166 */
161167export function httpHeadersToSpanAttributes (
162168 headers : Record < string , string | string [ ] | undefined > ,
163169 sendDefaultPii : boolean = false ,
170+ lifecycle : 'request' | 'response' = 'request' ,
164171) : Record < string , string > {
165172 const spanAttributes : Record < string , string > = { } ;
166173
@@ -189,10 +196,17 @@ export function httpHeadersToSpanAttributes(
189196
190197 const lowerCasedCookieKey = cookieKey . toLowerCase ( ) ;
191198
192- addSpanAttribute ( spanAttributes , lowerCasedHeaderKey , lowerCasedCookieKey , cookieValue , sendDefaultPii ) ;
199+ addSpanAttribute (
200+ spanAttributes ,
201+ lowerCasedHeaderKey ,
202+ lowerCasedCookieKey ,
203+ cookieValue ,
204+ sendDefaultPii ,
205+ lifecycle ,
206+ ) ;
193207 }
194208 } else {
195- addSpanAttribute ( spanAttributes , lowerCasedHeaderKey , '' , value , sendDefaultPii ) ;
209+ addSpanAttribute ( spanAttributes , lowerCasedHeaderKey , '' , value , sendDefaultPii , lifecycle ) ;
196210 }
197211 } ) ;
198212 } catch {
@@ -212,15 +226,15 @@ function addSpanAttribute(
212226 cookieKey : string ,
213227 value : string | string [ ] | undefined ,
214228 sendPii : boolean ,
229+ lifecycle : 'request' | 'response' ,
215230) : void {
216- const normalizedKey = cookieKey
217- ? `http.request.header.${ normalizeAttributeKey ( headerKey ) } .${ normalizeAttributeKey ( cookieKey ) } `
218- : `http.request.header.${ normalizeAttributeKey ( headerKey ) } ` ;
219-
220231 const headerValue = handleHttpHeader ( cookieKey || headerKey , value , sendPii ) ;
221- if ( headerValue ! == undefined ) {
222- spanAttributes [ normalizedKey ] = headerValue ;
232+ if ( headerValue == null ) {
233+ return ;
223234 }
235+
236+ const normalizedKey = `http.${ lifecycle } .header.${ normalizeAttributeKey ( headerKey ) } ${ cookieKey ? `.${ normalizeAttributeKey ( cookieKey ) } ` : '' } ` ;
237+ spanAttributes [ normalizedKey ] = headerValue ;
224238}
225239
226240function handleHttpHeader (
0 commit comments