@devries-cameron20 / t3code Mirror · read-only / commits / e8b877c547
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 09:30 UTC

fix(observability): record OTLP/JSON spans that leave out empty fields (#23177)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Victor Vogel committed Oct 31, 2026 at 21:26 UTC e8b877c5479dc4ca6fc1fbb0b123b8f4b490fb7f
2 files changed +77 -22
packages/shared/src/observability.test.ts
+52
@@ -6,6 +6,7 @@ import * as Deferred from "effect/Deferred";
6 import * as Fiber from "effect/Fiber";
7 import * as Exit from "effect/Exit";
8 import * as FileSystem from "effect/FileSystem";
9 +import { cast } from "effect/Function";
10 import * as Layer from "effect/Layer";
11 import * as Logger from "effect/Logger";
12 import * as Path from "effect/Path";
@@ -13,10 +14,12 @@ import * as Ref from "effect/Ref";
14 import * as References from "effect/References";
15 import * as Schema from "effect/Schema";
16 import * as Tracer from "effect/Tracer";
17 +import type { OtlpTracer } from "effect/observability";
18
19 import {
20 causeErrorTag,
21 compactTraceAttributes,
22 + decodeOtlpTraceRecords,
23 errorTag,
24 makeLocalFileTracer,
25 makeTraceSink,
@@ -557,6 +560,55 @@ describe("observability", () => {
560 });
561 });
562
563 +describe("decodeOtlpTraceRecords", () => {
564 + it("decodes spans when OTLP/JSON omits unset and empty fields", () => {
565 + // Protobuf JSON leaves out unset and empty fields, so this batch is valid.
566 + const payload = cast<unknown, OtlpTracer.TraceData>({
567 + resourceSpans: [
568 + {},
569 + {
570 + scopeSpans: [
571 + {},
572 + {
573 + spans: [
574 + {
575 + traceId: "trace-1",
576 + spanId: "span-1",
577 + name: "browser.request",
578 + startTimeUnixNano: "1000000",
579 + endTimeUnixNano: "3000000",
580 + attributes: [{ key: "tags", value: { arrayValue: {} } }],
581 + },
582 + ],
583 + },
584 + ],
585 + },
586 + ],
587 + });
588 +
589 + assert.deepStrictEqual(decodeOtlpTraceRecords(cast<unknown, OtlpTracer.TraceData>({})), []);
590 + assert.deepStrictEqual(decodeOtlpTraceRecords(payload), [
591 + {
592 + type: "otlp-span",
593 + name: "browser.request",
594 + traceId: "trace-1",
595 + spanId: "span-1",
596 + sampled: true,
597 + kind: "internal",
598 + startTimeUnixNano: "1000000",
599 + endTimeUnixNano: "3000000",
600 + durationMs: 2,
601 + attributes: { tags: [] },
602 + resourceAttributes: {},
603 + scope: { attributes: {} },
604 + events: [],
605 + links: [],
606 + status: { code: "0" },
607 + },
608 + ]);
609 + });
610 +});
611 +
612 describe("OtlpHeadersFromString", () => {
613 const decode = Schema.decodeUnknownSync(OtlpHeadersFromString);
614
packages/shared/src/observability.ts
+25 -22
@@ -185,6 +185,7 @@ type OtlpSpan = OtlpTracer.ScopeSpan["spans"][number];
185 type OtlpSpanEvent = OtlpSpan["events"][number];
186 type OtlpSpanLink = OtlpSpan["links"][number];
187 type OtlpSpanStatus = OtlpSpan["status"];
188 +type OtlpScope = OtlpTracer.ScopeSpan["scope"];
189
190 interface SerializableSpan {
191 readonly name: string;
@@ -686,24 +687,22 @@ export function decodeOtlpTraceRecords(
687 ): ReadonlyArray<OtlpTraceRecord> {
688 const records: Array<OtlpTraceRecord> = [];
689
689 - for (const resourceSpan of payload.resourceSpans) {
690 - const resourceAttributes = decodeAttributes(resourceSpan.resource?.attributes ?? []);
690 + // OTLP/JSON follows protobuf JSON, which omits unset and empty fields, so a
691 + // valid batch can leave out any field read here.
692 + for (const resourceSpan of payload.resourceSpans ?? []) {
693 + const resourceAttributes = decodeAttributes(resourceSpan.resource?.attributes);
694
692 - for (const scopeSpan of resourceSpan.scopeSpans) {
693 - for (const span of scopeSpan.spans) {
695 + for (const scopeSpan of resourceSpan.scopeSpans ?? []) {
696 + const scope: Partial<OtlpScope> = scopeSpan.scope ?? {};
697 + for (const span of scopeSpan.spans ?? []) {
698 records.push(
699 otlpSpanToTraceRecord({
700 resourceAttributes,
701 scopeAttributes: decodeAttributes(
698 - "attributes" in scopeSpan.scope && Array.isArray(scopeSpan.scope.attributes)
699 - ? scopeSpan.scope.attributes
700 - : [],
702 + "attributes" in scope && Array.isArray(scope.attributes) ? scope.attributes : [],
703 ),
702 - scopeName: scopeSpan.scope.name,
703 - scopeVersion:
704 - "version" in scopeSpan.scope && typeof scopeSpan.scope.version === "string"
705 - ? scopeSpan.scope.version
706 - : undefined,
704 + scopeName: scope.name,
705 + scopeVersion: typeof scope.version === "string" ? scope.version : undefined,
706 span,
707 }),
708 );
@@ -747,9 +746,9 @@ function otlpSpanToTraceRecord(input: {
746 };
747 }
748
750 -function decodeStatus(input: OtlpSpanStatus): OtlpTraceRecord["status"] {
751 - const code = String(input.code);
752 - const message = input.message;
749 +function decodeStatus(input: OtlpSpanStatus | undefined): OtlpTraceRecord["status"] {
750 + const code = String(input?.code ?? 0);
751 + const message = input?.message;
752
753 return {
754 code,
@@ -757,16 +756,20 @@ function decodeStatus(input: OtlpSpanStatus): OtlpTraceRecord["status"] {
756 };
757 }
758
760 -function decodeEvents(input: ReadonlyArray<OtlpSpanEvent>): ReadonlyArray<TraceRecordEvent> {
761 - return input.map((current) => ({
759 +function decodeEvents(
760 + input: ReadonlyArray<OtlpSpanEvent> | undefined,
761 +): ReadonlyArray<TraceRecordEvent> {
762 + return (input ?? []).map((current) => ({
763 name: current.name,
764 timeUnixNano: current.timeUnixNano,
765 attributes: decodeAttributes(current.attributes),
766 }));
767 }
768
768 -function decodeLinks(input: ReadonlyArray<OtlpSpanLink>): ReadonlyArray<TraceRecordLink> {
769 - return input.flatMap((current) => {
769 +function decodeLinks(
770 + input: ReadonlyArray<OtlpSpanLink> | undefined,
771 +): ReadonlyArray<TraceRecordLink> {
772 + return (input ?? []).flatMap((current) => {
773 const traceId = current.traceId;
774 const spanId = current.spanId;
775 return {
@@ -778,11 +781,11 @@ function decodeLinks(input: ReadonlyArray<OtlpSpanLink>): ReadonlyArray<TraceRec
781 }
782
783 function decodeAttributes(
781 - input: ReadonlyArray<OtlpResource.KeyValue>,
784 + input: ReadonlyArray<OtlpResource.KeyValue> | undefined,
785 ): Readonly<Record<string, unknown>> {
786 const entries: Record<string, unknown> = {};
787
785 - for (const attribute of input) {
788 + for (const attribute of input ?? []) {
789 entries[attribute.key] = decodeValue(attribute.value);
790 }
791
@@ -809,7 +812,7 @@ function decodeValue(input: OtlpResource.AnyValue | null | undefined): unknown {
812 return input.bytesValue;
813 }
814 if (input.arrayValue) {
812 - return input.arrayValue.values.map((entry) => decodeValue(entry));
815 + return (input.arrayValue.values ?? []).map((entry) => decodeValue(entry));
816 }
817 if (input.kvlistValue) {
818 return decodeAttributes(input.kvlistValue.values);