@@ -22,31 +22,7 @@ export const DEFAULT_PROPAGATOR_TYPES: PropagatorType[] = ['tracecontext', 'data
2222
2323/**
2424 * Default list of headers collected on resource events when {@link RumInitConfiguration.trackResourceHeaders | trackResourceHeaders}
25- * is set to `true`. Re-exported by the `@datadog/browser-rum` and `@datadog/browser-rum-slim` packages, and exposed on the
26- * `DD_RUM` global object when the SDK is loaded via the CDN, so it can be referenced when building a custom matcher list.
27- *
28- * @example NPM
29- * ```ts
30- * import { datadogRum, DEFAULT_TRACKED_RESOURCE_HEADERS } from '@datadog/browser-rum'
31- *
32- * datadogRum.init({
33- * // ...
34- * trackResourceHeaders: [
35- * ...DEFAULT_TRACKED_RESOURCE_HEADERS.map((name) => ({ name })),
36- * { name: 'x-request-id' },
37- * ],
38- * })
39- * ```
40- * @example CDN
41- * ```ts
42- * DD_RUM.init({
43- * // ...
44- * trackResourceHeaders: [
45- * ...DD_RUM.DEFAULT_TRACKED_RESOURCE_HEADERS.map((name) => ({ name })),
46- * { name: 'x-request-id' },
47- * ],
48- * })
49- * ```
25+ * is set to `true`.
5026 */
5127export const DEFAULT_TRACKED_RESOURCE_HEADERS = [
5228 'cache-control' ,
@@ -283,9 +259,9 @@ export interface RumInitConfiguration extends InitConfiguration {
283259 *
284260 * - `true`: collect {@link DEFAULT_TRACKED_RESOURCE_HEADERS} for all URLs, both directions
285261 * - `MatchHeader[]`: each {@link MatchHeader} targets a header name, with optional URL scope
286- * (`url`), value extraction (`extractor`), and `location`. By default, both request and
287- * response headers are captured; set `location` to `' request'` or `' response'` to restrict
288- * to one.
262+ * (`url`), value extraction (`extractor`), and `location`. When `name` is omitted, the matcher
263+ * applies to { @link DEFAULT_TRACKED_RESOURCE_HEADERS}. By default, both request and response
264+ * headers are captured; set `location` to `'request'` or `'response'` to restrict to one.
289265 *
290266 * Headers whose names match a built-in sensitive-data pattern are always dropped, regardless
291267 * of the configured matchers. The pattern blocks headers whose names contain: `token`, `cookie`,
@@ -296,10 +272,10 @@ export interface RumInitConfiguration extends InitConfiguration {
296272 * @defaultValue false (disabled)
297273 * @example
298274 * // Collect default headers plus custom ones for all URLs
299- * trackResourceHeaders: [
300- * ...DEFAULT_TRACKED_RESOURCE_HEADERS.map((h) => ({ name: h })),
301- * { name: 'x-request-id' },
302- * ]
275+ * trackResourceHeaders: [{}, { name: 'x-request-id' }]
276+ * @example
277+ * // Collect default headers from responses only
278+ * trackResourceHeaders: [{ location: 'response' } ]
303279 * @example
304280 * // URL-scoped rule: capture specific response headers only for calls to /api
305281 * trackResourceHeaders: [{ url: /\/api\//, name: 'cache-control', location: 'response' }]
@@ -386,7 +362,7 @@ export interface GraphQlUrlOption {
386362
387363export interface MatchHeader {
388364 url ?: MatchOption
389- name : MatchOption
365+ name ? : MatchOption
390366 extractor ?: RegExp
391367 location ?: 'request' | 'response' | 'any'
392368}
@@ -613,8 +589,12 @@ function validateAndBuildTrackResourceHeaders(initConfiguration: RumInitConfigur
613589 const result : MatchHeader [ ] = [ ]
614590
615591 option . forEach ( ( item , index ) => {
616- if ( ! isIndexableObject ( item ) || ! isMatchOption ( item . name ) ) {
617- display . warn ( `trackResourceHeaders[${ index } ] should be a MatchHeader object with a 'name' property` )
592+ if ( ! isIndexableObject ( item ) ) {
593+ display . warn ( `trackResourceHeaders[${ index } ] should be a MatchHeader object` )
594+ return
595+ }
596+ if ( item . name !== undefined && ! isMatchOption ( item . name ) ) {
597+ display . warn ( `trackResourceHeaders[${ index } ].name should be a MatchOption` )
618598 return
619599 }
620600 if ( item . url !== undefined && ! isMatchOption ( item . url ) ) {
@@ -625,14 +605,18 @@ function validateAndBuildTrackResourceHeaders(initConfiguration: RumInitConfigur
625605 display . warn ( `trackResourceHeaders[${ index } ].extractor should be a RegExp` )
626606 return
627607 }
628- if ( item . location !== undefined && ! VALID_HEADER_LOCATIONS . includes ( item . location ) ) {
608+ if (
609+ item . location !== undefined &&
610+ ( typeof item . location !== 'string' || ! VALID_HEADER_LOCATIONS . includes ( item . location ) )
611+ ) {
629612 display . warn ( `trackResourceHeaders[${ index } ].location should be 'request', 'response', or 'any'` )
630613 return
631614 }
632615
616+ const { name, ...rest } = item
633617 result . push ( {
634- ...item ,
635- name : typeof item . name === 'string' ? item . name . toLowerCase ( ) : item . name ,
618+ ...rest ,
619+ ... ( name !== undefined ? { name : typeof name === 'string' ? name . toLowerCase ( ) : name } : { } ) ,
636620 } )
637621 } )
638622
0 commit comments