Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/otlp-circular-arrays.md
Original file line number Diff line number Diff line change
@@ -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]"`.
26 changes: 21 additions & 5 deletions packages/effect/src/observability/OtlpResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown> | undefined): AnyValue => {
if (Array.isArray(value)) {
const path = ancestors ?? new Set<unknown>()
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
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions packages/effect/test/observability/OtlpLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown> = [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*() {
Expand Down
32 changes: 32 additions & 0 deletions packages/effect/test/observability/OtlpResource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,37 @@ describe("OtlpResource", () => {
{ stringValue: "-9223372036854775809" }
)
})

it("replaces an array that contains itself with a circular marker", () => {
const array: Array<unknown> = [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<unknown> = ["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] } }] }
})
})
})
})
21 changes: 20 additions & 1 deletion packages/effect/test/observability/OtlpTracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<unknown> = [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" })
Expand Down
Loading