| 1 | import type * as Otel from "@opentelemetry/api" |
| 2 | import * as Inspectable from "effect/Inspectable" |
| 3 | import * as Rec from "effect/Record" |
| 4 | |
| 5 | const bigint1e9 = BigInt(1_000_000_000) |
| 6 | |
| 7 | /** @internal */ |
| 8 | export const nanosToHrTime = (timestamp: bigint): Otel.HrTime => { |
| 9 | return [Number(timestamp / bigint1e9), Number(timestamp % bigint1e9)] |
| 10 | } |
| 11 | |
| 12 | /** @internal */ |
| 13 | export const recordToAttributes = (record: Record<string, unknown>): Otel.Attributes => { |
| 14 | const attributes: Otel.Attributes = {} |
| 15 | for (const [key, value] of Object.entries(record)) { |
| 16 | Rec.assignProperty(attributes, key, unknownToAttributeValue(value)) |
| 17 | } |
| 18 | return attributes |
| 19 | } |
| 20 | |
| 21 | /** @internal */ |
| 22 | export const unknownToAttributeValue = (value: unknown): Otel.AttributeValue => { |
| 23 | if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") { |
| 24 | return value |
| 25 | } else if (typeof value === "bigint") { |
| 26 | return value.toString() |
| 27 | } |
| 28 | return Inspectable.toStringUnknown(value) |
| 29 | } |