Skip to content

Commit

Permalink
✨ [RUM-1210] Add W3C tracecontext to default propagator types (#2443)
Browse files Browse the repository at this point in the history
* Ensure both Datadog and Tracecontext headers are present by default
  • Loading branch information
yannickadam authored Oct 4, 2023
1 parent 4196b47 commit 4c7860f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
14 changes: 8 additions & 6 deletions packages/rum-core/src/domain/configuration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DefaultPrivacyLevel, display } from '@datadog/browser-core'
import type { RumInitConfiguration } from './configuration'
import { serializeRumConfiguration, validateAndBuildRumConfiguration } from './configuration'
import { DEFAULT_PROPAGATOR_TYPES, serializeRumConfiguration, validateAndBuildRumConfiguration } from './configuration'

const DEFAULT_INIT_CONFIGURATION = { clientToken: 'xxx', applicationId: 'xxx' }

Expand Down Expand Up @@ -90,7 +90,7 @@ describe('validateAndBuildRumConfiguration', () => {
allowedTracingUrls: ['foo'],
service: 'bar',
})!.allowedTracingUrls
).toEqual([{ match: 'foo', propagatorTypes: ['datadog'] }])
).toEqual([{ match: 'foo', propagatorTypes: DEFAULT_PROPAGATOR_TYPES }])
})

it('accepts functions', () => {
Expand All @@ -102,7 +102,7 @@ describe('validateAndBuildRumConfiguration', () => {
allowedTracingUrls: [customOriginFunction],
service: 'bar',
})!.allowedTracingUrls
).toEqual([{ match: customOriginFunction, propagatorTypes: ['datadog'] }])
).toEqual([{ match: customOriginFunction, propagatorTypes: DEFAULT_PROPAGATOR_TYPES }])
})

it('accepts RegExp', () => {
Expand All @@ -112,7 +112,7 @@ describe('validateAndBuildRumConfiguration', () => {
allowedTracingUrls: [/az/i],
service: 'bar',
})!.allowedTracingUrls
).toEqual([{ match: /az/i, propagatorTypes: ['datadog'] }])
).toEqual([{ match: /az/i, propagatorTypes: DEFAULT_PROPAGATOR_TYPES }])
})

it('keeps headers', () => {
Expand Down Expand Up @@ -341,12 +341,14 @@ describe('validateAndBuildRumConfiguration', () => {
expect(serializeRumConfiguration(DEFAULT_INIT_CONFIGURATION).selected_tracing_propagators).toEqual([])
})

it('should return Datadog propagator type', () => {
it('should return the default propagator types', () => {
const simpleTracingConfig: RumInitConfiguration = {
...DEFAULT_INIT_CONFIGURATION,
allowedTracingUrls: ['foo'],
}
expect(serializeRumConfiguration(simpleTracingConfig).selected_tracing_propagators).toEqual(['datadog'])
expect(serializeRumConfiguration(simpleTracingConfig).selected_tracing_propagators).toEqual(
DEFAULT_PROPAGATOR_TYPES
)
})

it('should return all propagator types', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/rum-core/src/domain/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import type { RumEvent } from '../rumEvent.types'
import { isTracingOption } from './tracing/tracer'
import type { PropagatorType, TracingOption } from './tracing/tracer.types'

export const DEFAULT_PROPAGATOR_TYPES: PropagatorType[] = ['tracecontext', 'datadog']

export interface RumInitConfiguration extends InitConfiguration {
// global options
applicationId: string
Expand Down Expand Up @@ -144,7 +146,7 @@ function validateAndBuildTracingOptions(initConfiguration: RumInitConfiguration)
const tracingOptions: TracingOption[] = []
initConfiguration.allowedTracingUrls.forEach((option) => {
if (isMatchOption(option)) {
tracingOptions.push({ match: option, propagatorTypes: ['datadog'] })
tracingOptions.push({ match: option, propagatorTypes: DEFAULT_PROPAGATOR_TYPES })
} else if (isTracingOption(option)) {
tracingOptions.push(option)
} else {
Expand All @@ -170,7 +172,7 @@ function getSelectedTracingPropagators(configuration: RumInitConfiguration): Pro
if (Array.isArray(configuration.allowedTracingUrls) && configuration.allowedTracingUrls.length > 0) {
configuration.allowedTracingUrls.forEach((option) => {
if (isMatchOption(option)) {
usedTracingPropagators.add('datadog')
DEFAULT_PROPAGATOR_TYPES.forEach((propagatorType) => usedTracingPropagators.add(propagatorType))
} else if (getType(option) === 'object' && Array.isArray(option.propagatorTypes)) {
// Ensure we have an array, as we cannot rely on types yet (configuration is provided by users)
option.propagatorTypes.forEach((propagatorType) => usedTracingPropagators.add(propagatorType))
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/scenario/rum/tracing.scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ describe('tracing', () => {
checkTraceAssociatedToRumEvent(intakeRegistry)
})

// By default, we send both Datadog and W3C tracecontext headers
function checkRequestHeaders(rawHeaders: string) {
const headers: { [key: string]: string } = JSON.parse(rawHeaders)
expect(headers['x-datadog-trace-id']).toMatch(/\d+/)
expect(headers['x-datadog-origin']).toBe('rum')
expect(headers['traceparent']).toMatch(/^[0-9a-f]{2}-[0-9a-f]{32}-[0-9a-f]{16}-01$/)
expect(headers['x-foo']).toBe('bar, baz')
}

Expand Down

0 comments on commit 4c7860f

Please sign in to comment.