Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into benoit/make-xhr-proxy…
Browse files Browse the repository at this point in the history
…-stoppable
  • Loading branch information
BenoitZugmeyer committed Oct 14, 2021
2 parents 378fb4e + 332724b commit a157deb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/rum-core/src/boot/startRum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function startRum(
const { addError } = startErrorCollection(lifeCycle, foregroundContexts)
const { addAction } = startActionCollection(lifeCycle, domMutationObservable, configuration, foregroundContexts)

startRequestCollection(lifeCycle, configuration)
startRequestCollection(lifeCycle, configuration, session)
startPerformanceCollection(lifeCycle, configuration)

const internalContext = startInternalContext(initConfiguration.applicationId, session, parentContexts, urlContexts)
Expand Down
5 changes: 3 additions & 2 deletions packages/rum-core/src/domain/requestCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
FetchStartContext,
FetchCompleteContext,
} from '@datadog/browser-core'
import { RumSession } from '..'
import { LifeCycle, LifeCycleEventType } from './lifeCycle'
import { isAllowedRequestUrl } from './rumEventsCollection/resource/resourceUtils'
import { startTracer, TraceIdentifier, Tracer } from './tracing/tracer'
Expand Down Expand Up @@ -49,8 +50,8 @@ export interface RequestCompleteEvent {

let nextRequestIndex = 1

export function startRequestCollection(lifeCycle: LifeCycle, configuration: Configuration) {
const tracer = startTracer(configuration)
export function startRequestCollection(lifeCycle: LifeCycle, configuration: Configuration, session: RumSession) {
const tracer = startTracer(configuration, session)
trackXhr(lifeCycle, configuration, tracer)
trackFetch(lifeCycle, configuration, tracer)
}
Expand Down
41 changes: 27 additions & 14 deletions packages/rum-core/src/domain/tracing/tracer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Configuration, DEFAULT_CONFIGURATION, isIE, objectEntries } from '@datadog/browser-core'
import { setup, TestSetupBuilder } from '../../../test/specHelper'
import { createRumSessionMock, RumSessionMock } from '../../../test/mockRumSession'
import { RumFetchCompleteContext, RumFetchStartContext, RumXhrStartContext } from '../requestCollection'
import { startTracer, TraceIdentifier } from './tracer'

Expand All @@ -15,9 +16,11 @@ describe('tracer', () => {
url: 'http://foo.com',
}
let setupBuilder: TestSetupBuilder
let session: RumSessionMock

beforeEach(() => {
setupBuilder = setup()
session = createRumSessionMock()
})

afterEach(() => {
Expand All @@ -41,7 +44,7 @@ describe('tracer', () => {
})

it('should add traceId and spanId to context and add tracing headers', () => {
const tracer = startTracer(configuration as Configuration)
const tracer = startTracer(configuration as Configuration, session)
const context = { ...ALLOWED_DOMAIN_CONTEXT }
tracer.traceXhr(context, (xhrStub as unknown) as XMLHttpRequest)

Expand All @@ -51,7 +54,7 @@ describe('tracer', () => {
})

it('should not trace request on disallowed domain', () => {
const tracer = startTracer(configuration as Configuration)
const tracer = startTracer(configuration as Configuration, session)
const context = { ...DISALLOWED_DOMAIN_CONTEXT }
tracer.traceXhr(context, (xhrStub as unknown) as XMLHttpRequest)

Expand All @@ -60,14 +63,24 @@ describe('tracer', () => {
expect(xhrStub.headers).toEqual({})
})

it('should not trace request during untracked session', () => {
const tracer = startTracer(configuration as Configuration, session.setNotTracked())
const context = { ...ALLOWED_DOMAIN_CONTEXT }
tracer.traceXhr(context, (xhrStub as unknown) as XMLHttpRequest)

expect(context.traceId).toBeUndefined()
expect(context.spanId).toBeUndefined()
expect(xhrStub.headers).toEqual({})
})

it('should trace requests on configured origins', () => {
const configurationWithTracingUrls: Partial<Configuration> = {
...configuration,
allowedTracingOrigins: [/^https?:\/\/qux\.com/, 'http://bar.com'],
}
const stub = (xhrStub as unknown) as XMLHttpRequest

const tracer = startTracer(configurationWithTracingUrls as Configuration)
const tracer = startTracer(configurationWithTracingUrls as Configuration, session)

let context: Partial<RumXhrStartContext> = { url: 'http://qux.com' }
tracer.traceXhr(context, stub)
Expand All @@ -89,7 +102,7 @@ describe('tracer', () => {

it('should add traceId and spanId to context, and add tracing headers', () => {
const context: Partial<RumFetchStartContext> = { ...ALLOWED_DOMAIN_CONTEXT }
const tracer = startTracer(configuration as Configuration)
const tracer = startTracer(configuration as Configuration, session)
tracer.traceFetch(context)

expect(context.traceId).toBeDefined()
Expand All @@ -104,7 +117,7 @@ describe('tracer', () => {
init,
}

const tracer = startTracer(configuration as Configuration)
const tracer = startTracer(configuration as Configuration, session)
tracer.traceFetch(context)

expect(context.init).not.toBe(init)
Expand All @@ -121,7 +134,7 @@ describe('tracer', () => {
init: { headers, method: 'POST' },
}

const tracer = startTracer(configuration as Configuration)
const tracer = startTracer(configuration as Configuration, session)
tracer.traceFetch(context)

expect(context.init!.headers).not.toBe(headers)
Expand All @@ -142,7 +155,7 @@ describe('tracer', () => {
init: { headers, method: 'POST' },
}

const tracer = startTracer(configuration as Configuration)
const tracer = startTracer(configuration as Configuration, session)
tracer.traceFetch(context)

expect(context.init!.headers).not.toBe(headers)
Expand All @@ -167,7 +180,7 @@ describe('tracer', () => {
init: { headers, method: 'POST' },
}

const tracer = startTracer(configuration as Configuration)
const tracer = startTracer(configuration as Configuration, session)
tracer.traceFetch(context)

expect(context.init!.headers).not.toBe(headers)
Expand Down Expand Up @@ -195,7 +208,7 @@ describe('tracer', () => {
input: request,
}

const tracer = startTracer(configuration as Configuration)
const tracer = startTracer(configuration as Configuration, session)
tracer.traceFetch(context)

expect(context.init).toBe(undefined)
Expand All @@ -216,7 +229,7 @@ describe('tracer', () => {
}),
}

const tracer = startTracer(configuration as Configuration)
const tracer = startTracer(configuration as Configuration, session)
tracer.traceFetch(context)

expect(context.init!.headers).toEqual([
Expand All @@ -228,7 +241,7 @@ describe('tracer', () => {
it('should not trace request on disallowed domain', () => {
const context: Partial<RumFetchStartContext> = { ...DISALLOWED_DOMAIN_CONTEXT }

const tracer = startTracer(configuration as Configuration)
const tracer = startTracer(configuration as Configuration, session)
tracer.traceFetch(context)

expect(context.traceId).toBeUndefined()
Expand All @@ -244,7 +257,7 @@ describe('tracer', () => {
const quxDomainContext: Partial<RumFetchStartContext> = { url: 'http://qux.com' }
const barDomainContext: Partial<RumFetchStartContext> = { url: 'http://bar.com' }

const tracer = startTracer(configurationWithTracingUrls as Configuration)
const tracer = startTracer(configurationWithTracingUrls as Configuration, session)

tracer.traceFetch(quxDomainContext)
tracer.traceFetch(barDomainContext)
Expand All @@ -257,7 +270,7 @@ describe('tracer', () => {

describe('clearTracingIfCancelled', () => {
it('should clear tracing if status is 0', () => {
const tracer = startTracer(configuration as Configuration)
const tracer = startTracer(configuration as Configuration, session)
const context: RumFetchCompleteContext = {
status: 0,

Expand All @@ -271,7 +284,7 @@ describe('tracer', () => {
})

it('should not clear tracing if status is not 0', () => {
const tracer = startTracer(configuration as Configuration)
const tracer = startTracer(configuration as Configuration, session)
const context: RumFetchCompleteContext = {
status: 200,

Expand Down
10 changes: 6 additions & 4 deletions packages/rum-core/src/domain/tracing/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
RumXhrCompleteContext,
RumXhrStartContext,
} from '../requestCollection'
import { RumSession } from '../rumSession'

export interface Tracer {
traceFetch: (context: Partial<RumFetchStartContext>) => void
Expand Down Expand Up @@ -40,11 +41,11 @@ export function clearTracingIfNeeded(context: RumFetchCompleteContext | RumXhrCo
}
}

export function startTracer(configuration: Configuration): Tracer {
export function startTracer(configuration: Configuration, session: RumSession): Tracer {
return {
clearTracingIfNeeded,
traceFetch: (context) =>
injectHeadersIfTracingAllowed(configuration, context, (tracingHeaders: TracingHeaders) => {
injectHeadersIfTracingAllowed(configuration, context, session, (tracingHeaders: TracingHeaders) => {
if (context.input instanceof Request && !context.init?.headers) {
context.input = new Request(context.input)
Object.keys(tracingHeaders).forEach((key) => {
Expand All @@ -70,7 +71,7 @@ export function startTracer(configuration: Configuration): Tracer {
}
}),
traceXhr: (context, xhr) =>
injectHeadersIfTracingAllowed(configuration, context, (tracingHeaders: TracingHeaders) => {
injectHeadersIfTracingAllowed(configuration, context, session, (tracingHeaders: TracingHeaders) => {
Object.keys(tracingHeaders).forEach((name) => {
xhr.setRequestHeader(name, tracingHeaders[name])
})
Expand All @@ -81,9 +82,10 @@ export function startTracer(configuration: Configuration): Tracer {
function injectHeadersIfTracingAllowed(
configuration: Configuration,
context: Partial<RumFetchStartContext | RumXhrStartContext>,
session: RumSession,
inject: (tracingHeaders: TracingHeaders) => void
) {
if (!isTracingSupported() || !isAllowedUrl(configuration, context.url!)) {
if (!isTracingSupported() || !isAllowedUrl(configuration, context.url!) || !session.isTracked()) {
return
}

Expand Down

0 comments on commit a157deb

Please sign in to comment.