Skip to content
Draft
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
53 changes: 49 additions & 4 deletions integrations/otel-js/src/id-gen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,57 @@ describe("ID Generation", () => {
});

describe("getIdGenerator factory function", () => {
test("returns UUID generator by default", () => {
test("returns hex-id generator by default (after reset)", () => {
// The core SDK now defaults to OpenTelemetry-compatible hex ids even
// without compat installed, so resetting the compat globals leaves the
// hex default in place (BRAINTRUST_LEGACY_IDS opts back into UUIDs).
const prevLegacy = process.env.BRAINTRUST_LEGACY_IDS;
const prevOtel = process.env.BRAINTRUST_OTEL_COMPAT;
delete process.env.BRAINTRUST_LEGACY_IDS;
delete process.env.BRAINTRUST_OTEL_COMPAT;
resetOtelCompat();

const generator = getIdGenerator();
expect(generator).toBeInstanceOf(UUIDGenerator);
expect(generator.shareRootSpanId()).toBe(true);
try {
const generator = getIdGenerator();
expect(generator.shareRootSpanId()).toBe(false);
expect(/^[0-9a-f]{16}$/.test(generator.getSpanId())).toBe(true);
} finally {
if (prevLegacy === undefined) {
delete process.env.BRAINTRUST_LEGACY_IDS;
} else {
process.env.BRAINTRUST_LEGACY_IDS = prevLegacy;
}
if (prevOtel === undefined) {
delete process.env.BRAINTRUST_OTEL_COMPAT;
} else {
process.env.BRAINTRUST_OTEL_COMPAT = prevOtel;
}
}
});

test("returns UUID generator when BRAINTRUST_LEGACY_IDS is set", () => {
const prevLegacy = process.env.BRAINTRUST_LEGACY_IDS;
const prevOtel = process.env.BRAINTRUST_OTEL_COMPAT;
delete process.env.BRAINTRUST_OTEL_COMPAT;
process.env.BRAINTRUST_LEGACY_IDS = "true";
resetOtelCompat();

try {
const generator = getIdGenerator();
expect(generator).toBeInstanceOf(UUIDGenerator);
expect(generator.shareRootSpanId()).toBe(true);
} finally {
if (prevLegacy === undefined) {
delete process.env.BRAINTRUST_LEGACY_IDS;
} else {
process.env.BRAINTRUST_LEGACY_IDS = prevLegacy;
}
if (prevOtel === undefined) {
delete process.env.BRAINTRUST_OTEL_COMPAT;
} else {
process.env.BRAINTRUST_OTEL_COMPAT = prevOtel;
}
}
});

test("returns OTEL generator when otel is initialized", () => {
Expand Down
Loading
Loading