|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { buildOtlpTracePayload } from "./otlpTrace.js"; |
| 3 | + |
| 4 | +describe("buildOtlpTracePayload", () => { |
| 5 | + it("builds valid OTLP JSON with timing attributes", () => { |
| 6 | + const payload = buildOtlpTracePayload({ |
| 7 | + traceId: "abcd1234abcd1234abcd1234abcd1234", |
| 8 | + parentSpanId: "1234567890abcdef", |
| 9 | + spanName: "compute.provision", |
| 10 | + startTimeMs: 1000, |
| 11 | + endTimeMs: 1250, |
| 12 | + resourceAttributes: { |
| 13 | + "ctx.environment.id": "env_123", |
| 14 | + "ctx.organization.id": "org_456", |
| 15 | + "ctx.project.id": "proj_789", |
| 16 | + "ctx.run.id": "run_abc", |
| 17 | + }, |
| 18 | + spanAttributes: { |
| 19 | + "compute.total_ms": 250, |
| 20 | + "compute.gateway.schedule_ms": 1, |
| 21 | + "compute.cache.image_cached": true, |
| 22 | + }, |
| 23 | + }); |
| 24 | + |
| 25 | + expect(payload.resourceSpans).toHaveLength(1); |
| 26 | + |
| 27 | + const resourceSpan = payload.resourceSpans[0]!; |
| 28 | + |
| 29 | + // $trigger=true so the webapp accepts it |
| 30 | + const triggerAttr = resourceSpan.resource.attributes.find((a) => a.key === "$trigger"); |
| 31 | + expect(triggerAttr).toEqual({ key: "$trigger", value: { boolValue: true } }); |
| 32 | + |
| 33 | + // Resource attributes |
| 34 | + const envAttr = resourceSpan.resource.attributes.find( |
| 35 | + (a) => a.key === "ctx.environment.id" |
| 36 | + ); |
| 37 | + expect(envAttr).toEqual({ |
| 38 | + key: "ctx.environment.id", |
| 39 | + value: { stringValue: "env_123" }, |
| 40 | + }); |
| 41 | + |
| 42 | + // Span basics |
| 43 | + const span = resourceSpan.scopeSpans[0]!.spans[0]!; |
| 44 | + expect(span.name).toBe("compute.provision"); |
| 45 | + expect(span.traceId).toBe("abcd1234abcd1234abcd1234abcd1234"); |
| 46 | + expect(span.parentSpanId).toBe("1234567890abcdef"); |
| 47 | + |
| 48 | + // Integer attribute |
| 49 | + const totalMs = span.attributes.find((a) => a.key === "compute.total_ms"); |
| 50 | + expect(totalMs).toEqual({ key: "compute.total_ms", value: { intValue: 250 } }); |
| 51 | + |
| 52 | + // Boolean attribute |
| 53 | + const cached = span.attributes.find((a) => a.key === "compute.cache.image_cached"); |
| 54 | + expect(cached).toEqual({ key: "compute.cache.image_cached", value: { boolValue: true } }); |
| 55 | + }); |
| 56 | + |
| 57 | + it("generates a valid 16-char hex span ID", () => { |
| 58 | + const payload = buildOtlpTracePayload({ |
| 59 | + traceId: "abcd1234abcd1234abcd1234abcd1234", |
| 60 | + spanName: "test", |
| 61 | + startTimeMs: 1000, |
| 62 | + endTimeMs: 1001, |
| 63 | + resourceAttributes: {}, |
| 64 | + spanAttributes: {}, |
| 65 | + }); |
| 66 | + |
| 67 | + const span = payload.resourceSpans[0]!.scopeSpans[0]!.spans[0]!; |
| 68 | + expect(span.spanId).toMatch(/^[0-9a-f]{16}$/); |
| 69 | + }); |
| 70 | + |
| 71 | + it("converts timestamps to nanoseconds", () => { |
| 72 | + const payload = buildOtlpTracePayload({ |
| 73 | + traceId: "abcd1234abcd1234abcd1234abcd1234", |
| 74 | + spanName: "test", |
| 75 | + startTimeMs: 1000, |
| 76 | + endTimeMs: 1250, |
| 77 | + resourceAttributes: {}, |
| 78 | + spanAttributes: {}, |
| 79 | + }); |
| 80 | + |
| 81 | + const span = payload.resourceSpans[0]!.scopeSpans[0]!.spans[0]!; |
| 82 | + expect(span.startTimeUnixNano).toBe("1000000000"); |
| 83 | + expect(span.endTimeUnixNano).toBe("1250000000"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("omits parentSpanId when not provided", () => { |
| 87 | + const payload = buildOtlpTracePayload({ |
| 88 | + traceId: "abcd1234abcd1234abcd1234abcd1234", |
| 89 | + spanName: "test", |
| 90 | + startTimeMs: 1000, |
| 91 | + endTimeMs: 1001, |
| 92 | + resourceAttributes: {}, |
| 93 | + spanAttributes: {}, |
| 94 | + }); |
| 95 | + |
| 96 | + const span = payload.resourceSpans[0]!.scopeSpans[0]!.spans[0]!; |
| 97 | + expect(span.parentSpanId).toBeUndefined(); |
| 98 | + }); |
| 99 | + |
| 100 | + it("handles double values for non-integer numbers", () => { |
| 101 | + const payload = buildOtlpTracePayload({ |
| 102 | + traceId: "abcd1234abcd1234abcd1234abcd1234", |
| 103 | + spanName: "test", |
| 104 | + startTimeMs: 1000, |
| 105 | + endTimeMs: 1001, |
| 106 | + resourceAttributes: {}, |
| 107 | + spanAttributes: { "compute.cpu": 0.25 }, |
| 108 | + }); |
| 109 | + |
| 110 | + const span = payload.resourceSpans[0]!.scopeSpans[0]!.spans[0]!; |
| 111 | + const cpu = span.attributes.find((a) => a.key === "compute.cpu"); |
| 112 | + expect(cpu).toEqual({ key: "compute.cpu", value: { doubleValue: 0.25 } }); |
| 113 | + }); |
| 114 | +}); |
0 commit comments