|
| 1 | +import { afterEach, describe, expect, test } from "bun:test" |
| 2 | +import { resource } from "../../src/effect/observability" |
| 3 | + |
| 4 | +const otelResourceAttributes = process.env.OTEL_RESOURCE_ATTRIBUTES |
| 5 | +const opencodeClient = process.env.OPENCODE_CLIENT |
| 6 | + |
| 7 | +afterEach(() => { |
| 8 | + if (otelResourceAttributes === undefined) delete process.env.OTEL_RESOURCE_ATTRIBUTES |
| 9 | + else process.env.OTEL_RESOURCE_ATTRIBUTES = otelResourceAttributes |
| 10 | + |
| 11 | + if (opencodeClient === undefined) delete process.env.OPENCODE_CLIENT |
| 12 | + else process.env.OPENCODE_CLIENT = opencodeClient |
| 13 | +}) |
| 14 | + |
| 15 | +describe("resource", () => { |
| 16 | + test("parses and decodes OTEL resource attributes", () => { |
| 17 | + process.env.OTEL_RESOURCE_ATTRIBUTES = |
| 18 | + "service.namespace=anomalyco,team=platform%2Cobservability,label=hello%3Dworld,key%2Fname=value%20here" |
| 19 | + |
| 20 | + expect(resource().attributes).toMatchObject({ |
| 21 | + "service.namespace": "anomalyco", |
| 22 | + team: "platform,observability", |
| 23 | + label: "hello=world", |
| 24 | + "key/name": "value here", |
| 25 | + }) |
| 26 | + }) |
| 27 | + |
| 28 | + test("drops OTEL resource attributes when any entry is invalid", () => { |
| 29 | + process.env.OTEL_RESOURCE_ATTRIBUTES = "service.namespace=anomalyco,broken" |
| 30 | + |
| 31 | + expect(resource().attributes["service.namespace"]).toBeUndefined() |
| 32 | + expect(resource().attributes["opencode.client"]).toBeDefined() |
| 33 | + }) |
| 34 | + |
| 35 | + test("keeps built-in attributes when env values conflict", () => { |
| 36 | + process.env.OPENCODE_CLIENT = "cli" |
| 37 | + process.env.OTEL_RESOURCE_ATTRIBUTES = |
| 38 | + "opencode.client=web,service.instance.id=override,service.namespace=anomalyco" |
| 39 | + |
| 40 | + expect(resource().attributes).toMatchObject({ |
| 41 | + "opencode.client": "cli", |
| 42 | + "service.namespace": "anomalyco", |
| 43 | + }) |
| 44 | + expect(resource().attributes["service.instance.id"]).not.toBe("override") |
| 45 | + }) |
| 46 | +}) |
0 commit comments