Skip to content

Commit

Permalink
fix encoding of logs to tracer span events (#3644)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Sep 20, 2024
1 parent c642abc commit bb5ec6b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .changeset/tricky-bears-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"effect": patch
"@effect/opentelemetry": patch
---

fix encoding of logs to tracer span events
3 changes: 3 additions & 0 deletions packages/effect/src/Inspectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ export abstract class Class {
* @since 2.0.0
*/
export const toStringUnknown = (u: unknown, whitespace: number | string | undefined = 2): string => {
if (typeof u === "string") {
return u
}
try {
return typeof u === "object" ? stringifyCircular(u, whitespace) : String(u)
} catch (_) {
Expand Down
24 changes: 15 additions & 9 deletions packages/effect/src/internal/fiberRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1471,26 +1471,32 @@ export const tracerLogger = globalValue(
logLevel,
message
}) => {
const span = Option.flatMap(fiberRefs.get(context, core.currentContext), Context.getOption(tracer.spanTag))
const clockService = Option.map(
fiberRefs.get(context, defaultServices.currentServices),
(_) => Context.get(_, clock.clockTag)
const span = Context.getOption(
fiberRefs.getOrDefault(context, core.currentContext),
tracer.spanTag
)
if (span._tag === "None" || span.value._tag === "ExternalSpan" || clockService._tag === "None") {
if (span._tag === "None" || span.value._tag === "ExternalSpan") {
return
}
const clockService = Context.unsafeGet(
fiberRefs.getOrDefault(context, defaultServices.currentServices),
clock.clockTag
)

const attributes = Object.fromEntries(HashMap.map(annotations, Inspectable.toStringUnknown))
const attributes: Record<string, unknown> = {}
for (const [key, value] of annotations) {
attributes[key] = value
}
attributes["effect.fiberId"] = FiberId.threadName(fiberId)
attributes["effect.logLevel"] = logLevel.label

if (cause !== null && cause._tag !== "Empty") {
attributes["effect.cause"] = internalCause.pretty(cause)
attributes["effect.cause"] = internalCause.pretty(cause, { renderErrorCause: true })
}

span.value.event(
String(message),
clockService.value.unsafeCurrentTimeNanos(),
Inspectable.toStringUnknown(Array.isArray(message) ? message[0] : message),
clockService.unsafeCurrentTimeNanos(),
attributes
)
})
Expand Down
12 changes: 2 additions & 10 deletions packages/opentelemetry/src/internal/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as Cause from "effect/Cause"
import * as Context from "effect/Context"
import * as Effect from "effect/Effect"
import type { Exit } from "effect/Exit"
import * as Inspectable from "effect/Inspectable"
import * as Layer from "effect/Layer"
import * as Option from "effect/Option"
import * as EffectTracer from "effect/Tracer"
Expand Down Expand Up @@ -301,14 +302,5 @@ const unknownToAttributeValue = (value: unknown): OtelApi.AttributeValue => {
} else if (typeof value === "bigint") {
return Number(value)
}

return objectToAttribute(value)
}

const objectToAttribute = (value: unknown): string => {
try {
return JSON.stringify(value, null, 2)
} catch {
return String(value)
}
return Inspectable.toStringUnknown(value)
}

0 comments on commit bb5ec6b

Please sign in to comment.