| 1 | /** |
| 2 | * Serializes OTLP payloads into HTTP request bodies. |
| 3 | * |
| 4 | * Signal exporters build trace, metric, and log data structures in memory. This |
| 5 | * module provides the service that turns those structures into JSON or protobuf |
| 6 | * HTTP bodies before they are posted to an OTLP collector. |
| 7 | * |
| 8 | * @stability unstable |
| 9 | * @since 4.0.0 |
| 10 | */ |
| 11 | import * as Context from "../Context.ts" |
| 12 | import * as HttpBody from "../http/HttpBody.ts" |
| 13 | import * as Layer from "../Layer.ts" |
| 14 | import * as otlpProtobuf from "./internal/otlpProtobuf.ts" |
| 15 | import type { LogsData } from "./OtlpLogger.ts" |
| 16 | import type { MetricsData } from "./OtlpMetrics.ts" |
| 17 | import type { TraceData } from "./OtlpTracer.ts" |
| 18 | |
| 19 | /** |
| 20 | * Service for serializing OTLP traces, metrics, and logs into HTTP request |
| 21 | * bodies. |
| 22 | * |
| 23 | * @stability unstable |
| 24 | * @category services |
| 25 | * @since 4.0.0 |
| 26 | */ |
| 27 | export class OtlpSerialization extends Context.Service<OtlpSerialization, { |
| 28 | readonly traces: (data: TraceData) => HttpBody.HttpBody |
| 29 | readonly metrics: (data: MetricsData) => HttpBody.HttpBody |
| 30 | readonly logs: (data: LogsData) => HttpBody.HttpBody |
| 31 | }>()("effect/observability/OtlpSerialization") {} |
| 32 | |
| 33 | /** |
| 34 | * Provides `OtlpSerialization` using OTLP/HTTP JSON bodies. |
| 35 | * |
| 36 | * @stability unstable |
| 37 | * @category layers |
| 38 | * @since 4.0.0 |
| 39 | */ |
| 40 | export const layerJson = Layer.succeed(OtlpSerialization, { |
| 41 | traces: (spans) => HttpBody.jsonUnsafe(spans), |
| 42 | metrics: (metrics) => HttpBody.jsonUnsafe(metrics), |
| 43 | logs: (logs) => HttpBody.jsonUnsafe(logs) |
| 44 | }) |
| 45 | |
| 46 | /** |
| 47 | * Provides `OtlpSerialization` using protobuf-encoded OTLP bodies with the |
| 48 | * `application/x-protobuf` content type. |
| 49 | * |
| 50 | * @stability unstable |
| 51 | * @category layers |
| 52 | * @since 4.0.0 |
| 53 | */ |
| 54 | export const layerProtobuf = Layer.succeed(OtlpSerialization, { |
| 55 | traces: (spans) => |
| 56 | HttpBody.uint8Array( |
| 57 | otlpProtobuf.encodeTracesData(spans as any), |
| 58 | "application/x-protobuf" |
| 59 | ), |
| 60 | metrics: (metrics) => |
| 61 | HttpBody.uint8Array( |
| 62 | otlpProtobuf.encodeMetricsData(metrics as any), |
| 63 | "application/x-protobuf" |
| 64 | ), |
| 65 | logs: (logs) => |
| 66 | HttpBody.uint8Array( |
| 67 | otlpProtobuf.encodeLogsData(logs as any), |
| 68 | "application/x-protobuf" |
| 69 | ) |
| 70 | }) |