From 47a498020183dfa33e136d68823e623c4acaa20d Mon Sep 17 00:00:00 2001 From: Mariia Skripchenko <61115099+marychatte@users.noreply.github.com> Date: Mon, 18 Nov 2024 13:53:05 +0100 Subject: [PATCH] Update opentelemetry example --- opentelemetry/README.md | 46 ++----------- opentelemetry/build.gradle.kts | 2 +- opentelemetry/client/build.gradle.kts | 2 +- .../extractions/attributeExtractor.kt | 69 ------------------- .../extractions/capturedHeaders.kt | 21 ------ .../emitExperimentalHttpClientMetrics.kt | 8 --- .../opentelemetry/extractions/knownMethods.kt | 13 ---- .../opentelemetry/setupClientTelemetry.kt | 3 +- opentelemetry/gradle.properties | 8 ++- opentelemetry/server/build.gradle.kts | 2 +- .../extractions/attributeExtractor.kt | 65 ----------------- .../extractions/capturedHeaders.kt | 21 ------ .../opentelemetry/extractions/knownMethods.kt | 13 ---- .../extractions/spanKindExtractor.kt | 15 ---- .../extractions/spanStatusExtractor.kt | 26 ------- .../opentelemetry/setupServerTelemetry.kt | 3 +- opentelemetry/shared/build.gradle.kts | 6 +- 17 files changed, 19 insertions(+), 304 deletions(-) delete mode 100644 opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/attributeExtractor.kt delete mode 100644 opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/capturedHeaders.kt delete mode 100644 opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/emitExperimentalHttpClientMetrics.kt delete mode 100644 opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/knownMethods.kt delete mode 100644 opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/attributeExtractor.kt delete mode 100644 opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/capturedHeaders.kt delete mode 100644 opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/knownMethods.kt delete mode 100644 opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/spanKindExtractor.kt delete mode 100644 opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/spanStatusExtractor.kt diff --git a/opentelemetry/README.md b/opentelemetry/README.md index 70e0e8ad..c0b0bcae 100644 --- a/opentelemetry/README.md +++ b/opentelemetry/README.md @@ -4,48 +4,12 @@ plugins for the Ktor client and server respectively. For the source code, see the [repository on GitHub](https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/ktor). -This project contains extension functions for plugins that allow you to write code in the Ktor DSL style. +This project contains examples of how to use the `KtorClientTracing` and `KtorServerTracing` plugins. -Take the following code as an example: - -```kotlin -install(KtorServerTracing) { - ... - addAttributeExtractor( - object : AttributesExtractor { - override fun onEnd( - attributes: AttributesBuilder, - context: Context, - request: ApplicationRequest, - response: ApplicationResponse?, - error: Throwable? - ) { - attributes.put("end-time", Instant.now().toEpochMilli()) - } - } - ) - ... -} -``` - -Rewritten in Ktor DSL style, it looks like the following: - -```kotlin -install(KtorServerTracing) { - ... - attributeExtractor { - onEnd { - attributes.put("end-time", Instant.now().toEpochMilli()) - } - } - ... -} -``` - -You can find all extensions for the client plugin `KtorClientTracing` in -the [extractions](./client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/) folder. \ -And you can find all extensions for the server plugin `KtorServerTracing` in -the [extractions](./server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/) folder. +You can find examples for the client plugin `KtorClientTracing` in +the [extractions](./client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry) folder. \ +And you can find examples for the server plugin `KtorServerTracing` in +the [extractions](./server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry) folder. ## Running diff --git a/opentelemetry/build.gradle.kts b/opentelemetry/build.gradle.kts index 0b495970..d168e2c3 100644 --- a/opentelemetry/build.gradle.kts +++ b/opentelemetry/build.gradle.kts @@ -1,7 +1,7 @@ description = "OpenTelemetry-Ktor example" plugins { - id("com.avast.gradle.docker-compose") version "0.14.0" + id("com.avast.gradle.docker-compose") version "0.17.1" } subprojects { diff --git a/opentelemetry/client/build.gradle.kts b/opentelemetry/client/build.gradle.kts index 9a50f7ab..7b25b0aa 100644 --- a/opentelemetry/client/build.gradle.kts +++ b/opentelemetry/client/build.gradle.kts @@ -24,5 +24,5 @@ dependencies { implementation("io.ktor:ktor-client-websockets:$ktor_version") implementation("ch.qos.logback:logback-classic:$logback_version") - implementation("io.opentelemetry.instrumentation:opentelemetry-ktor-2.0:$opentelemetry_version-alpha") + implementation("io.opentelemetry.instrumentation:opentelemetry-ktor-3.0:$opentelemetry_version-alpha") } \ No newline at end of file diff --git a/opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/attributeExtractor.kt b/opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/attributeExtractor.kt deleted file mode 100644 index 6c77363f..00000000 --- a/opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/attributeExtractor.kt +++ /dev/null @@ -1,69 +0,0 @@ -package opentelemetry.ktor.example.plugins.opentelemetry.extractions - -import io.ktor.client.request.* -import io.ktor.client.statement.* -import io.opentelemetry.api.common.AttributesBuilder -import io.opentelemetry.context.Context -import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor -import io.opentelemetry.instrumentation.ktor.v2_0.client.KtorClientTracingBuilder - -// addAttributeExtractor -fun KtorClientTracingBuilder.attributeExtractor( - extractorBuilder: ExtractorBuilder.() -> Unit = {} -) { - val builder = ExtractorBuilder().apply(extractorBuilder).build() - addAttributesExtractors( - object : AttributesExtractor { - override fun onStart( - attributes: AttributesBuilder, - parentContext: Context, - request: HttpRequestData - ) { - builder.onStart(OnStartData(attributes, parentContext, request)) - } - - override fun onEnd( - attributes: AttributesBuilder, - context: Context, - request: HttpRequestData, - response: HttpResponse?, - error: Throwable? - ) { - builder.onEnd(OnEndData(attributes, context, request, response, error)) - } - } - ) -} - -class ExtractorBuilder { - private var onStart: OnStartData.() -> Unit = {} - private var onEnd: OnEndData.() -> Unit = {} - - fun onStart(block: OnStartData.() -> Unit) { - onStart = block - } - - fun onEnd(block: OnEndData.() -> Unit) { - onEnd = block - } - - internal fun build(): Extractor { - return Extractor(onStart, onEnd) - } -} - -internal class Extractor(val onStart: OnStartData.() -> Unit, val onEnd: OnEndData.() -> Unit) - -data class OnStartData( - val attributes: AttributesBuilder, - val parentContext: Context, - val request: HttpRequestData -) - -data class OnEndData( - val attributes: AttributesBuilder, - val parentContext: Context, - val request: HttpRequestData, - val response: HttpResponse?, - val error: Throwable? -) \ No newline at end of file diff --git a/opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/capturedHeaders.kt b/opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/capturedHeaders.kt deleted file mode 100644 index de3cdcef..00000000 --- a/opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/capturedHeaders.kt +++ /dev/null @@ -1,21 +0,0 @@ -package opentelemetry.ktor.example.plugins.opentelemetry.extractions - -import io.opentelemetry.instrumentation.ktor.v2_0.client.KtorClientTracingBuilder - -// setCapturedRequestHeaders -fun KtorClientTracingBuilder.capturedRequestHeaders(vararg headers: String) { - capturedRequestHeaders(headers.asIterable()) -} - -fun KtorClientTracingBuilder.capturedRequestHeaders(headers: Iterable) { - setCapturedRequestHeaders(headers.toList()) -} - -// setCapturedResponseHeaders -fun KtorClientTracingBuilder.capturedResponseHeaders(vararg headers: String) { - capturedResponseHeaders(headers.asIterable()) -} - -fun KtorClientTracingBuilder.capturedResponseHeaders(headers: Iterable) { - setCapturedResponseHeaders(headers.toList()) -} \ No newline at end of file diff --git a/opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/emitExperimentalHttpClientMetrics.kt b/opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/emitExperimentalHttpClientMetrics.kt deleted file mode 100644 index 74743153..00000000 --- a/opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/emitExperimentalHttpClientMetrics.kt +++ /dev/null @@ -1,8 +0,0 @@ -package opentelemetry.ktor.example.plugins.opentelemetry.extractions - -import io.opentelemetry.instrumentation.ktor.v2_0.client.KtorClientTracingBuilder - -// setEmitExperimentalHttpClientMetrics -fun KtorClientTracingBuilder.emitExperimentalHttpClientMetrics() { - setEmitExperimentalHttpClientMetrics(true) -} \ No newline at end of file diff --git a/opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/knownMethods.kt b/opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/knownMethods.kt deleted file mode 100644 index 7a86d298..00000000 --- a/opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/knownMethods.kt +++ /dev/null @@ -1,13 +0,0 @@ -package opentelemetry.ktor.example.plugins.opentelemetry.extractions - -import io.ktor.http.* -import io.opentelemetry.instrumentation.ktor.v2_0.client.KtorClientTracingBuilder - -// setKnownMethods -fun KtorClientTracingBuilder.knownMethods(vararg methods: HttpMethod) { - knownMethods(methods.asIterable()) -} - -fun KtorClientTracingBuilder.knownMethods(methods: Iterable) { - setKnownMethods(methods.map { it.value }.toSet()) -} \ No newline at end of file diff --git a/opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/setupClientTelemetry.kt b/opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/setupClientTelemetry.kt index e41c7792..ae3d1b5a 100644 --- a/opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/setupClientTelemetry.kt +++ b/opentelemetry/client/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/setupClientTelemetry.kt @@ -3,11 +3,10 @@ package opentelemetry.ktor.example.plugins.opentelemetry import io.ktor.client.* import io.ktor.client.engine.cio.* import io.ktor.http.* -import io.opentelemetry.instrumentation.ktor.v2_0.client.KtorClientTracing +import io.opentelemetry.instrumentation.ktor.v3_0.client.KtorClientTracing import opentelemetry.ktor.example.CUSTOM_HEADER import opentelemetry.ktor.example.CUSTOM_METHOD import opentelemetry.ktor.example.getOpenTelemetry -import opentelemetry.ktor.example.plugins.opentelemetry.extractions.* /** * Install OpenTelemetry on the client. diff --git a/opentelemetry/gradle.properties b/opentelemetry/gradle.properties index 126fcff8..37d0b2d3 100644 --- a/opentelemetry/gradle.properties +++ b/opentelemetry/gradle.properties @@ -1,7 +1,9 @@ ktor_version=3.0.1 kotlin_version=2.0.20 -logback_version=1.4.11 +logback_version=1.5.12 kotlin.code.style=official -opentelemetry_version=1.32.0 -opentelemetry_semconv_version=1.21.0-alpha \ No newline at end of file +opentelemetry_version=2.10.0 +opentelemetry_semconv_version=1.21.0-alpha +opentelemetry_exporter_otlp_version=1.44.1 +opentelemetry_sdk_extension_autoconfigure_version=1.44.1 \ No newline at end of file diff --git a/opentelemetry/server/build.gradle.kts b/opentelemetry/server/build.gradle.kts index 636bce90..4fba41c4 100644 --- a/opentelemetry/server/build.gradle.kts +++ b/opentelemetry/server/build.gradle.kts @@ -23,5 +23,5 @@ dependencies { implementation("io.ktor:ktor-server-websockets:$ktor_version") implementation("ch.qos.logback:logback-classic:$logback_version") - implementation("io.opentelemetry.instrumentation:opentelemetry-ktor-2.0:$opentelemetry_version-alpha") + implementation("io.opentelemetry.instrumentation:opentelemetry-ktor-3.0:$opentelemetry_version-alpha") } \ No newline at end of file diff --git a/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/attributeExtractor.kt b/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/attributeExtractor.kt deleted file mode 100644 index b17a01f0..00000000 --- a/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/attributeExtractor.kt +++ /dev/null @@ -1,65 +0,0 @@ -package opentelemetry.ktor.example.plugins.opentelemetry.extractions - -import io.ktor.server.request.* -import io.ktor.server.response.* -import io.opentelemetry.api.common.AttributesBuilder -import io.opentelemetry.context.Context -import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor -import io.opentelemetry.instrumentation.ktor.v2_0.server.KtorServerTracing - -// addAttributeExtractor -fun KtorServerTracing.Configuration.attributeExtractor( - extractorBuilder: ExtractorBuilder.() -> Unit = {} -) { - val builder = ExtractorBuilder().apply(extractorBuilder).build() - addAttributeExtractor( - object : AttributesExtractor { - override fun onStart(attributes: AttributesBuilder, parentContext: Context, request: ApplicationRequest) { - builder.onStart(OnStartData(attributes, parentContext, request)) - } - - override fun onEnd( - attributes: AttributesBuilder, - context: Context, - request: ApplicationRequest, - response: ApplicationResponse?, - error: Throwable? - ) { - builder.onEnd(OnEndData(attributes, context, request, response, error)) - } - } - ) -} - -class ExtractorBuilder { - private var onStart: OnStartData.() -> Unit = {} - private var onEnd: OnEndData.() -> Unit = {} - - fun onStart(block: OnStartData.() -> Unit) { - onStart = block - } - - fun onEnd(block: OnEndData.() -> Unit) { - onEnd = block - } - - internal fun build(): Extractor { - return Extractor(onStart, onEnd) - } -} - -internal class Extractor(val onStart: OnStartData.() -> Unit, val onEnd: OnEndData.() -> Unit) - -data class OnStartData( - val attributes: AttributesBuilder, - val parentContext: Context, - val request: ApplicationRequest -) - -data class OnEndData( - val attributes: AttributesBuilder, - val parentContext: Context, - val request: ApplicationRequest, - val response: ApplicationResponse?, - val error: Throwable? -) \ No newline at end of file diff --git a/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/capturedHeaders.kt b/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/capturedHeaders.kt deleted file mode 100644 index 523b0d61..00000000 --- a/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/capturedHeaders.kt +++ /dev/null @@ -1,21 +0,0 @@ -package opentelemetry.ktor.example.plugins.opentelemetry.extractions - -import io.opentelemetry.instrumentation.ktor.v2_0.server.KtorServerTracing - -// setCapturedRequestHeaders -fun KtorServerTracing.Configuration.capturedRequestHeaders(vararg headers: String) { - capturedRequestHeaders(headers.asIterable()) -} - -fun KtorServerTracing.Configuration.capturedRequestHeaders(headers: Iterable) { - setCapturedRequestHeaders(headers.toList()) -} - -// setCapturedResponseHeaders -fun KtorServerTracing.Configuration.capturedResponseHeaders(vararg headers: String) { - capturedResponseHeaders(headers.asIterable()) -} - -fun KtorServerTracing.Configuration.capturedResponseHeaders(headers: Iterable) { - setCapturedResponseHeaders(headers.toList()) -} \ No newline at end of file diff --git a/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/knownMethods.kt b/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/knownMethods.kt deleted file mode 100644 index ebae1183..00000000 --- a/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/knownMethods.kt +++ /dev/null @@ -1,13 +0,0 @@ -package opentelemetry.ktor.example.plugins.opentelemetry.extractions - -import io.ktor.http.* -import io.opentelemetry.instrumentation.ktor.v2_0.server.KtorServerTracing - -// setKnownMethods -fun KtorServerTracing.Configuration.knownMethods(vararg methods: HttpMethod) { - knownMethods(methods.asIterable()) -} - -fun KtorServerTracing.Configuration.knownMethods(methods: Iterable) { - setKnownMethods(methods.map { it.value }.toSet()) -} \ No newline at end of file diff --git a/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/spanKindExtractor.kt b/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/spanKindExtractor.kt deleted file mode 100644 index 6c5fe63f..00000000 --- a/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/spanKindExtractor.kt +++ /dev/null @@ -1,15 +0,0 @@ -package opentelemetry.ktor.example.plugins.opentelemetry.extractions - -import io.ktor.server.request.* -import io.opentelemetry.api.trace.SpanKind -import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor -import io.opentelemetry.instrumentation.ktor.v2_0.server.KtorServerTracing - -// setSpanKindExtractor -fun KtorServerTracing.Configuration.spanKindExtractor(extract: ApplicationRequest.() -> SpanKind) { - setSpanKindExtractor { - SpanKindExtractor { request: ApplicationRequest -> - extract(request) - } - } -} \ No newline at end of file diff --git a/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/spanStatusExtractor.kt b/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/spanStatusExtractor.kt deleted file mode 100644 index 92a49001..00000000 --- a/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/extractions/spanStatusExtractor.kt +++ /dev/null @@ -1,26 +0,0 @@ -package opentelemetry.ktor.example.plugins.opentelemetry.extractions - -import io.ktor.server.request.* -import io.ktor.server.response.* -import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusBuilder -import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusExtractor -import io.opentelemetry.instrumentation.ktor.v2_0.server.KtorServerTracing - -// setStatusExtractor -fun KtorServerTracing.Configuration.spanStatusExtractor(extract: SpanStatusData.() -> Unit) { - setStatusExtractor { - SpanStatusExtractor { spanStatusBuilder: SpanStatusBuilder, - request: ApplicationRequest, - response: ApplicationResponse?, - throwable: Throwable? -> - extract(SpanStatusData(spanStatusBuilder, request, response, throwable)) - } - } -} - -data class SpanStatusData( - val spanStatusBuilder: SpanStatusBuilder, - val request: ApplicationRequest, - val response: ApplicationResponse?, - val error: Throwable? -) \ No newline at end of file diff --git a/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/setupServerTelemetry.kt b/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/setupServerTelemetry.kt index b46d5b7d..cf962476 100644 --- a/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/setupServerTelemetry.kt +++ b/opentelemetry/server/src/main/kotlin/opentelemetry/ktor/example/plugins/opentelemetry/setupServerTelemetry.kt @@ -6,11 +6,10 @@ import io.ktor.server.request.* import io.opentelemetry.api.OpenTelemetry import io.opentelemetry.api.trace.SpanKind import io.opentelemetry.api.trace.StatusCode -import io.opentelemetry.instrumentation.ktor.v2_0.server.KtorServerTracing +import io.opentelemetry.instrumentation.ktor.v3_0.server.KtorServerTracing import opentelemetry.ktor.example.CUSTOM_HEADER import opentelemetry.ktor.example.CUSTOM_METHOD import opentelemetry.ktor.example.getOpenTelemetry -import opentelemetry.ktor.example.plugins.opentelemetry.extractions.* import java.time.Instant const val serviceName = "opentelemetry-ktor-sample-server" diff --git a/opentelemetry/shared/build.gradle.kts b/opentelemetry/shared/build.gradle.kts index eda3157c..507dd765 100644 --- a/opentelemetry/shared/build.gradle.kts +++ b/opentelemetry/shared/build.gradle.kts @@ -1,5 +1,7 @@ val opentelemetry_version: String by project val opentelemetry_semconv_version: String by project +val opentelemetry_exporter_otlp_version: String by project +val opentelemetry_sdk_extension_autoconfigure_version: String by project plugins { kotlin("jvm") version "2.0.20" @@ -7,8 +9,8 @@ plugins { } dependencies { - implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure:$opentelemetry_version"); - implementation("io.opentelemetry:opentelemetry-exporter-otlp:$opentelemetry_version"); + implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure:$opentelemetry_sdk_extension_autoconfigure_version"); + implementation("io.opentelemetry:opentelemetry-exporter-otlp:$opentelemetry_exporter_otlp_version"); implementation("io.opentelemetry.semconv:opentelemetry-semconv:$opentelemetry_semconv_version") implementation("io.ktor:ktor-server-core-jvm")