-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Open Telemetry Support for KTOR #5497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Subhanshu20135
wants to merge
10
commits into
ktorio:main
Choose a base branch
from
The-Developer-Diaries:feature/open-telemetry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0517799
Added Open Telemetry Support for KTOR
Subhanshu20135 10535a1
Uplifted Open Telemetry Version to v1.54.0
Subhanshu20135 bd1c8d8
Update ktor-client/ktor-client-plugins/ktor-client-opentelemetry/jvm/…
Subhanshu20135 bdc637a
Update ktor-client/ktor-client-plugins/ktor-client-opentelemetry/READ…
Subhanshu20135 2ccc508
Update ktor-client/ktor-client-plugins/ktor-client-opentelemetry/READ…
Subhanshu20135 a480798
Update ktor-server/ktor-server-plugins/ktor-server-opentelemetry/READ…
Subhanshu20135 e7c8d6c
Update ktor-client/ktor-client-plugins/ktor-client-opentelemetry/READ…
Subhanshu20135 cdf9a8c
Fixed Issues
Subhanshu20135 517bfd6
Fixed Issues: Removed unnecessary
Subhanshu20135 d5aa626
Refactored exception handling in OpenTelemetry plugin to remove redun…
Subhanshu20135 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
ktor-client/ktor-client-plugins/ktor-client-opentelemetry/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| # ktor-client-opentelemetry | ||
|
|
||
| Native OpenTelemetry integration for Ktor HTTP client. Provides distributed tracing, metrics, and trace context injection for outgoing requests following [OpenTelemetry semantic conventions](https://opentelemetry.io/docs/specs/semconv/http/http-spans/). | ||
|
|
||
| ## Installation | ||
|
|
||
| Add the dependency to your project: | ||
|
|
||
| ```kotlin | ||
| dependencies { | ||
| implementation("io.ktor:ktor-client-opentelemetry:$ktor_version") | ||
| } | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Basic setup | ||
|
|
||
| ```kotlin | ||
| import io.ktor.client.plugins.opentelemetry.* | ||
| import io.opentelemetry.sdk.OpenTelemetrySdk | ||
|
|
||
| val openTelemetry = OpenTelemetrySdk.builder() | ||
| .setTracerProvider(sdkTracerProvider) | ||
| .setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance())) | ||
| .build() | ||
|
|
||
| val client = HttpClient(CIO) { | ||
| install(OpenTelemetry) { | ||
| openTelemetry = openTelemetry | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Individual component configuration | ||
|
|
||
| ```kotlin | ||
| val client = HttpClient(CIO) { | ||
| install(OpenTelemetry) { | ||
| tracerProvider = sdkTracerProvider | ||
| meterProvider = sdkMeterProvider | ||
| propagators = W3CTraceContextPropagator.getInstance() | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Header capture | ||
|
|
||
| ```kotlin | ||
| val client = HttpClient(CIO) { | ||
| install(OpenTelemetry) { | ||
| openTelemetry = otel | ||
| captureRequestHeaders("X-Request-ID", "X-Correlation-ID") | ||
| captureResponseHeaders("X-RateLimit-Remaining") | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Filtering | ||
|
|
||
| ```kotlin | ||
| val client = HttpClient(CIO) { | ||
| install(OpenTelemetry) { | ||
| openTelemetry = otel | ||
| filter { request -> !request.url.encodedPath.startsWith("/internal") } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Features | ||
|
|
||
| ### Client spans | ||
|
|
||
| Creates a `CLIENT` span for each outgoing HTTP request with attributes: | ||
|
|
||
| | Attribute | Description | | ||
| |---|---| | ||
| | `http.request.method` | HTTP method (GET, POST, etc.) | | ||
| | `server.address` | Target server hostname | | ||
| | `server.port` | Target server port | | ||
| | `url.full` | Full request URL | | ||
| | `http.response.status_code` | Response status code | | ||
| | `error.type` | Exception class name or HTTP status code (on error) | | ||
|
|
||
| ### Trace context injection | ||
|
|
||
| Automatically injects trace context headers into outgoing requests that pass the configured `filter`, using the configured `TextMapPropagator`. With W3C Trace Context (default), the `traceparent` and `tracestate` headers are added to each traced request. | ||
|
|
||
| ### Server-client context propagation | ||
|
|
||
| When used together with [ktor-server-opentelemetry](../../../ktor-server/ktor-server-plugins/ktor-server-opentelemetry), trace context flows automatically from incoming server requests to outgoing client requests. The server plugin propagates the OTEL context through Kotlin coroutines, so client spans created inside a request handler automatically become children of the server span: | ||
|
|
||
| ```kotlin | ||
| fun Application.module() { | ||
| install(io.ktor.server.plugins.opentelemetry.OpenTelemetry) { | ||
| openTelemetry = otel | ||
| } | ||
|
|
||
| val client = HttpClient(CIO) { | ||
| install(io.ktor.client.plugins.opentelemetry.OpenTelemetry) { | ||
| openTelemetry = otel | ||
| } | ||
| } | ||
|
|
||
| routing { | ||
| get("/api/users/{id}") { | ||
| // The client span for this call is automatically a child of the server span | ||
| val user = client.get("http://user-service/users/${call.parameters["id"]}") | ||
| call.respond(user.body()) | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| This produces a distributed trace: | ||
|
|
||
| ```text | ||
| [Server] GET /api/users/{id} | ||
| └── [Client] HTTP GET → user-service | ||
| ``` | ||
|
|
||
| ### Metrics | ||
|
|
||
| | Metric | Type | Description | | ||
| |---|---|---| | ||
| | `http.client.request.duration` | Histogram (seconds) | Duration of HTTP client requests | | ||
|
|
||
| Metric attributes include `http.request.method`, `http.response.status_code`, and `server.address`. | ||
|
|
||
| ### Error handling | ||
|
|
||
| - Network errors and exceptions are recorded on the span via `Span.recordException()` | ||
| - HTTP 4xx/5xx responses set the span status to `ERROR` | ||
| - The `error.type` attribute is set to the exception class name or HTTP status code | ||
| - Spans are always ended, even when exceptions occur, preventing span leaks | ||
23 changes: 23 additions & 0 deletions
23
ktor-client/ktor-client-plugins/ktor-client-opentelemetry/api/ktor-client-opentelemetry.api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| public final class io/ktor/client/plugins/opentelemetry/OpenTelemetryClientConfig { | ||
| public fun <init> ()V | ||
| public final fun captureRequestHeaders ([Ljava/lang/String;)V | ||
| public final fun captureResponseHeaders ([Ljava/lang/String;)V | ||
| public final fun filter (Lkotlin/jvm/functions/Function1;)V | ||
| public final fun getInstrumentationName ()Ljava/lang/String; | ||
| public final fun getInstrumentationVersion ()Ljava/lang/String; | ||
| public final fun getMeterProvider ()Lio/opentelemetry/api/metrics/MeterProvider; | ||
| public final fun getOpenTelemetry ()Lio/opentelemetry/api/OpenTelemetry; | ||
| public final fun getPropagators ()Lio/opentelemetry/context/propagation/TextMapPropagator; | ||
| public final fun getTracerProvider ()Lio/opentelemetry/api/trace/TracerProvider; | ||
| public final fun setInstrumentationName (Ljava/lang/String;)V | ||
| public final fun setInstrumentationVersion (Ljava/lang/String;)V | ||
| public final fun setMeterProvider (Lio/opentelemetry/api/metrics/MeterProvider;)V | ||
| public final fun setOpenTelemetry (Lio/opentelemetry/api/OpenTelemetry;)V | ||
| public final fun setPropagators (Lio/opentelemetry/context/propagation/TextMapPropagator;)V | ||
| public final fun setTracerProvider (Lio/opentelemetry/api/trace/TracerProvider;)V | ||
| } | ||
|
|
||
| public final class io/ktor/client/plugins/opentelemetry/OpenTelemetryKt { | ||
| public static final fun getOpenTelemetry ()Lio/ktor/client/plugins/api/ClientPlugin; | ||
| } | ||
|
|
Empty file.
20 changes: 20 additions & 0 deletions
20
ktor-client/ktor-client-plugins/ktor-client-opentelemetry/build.gradle.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| * Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
| */ | ||
|
|
||
| plugins { | ||
| id("ktorbuild.project.client-plugin") | ||
| } | ||
|
|
||
| kotlin { | ||
| sourceSets { | ||
| jvmMain.dependencies { | ||
| api(libs.opentelemetry.api) | ||
| } | ||
| jvmTest.dependencies { | ||
| implementation(libs.opentelemetry.sdk) | ||
| implementation(libs.opentelemetry.sdk.testing) | ||
| implementation(projects.ktorServerTestHost) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.