Skip to content
TypeScript

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

Victor Vogele8b877c5479d
diff --git a/packages/shared/src/observability.test.ts b/packages/shared/src/observability.test.ts
index 4a1119ef0b3..79d6b9ec9a5 100644
--- a/packages/shared/src/observability.test.ts
+++ b/packages/shared/src/observability.test.ts
@@ -6,6 +6,7 @@ import * as Deferred from "effect/Deferred";
import * as Fiber from "effect/Fiber";
import * as Exit from "effect/Exit";
import * as FileSystem from "effect/FileSystem";
+import { cast } from "effect/Function";
import * as Layer from "effect/Layer";
import * as Logger from "effect/Logger";
import * as Path from "effect/Path";
@@ -13,10 +14,12 @@ import * as Ref from "effect/Ref";
import * as References from "effect/References";
import * as Schema from "effect/Schema";
import * as Tracer from "effect/Tracer";
+import type { OtlpTracer } from "effect/observability";
import {
causeErrorTag,
compactTraceAttributes,
+ decodeOtlpTraceRecords,
errorTag,
makeLocalFileTracer,
makeTraceSink,
@@ -557,6 +560,55 @@ describe("observability", () => {
});
});
+describe("decodeOtlpTraceRecords", () => {
+ it("decodes spans when OTLP/JSON omits unset and empty fields", () => {
+ // Protobuf JSON leaves out unset and empty fields, so this batch is valid.
+ const payload = cast<unknown, OtlpTracer.TraceData>({
+ resourceSpans: [
+ {},
+ {
+ scopeSpans: [
+ {},
+ {
+ spans: [
+ {
+ traceId: "trace-1",
+ spanId: "span-1",
+ name: "browser.request",
+ startTimeUnixNano: "1000000",
+ endTimeUnixNano: "3000000",
+ attributes: [{ key: "tags", value: { arrayValue: {} } }],
+ },
+ ],
+ },
+ ],
+ },
+ ],
+ });
+
+ assert.deepStrictEqual(decodeOtlpTraceRecords(cast<unknown, OtlpTracer.TraceData>({})), []);
+ assert.deepStrictEqual(decodeOtlpTraceRecords(payload), [
+ {
+ type: "otlp-span",
+ name: "browser.request",
+ traceId: "trace-1",
+ spanId: "span-1",
+ sampled: true,
+ kind: "internal",
+ startTimeUnixNano: "1000000",
+ endTimeUnixNano: "3000000",
+ durationMs: 2,
+ attributes: { tags: [] },
+ resourceAttributes: {},
+ scope: { attributes: {} },
+ events: [],
+ links: [],
+ status: { code: "0" },
+ },
+ ]);
+ });
+});
+
describe("OtlpHeadersFromString", () => {
const decode = Schema.decodeUnknownSync(OtlpHeadersFromString);
diff --git a/packages/shared/src/observability.ts b/packages/shared/src/observability.ts
index ec2eeb544e6..b32edb17015 100644
--- a/packages/shared/src/observability.ts
+++ b/packages/shared/src/observability.ts
@@ -185,6 +185,7 @@ type OtlpSpan = OtlpTracer.ScopeSpan["spans"][number];
type OtlpSpanEvent = OtlpSpan["events"][number];
type OtlpSpanLink = OtlpSpan["links"][number];
type OtlpSpanStatus = OtlpSpan["status"];
+type OtlpScope = OtlpTracer.ScopeSpan["scope"];
interface SerializableSpan {
readonly name: string;
@@ -686,24 +687,22 @@ export function decodeOtlpTraceRecords(
): ReadonlyArray<OtlpTraceRecord> {
const records: Array<OtlpTraceRecord> = [];
- for (const resourceSpan of payload.resourceSpans) {
- const resourceAttributes = decodeAttributes(resourceSpan.resource?.attributes ?? []);
+ // OTLP/JSON follows protobuf JSON, which omits unset and empty fields, so a
+ // valid batch can leave out any field read here.
+ for (const resourceSpan of payload.resourceSpans ?? []) {
+ const resourceAttributes = decodeAttributes(resourceSpan.resource?.attributes);
- for (const scopeSpan of resourceSpan.scopeSpans) {
- for (const span of scopeSpan.spans) {
+ for (const scopeSpan of resourceSpan.scopeSpans ?? []) {
+ const scope: Partial<OtlpScope> = scopeSpan.scope ?? {};
+ for (const span of scopeSpan.spans ?? []) {
records.push(
otlpSpanToTraceRecord({
resourceAttributes,
scopeAttributes: decodeAttributes(
- "attributes" in scopeSpan.scope && Array.isArray(scopeSpan.scope.attributes)
- ? scopeSpan.scope.attributes
- : [],
+ "attributes" in scope && Array.isArray(scope.attributes) ? scope.attributes : [],
),
- scopeName: scopeSpan.scope.name,
- scopeVersion:
- "version" in scopeSpan.scope && typeof scopeSpan.scope.version === "string"
- ? scopeSpan.scope.version
- : undefined,
+ scopeName: scope.name,
+ scopeVersion: typeof scope.version === "string" ? scope.version : undefined,
span,
}),
);
@@ -747,9 +746,9 @@ function otlpSpanToTraceRecord(input: {
};
}
-function decodeStatus(input: OtlpSpanStatus): OtlpTraceRecord["status"] {
- const code = String(input.code);
- const message = input.message;
+function decodeStatus(input: OtlpSpanStatus | undefined): OtlpTraceRecord["status"] {
+ const code = String(input?.code ?? 0);
+ const message = input?.message;
return {
code,
@@ -757,16 +756,20 @@ function decodeStatus(input: OtlpSpanStatus): OtlpTraceRecord["status"] {
};
}
-function decodeEvents(input: ReadonlyArray<OtlpSpanEvent>): ReadonlyArray<TraceRecordEvent> {
- return input.map((current) => ({
+function decodeEvents(
+ input: ReadonlyArray<OtlpSpanEvent> | undefined,
+): ReadonlyArray<TraceRecordEvent> {
+ return (input ?? []).map((current) => ({
name: current.name,
timeUnixNano: current.timeUnixNano,
attributes: decodeAttributes(current.attributes),
}));
}
-function decodeLinks(input: ReadonlyArray<OtlpSpanLink>): ReadonlyArray<TraceRecordLink> {
- return input.flatMap((current) => {
+function decodeLinks(
+ input: ReadonlyArray<OtlpSpanLink> | undefined,
+): ReadonlyArray<TraceRecordLink> {
+ return (input ?? []).flatMap((current) => {
const traceId = current.traceId;
const spanId = current.spanId;
return {
@@ -778,11 +781,11 @@ function decodeLinks(input: ReadonlyArray<OtlpSpanLink>): ReadonlyArray<TraceRec
}
function decodeAttributes(
- input: ReadonlyArray<OtlpResource.KeyValue>,
+ input: ReadonlyArray<OtlpResource.KeyValue> | undefined,
): Readonly<Record<string, unknown>> {
const entries: Record<string, unknown> = {};
- for (const attribute of input) {
+ for (const attribute of input ?? []) {
entries[attribute.key] = decodeValue(attribute.value);
}
@@ -809,7 +812,7 @@ function decodeValue(input: OtlpResource.AnyValue | null | undefined): unknown {
return input.bytesValue;
}
if (input.arrayValue) {
- return input.arrayValue.values.map((entry) => decodeValue(entry));
+ return (input.arrayValue.values ?? []).map((entry) => decodeValue(entry));
}
if (input.kvlistValue) {
return decodeAttributes(input.kvlistValue.values);