| 1 | import assert from "node:assert/strict"; |
| 2 | import test from "node:test"; |
| 3 | |
| 4 | import { observabilityMetadata, resourceAttributes, telemetryTags, withOtel } from "./telemetry.ts"; |
| 5 | |
| 6 | const app = { project: "prj_1", deployment: "dpl_1", environment: "production", service: "web", commit: "abc123" }; |
| 7 | |
| 8 | test("an app's OpenTelemetry variables point at the project, unless it set its own", () => { |
| 9 | const given = withOtel({ secrets: {}, variables: { FROM: "config" } }, app, "g1to_prj_1_secret"); |
| 10 | assert.equal(given.variables.OTEL_EXPORTER_OTLP_ENDPOINT, "https://otel.g1t.sh"); |
| 11 | assert.equal(given.variables.OTEL_SERVICE_NAME, "web"); |
| 12 | assert.equal(given.secrets.OTEL_EXPORTER_OTLP_HEADERS, "authorization=Bearer g1to_prj_1_secret"); |
| 13 | assert.match(given.variables.OTEL_RESOURCE_ATTRIBUTES, /g1t\.deployment\.id=dpl_1/); |
| 14 | assert.equal(given.variables.FROM, "config"); |
| 15 | // The project's own endpoint wins, whole. |
| 16 | const own = { secrets: {}, variables: { OTEL_EXPORTER_OTLP_ENDPOINT: "https://collector.example.com" } }; |
| 17 | assert.deepEqual(withOtel(own, app, "g1to_x"), own); |
| 18 | // No token (an untrusted build): nothing added. |
| 19 | assert.deepEqual(withOtel({ secrets: {}, variables: {} }, app, null), { secrets: {}, variables: {} }); |
| 20 | }); |
| 21 | |
| 22 | test("tags and resource attributes name the project and deployment", () => { |
| 23 | assert.deepEqual(telemetryTags("prj_1", "dpl_1"), ["g1t-p:prj_1", "g1t-d:dpl_1"]); |
| 24 | assert.equal(resourceAttributes(app), "deployment.environment.name=production,g1t.deployment.id=dpl_1,g1t.project.id=prj_1,vcs.ref.head.revision=abc123"); |
| 25 | }); |
| 26 | |
| 27 | test("Cloudflare's traces go to the destination, logs stay with the Tail Worker", () => { |
| 28 | assert.deepEqual(observabilityMetadata(null), {}); |
| 29 | const metadata = observabilityMetadata("g1t-traces").observability as Record<string, any>; |
| 30 | assert.equal(metadata.enabled, true); |
| 31 | assert.deepEqual(metadata.traces.destinations, ["g1t-traces"]); |
| 32 | assert.equal(metadata.traces.persist, false); |
| 33 | assert.equal(metadata.logs.enabled, false); |
| 34 | }); |