otlpnamespacenamespace otlpOTLP/HTTP with JSON, both ways (commons 0001-telemetry FR-004): export requests of traces, logs and metrics as the OTLP specification's JSON encoding writes them (camel-case field names, trace and span ids as hexadecimal, 64-bit integers as decimal strings, enumerations as integers, bytes as base64, signals grouped by resource and scope), the export responses with their partial success, and the decoding of each back to the values of ./otel.ts: a failure is a Result naming the path, a 64-bit integer is read from a JSON number or a string, an enumeration from its number or its name, and a field the decoder does not know is ignored.
import { decodeTraces, encodeTraces } from "./otlp.ts";
const request = encodeTraces([{ resource: { attributes: { "service.name": "build" } }, scope: { name: "ifc" }, spans: [] }]);
decodeTraces(JSON.parse(JSON.stringify(request))); // { kind: "ok", value: [ ...the same batch ] }otlpnamespacenamespace otlpotlp.TraceBatchtypeno test imports ittype TraceBatch = { readonly resource: Resource; readonly scope: Scope; readonly spans: readonly SpanData[] };The spans of one resource and scope, as OTLP groups them.
otlp.LogBatchtypeno test imports ittype LogBatch = { readonly resource: Resource; readonly scope: Scope; readonly records: readonly LogRecord[] };The log records of one resource and scope, as OTLP groups them.
otlp.MetricBatchtypeno test imports ittype MetricBatch = { readonly resource: Resource; readonly scope: Scope; readonly metrics: readonly Metric[] };The metrics of one resource and scope, as OTLP groups them.
otlp.ExportResponsetypeno test imports ittype ExportResponse = { readonly rejected: number; readonly errorMessage: string };An export response: how many signals were rejected (spans, log records or data points) and why; zero and "" for full success.
otlp.encodeAnyValuefunctionno test imports itfunction encodeAnyValue(value: AnyValue): JsonObjectA value as OTLP's JSON AnyValue.
otlp.encodeAttributesfunctionno test imports itfunction encodeAttributes(attributes: Readonly<Record<string, AttributeValue>>): Json[]Attributes as OTLP's JSON list of KeyValue.
otlp.encodeSpanfunctionno test imports itfunction encodeSpan(s: SpanData): JsonObjectA finished span as OTLP's JSON Span.
otlp.encodeLogRecordfunctionno test imports itfunction encodeLogRecord(r: LogRecord): JsonObjectA log record as OTLP's JSON LogRecord.
otlp.encodeMetricfunctionno test imports itfunction encodeMetric(m: Metric): JsonObjectA metric as OTLP's JSON Metric.
otlp.encodeTracesfunctionno test imports itfunction encodeTraces(batches: readonly TraceBatch[]): JsonObjectTrace batches as an OTLP/JSON ExportTraceServiceRequest.
otlp.encodeLogsfunctionno test imports itfunction encodeLogs(batches: readonly LogBatch[]): JsonObjectLog batches as an OTLP/JSON ExportLogsServiceRequest.
otlp.encodeMetricsfunctionno test imports itfunction encodeMetrics(batches: readonly MetricBatch[]): JsonObjectMetric batches as an OTLP/JSON ExportMetricsServiceRequest.
otlp.encodeResponsefunctionno test imports itfunction encodeResponse(signal: SignalName, response: ExportResponse): JsonObjectAn export response as OTLP/JSON: {} for full success, else partialSuccess with the rejected count (a decimal string) and the message.
otlp.decodeAnyValueconstno test imports itconst decodeAnyValue: Decoder<AnyValue>OTLP's JSON AnyValue as a value; an AnyValue that holds none is refused.
otlp.decodeAttributeValueconstno test imports itconst decodeAttributeValue: Decoder<AttributeValue>An AnyValue that is an attribute value: a primitive, or an array of primitives of one type.
otlp.decodeAttributesconstno test imports itconst decodeAttributes: Decoder<Readonly<Record<string, AttributeValue>>>OTLP's JSON list of KeyValue as attributes.
otlp.decodeSpanconstno test imports itconst decodeSpan: Decoder<SpanData>OTLP's JSON Span as a finished span.
otlp.decodeLogRecordconstno test imports itconst decodeLogRecord: Decoder<LogRecord>OTLP's JSON LogRecord as a log record; its severity number must be 1 to 24.
otlp.decodeMetricconstno test imports itconst decodeMetric: Decoder<Metric>OTLP's JSON Metric as a metric: a sum, a gauge or a histogram (other data is refused).
otlp.decodeTracesfunctionno test imports itfunction decodeTraces(input: unknown): Result<readonly TraceBatch[], DecodeError>An OTLP/JSON ExportTraceServiceRequest as trace batches, one for each scope of each resource; a failure names its path.
otlp.decodeLogsfunctionno test imports itfunction decodeLogs(input: unknown): Result<readonly LogBatch[], DecodeError>An OTLP/JSON ExportLogsServiceRequest as log batches, one for each scope of each resource; a failure names its path.
otlp.decodeMetricsfunctionno test imports itfunction decodeMetrics(input: unknown): Result<readonly MetricBatch[], DecodeError>An OTLP/JSON ExportMetricsServiceRequest as metric batches, one for each scope of each resource; a failure names its path.
otlp.decodeResponsefunctionno test imports itfunction decodeResponse(signal: SignalName, input: unknown): Result<ExportResponse, DecodeError>An OTLP/JSON export response; an absent partialSuccess is full success.
otlp.countSignalsfunctionno test imports itfunction countSignals(batches: readonly LogBatch[] | readonly MetricBatch[] | readonly TraceBatch[]): numberThe number of signals in trace, log or metric batches: spans, log records, or data points (what a partial success counts).
TraceBatchtypetype TraceBatch = { readonly resource: Resource; readonly scope: Scope; readonly spans: readonly SpanData[] };The spans of one resource and scope, as OTLP groups them.
LogBatchtypetype LogBatch = { readonly resource: Resource; readonly scope: Scope; readonly records: readonly LogRecord[] };The log records of one resource and scope, as OTLP groups them.
MetricBatchtypetype MetricBatch = { readonly resource: Resource; readonly scope: Scope; readonly metrics: readonly Metric[] };The metrics of one resource and scope, as OTLP groups them.
ExportResponsetypeno test imports ittype ExportResponse = { readonly rejected: number; readonly errorMessage: string };An export response: how many signals were rejected (spans, log records or data points) and why; zero and "" for full success.
encodeAnyValuefunctionno test imports itfunction encodeAnyValue(value: AnyValue): JsonObjectA value as OTLP's JSON AnyValue.
encodeAttributesfunctionno test imports itfunction encodeAttributes(attributes: Readonly<Record<string, AttributeValue>>): Json[]Attributes as OTLP's JSON list of KeyValue.
encodeSpanfunctionno test imports itfunction encodeSpan(s: SpanData): JsonObjectA finished span as OTLP's JSON Span.
encodeLogRecordfunctionno test imports itfunction encodeLogRecord(r: LogRecord): JsonObjectA log record as OTLP's JSON LogRecord.
encodeMetricfunctionno test imports itfunction encodeMetric(m: Metric): JsonObjectA metric as OTLP's JSON Metric.
encodeTracesfunctionfunction encodeTraces(batches: readonly TraceBatch[]): JsonObjectTrace batches as an OTLP/JSON ExportTraceServiceRequest.
encodeLogsfunctionfunction encodeLogs(batches: readonly LogBatch[]): JsonObjectLog batches as an OTLP/JSON ExportLogsServiceRequest.
encodeMetricsfunctionfunction encodeMetrics(batches: readonly MetricBatch[]): JsonObjectMetric batches as an OTLP/JSON ExportMetricsServiceRequest.
encodeResponsefunctionfunction encodeResponse(signal: SignalName, response: ExportResponse): JsonObjectAn export response as OTLP/JSON: {} for full success, else partialSuccess with the rejected count (a decimal string) and the message.
decodeAnyValueconstno test imports itconst decodeAnyValue: Decoder<AnyValue>OTLP's JSON AnyValue as a value; an AnyValue that holds none is refused.
decodeAttributeValueconstno test imports itconst decodeAttributeValue: Decoder<AttributeValue>An AnyValue that is an attribute value: a primitive, or an array of primitives of one type.
decodeAttributesconstno test imports itconst decodeAttributes: Decoder<Readonly<Record<string, AttributeValue>>>OTLP's JSON list of KeyValue as attributes.
decodeSpanconstno test imports itconst decodeSpan: Decoder<SpanData>OTLP's JSON Span as a finished span.
decodeLogRecordconstno test imports itconst decodeLogRecord: Decoder<LogRecord>OTLP's JSON LogRecord as a log record; its severity number must be 1 to 24.
decodeMetricconstno test imports itconst decodeMetric: Decoder<Metric>OTLP's JSON Metric as a metric: a sum, a gauge or a histogram (other data is refused).
decodeTracesfunctionfunction decodeTraces(input: unknown): Result<readonly TraceBatch[], DecodeError>An OTLP/JSON ExportTraceServiceRequest as trace batches, one for each scope of each resource; a failure names its path.
decodeLogsfunctionfunction decodeLogs(input: unknown): Result<readonly LogBatch[], DecodeError>An OTLP/JSON ExportLogsServiceRequest as log batches, one for each scope of each resource; a failure names its path.
decodeMetricsfunctionfunction decodeMetrics(input: unknown): Result<readonly MetricBatch[], DecodeError>An OTLP/JSON ExportMetricsServiceRequest as metric batches, one for each scope of each resource; a failure names its path.
decodeResponsefunctionfunction decodeResponse(signal: SignalName, input: unknown): Result<ExportResponse, DecodeError>An OTLP/JSON export response; an absent partialSuccess is full success.
countSignalsfunctionfunction countSignals(batches: readonly LogBatch[] | readonly MetricBatch[] | readonly TraceBatch[]): numberThe number of signals in trace, log or metric batches: spans, log records, or data points (what a partial success counts).