Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { OpenTelemetryConstants } from '../constants';
* .tenantId("tenant-123")
* .agentId("agent-456")
* .correlationId("corr-789")
* .sessionId("session-0001")
* .build();
*
* scope.enter();
Expand Down Expand Up @@ -99,6 +100,16 @@ export class BaggageBuilder {
return this;
}

/**
* Set the session ID baggage value.
* @param value The session ID
* @returns Self for method chaining
*/
sessionId(value: string | undefined): BaggageBuilder {
Comment thread
fpfp100 marked this conversation as resolved.
Outdated
Comment thread
fpfp100 marked this conversation as resolved.
Outdated
this.set(OpenTelemetryConstants.SESSION_ID_KEY, value);
return this;
}

/**
* Set the caller ID baggage value.
* @param value The caller ID
Expand Down Expand Up @@ -262,17 +273,20 @@ export class BaggageBuilder {
* @param tenantId The tenant ID
* @param agentId The agent ID
* @param correlationId The correlation ID
* @param sessionId Optional session ID
* @returns A context manager that restores the previous baggage on exit
*/
static setRequestContext(
tenantId?: string | null,
agentId?: string | null,
correlationId?: string | null
correlationId?: string | null,
sessionId?: string
Comment thread
fpfp100 marked this conversation as resolved.
Outdated
Comment thread
fpfp100 marked this conversation as resolved.
Outdated
): BaggageScope {
return new BaggageBuilder()
.tenantId(tenantId)
.agentId(agentId)
.correlationId(correlationId)
.sessionId(sessionId)
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const GENERIC_ATTRIBUTES: readonly string[] = [
consts.CUSTOM_PARENT_SPAN_ID_KEY,
consts.CUSTOM_SPAN_NAME_KEY,
consts.CORRELATION_ID_KEY,
consts.SESSION_ID_KEY,
consts.GEN_AI_CONVERSATION_ID_KEY,
consts.GEN_AI_CONVERSATION_ITEM_LINK_KEY,
consts.GEN_AI_OPERATION_NAME_KEY,
Expand Down
34 changes: 33 additions & 1 deletion tests/observability/core/BaggageBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// ------------------------------------------------------------------------------

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

Expand Down Expand Up @@ -133,6 +133,38 @@ describe('BaggageBuilder', () => {
});
});

describe('sessionId support', () => {
it('should set sessionId via fluent API', () => {
const scope = new BaggageBuilder()
.tenantId('tenant-123')
.agentId('agent-456')
.correlationId('corr-789')
.sessionId('session-0001')
.build();
const bag = propagation.getBaggage((scope as any).contextWithBaggage);
expect(bag?.getEntry(OpenTelemetryConstants.SESSION_ID_KEY)?.value).toBe('session-0001');
});

it('should set sessionId via static setRequestContext', () => {
const scope = BaggageBuilder.setRequestContext(
'tenant-123',
'agent-456',
'corr-789',
'session-0002'
);
const bag = propagation.getBaggage((scope as any).contextWithBaggage);
expect(bag?.getEntry(OpenTelemetryConstants.SESSION_ID_KEY)?.value).toBe('session-0002');
});

it('should omit empty sessionId value', () => {
const scope = new BaggageBuilder()
.sessionId(' ')
.build();
const bag = propagation.getBaggage((scope as any).contextWithBaggage);
expect(bag?.getEntry(OpenTelemetryConstants.SESSION_ID_KEY)).toBeUndefined();
});
});

});

describe('BaggageScope', () => {
Expand Down
19 changes: 16 additions & 3 deletions tests/observability/core/SpanProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ describe('SpanProcessor', () => {

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

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

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

Expand All @@ -52,10 +51,23 @@ describe('SpanProcessor', () => {
}
});

// The span processor should have copied baggage to attributes
expect(testSpan).toBeDefined();
});


it('should copy sessionId from baggage to span', () => {
let baggage = propagation.createBaggage();
baggage = baggage.setEntry(OpenTelemetryConstants.SESSION_ID_KEY, { value: 'session-abc' });

const ctx = propagation.setBaggage(context.active(), baggage);
const tracer = provider.getTracer('test');
const testSpan = tracer.startSpan('test-span', { kind: SpanKind.CLIENT }, ctx as any);
testSpan.end();

const attrs = (testSpan as any)._attributes ?? (testSpan as any).attributes ?? {};
expect(attrs[OpenTelemetryConstants.SESSION_ID_KEY]).toBe('session-abc');
});

it('should copy invoke agent attributes for invoke_agent operations', () => {
// Set baggage with invoke agent specific fields
const baggageEntries = {
Expand Down Expand Up @@ -139,6 +151,7 @@ describe('SpanProcessor', () => {
expect(GENERIC_ATTRIBUTES).toContain(OpenTelemetryConstants.TENANT_ID_KEY);
expect(GENERIC_ATTRIBUTES).toContain(OpenTelemetryConstants.CORRELATION_ID_KEY);
expect(GENERIC_ATTRIBUTES).toContain(OpenTelemetryConstants.GEN_AI_AGENT_ID_KEY);
expect(GENERIC_ATTRIBUTES).toContain(OpenTelemetryConstants.SESSION_ID_KEY);
});

it('should apply invoke agent specific attributes', () => {
Expand Down
Loading