observability.test.ts 1.7 KB

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