Skip to content

Commit 8ea0ee1

Browse files
fpfp100jsl517
andauthored
Expose session Id for baggage builder (#71)
* session id * fix test * comment and fix test * fix build failure * remove session id from setRequestContext --------- Co-authored-by: jsl517 <pefan@microsoft.com>
1 parent c27346a commit 8ea0ee1

4 files changed

Lines changed: 50 additions & 5 deletions

File tree

packages/agents-a365-observability/src/tracing/middleware/BaggageBuilder.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ export class BaggageBuilder {
9999
return this;
100100
}
101101

102+
/**
103+
* Set the session ID baggage value.
104+
* @param value The session ID
105+
* @returns Self for method chaining
106+
*/
107+
sessionId(value: string): BaggageBuilder {
108+
this.set(OpenTelemetryConstants.SESSION_ID_KEY, value);
109+
return this;
110+
}
111+
102112
/**
103113
* Set the caller ID baggage value.
104114
* @param value The caller ID
@@ -267,7 +277,7 @@ export class BaggageBuilder {
267277
static setRequestContext(
268278
tenantId?: string | null,
269279
agentId?: string | null,
270-
correlationId?: string | null
280+
correlationId?: string | null,
271281
): BaggageScope {
272282
return new BaggageBuilder()
273283
.tenantId(tenantId)

packages/agents-a365-observability/src/tracing/processors/util.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const GENERIC_ATTRIBUTES: readonly string[] = [
1212
consts.CUSTOM_PARENT_SPAN_ID_KEY,
1313
consts.CUSTOM_SPAN_NAME_KEY,
1414
consts.CORRELATION_ID_KEY,
15+
consts.SESSION_ID_KEY,
1516
consts.GEN_AI_CONVERSATION_ID_KEY,
1617
consts.GEN_AI_CONVERSATION_ITEM_LINK_KEY,
1718
consts.GEN_AI_OPERATION_NAME_KEY,

tests/observability/core/BaggageBuilder.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Copyright (c) Microsoft Corporation. All rights reserved.
33
// ------------------------------------------------------------------------------
44

5-
import { context } from '@opentelemetry/api';
5+
import { context, propagation } from '@opentelemetry/api';
66
import { BaggageBuilder, BaggageScope } from '@microsoft/agents-a365-observability/dist/cjs/tracing/middleware/BaggageBuilder';
77
import { OpenTelemetryConstants } from '@microsoft/agents-a365-observability/dist/cjs/tracing/constants';
88

@@ -133,6 +133,27 @@ describe('BaggageBuilder', () => {
133133
});
134134
});
135135

136+
describe('sessionId support', () => {
137+
it('should set sessionId via fluent API', () => {
138+
const scope = new BaggageBuilder()
139+
.tenantId('tenant-123')
140+
.agentId('agent-456')
141+
.correlationId('corr-789')
142+
.sessionId('session-0001')
143+
.build();
144+
const bag = propagation.getBaggage((scope as any).contextWithBaggage);
145+
expect(bag?.getEntry(OpenTelemetryConstants.SESSION_ID_KEY)?.value).toBe('session-0001');
146+
});
147+
148+
it('should omit empty sessionId value', () => {
149+
const scope = new BaggageBuilder()
150+
.sessionId(' ')
151+
.build();
152+
const bag = propagation.getBaggage((scope as any).contextWithBaggage);
153+
expect(bag?.getEntry(OpenTelemetryConstants.SESSION_ID_KEY)).toBeUndefined();
154+
});
155+
});
156+
136157
});
137158

138159
describe('BaggageScope', () => {

tests/observability/core/SpanProcessor.test.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ describe('SpanProcessor', () => {
2727

2828
describe('baggage to span attribute enrichment', () => {
2929
it('should copy generic attributes from baggage to span', () => {
30-
// Set baggage
3130
const baggageEntries = {
3231
[OpenTelemetryConstants.TENANT_ID_KEY]: 'tenant-123',
3332
[OpenTelemetryConstants.CORRELATION_ID_KEY]: 'corr-456',
@@ -41,7 +40,7 @@ describe('SpanProcessor', () => {
4140

4241
const ctx = propagation.setBaggage(context.active(), baggage);
4342

44-
// Create a span in this context
43+
// Create a span in this context (parentContext not passed so processor may no-op)
4544
const tracer = provider.getTracer('test');
4645
let testSpan: Span | undefined;
4746

@@ -52,10 +51,23 @@ describe('SpanProcessor', () => {
5251
}
5352
});
5453

55-
// The span processor should have copied baggage to attributes
5654
expect(testSpan).toBeDefined();
5755
});
5856

57+
58+
it('should copy sessionId from baggage to span', () => {
59+
let baggage = propagation.createBaggage();
60+
baggage = baggage.setEntry(OpenTelemetryConstants.SESSION_ID_KEY, { value: 'session-abc' });
61+
62+
const ctx = propagation.setBaggage(context.active(), baggage);
63+
const tracer = provider.getTracer('test');
64+
const testSpan = tracer.startSpan('test-span', { kind: SpanKind.CLIENT }, ctx as any);
65+
testSpan.end();
66+
67+
const attrs = (testSpan as any)._attributes ?? (testSpan as any).attributes ?? {};
68+
expect(attrs[OpenTelemetryConstants.SESSION_ID_KEY]).toBe('session-abc');
69+
});
70+
5971
it('should copy invoke agent attributes for invoke_agent operations', () => {
6072
// Set baggage with invoke agent specific fields
6173
const baggageEntries = {
@@ -139,6 +151,7 @@ describe('SpanProcessor', () => {
139151
expect(GENERIC_ATTRIBUTES).toContain(OpenTelemetryConstants.TENANT_ID_KEY);
140152
expect(GENERIC_ATTRIBUTES).toContain(OpenTelemetryConstants.CORRELATION_ID_KEY);
141153
expect(GENERIC_ATTRIBUTES).toContain(OpenTelemetryConstants.GEN_AI_AGENT_ID_KEY);
154+
expect(GENERIC_ATTRIBUTES).toContain(OpenTelemetryConstants.SESSION_ID_KEY);
142155
});
143156

144157
it('should apply invoke agent specific attributes', () => {

0 commit comments

Comments
 (0)