1import type * as Otel from "@opentelemetry/api"
2import * as Inspectable from "effect/Inspectable"
3import * as Rec from "effect/Record"
4
5const bigint1e9 = BigInt(1_000_000_000)
6
7/** @internal */
8export const nanosToHrTime = (timestamp: bigint): Otel.HrTime => {
9 return [Number(timestamp / bigint1e9), Number(timestamp % bigint1e9)]
10}
11
12/** @internal */
13export 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 */
22export 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}