Skip to content
Merged
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
34 changes: 24 additions & 10 deletions tests/observability/core/observabilityBuilder-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { ObservabilityBuilder } from '@microsoft/agents-a365-observability/src/ObservabilityBuilder';
import { ClusterCategory } from '@microsoft/agents-a365-runtime';
import { ATTR_SERVICE_NAMESPACE } from '@opentelemetry/semantic-conventions';
import * as resources from '@opentelemetry/resources';
import { trace } from '@opentelemetry/api';

// Mock the Agent365Exporter so we can capture the constructed options without performing network calls.
jest.mock('@microsoft/agents-a365-observability/src/tracing/exporter/Agent365Exporter', () => {
Expand Down Expand Up @@ -79,19 +79,33 @@ describe('ObservabilityBuilder exporterOptions merging', () => {
});

describe('ObservabilityBuilder serviceNamespace', () => {
it('includes service.namespace only when withServiceNamespace is called', () => {
const resourceSpy = jest.spyOn(resources, 'resourceFromAttributes');
let createResourceSpy: jest.SpyInstance;

beforeEach(() => {
process.env.ENABLE_A365_OBSERVABILITY_EXPORTER = 'true';
// Spy on the private createResource method to capture the resource attributes.
createResourceSpy = jest.spyOn(ObservabilityBuilder.prototype as any, 'createResource');
// Force the builder to take the NodeSDK code path (which calls createResource)
// by making getTracerProvider return a provider without addSpanProcessor/resource.
jest.spyOn(trace, 'getTracerProvider').mockReturnValue({} as any);
});

afterEach(() => {
delete process.env.ENABLE_A365_OBSERVABILITY_EXPORTER;
});

// Without namespace
it('does not include service.namespace when withServiceNamespace is not called', () => {
new ObservabilityBuilder().withService('svc').build();
expect(resourceSpy.mock.calls[0][0]).not.toHaveProperty(ATTR_SERVICE_NAMESPACE);
expect(createResourceSpy).toHaveBeenCalled();
const result = createResourceSpy.mock.results[0].value;
// The resource should not contain ATTR_SERVICE_NAMESPACE
expect(result?.attributes?.[ATTR_SERVICE_NAMESPACE]).toBeUndefined();
});

// With namespace
it('includes service.namespace when withServiceNamespace is called', () => {
new ObservabilityBuilder().withService('svc').withServiceNamespace('ns').build();
expect(resourceSpy.mock.calls[1][0][ATTR_SERVICE_NAMESPACE]).toBe('ns');

delete process.env.ENABLE_A365_OBSERVABILITY_EXPORTER;
resourceSpy.mockRestore();
expect(createResourceSpy).toHaveBeenCalled();
const result = createResourceSpy.mock.results[0].value;
expect(result?.attributes?.[ATTR_SERVICE_NAMESPACE]).toBe('ns');
});
});
Loading