Skip to content

Commit

Permalink
👌 Renamed variables to follow OTel conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickadam committed Dec 11, 2022
1 parent 36f0463 commit dc7d21d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 69 deletions.
43 changes: 24 additions & 19 deletions packages/rum-core/src/domain/configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ describe('validateAndBuildRumConfiguration', () => {
allowedTracingOrigins: ['foo'],
service: 'bar',
})!.allowedTracingUrls
).toEqual([{ match: 'foo', headersTypes: ['dd'] }])
).toEqual([{ match: 'foo', propagatorTypes: ['datadog'] }])
})

it('accepts functions', () => {
Expand Down Expand Up @@ -219,7 +219,7 @@ describe('validateAndBuildRumConfiguration', () => {
allowedTracingUrls: ['foo'],
service: 'bar',
})!.allowedTracingUrls
).toEqual([{ match: 'foo', headersTypes: ['dd'] }])
).toEqual([{ match: 'foo', propagatorTypes: ['datadog'] }])
})

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

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

it('keeps headers', () => {
expect(
validateAndBuildRumConfiguration({
...DEFAULT_INIT_CONFIGURATION,
allowedTracingUrls: [{ match: 'simple', headersTypes: ['b3m', 'w3c'] }],
allowedTracingUrls: [{ match: 'simple', propagatorTypes: ['b3multi', 'tracecontext'] }],
service: 'bar',
})!.allowedTracingUrls
).toEqual([{ match: 'simple', headersTypes: ['b3m', 'w3c'] }])
).toEqual([{ match: 'simple', propagatorTypes: ['b3multi', 'tracecontext'] }])
})

it('should filter out unexpected parameter types', () => {
expect(
validateAndBuildRumConfiguration({
...DEFAULT_INIT_CONFIGURATION,
service: 'bar',
allowedTracingUrls: [42 as any, undefined, { match: 42 as any, headersTypes: ['dd'] }, { match: 'toto' }],
allowedTracingUrls: [
42 as any,
undefined,
{ match: 42 as any, propagatorTypes: ['datadog'] },
{ match: 'toto' },
],
})!.allowedTracingUrls
).toEqual([])

Expand Down Expand Up @@ -458,32 +463,32 @@ describe('validateAndBuildRumConfiguration', () => {
})

describe('serializeRumConfiguration', () => {
describe('selected tracing headers serialization', () => {
it('should not return any header type', () => {
expect(serializeRumConfiguration(DEFAULT_INIT_CONFIGURATION).selected_tracing_headers).toEqual([])
describe('selected tracing propagators serialization', () => {
it('should not return any propagator type', () => {
expect(serializeRumConfiguration(DEFAULT_INIT_CONFIGURATION).selected_tracing_propagators).toEqual([])
})

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

it('should return all header types', () => {
it('should return all propagator types', () => {
const complexTracingConfig: RumInitConfiguration = {
...DEFAULT_INIT_CONFIGURATION,
allowedTracingUrls: [
'foo',
{ match: 'first', headersTypes: ['dd'] },
{ match: 'test', headersTypes: ['w3c'] },
{ match: 'other', headersTypes: ['b3'] },
{ match: 'final', headersTypes: ['b3m'] },
{ match: 'first', propagatorTypes: ['datadog'] },
{ match: 'test', propagatorTypes: ['tracecontext'] },
{ match: 'other', propagatorTypes: ['b3'] },
{ match: 'final', propagatorTypes: ['b3multi'] },
],
}
expect(serializeRumConfiguration(complexTracingConfig).selected_tracing_headers).toEqual(
jasmine.arrayWithExactContents(['dd', 'b3', 'b3m', 'w3c'])
expect(serializeRumConfiguration(complexTracingConfig).selected_tracing_propagators).toEqual(
jasmine.arrayWithExactContents(['datadog', 'b3', 'b3multi', 'tracecontext'])
)
})
})
Expand Down
28 changes: 14 additions & 14 deletions packages/rum-core/src/domain/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import type { RumEventDomainContext } from '../domainContext.types'
import type { RumEvent } from '../rumEvent.types'
import { isTracingOption } from './tracing/tracer'
import type { TracingOption, TracingHeadersType } from './tracing/tracer.types'
import type { PropagatorType, TracingOption } from './tracing/tracer.types'

export interface RumInitConfiguration extends InitConfiguration {
// global options
Expand Down Expand Up @@ -171,7 +171,7 @@ function validateAndBuildTracingOptions(initConfiguration: RumInitConfiguration)
const tracingOptions: TracingOption[] = []
initConfiguration.allowedTracingUrls.forEach((option) => {
if (isMatchOption(option)) {
tracingOptions.push({ match: option, headersTypes: ['dd'] })
tracingOptions.push({ match: option, propagatorTypes: ['datadog'] })
} else if (isTracingOption(option)) {
tracingOptions.push(option)
} else {
Expand Down Expand Up @@ -228,31 +228,31 @@ function convertLegacyMatchOptionToTracingOption(item: MatchOption): TracingOpti
return undefined
}

return { match, headersTypes: ['dd'] }
return { match, propagatorTypes: ['datadog'] }
}

/**
* Combines the selected tracing headers from the different options in allowedTracingUrls,
* and assumes 'dd' has been selected when using allowedTracingOrigins
* Combines the selected tracing propagators from the different options in allowedTracingUrls,
* and assumes 'datadog' has been selected when using allowedTracingOrigins
*/
function getSelectedTracingHeaders(configuration: RumInitConfiguration): TracingHeadersType[] {
const usedTracingHeaders = new Set<TracingHeadersType>()
function getSelectedTracingPropagators(configuration: RumInitConfiguration): PropagatorType[] {
const usedTracingPropagators = new Set<PropagatorType>()

if (Array.isArray(configuration.allowedTracingUrls) && configuration.allowedTracingUrls.length > 0) {
configuration.allowedTracingUrls.forEach((config) => {
if (isMatchOption(config)) {
usedTracingHeaders.add('dd')
configuration.allowedTracingUrls.forEach((option) => {
if (isMatchOption(option)) {
usedTracingPropagators.add('datadog')
} else {
config.headersTypes.forEach((headerType) => usedTracingHeaders.add(headerType))
option.propagatorTypes.forEach((propagatorType) => usedTracingPropagators.add(propagatorType))
}
})
}

if (Array.isArray(configuration.allowedTracingOrigins) && configuration.allowedTracingOrigins.length > 0) {
usedTracingHeaders.add('dd')
usedTracingPropagators.add('datadog')
}

return arrayFrom(usedTracingHeaders)
return arrayFrom(usedTracingPropagators)
}

export function serializeRumConfiguration(configuration: RumInitConfiguration): RawTelemetryConfiguration {
Expand All @@ -269,7 +269,7 @@ export function serializeRumConfiguration(configuration: RumInitConfiguration):
Array.isArray(configuration.allowedTracingOrigins) && configuration.allowedTracingOrigins.length > 0,
use_allowed_tracing_urls:
Array.isArray(configuration.allowedTracingUrls) && configuration.allowedTracingUrls.length > 0,
selected_tracing_headers: getSelectedTracingHeaders(configuration),
selected_tracing_propagators: getSelectedTracingPropagators(configuration),
default_privacy_level: configuration.defaultPrivacyLevel,
use_excluded_activity_urls:
Array.isArray(configuration.allowedTracingOrigins) && configuration.allowedTracingOrigins.length > 0,
Expand Down
44 changes: 22 additions & 22 deletions packages/rum-core/src/domain/tracing/tracer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('tracer', () => {
clientToken: 'xxx',
applicationId: 'xxx',
service: 'service',
allowedTracingUrls: [{ match: window.location.origin, headersTypes: ['dd'] }],
allowedTracingUrls: [{ match: window.location.origin, propagatorTypes: ['datadog'] }],
}

beforeEach(() => {
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('tracer', () => {
const configurationWithAllOtelHeaders = validateAndBuildRumConfiguration({
...INIT_CONFIGURATION,
tracingSampleRate: 50,
allowedTracingUrls: [{ match: window.location.origin, headersTypes: ['b3', 'w3c', 'b3m'] }],
allowedTracingUrls: [{ match: window.location.origin, propagatorTypes: ['b3', 'tracecontext', 'b3multi'] }],
})!

const tracer = startTracer(configurationWithAllOtelHeaders, sessionManager)
Expand Down Expand Up @@ -157,13 +157,13 @@ describe('tracer', () => {
expect(context.spanId).toBeDefined()
})

it('should add only B3 Multiple headers', () => {
const configurationWithB3Multi = validateAndBuildRumConfiguration({
it('should add headers only for B3 Multiple propagator', () => {
const configurationWithb3multi = validateAndBuildRumConfiguration({
...INIT_CONFIGURATION,
allowedTracingUrls: [{ match: window.location.origin, headersTypes: ['b3m'] }],
allowedTracingUrls: [{ match: window.location.origin, propagatorTypes: ['b3multi'] }],
})!

const tracer = startTracer(configurationWithB3Multi, sessionManager)
const tracer = startTracer(configurationWithb3multi, sessionManager)
const context = { ...ALLOWED_DOMAIN_CONTEXT }
tracer.traceXhr(context, xhrStub as unknown as XMLHttpRequest)

Expand All @@ -181,13 +181,13 @@ describe('tracer', () => {
expect(xhrStub.headers['x-datadog-sampling-priority']).toBeUndefined()
})

it('should add B3 (single) and W3C headers', () => {
const configurationWithB3andW3C = validateAndBuildRumConfiguration({
it('should add headers for B3 (single) and tracecontext propagators', () => {
const configurationWithB3andTracecontext = validateAndBuildRumConfiguration({
...INIT_CONFIGURATION,
allowedTracingUrls: [{ match: window.location.origin, headersTypes: ['b3', 'w3c'] }],
allowedTracingUrls: [{ match: window.location.origin, propagatorTypes: ['b3', 'tracecontext'] }],
})!

const tracer = startTracer(configurationWithB3andW3C, sessionManager)
const tracer = startTracer(configurationWithB3andTracecontext, sessionManager)
const context = { ...ALLOWED_DOMAIN_CONTEXT }
tracer.traceXhr(context, xhrStub as unknown as XMLHttpRequest)

Expand All @@ -202,7 +202,7 @@ describe('tracer', () => {
it('should not add any headers', () => {
const configurationWithoutHeaders = validateAndBuildRumConfiguration({
...INIT_CONFIGURATION,
allowedTracingUrls: [{ match: window.location.origin, headersTypes: [] }],
allowedTracingUrls: [{ match: window.location.origin, propagatorTypes: [] }],
})!

const tracer = startTracer(configurationWithoutHeaders, sessionManager)
Expand All @@ -215,10 +215,10 @@ describe('tracer', () => {
expect(xhrStub.headers['X-B3-TraceId']).toBeUndefined()
})

it('should ignore wrong header types', () => {
it('should ignore wrong propagator types', () => {
const configurationWithBadParams = validateAndBuildRumConfiguration({
...INIT_CONFIGURATION,
allowedTracingUrls: [{ match: window.location.origin, headersTypes: ['foo', 32, () => true] as any }],
allowedTracingUrls: [{ match: window.location.origin, propagatorTypes: ['foo', 32, () => true] as any }],
})!

const tracer = startTracer(configurationWithBadParams, sessionManager)
Expand Down Expand Up @@ -469,13 +469,13 @@ describe('tracer', () => {
expect(dynamicDomainContext.spanId).toBeDefined()
})

it('should add only B3 Multiple headers', () => {
const configurationWithB3Multi = validateAndBuildRumConfiguration({
it('should add headers only for B3 Multiple propagator', () => {
const configurationWithb3multi = validateAndBuildRumConfiguration({
...INIT_CONFIGURATION,
allowedTracingUrls: [{ match: window.location.origin, headersTypes: ['b3m'] }],
allowedTracingUrls: [{ match: window.location.origin, propagatorTypes: ['b3multi'] }],
})!

const tracer = startTracer(configurationWithB3Multi, sessionManager)
const tracer = startTracer(configurationWithb3multi, sessionManager)
const context: Partial<RumFetchStartContext> = { ...ALLOWED_DOMAIN_CONTEXT }
tracer.traceFetch(context)

Expand All @@ -497,13 +497,13 @@ describe('tracer', () => {
expect(context.init!.headers).not.toContain(jasmine.arrayContaining(['x-datadog-sampling-priority']))
})

it('should add B3 (single) and W3C headers', () => {
const configurationWithB3andW3C = validateAndBuildRumConfiguration({
it('should add headers for b3 (single) and tracecontext propagators', () => {
const configurationWithB3andTracecontext = validateAndBuildRumConfiguration({
...INIT_CONFIGURATION,
allowedTracingUrls: [{ match: window.location.origin, headersTypes: ['b3', 'w3c'] }],
allowedTracingUrls: [{ match: window.location.origin, propagatorTypes: ['b3', 'tracecontext'] }],
})!

const tracer = startTracer(configurationWithB3andW3C, sessionManager)
const tracer = startTracer(configurationWithB3andTracecontext, sessionManager)
const context: Partial<RumFetchStartContext> = { ...ALLOWED_DOMAIN_CONTEXT }
tracer.traceFetch(context)

Expand All @@ -518,7 +518,7 @@ describe('tracer', () => {
it('should not add any headers', () => {
const configurationWithoutHeaders = validateAndBuildRumConfiguration({
...INIT_CONFIGURATION,
allowedTracingUrls: [{ match: window.location.origin, headersTypes: [] }],
allowedTracingUrls: [{ match: window.location.origin, propagatorTypes: [] }],
})!

const tracer = startTracer(configurationWithoutHeaders, sessionManager)
Expand Down
20 changes: 11 additions & 9 deletions packages/rum-core/src/domain/tracing/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
RumXhrStartContext,
} from '../requestCollection'
import type { RumSessionManager } from '../rumSessionManager'
import type { TracingOption, TracingHeadersType } from './tracer.types'
import type { PropagatorType, TracingOption } from './tracer.types'

export interface Tracer {
traceFetch: (context: Partial<RumFetchStartContext>) => void
Expand All @@ -32,7 +32,9 @@ interface TracingHeaders {
export function isTracingOption(item: unknown): item is TracingOption {
const expectedItem = item as TracingOption
return (
getType(expectedItem) === 'object' && isMatchOption(expectedItem.match) && Array.isArray(expectedItem.headersTypes)
getType(expectedItem) === 'object' &&
isMatchOption(expectedItem.match) &&
Array.isArray(expectedItem.propagatorTypes)
)
}

Expand Down Expand Up @@ -119,7 +121,7 @@ function injectHeadersIfTracingAllowed(
context.traceId = new TraceIdentifier()
context.spanId = new TraceIdentifier()
context.traceSampled = !isNumber(configuration.tracingSampleRate) || performDraw(configuration.tracingSampleRate)
inject(makeTracingHeaders(context.traceId, context.spanId, context.traceSampled, tracingOption.headersTypes))
inject(makeTracingHeaders(context.traceId, context.spanId, context.traceSampled, tracingOption.propagatorTypes))
}

export function isTracingSupported() {
Expand All @@ -138,13 +140,13 @@ function makeTracingHeaders(
traceId: TraceIdentifier,
spanId: TraceIdentifier,
traceSampled: boolean,
requestedHeadersTypes: TracingHeadersType[]
propagatorTypes: PropagatorType[]
): TracingHeaders {
const tracingHeaders: TracingHeaders = {}

requestedHeadersTypes.forEach((requestedHeader) => {
switch (requestedHeader) {
case 'dd': {
propagatorTypes.forEach((propagatorType) => {
switch (propagatorType) {
case 'datadog': {
assign(tracingHeaders, {
'x-datadog-origin': 'rum',
'x-datadog-parent-id': spanId.toDecimalString(),
Expand All @@ -154,7 +156,7 @@ function makeTracingHeaders(
break
}
// https://www.w3.org/TR/trace-context/
case 'w3c': {
case 'tracecontext': {
assign(tracingHeaders, {
traceparent: `00-0000000000000000${traceId.toPaddedHexadecimalString()}-${spanId.toPaddedHexadecimalString()}-0${
traceSampled ? '1' : '0'
Expand All @@ -171,7 +173,7 @@ function makeTracingHeaders(
})
break
}
case 'b3m': {
case 'b3multi': {
assign(tracingHeaders, {
'X-B3-TraceId': traceId.toPaddedHexadecimalString(),
'X-B3-SpanId': spanId.toPaddedHexadecimalString(),
Expand Down
10 changes: 5 additions & 5 deletions packages/rum-core/src/domain/tracing/tracer.types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { MatchOption } from '@datadog/browser-core'

/**
* dd: Datadog (x-datadog-*)
* w3c: Trace Context (traceparent)
* datadog: Datadog (x-datadog-*)
* tracecontext: W3C Trace Context (traceparent)
* b3: B3 Single Header (b3)
* b3m: B3 Multi Headers (X-B3-*)
* b3multi: B3 Multiple Headers (X-B3-*)
*/
export type TracingHeadersType = 'dd' | 'b3' | 'b3m' | 'w3c'
export type TracingOption = { match: MatchOption; headersTypes: TracingHeadersType[] }
export type PropagatorType = 'datadog' | 'b3' | 'b3multi' | 'tracecontext'
export type TracingOption = { match: MatchOption; propagatorTypes: PropagatorType[] }

0 comments on commit dc7d21d

Please sign in to comment.