OtlpHttpEdge

class OtlpHttpEdge(client: HttpClient, endpoint: String, store: DurableStore, maxSentIds: Int = DEFAULT_MAX_SENT_IDS, wire: OtlpWireFormat = OtlpWireFormat.JSON, @IntroducedAt(version = "0.7.5") resource: Map<String, String> = emptyMap(), @IntroducedAt(version = "0.7.5") scopeName: String = "", @IntroducedAt(version = "0.7.5") scopeVersion: String = "", @IntroducedAt(version = "0.7.6") maxRequestBytes: Int = DEFAULT_MAX_REQUEST_BYTES) : OtlpEdge

A Ktor OTLP/HTTP OtlpEdge. POSTs each signal to /v1/{traces,logs,metrics} and reconciles by a producer-local sent-set persisted in store — because OTLP/HTTP is write-only, there is no collector read-back.

The wire encoding is selectable via wire: OTLP/JSON (application/json, the default) or OTLP/protobuf (application/x-protobuf, the canonical, more compact OTLP wire many collectors default to). The digest/bridge reconciliation is identical for both — only the request body bytes and Content-Type differ.

The digest is what this producer has already successfully delivered to this endpoint: span and log ids in a bounded id-set, metric series as MetricKey → value hash. The set is folded forward only after a 2xx response, so a failed POST leaves the digest untouched and the next drain retries. A lost sent-set can cause re-POSTs, so delivery deduplication relies on the backend applying records idempotently by id. This sent-set does not repair distinct stamped span values created by retrying an export after an indeterminate local write.

The metric map is read from store by the digest and again before each write, and decoded only when its bytes changed since this edge last saw them. With one edge per store and endpoint that is never, so a drain that sends pays two reads, one encode and one write of the map, where it used to pay two decodes on top.

The id-set is capped at maxSentIds (drop-oldest) so a long-lived producer's sent-set cannot grow without bound; choose a cap that exceeds the device's realistic offline window.

One signal, several requests

A signal whose request would be bigger than maxRequestBytes is split into several, each under the cap, sent one after another. Records that share an id, such as two stamped copies of one span, are grouped and always ride in one request, placed where that id first appears in the batch; otherwise the batch's order holds. A span travels with the links it owns, and every request carries the resource and scope. A request's records count as delivered only once that request is acknowledged, and the sent-set is written once per signal, after the last request, from a fresh read. So a refused request, a transport failure or a cancellation part-way still records the requests that were acknowledged, and the next drain sends only the rest. The send then throws, so the bridge reports that signal failed with a count of 0, although the collector holds part of it (see OtlpEdge). A process killed before that write resends the signal's whole batch on the next drain, the bound an unsplit request already had.

Too big is decided by this edge's own measurement and by a 413 answer, never by a 400: a collector may answer an oversized body with either, and answers a malformed one with 400 too. A 413 on a request of several records halves it and sends the halves; the smaller size is not remembered. A record bigger than the cap on its own is sent alone, after every other request is acknowledged, because the cap is a guess and the collector may still take it. A 413 on that record records it as delivered, with a WARN naming it and its size: the same bytes would be refused again, and keeping it would fail the signal on every drain until the buffer evicts it. A 413 on a lone record inside the cap fails the signal instead: something in front of the collector limits bodies below maxRequestBytes, and folding would report records sent that the collector never took. Unlike other refusals it does not stop the sends: the same record is refused on every drain, so the rest are still offered and recorded, and the signal fails after them. Lower maxRequestBytes to that limit.

Parameters

client

caller-owned Ktor HttpClient — it owns the engine, timeouts, TLS, and any auth headers. kuilt does not create or close it, and this module ships no engine: depend on one (ktor-client-okhttp, ktor-client-java, ktor-client-darwin, …) and pass it, as in HttpClient(OkHttp).

endpoint

collector base URL, e.g. https://collector:4318. Any trailing / is trimmed, so .../:4318/ and .../:4318 are one collector. The sent-set StoreKey identifies the endpoint by a 128-bit SHA-256 tag rather than by the URL, so two collectors never share a sent-set, the key is a fixed width whatever the URL's length, and a URL carrying credentials never reaches a filename. Length is unconstrained here.

store

durable persistence for the per-endpoint sent-set. Give each producer its own store, or its own endpoint: every drain prunes the metric record to the draining producer's live series, so two producers sharing one store and endpoint would each prune the other's, then resend their whole metric set on every drain.

maxSentIds

cap on the span/log sent-set size (drop-oldest). The metric sent-versions map takes no cap: every drain prunes it to the series the producer still holds (retainMetrics), so it is bounded by the producer's maxMetrics.

wire

OTLP wire encoding to emit. Defaults to OtlpWireFormat.JSON.

resource

this producer's resource attributes, sent as the resource of every request on both wires — service.name and whatever else tells one producer from another, so a collector or backend files its records under their own service rather than one unknown one. Fixed for the edge's life, as a producer's resource is. Empty, the default, sends no resource at all.

scopeName

the instrumentation scope's name, sent as the scope of every request on both wires: the library that recorded the telemetry.

scopeVersion

the instrumentation scope's version. When both scopeName and scopeVersion are empty, the default, no scope is sent; OTLP reads an empty name as an unknown scope either way.

maxRequestBytes

the largest body, in bytes before any compression the client applies, that one request may carry; a batch over it is split. Defaults to DEFAULT_MAX_REQUEST_BYTES, 1 MiB, the limit a reverse proxy commonly enforces in front of a collector, which itself defaults to 20 MiB. It must hold one minimal record inside this edge's resource and scope, or construction throws IllegalArgumentException: an envelope that fills the cap would otherwise send every record alone.

Constructors

Link copied to clipboard
constructor(client: HttpClient, endpoint: String, store: DurableStore, maxSentIds: Int = DEFAULT_MAX_SENT_IDS, wire: OtlpWireFormat = OtlpWireFormat.JSON, @IntroducedAt(version = "0.7.5") resource: Map<String, String> = emptyMap(), @IntroducedAt(version = "0.7.5") scopeName: String = "", @IntroducedAt(version = "0.7.5") scopeVersion: String = "", @IntroducedAt(version = "0.7.6") maxRequestBytes: Int = DEFAULT_MAX_REQUEST_BYTES)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
open suspend override fun digest(): SpanDigest
Link copied to clipboard
open suspend override fun logDigest(): LogDigest
Link copied to clipboard
open suspend override fun metricDigest(): MetricDigest
Link copied to clipboard
open suspend override fun retainMetrics(live: Set<MetricKey>)
Link copied to clipboard
open suspend override fun send(spans: Set<SpanRecord>, links: List<SpanLink>)
Link copied to clipboard
open suspend override fun sendLogs(logs: Set<LogRecord>)
Link copied to clipboard
open suspend override fun sendMetrics(points: Set<MetricPoint>)