Skip to content

Commit fc66de8

Browse files
authored
Merge pull request #853 from DataDog/xgouchet/RUM-8410/propagate_session_headers
RUM-8410 update the way session id is propagated
2 parents 4095c48 + c0d8faa commit fc66de8

File tree

2 files changed

+45
-30
lines changed

2 files changed

+45
-30
lines changed

packages/core/src/rum/__tests__/DdRum.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { DatadogTracingContext } from '../instrumentation/resourceTracking/distr
2020
import { DatadogTracingIdentifier } from '../instrumentation/resourceTracking/distributedTracing/DatadogTracingIdentifier';
2121
import { TracingIdFormat } from '../instrumentation/resourceTracking/distributedTracing/TracingIdentifier';
2222
import { TracingIdentifierUtils } from '../instrumentation/resourceTracking/distributedTracing/__tests__/__utils__/TracingIdentifierUtils';
23+
import { setCachedRumSessionId } from '../sessionId/sessionIdHelper';
2324
import type { FirstPartyHost } from '../types';
2425
import { ErrorSource, PropagatorType, RumActionType } from '../types';
2526

@@ -46,6 +47,7 @@ describe('DdRum', () => {
4647
beforeEach(() => {
4748
jest.clearAllMocks();
4849
BufferSingleton.onInitialization();
50+
setCachedRumSessionId(null);
4951
});
5052

5153
describe('Context validation', () => {
@@ -980,6 +982,32 @@ describe('DdRum', () => {
980982
}
981983
});
982984

985+
it('tracing context contains RUM session ID in baggage when RUM Session ID is cached', () => {
986+
for (let i = 0; i < 100; i++) {
987+
const randomSessionId = `test-${Math.random()}`;
988+
setCachedRumSessionId(randomSessionId);
989+
990+
const tracingContext = DdRum.getTracingContextForPropagators(
991+
[
992+
PropagatorType.DATADOG,
993+
PropagatorType.TRACECONTEXT,
994+
PropagatorType.B3MULTI,
995+
PropagatorType.B3
996+
],
997+
100
998+
);
999+
1000+
const requestHeaders = tracingContext.getHeadersForRequest();
1001+
expect(requestHeaders).toHaveProperty('baggage');
1002+
expect(requestHeaders['baggage']).toBe(
1003+
`session.id=${randomSessionId}`
1004+
);
1005+
1006+
const resourceContext = tracingContext.getRumResourceContext();
1007+
expect(resourceContext['baggage']).toBeUndefined();
1008+
}
1009+
});
1010+
9831011
it('injects headers and context correctly with all propagators and sampling rate (50% 0, 50% 100)', () => {
9841012
for (let i = 0; i < 100; i++) {
9851013
const tracingSamplingRate =

packages/core/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.ts

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ export const TRACE_ID_HEADER_KEY = 'x-datadog-trace-id';
2828
export const PARENT_ID_HEADER_KEY = 'x-datadog-parent-id';
2929
export const TAGS_HEADER_KEY = 'x-datadog-tags';
3030
export const DD_TRACE_ID_TAG = '_dd.p.tid';
31-
export const DD_RUM_SESSION_ID_TAG = '_dd.p.rsid';
31+
export const DD_RUM_SESSION_ID_TAG = 'session.id';
3232

3333
/**
3434
* OTel headers
3535
*/
3636
export const TRACECONTEXT_HEADER_KEY = 'traceparent';
3737
export const TRACESTATE_HEADER_KEY = 'tracestate';
38+
export const BAGGAGE_HEADER_KEY = 'baggage';
3839
export const B3_HEADER_KEY = 'b3';
3940
export const B3_MULTI_TRACE_ID_HEADER_KEY = 'X-B3-TraceId';
4041
export const B3_MULTI_SPAN_ID_HEADER_KEY = 'X-B3-SpanId';
@@ -72,23 +73,12 @@ export const getTracingHeadersFromAttributes = (
7273
)
7374
}
7475
);
75-
if (tracingAttributes.rumSessionId) {
76-
headers.push({
77-
header: TAGS_HEADER_KEY,
78-
value: `${DD_TRACE_ID_TAG}=${tracingAttributes.traceId.toString(
79-
TracingIdFormat.paddedHighHex
80-
)},${DD_RUM_SESSION_ID_TAG}=${
81-
tracingAttributes.rumSessionId
82-
}`
83-
});
84-
} else {
85-
headers.push({
86-
header: TAGS_HEADER_KEY,
87-
value: `${DD_TRACE_ID_TAG}=${tracingAttributes.traceId.toString(
88-
TracingIdFormat.paddedHighHex
89-
)}`
90-
});
91-
}
76+
headers.push({
77+
header: TAGS_HEADER_KEY,
78+
value: `${DD_TRACE_ID_TAG}=${tracingAttributes.traceId.toString(
79+
TracingIdFormat.paddedHighHex
80+
)}`
81+
});
9282
break;
9383
}
9484
case PropagatorType.TRACECONTEXT: {
@@ -108,8 +98,7 @@ export const getTracingHeadersFromAttributes = (
10898
header: TRACESTATE_HEADER_KEY,
10999
value: generateTraceStateHeader({
110100
parentId: tracingAttributes.spanId,
111-
isSampled,
112-
rumSessionId: tracingAttributes.rumSessionId
101+
isSampled
113102
})
114103
}
115104
);
@@ -148,6 +137,12 @@ export const getTracingHeadersFromAttributes = (
148137
);
149138
}
150139
}
140+
if (tracingAttributes.rumSessionId) {
141+
headers.push({
142+
header: BAGGAGE_HEADER_KEY,
143+
value: `${DD_RUM_SESSION_ID_TAG}=${tracingAttributes.rumSessionId}`
144+
});
145+
}
151146
});
152147

153148
return headers;
@@ -241,23 +236,15 @@ const generateTraceContextHeader = ({
241236

242237
const generateTraceStateHeader = ({
243238
parentId,
244-
isSampled,
245-
rumSessionId
239+
isSampled
246240
}: {
247241
parentId: SpanId;
248242
isSampled: boolean;
249-
rumSessionId: string | null;
250243
}) => {
251244
const sampled = `s:${isSampled ? '1' : '0'}`;
252245
const origin = 'o:rum';
253246
const parent = `p:${parentId.toString(TracingIdFormat.paddedHex)}`;
254-
const baseHeaderValue = `dd=${sampled};${origin};${parent}`;
255-
if (rumSessionId) {
256-
const session = `t.rsid:${rumSessionId}`;
257-
return `${baseHeaderValue};${session}`;
258-
} else {
259-
return baseHeaderValue;
260-
}
247+
return `dd=${sampled};${origin};${parent}`;
261248
};
262249

263250
const generateB3Header = ({

0 commit comments

Comments
 (0)