@@ -17,35 +17,26 @@ import io.opentelemetry.api.common.AttributesBuilder
1717import io.opentelemetry.api.trace.SpanKind
1818import io.opentelemetry.context.Context
1919import io.opentelemetry.extension.kotlin.asContextElement
20+ import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpServerTelemetryBuilder
2021import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor
2122import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter
2223import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor
2324import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusBuilder
2425import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusExtractor
2526import io.opentelemetry.instrumentation.api.internal.InstrumenterUtil
26- import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesExtractor
27- import io.opentelemetry.instrumentation.api.semconv.http.HttpServerMetrics
2827import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute
2928import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRouteSource
30- import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanNameExtractor
3129import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanStatusExtractor
3230import io.opentelemetry.instrumentation.ktor.v2_0.InstrumentationProperties.INSTRUMENTATION_NAME
3331import kotlinx.coroutines.withContext
32+ import java.util.*
3433
3534class KtorServerTracing private constructor(
3635 private val instrumenter : Instrumenter <ApplicationRequest , ApplicationResponse >,
3736) {
3837
3938 class Configuration {
40- internal lateinit var openTelemetry: OpenTelemetry
41-
42- internal val additionalExtractors = mutableListOf<AttributesExtractor <in ApplicationRequest , in ApplicationResponse >>()
43-
44- internal val httpAttributesExtractorBuilder = HttpServerAttributesExtractor .builder(KtorHttpServerAttributesGetter .INSTANCE )
45-
46- internal val httpSpanNameExtractorBuilder = HttpSpanNameExtractor .builder(KtorHttpServerAttributesGetter .INSTANCE )
47-
48- internal val httpServerRouteBuilder = HttpServerRoute .builder(KtorHttpServerAttributesGetter .INSTANCE )
39+ internal lateinit var serverBuilder: DefaultHttpServerTelemetryBuilder <ApplicationRequest , ApplicationResponse >
4940
5041 internal var statusExtractor:
5142 (SpanStatusExtractor <ApplicationRequest , ApplicationResponse >) -> SpanStatusExtractor <in ApplicationRequest , in ApplicationResponse > = { a -> a }
@@ -54,7 +45,12 @@ class KtorServerTracing private constructor(
5445 (SpanKindExtractor <ApplicationRequest >) -> SpanKindExtractor <ApplicationRequest > = { a -> a }
5546
5647 fun setOpenTelemetry (openTelemetry : OpenTelemetry ) {
57- this .openTelemetry = openTelemetry
48+ this .serverBuilder = DefaultHttpServerTelemetryBuilder (
49+ INSTRUMENTATION_NAME ,
50+ openTelemetry,
51+ KtorHttpServerAttributesGetter .INSTANCE ,
52+ Optional .empty()
53+ )
5854 }
5955
6056 @Deprecated(" Please use method `spanStatusExtractor`" )
@@ -113,7 +109,7 @@ class KtorServerTracing private constructor(
113109
114110 fun attributeExtractor (extractorBuilder : ExtractorBuilder .() -> Unit = {}) {
115111 val builder = ExtractorBuilder ().apply (extractorBuilder).build()
116- additionalExtractors.add (
112+ serverBuilder.addAttributesExtractor (
117113 object : AttributesExtractor <ApplicationRequest , ApplicationResponse > {
118114 override fun onStart (attributes : AttributesBuilder , parentContext : Context , request : ApplicationRequest ) {
119115 builder.onStart(OnStartData (attributes, parentContext, request))
@@ -168,7 +164,7 @@ class KtorServerTracing private constructor(
168164 fun capturedRequestHeaders (vararg headers : String ) = capturedRequestHeaders(headers.asIterable())
169165
170166 fun capturedRequestHeaders (headers : Iterable <String >) {
171- httpAttributesExtractorBuilder .setCapturedRequestHeaders(headers.toList())
167+ serverBuilder .setCapturedRequestHeaders(headers.toList())
172168 }
173169
174170 @Deprecated(
@@ -180,7 +176,7 @@ class KtorServerTracing private constructor(
180176 fun capturedResponseHeaders (vararg headers : String ) = capturedResponseHeaders(headers.asIterable())
181177
182178 fun capturedResponseHeaders (headers : Iterable <String >) {
183- httpAttributesExtractorBuilder .setCapturedResponseHeaders(headers.toList())
179+ serverBuilder .setCapturedResponseHeaders(headers.toList())
184180 }
185181
186182 @Deprecated(
@@ -198,13 +194,11 @@ class KtorServerTracing private constructor(
198194
199195 fun knownMethods (methods : Iterable <String >) {
200196 methods.toSet().apply {
201- httpAttributesExtractorBuilder.setKnownMethods(this )
202- httpSpanNameExtractorBuilder.setKnownMethods(this )
203- httpServerRouteBuilder.setKnownMethods(this )
197+ serverBuilder.setKnownMethods(this )
204198 }
205199 }
206200
207- internal fun isOpenTelemetryInitialized (): Boolean = this ::openTelemetry .isInitialized
201+ internal fun isOpenTelemetryInitialized (): Boolean = this ::serverBuilder .isInitialized
208202 }
209203
210204 private fun start (call : ApplicationCall ): Context ? {
@@ -234,19 +228,10 @@ class KtorServerTracing private constructor(
234228
235229 val httpAttributesGetter = KtorHttpServerAttributesGetter .INSTANCE
236230
237- val instrumenterBuilder = Instrumenter .builder<ApplicationRequest , ApplicationResponse >(
238- configuration.openTelemetry,
239- INSTRUMENTATION_NAME ,
240- configuration.httpSpanNameExtractorBuilder.build()
241- )
242-
243- configuration.additionalExtractors.forEach { instrumenterBuilder.addAttributesExtractor(it) }
231+ val instrumenterBuilder = configuration.serverBuilder.instrumenterBuilder { }
244232
245233 with (instrumenterBuilder) {
246234 setSpanStatusExtractor(configuration.statusExtractor(HttpSpanStatusExtractor .create(httpAttributesGetter)))
247- addAttributesExtractor(configuration.httpAttributesExtractorBuilder.build())
248- addOperationMetrics(HttpServerMetrics .get())
249- addContextCustomizer(configuration.httpServerRouteBuilder.build())
250235 }
251236
252237 val instrumenter = InstrumenterUtil .buildUpstreamInstrumenter(
0 commit comments