-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathaddHeadersAsAttributes.ts
More file actions
27 lines (22 loc) · 904 Bytes
/
addHeadersAsAttributes.ts
File metadata and controls
27 lines (22 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import type { Span, WebFetchHeaders } from '@sentry/core';
import { getClient, httpHeadersToSpanAttributes, winterCGHeadersToDict } from '@sentry/core';
/**
* Extracts HTTP request headers as span attributes and optionally applies them to a span.
*/
export function addHeadersAsAttributes(
headers: WebFetchHeaders | Headers | Record<string, string | string[] | undefined> | undefined,
span?: Span,
): Record<string, string> {
if (!headers) {
return {};
}
const headersDict: Record<string, string | string[] | undefined> =
headers instanceof Headers || (typeof headers === 'object' && 'get' in headers)
? winterCGHeadersToDict(headers as Headers)
: headers;
const headerAttributes = httpHeadersToSpanAttributes(headersDict, getClient()?.getOptions().sendDefaultPii ?? false);
if (span) {
span.setAttributes(headerAttributes);
}
return headerAttributes;
}