Configure OpenTelemetry Kotlin

Ви переглядаєте англійську версію сторінки, тому що її ще не було повністю перекладеною українською. Бажаєте допомогти? Дивіться як взяти Участь.

PS. Неофіційний український переклад (не перевірений і не ухвалений OpenTelemetry) доступний на сайті члена спільноти, створеному на основі PR #5891. Ми надаємо це посилання як тимчасовий захід підтримки українських читачів та потенційних учасників, доки не буде готовий офіційний переклад.

The OpenTelemetry Kotlin SDK is configured at initialization through its DSL parameter. Both createOpenTelemetry and createCompatOpenTelemetry use the same DSL, a full example of which is shown below:

val otel: OpenTelemetry = createOpenTelemetry {
    // configure SDK here
}

The following sections show how to configure different aspects of the SDK’s behavior. This is not an exhaustive list of ways to configure the SDK; rather it attempts to highlight the most common use cases.

Export

Exporting telemetry via OTLP

OpenTelemetry Kotlin supports exporting via OTLP over HTTP in binary encoding. Logs and traces can be exported with the following configuration, which sends data to the default port for the OpenTelemetry Collector:

val baseUrl = "http://localhost:4318"
val otel: OpenTelemetry = createOpenTelemetry {
    tracerProvider {
        export {
            batchSpanProcessor(otlpHttpSpanExporter(baseUrl))
        }
    }
    loggerProvider {
        export {
            batchLogRecordProcessor(otlpHttpLogRecordExporter(baseUrl))
        }
    }
}

Resource

Resources can be configured globally to add attributes to all signals:

val otel: OpenTelemetry = createOpenTelemetry {
    serviceName = "checkout"

    resource(schemaUrl = "https://opentelemetry.io/schemas/1.30.0") {
        setStringAttribute("service.namespace", "payments")
        setBooleanAttribute("feature.experimental_checkout", true)
        setLongAttribute("service.instance.replica", 3)
        setDoubleAttribute("rollout.percentage", 0.25)
        setStringListAttribute("service.tags", listOf("checkout", "v2"))
    }

    tracerProvider {
        resource {}
    }
}

It’s also possible to scope a resource to a single signal. This is merged with any global configuration (local takes priority):

val otel: OpenTelemetry = createOpenTelemetry {
    tracerProvider {
        resource {
            setStringAttribute("service.namespace", "payments")
        }
    }
}

Syntactic sugar also allows setting the Resources from a Map<String, Any>:

val otel: OpenTelemetry = createOpenTelemetry {
    resource {
        resource(mapOf("service.namespace" to "payments"))
    }
}

By default, the SDK always sets service.name, service.version, and telemetry.sdk.*. These can be overriden by supplying your own value.

Limits

Attribute limits

Attribute limits limit the number of attributes and the length of their values in characters. These can be configured globally as shown below:

val otel: OpenTelemetry = createOpenTelemetry {
    attributeLimits {
        attributeCountLimit = 200
        attributeValueLengthLimit = 256
    }
}

Log Limits

Log Limits act similarly to the attribute limits section, but are local to log records:

val otel: OpenTelemetry = createOpenTelemetry {
    loggerProvider {
        logLimits {
            attributeCountLimit = 200
            attributeValueLengthLimit = 256
        }
    }
}

Span Limits

Span Limits provide the same limits as previously shown in the attribute limits section, and also provide configuration that restricts how many spans and events are captured:

val otel: OpenTelemetry = createOpenTelemetry {
    tracerProvider {
        spanLimits = 200
        eventCountLimit = 250
        attributeCountPerEventLimit = 50
        attributeCountPerLinkLimit = 20
        attributeCountLimit = 200
        attributeValueLengthLimit = 256
    }
}

Implicit Context storage

As described in the implicit context instrumentation guide it’s possible to alter the default mechanism that stores the implicit context. By default this is stored in a global, process-wide mechanism, but can be changed to thread-local via storageMode:

val otel: OpenTelemetry = createOpenTelemetry {
    context {
        storageMode = ImplicitContextStorageMode.THREAD_LOCAL
        // storage { MyCustomStorage() }
    }
}

You can implement custom storage mechanisms too. The example below implements the same behavior as OpenTelemetry Kotlin’s default approach to global context storage:

val otel: OpenTelemetry = createOpenTelemetry {
    context {
        storage { MyCustomStorage() }
    }
}

class CustomStorage(private val default: Context): ImplicitContextStorage {

    private var ref: Context = default

    override fun setImplicitContext(context: Context) {
        ref = context
    }

    override fun implicitContext(): Context = ref
}

Востаннє змінено September 9, 2026: Additional docs for the OpenTelemetry Kotlin SDK (#9984) (823c9105)