-
Notifications
You must be signed in to change notification settings - Fork 878
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
Rename ktor tracing to ktor telemetry #12855
Merged
Merged
Changes from 26 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
04d72e2
Rename ktor tracing to ktor telemetry
trask 77012ed
Fix javadoc todo
trask 2c08222
Move under v2_0.common
trask ab1bf85
fix
trask 959499e
-fix
trask 3dc49f8
spotless
trask 4e8bdb3
rename-var
trask 9397547
Remove deprecated from the new classes
trask 12a37b7
remove reflection
trask 2b11a5a
volatile
trask 3f54de5
spotless
trask b38566d
Merge remote-tracking branch 'upstream/main' into rename-ktor-tracing
trask 26ddaed
use kotlin internal
trask cb5971d
fix
trask b28385f
Combine client and server package
trask 24671dd
Combine client and server package
trask a4ebcac
update docs
trask c909356
spotless
trask 36f3d1e
cleanup
trask 3863848
Test new code
trask 112eb3d
more moving
trask 76494b8
spotless
trask 7f04a8b
Merge remote-tracking branch 'upstream/main' into rename-ktor-tracing
trask 8e5ff43
Merge remote-tracking branch 'upstream/main' into rename-ktor-tracing
trask 2394b8f
Merge remote-tracking branch 'upstream/main' into rename-ktor-tracing
trask 07a380b
Remove unused, and add experimental server telemetry
trask 6260a86
test deprecated code
trask a01fa94
Merge remote-tracking branch 'upstream/main' into rename-ktor-tracing
trask fc032cb
simpler
trask 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
This file contains 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
169 changes: 169 additions & 0 deletions
169
...library/src/main/kotlin/io/opentelemetry/instrumentation/ktor/v1_0/KtorServerTelemetry.kt
This file contains 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,169 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.ktor.v1_0 | ||
|
||
import io.ktor.application.* | ||
import io.ktor.request.* | ||
import io.ktor.response.* | ||
import io.ktor.routing.* | ||
import io.ktor.util.* | ||
import io.ktor.util.pipeline.* | ||
import io.opentelemetry.api.OpenTelemetry | ||
import io.opentelemetry.context.Context | ||
import io.opentelemetry.extension.kotlin.asContextElement | ||
import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpServerInstrumenterBuilder | ||
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter | ||
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor | ||
import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusBuilder | ||
import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusExtractor | ||
import io.opentelemetry.instrumentation.api.internal.InstrumenterUtil | ||
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute | ||
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRouteSource | ||
import kotlinx.coroutines.withContext | ||
|
||
class KtorServerTelemetry private constructor( | ||
private val instrumenter: Instrumenter<ApplicationRequest, ApplicationResponse>, | ||
) { | ||
|
||
class Configuration { | ||
internal lateinit var builder: DefaultHttpServerInstrumenterBuilder<ApplicationRequest, ApplicationResponse> | ||
|
||
internal var spanKindExtractor: | ||
(SpanKindExtractor<ApplicationRequest>) -> SpanKindExtractor<ApplicationRequest> = { a -> a } | ||
|
||
fun setOpenTelemetry(openTelemetry: OpenTelemetry) { | ||
this.builder = | ||
DefaultHttpServerInstrumenterBuilder.create( | ||
INSTRUMENTATION_NAME, | ||
openTelemetry, | ||
KtorHttpServerAttributesGetter.INSTANCE | ||
) | ||
} | ||
|
||
fun setStatusExtractor( | ||
extractor: (SpanStatusExtractor<in ApplicationRequest, in ApplicationResponse>) -> SpanStatusExtractor<in ApplicationRequest, in ApplicationResponse> | ||
) { | ||
builder.setStatusExtractor { prevExtractor -> | ||
SpanStatusExtractor { spanStatusBuilder: SpanStatusBuilder, | ||
request: ApplicationRequest, | ||
response: ApplicationResponse?, | ||
throwable: Throwable? -> | ||
extractor(prevExtractor).extract(spanStatusBuilder, request, response, throwable) | ||
} | ||
} | ||
} | ||
|
||
fun setSpanKindExtractor(extractor: (SpanKindExtractor<ApplicationRequest>) -> SpanKindExtractor<ApplicationRequest>) { | ||
this.spanKindExtractor = extractor | ||
} | ||
|
||
fun addAttributesExtractor(extractor: AttributesExtractor<in ApplicationRequest, in ApplicationResponse>) { | ||
builder.addAttributesExtractor(extractor) | ||
} | ||
|
||
fun setCapturedRequestHeaders(requestHeaders: List<String>) { | ||
builder.setCapturedRequestHeaders(requestHeaders) | ||
} | ||
|
||
fun setCapturedResponseHeaders(responseHeaders: List<String>) { | ||
builder.setCapturedResponseHeaders(responseHeaders) | ||
} | ||
|
||
fun setKnownMethods(knownMethods: Set<String>) { | ||
builder.setKnownMethods(knownMethods) | ||
} | ||
|
||
internal fun isOpenTelemetryInitialized(): Boolean = this::builder.isInitialized | ||
} | ||
|
||
private fun start(call: ApplicationCall): Context? { | ||
val parentContext = Context.current() | ||
if (!instrumenter.shouldStart(parentContext, call.request)) { | ||
return null | ||
} | ||
|
||
return instrumenter.start(parentContext, call.request) | ||
} | ||
|
||
private fun end(context: Context, call: ApplicationCall, error: Throwable?) { | ||
instrumenter.end(context, call.request, call.response, error) | ||
} | ||
|
||
companion object Feature : ApplicationFeature<Application, Configuration, KtorServerTelemetry> { | ||
private const val INSTRUMENTATION_NAME = "io.opentelemetry.ktor-1.0" | ||
|
||
private val contextKey = AttributeKey<Context>("OpenTelemetry") | ||
private val errorKey = AttributeKey<Throwable>("OpenTelemetryException") | ||
|
||
override val key: AttributeKey<KtorServerTelemetry> = AttributeKey("OpenTelemetry") | ||
|
||
override fun install(pipeline: Application, configure: Configuration.() -> Unit): KtorServerTelemetry { | ||
val configuration = Configuration().apply(configure) | ||
|
||
if (!configuration.isOpenTelemetryInitialized()) { | ||
throw IllegalArgumentException("OpenTelemetry must be set") | ||
} | ||
|
||
val instrumenter = InstrumenterUtil.buildUpstreamInstrumenter( | ||
configuration.builder.instrumenterBuilder(), | ||
ApplicationRequestGetter, | ||
configuration.spanKindExtractor(SpanKindExtractor.alwaysServer()) | ||
) | ||
|
||
val feature = KtorServerTelemetry(instrumenter) | ||
|
||
val startPhase = PipelinePhase("OpenTelemetry") | ||
pipeline.insertPhaseBefore(ApplicationCallPipeline.Monitoring, startPhase) | ||
pipeline.intercept(startPhase) { | ||
val context = feature.start(call) | ||
|
||
if (context != null) { | ||
call.attributes.put(contextKey, context) | ||
withContext(context.asContextElement()) { | ||
try { | ||
proceed() | ||
} catch (err: Throwable) { | ||
// Stash error for reporting later since need ktor to finish setting up the response | ||
call.attributes.put(errorKey, err) | ||
throw err | ||
} | ||
} | ||
} else { | ||
proceed() | ||
} | ||
} | ||
|
||
val postSendPhase = PipelinePhase("OpenTelemetryPostSend") | ||
pipeline.sendPipeline.insertPhaseAfter(ApplicationSendPipeline.After, postSendPhase) | ||
pipeline.sendPipeline.intercept(postSendPhase) { | ||
val context = call.attributes.getOrNull(contextKey) | ||
if (context != null) { | ||
var error: Throwable? = call.attributes.getOrNull(errorKey) | ||
try { | ||
proceed() | ||
} catch (t: Throwable) { | ||
error = t | ||
throw t | ||
} finally { | ||
feature.end(context, call, error) | ||
} | ||
} else { | ||
proceed() | ||
} | ||
} | ||
|
||
pipeline.environment.monitor.subscribe(Routing.RoutingCallStarted) { call -> | ||
val context = call.attributes.getOrNull(contextKey) | ||
if (context != null) { | ||
HttpServerRoute.update(context, HttpServerRouteSource.SERVER, { _, arg -> arg.route.parent.toString() }, call) | ||
} | ||
} | ||
|
||
return feature | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
42 changes: 42 additions & 0 deletions
42
...n/kotlin/io/opentelemetry/instrumentation/ktor/v2_0/common/AbstractKtorClientTelemetry.kt
This file contains 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,42 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.ktor.v2_0.common | ||
|
||
import io.ktor.client.call.* | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import io.opentelemetry.context.Context | ||
import io.opentelemetry.context.propagation.ContextPropagators | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter | ||
|
||
abstract class AbstractKtorClientTelemetry( | ||
private val instrumenter: Instrumenter<HttpRequestData, HttpResponse>, | ||
private val propagators: ContextPropagators, | ||
) { | ||
|
||
internal fun createSpan(requestBuilder: HttpRequestBuilder): Context? { | ||
val parentContext = Context.current() | ||
val requestData = requestBuilder.build() | ||
|
||
return if (instrumenter.shouldStart(parentContext, requestData)) { | ||
instrumenter.start(parentContext, requestData) | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
internal fun populateRequestHeaders(requestBuilder: HttpRequestBuilder, context: Context) { | ||
propagators.textMapPropagator.inject(context, requestBuilder, KtorHttpHeadersSetter) | ||
} | ||
|
||
internal fun endSpan(context: Context, call: HttpClientCall, error: Throwable?) { | ||
endSpan(context, HttpRequestBuilder().takeFrom(call.request), call.response, error) | ||
} | ||
|
||
internal fun endSpan(context: Context, requestBuilder: HttpRequestBuilder, response: HttpResponse?, error: Throwable?) { | ||
instrumenter.end(context, requestBuilder.build(), response, error) | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
...n/io/opentelemetry/instrumentation/ktor/v2_0/common/AbstractKtorClientTelemetryBuilder.kt
This file contains 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,119 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.ktor.v2_0.common | ||
|
||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import io.ktor.http.* | ||
import io.opentelemetry.api.OpenTelemetry | ||
import io.opentelemetry.api.common.AttributesBuilder | ||
import io.opentelemetry.context.Context | ||
import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpClientInstrumenterBuilder | ||
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor | ||
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor | ||
import io.opentelemetry.instrumentation.ktor.v2_0.common.internal.KtorBuilderUtil | ||
import java.util.function.Function | ||
|
||
abstract class AbstractKtorClientTelemetryBuilder( | ||
private val instrumentationName: String | ||
) { | ||
companion object { | ||
init { | ||
KtorBuilderUtil.clientBuilderExtractor = { it.builder } | ||
} | ||
} | ||
|
||
internal lateinit var openTelemetry: OpenTelemetry | ||
internal lateinit var internalBuilder: DefaultHttpClientInstrumenterBuilder<HttpRequestData, HttpResponse> | ||
protected lateinit var builder: DefaultHttpClientInstrumenterBuilder<HttpRequestData, HttpResponse> | ||
|
||
fun setOpenTelemetry(openTelemetry: OpenTelemetry) { | ||
this.openTelemetry = openTelemetry | ||
this.internalBuilder = DefaultHttpClientInstrumenterBuilder.create( | ||
instrumentationName, | ||
openTelemetry, | ||
KtorHttpClientAttributesGetter | ||
) | ||
this.builder = internalBuilder | ||
} | ||
|
||
protected fun getOpenTelemetry(): OpenTelemetry { | ||
return openTelemetry | ||
} | ||
|
||
fun capturedRequestHeaders(vararg headers: String) = capturedRequestHeaders(headers.asIterable()) | ||
|
||
fun capturedRequestHeaders(headers: Iterable<String>) { | ||
builder.setCapturedRequestHeaders(headers.toList()) | ||
} | ||
|
||
fun capturedResponseHeaders(vararg headers: String) = capturedResponseHeaders(headers.asIterable()) | ||
|
||
fun capturedResponseHeaders(headers: Iterable<String>) { | ||
builder.setCapturedResponseHeaders(headers.toList()) | ||
} | ||
|
||
fun knownMethods(vararg methods: String) = knownMethods(methods.asIterable()) | ||
|
||
fun knownMethods(vararg methods: HttpMethod) = knownMethods(methods.asIterable()) | ||
|
||
@JvmName("knownMethodsJvm") | ||
fun knownMethods(methods: Iterable<HttpMethod>) = knownMethods(methods.map { it.value }) | ||
|
||
fun knownMethods(methods: Iterable<String>) { | ||
builder.setKnownMethods(methods.toSet()) | ||
} | ||
|
||
fun attributesExtractor(extractorBuilder: ExtractorBuilder.() -> Unit = {}) { | ||
val builder = ExtractorBuilder().apply(extractorBuilder).build() | ||
this.builder.addAttributesExtractor(object : AttributesExtractor<HttpRequestData, HttpResponse> { | ||
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)) | ||
} | ||
}) | ||
} | ||
|
||
fun spanNameExtractor(spanNameExtractorTransformer: Function<SpanNameExtractor<in HttpRequestData>, out SpanNameExtractor<in HttpRequestData>>) { | ||
builder.setSpanNameExtractor(spanNameExtractorTransformer) | ||
} | ||
|
||
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? | ||
) | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't figure out how to have both internal and protected access
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
internal
should be enough -- is there a reason why it shouldn't be public? It can only be used by other classes in this module when it'sinternal
. 👍🏻There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found a better way than duplicating the field, I created an
internal
accessor to theprotected
field