Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/core/src/rum/__tests__/DdRum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { DatadogTracingContext } from '../instrumentation/resourceTracking/distr
import { DatadogTracingIdentifier } from '../instrumentation/resourceTracking/distributedTracing/DatadogTracingIdentifier';
import { TracingIdFormat } from '../instrumentation/resourceTracking/distributedTracing/TracingIdentifier';
import { TracingIdentifierUtils } from '../instrumentation/resourceTracking/distributedTracing/__tests__/__utils__/TracingIdentifierUtils';
import { setCachedRumSessionId } from '../sessionId/sessionIdHelper';
import type { FirstPartyHost } from '../types';
import { ErrorSource, PropagatorType, RumActionType } from '../types';

Expand All @@ -46,6 +47,7 @@ describe('DdRum', () => {
beforeEach(() => {
jest.clearAllMocks();
BufferSingleton.onInitialization();
setCachedRumSessionId(null);
});

describe('Context validation', () => {
Expand Down Expand Up @@ -980,6 +982,32 @@ describe('DdRum', () => {
}
});

it('tracing context contains RUM session ID in baggage when RUM Session ID is cached', () => {
for (let i = 0; i < 100; i++) {
const randomSessionId = `test-${Math.random()}`;
setCachedRumSessionId(randomSessionId);

const tracingContext = DdRum.getTracingContextForPropagators(
[
PropagatorType.DATADOG,
PropagatorType.TRACECONTEXT,
PropagatorType.B3MULTI,
PropagatorType.B3
],
100
);

const requestHeaders = tracingContext.getHeadersForRequest();
expect(requestHeaders).toHaveProperty('baggage');
expect(requestHeaders['baggage']).toBe(
`session.id=${randomSessionId}`
);

const resourceContext = tracingContext.getRumResourceContext();
expect(resourceContext['baggage']).toBeUndefined();
Comment on lines +1006 to +1007
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xgouchet I am explicitly checking that we are not injecting the RUM Session ID in the Resource Context, as this is the current behaviour.

}
});

it('injects headers and context correctly with all propagators and sampling rate (50% 0, 50% 100)', () => {
for (let i = 0; i < 100; i++) {
const tracingSamplingRate =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ export const TRACE_ID_HEADER_KEY = 'x-datadog-trace-id';
export const PARENT_ID_HEADER_KEY = 'x-datadog-parent-id';
export const TAGS_HEADER_KEY = 'x-datadog-tags';
export const DD_TRACE_ID_TAG = '_dd.p.tid';
export const DD_RUM_SESSION_ID_TAG = '_dd.p.rsid';
export const DD_RUM_SESSION_ID_TAG = 'session.id';

/**
* OTel headers
*/
export const TRACECONTEXT_HEADER_KEY = 'traceparent';
export const TRACESTATE_HEADER_KEY = 'tracestate';
export const BAGGAGE_HEADER_KEY = 'baggage';
export const B3_HEADER_KEY = 'b3';
export const B3_MULTI_TRACE_ID_HEADER_KEY = 'X-B3-TraceId';
export const B3_MULTI_SPAN_ID_HEADER_KEY = 'X-B3-SpanId';
Expand Down Expand Up @@ -72,23 +73,12 @@ export const getTracingHeadersFromAttributes = (
)
}
);
if (tracingAttributes.rumSessionId) {
headers.push({
header: TAGS_HEADER_KEY,
value: `${DD_TRACE_ID_TAG}=${tracingAttributes.traceId.toString(
TracingIdFormat.paddedHighHex
)},${DD_RUM_SESSION_ID_TAG}=${
tracingAttributes.rumSessionId
}`
});
} else {
headers.push({
header: TAGS_HEADER_KEY,
value: `${DD_TRACE_ID_TAG}=${tracingAttributes.traceId.toString(
TracingIdFormat.paddedHighHex
)}`
});
}
headers.push({
header: TAGS_HEADER_KEY,
value: `${DD_TRACE_ID_TAG}=${tracingAttributes.traceId.toString(
TracingIdFormat.paddedHighHex
)}`
});
break;
}
case PropagatorType.TRACECONTEXT: {
Expand All @@ -108,8 +98,7 @@ export const getTracingHeadersFromAttributes = (
header: TRACESTATE_HEADER_KEY,
value: generateTraceStateHeader({
parentId: tracingAttributes.spanId,
isSampled,
rumSessionId: tracingAttributes.rumSessionId
isSampled
})
}
);
Expand Down Expand Up @@ -148,6 +137,12 @@ export const getTracingHeadersFromAttributes = (
);
}
}
if (tracingAttributes.rumSessionId) {
headers.push({
header: BAGGAGE_HEADER_KEY,
value: `${DD_RUM_SESSION_ID_TAG}=${tracingAttributes.rumSessionId}`
});
}
});

return headers;
Expand Down Expand Up @@ -241,23 +236,15 @@ const generateTraceContextHeader = ({

const generateTraceStateHeader = ({
parentId,
isSampled,
rumSessionId
isSampled
}: {
parentId: SpanId;
isSampled: boolean;
rumSessionId: string | null;
}) => {
const sampled = `s:${isSampled ? '1' : '0'}`;
const origin = 'o:rum';
const parent = `p:${parentId.toString(TracingIdFormat.paddedHex)}`;
const baseHeaderValue = `dd=${sampled};${origin};${parent}`;
if (rumSessionId) {
const session = `t.rsid:${rumSessionId}`;
return `${baseHeaderValue};${session}`;
} else {
return baseHeaderValue;
}
return `dd=${sampled};${origin};${parent}`;
};

const generateB3Header = ({
Expand Down
Loading