DatadogTracingAppender

class DatadogTracingAppender(service: String, agentUrl: String = "http://localhost:8126", batchSize: Int = 100, batchTimeout: Duration = 2.seconds, env: String? = null, version: String? = null, spanType: String = "custom", engine: HttpClientEngine? = null) : BatchAppender<TracingEvent> (source)

An io.github.smyrgeorge.log4k.Appender that publishes finished spans to a local Datadog Agent over its APM trace intake API (PUT /v0.4/traces, JSON). Register it with RootLogger.Tracing.appenders.register(...).

Spans are shipped in batches (via BatchAppender): a batch is sent as soon as it reaches batchSize spans or when batchTimeout elapses since the first span of the batch arrived — so low traffic never holds spans back for long. Within a batch, spans are grouped by trace, as the intake API expects.

The HTTP transport is a Ktor client: pass an engine explicitly, or add exactly one Ktor engine artifact for your platform (CIO, Darwin, Curl, WinHttp, Js, ...) to the runtime classpath and leave it null so the engine is discovered automatically.

The agent must have APM enabled (DD_APM_ENABLED=true); it listens on port 8126 by default.

Mapping notes:

  • log4k's OpenTelemetry-style hex ids are converted to Datadog's numeric ids: the low 64 bits are used, and for 128-bit trace ids the high 64 bits are preserved in the _dd.p.tid tag. Non-hexadecimal ids (e.g. from a remote/propagated context) are hashed (FNV-1a).

  • Finite numeric span tags become Datadog metrics; everything else — including non-finite numbers, which metrics cannot represent — goes to meta (stringified).

  • Span events are encoded as a JSON array in meta.events, mirroring the OpenTelemetry ingestion convention. An error status sets error=1 plus error.type/message/stack.

Delivery is best-effort: agent failures are reported to the console, and the batch is dropped — the appender itself keeps running.

Parameters

service

The Datadog service name the spans are reported under.

agentUrl

Base URL of the local Datadog Agent. Defaults to http://localhost:8126.

batchSize

Maximum number of spans per request. Defaults to 100.

batchTimeout

Maximum time to hold an incomplete batch. Defaults to 2 seconds.

env

Optional env tag applied to every span.

version

Optional version tag applied to every span.

spanType

Datadog span type (e.g. web, db, custom). Defaults to custom.

engine

Optional Ktor HttpClientEngine. When null, the engine available on the classpath is used.

Constructors

Link copied to clipboard
constructor(service: String, agentUrl: String = "http://localhost:8126", batchSize: Int = 100, batchTimeout: Duration = 2.seconds, env: String? = null, version: String? = null, spanType: String = "custom", engine: HttpClientEngine? = null)

Properties

Link copied to clipboard
override val name: String

Functions

Link copied to clipboard
suspend override fun append(event: TracingEvent)
Link copied to clipboard
open suspend override fun handle(event: List<TracingEvent>)
Link copied to clipboard
open override fun setup(flow: Flow<TracingEvent>): Flow<List<TracingEvent>>