diff --git a/.changeset/otlp-circular-arrays.md b/.changeset/otlp-circular-arrays.md new file mode 100644 index 00000000000..7d739653176 --- /dev/null +++ b/.changeset/otlp-circular-arrays.md @@ -0,0 +1,5 @@ +--- +"effect": patch +--- + +Fix the OTLP logger and tracer overflowing the stack on self-referencing arrays; the repeated array is exported as `"[Circular]"`. diff --git a/packages/effect/src/observability/OtlpResource.ts b/packages/effect/src/observability/OtlpResource.ts index 675d2bcf201..6f81c3a3d26 100644 --- a/packages/effect/src/observability/OtlpResource.ts +++ b/packages/effect/src/observability/OtlpResource.ts @@ -187,19 +187,35 @@ const int64Bound = 2 ** 63 * **Details** * * Arrays are converted recursively, primitive values use their matching OTLP - * fields, and unsupported values are formatted as strings. Integers outside the - * signed 64-bit range fall back to `doubleValue` for numbers and `stringValue` - * for bigints. + * fields, and unsupported values are formatted as strings. An array nested + * inside itself is converted to the string `"[Circular]"` where it repeats. + * Integers outside the signed 64-bit range fall back to `doubleValue` for + * numbers and `stringValue` for bigints. * * @stability unstable * @category attributes * @since 4.0.0 */ -export const unknownToAttributeValue = (value: unknown): AnyValue => { +export const unknownToAttributeValue = (value: unknown): AnyValue => toAttributeValue(value, undefined) + +const CIRCULAR = "[Circular]" + +// `ancestors` holds the arrays on the current path, so a cycle is cut where it +// repeats while an array shared by siblings is converted each time +const toAttributeValue = (value: unknown, ancestors: Set | undefined): AnyValue => { if (Array.isArray(value)) { + const path = ancestors ?? new Set() + if (path.has(value)) { + return { + stringValue: CIRCULAR + } + } + path.add(value) + const values = value.map((item) => toAttributeValue(item, path)) + path.delete(value) return { arrayValue: { - values: value.map(unknownToAttributeValue) + values } } } diff --git a/packages/effect/test/observability/OtlpLogger.test.ts b/packages/effect/test/observability/OtlpLogger.test.ts index 800831f7548..fed4bc85831 100644 --- a/packages/effect/test/observability/OtlpLogger.test.ts +++ b/packages/effect/test/observability/OtlpLogger.test.ts @@ -112,6 +112,20 @@ describe("OtlpLogger", () => { }).pipe(Effect.provide(makeTestLayer(httpClient))) }) + it.effect("exports a self-referencing array in the message and annotations", () => { + const { httpClient, requests } = capture() + const array: Array = [1] + array.push(array) + return Effect.gen(function*() { + yield* Effect.log(array).pipe(Effect.annotateLogs("array", array)) + yield* (yield* OtlpExporter.Flusher).flush + + const expected = { arrayValue: { values: [{ intValue: 1 }, { stringValue: "[Circular]" }] } } + assert.deepStrictEqual(logRecordOf(requests[0]).body, expected) + assert.deepStrictEqual(attributesOf(requests[0]).array, expected) + }).pipe(Effect.provide(makeTestLayer(httpClient))) + }) + it.effect("pads a 64-bit trace id to 32 hex characters", () => { const { httpClient, requests } = capture() return Effect.gen(function*() { diff --git a/packages/effect/test/observability/OtlpResource.test.ts b/packages/effect/test/observability/OtlpResource.test.ts index 8ee91c9b7f3..9b178019793 100644 --- a/packages/effect/test/observability/OtlpResource.test.ts +++ b/packages/effect/test/observability/OtlpResource.test.ts @@ -213,5 +213,37 @@ describe("OtlpResource", () => { { stringValue: "-9223372036854775809" } ) }) + + it("replaces an array that contains itself with a circular marker", () => { + const array: Array = [1] + array.push(array) + + assert.deepStrictEqual(OtlpResource.unknownToAttributeValue(array), { + arrayValue: { values: [{ intValue: 1 }, { stringValue: "[Circular]" }] } + }) + }) + + it("replaces an array that contains itself through a nested array", () => { + const outer: Array = ["outer"] + outer.push(["inner", outer]) + + assert.deepStrictEqual(OtlpResource.unknownToAttributeValue(outer), { + arrayValue: { + values: [ + { stringValue: "outer" }, + { arrayValue: { values: [{ stringValue: "inner" }, { stringValue: "[Circular]" }] } } + ] + } + }) + }) + + it("converts an array that appears more than once without a cycle each time", () => { + const shared = [1] + const sharedValue = { arrayValue: { values: [{ intValue: 1 }] } } + + assert.deepStrictEqual(OtlpResource.unknownToAttributeValue([shared, [shared]]), { + arrayValue: { values: [sharedValue, { arrayValue: { values: [sharedValue] } }] } + }) + }) }) }) diff --git a/packages/effect/test/observability/OtlpTracer.test.ts b/packages/effect/test/observability/OtlpTracer.test.ts index be20f919a04..765be0600eb 100644 --- a/packages/effect/test/observability/OtlpTracer.test.ts +++ b/packages/effect/test/observability/OtlpTracer.test.ts @@ -8,7 +8,12 @@ import * as Version from "effect/Version" const capture = () => { const exports: Array<{ scope: { name: string; version: string } - spans: Array<{ traceId: string; links: Array<{ traceId: string }>; status: { code: number } }> + spans: Array<{ + traceId: string + attributes: Array<{ key: string; value: unknown }> + links: Array<{ traceId: string }> + status: { code: number } + }> }> = [] const httpClient = HttpClient.makeWith( Effect.fnUntraced(function*(requestEffect) { @@ -53,6 +58,20 @@ describe("OtlpTracer", () => { }).pipe(Effect.provide(layer)) }) + it.effect("exports a self-referencing array attribute", () => { + const { exports, layer } = capture() + const array: Array = [1] + array.push(array) + return Effect.gen(function*() { + yield* Effect.void.pipe(Effect.withSpan("test", { attributes: { array } })) + yield* (yield* OtlpExporter.Flusher).flush + assert.deepStrictEqual(exports[0].spans[0].attributes, [{ + key: "array", + value: { arrayValue: { values: [{ intValue: 1 }, { stringValue: "[Circular]" }] } } + }]) + }).pipe(Effect.provide(layer)) + }) + it.effect("pads 64-bit trace ids in spans and links", () => { const { exports, layer } = capture() const parent = Tracer.externalSpan({ traceId: "463ac35c9f6413ad", spanId: "0020000000000001" })