-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2073 from DataDog/mconstantin/rum-3438/support-pa…
…rent-otel-span-in-interceptor RUM-3438 Add the OkHttp Otel extensions module
- Loading branch information
Showing
27 changed files
with
534 additions
and
8 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Built application files | ||
*.apk | ||
*.ap_ | ||
*.aab | ||
|
||
# Files for the ART/Dalvik VM | ||
*.dex | ||
|
||
# Java class files | ||
*.class | ||
|
||
# Generated files | ||
bin/ | ||
gen/ | ||
out/ | ||
|
||
# Gradle files | ||
build/ |
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,7 @@ | ||
# Datadog SDK OpenTelemetry extensions for OkHttp | ||
|
||
See the dedicated [Datadog SDK instrumentation for OkHttp documentation][1] to learn how track network requests made by the `OkHttp` library automatically. | ||
See the dedicated [Datadog SDK Android for OpenTelemetry documentation][2] to learn how to add a parent span to network requests made by the `OkHttp` library. | ||
|
||
[1]: https://docs.datadoghq.com/real_user_monitoring/android/advanced_configuration/#automatically-track-network-requests | ||
[2]: https://docs.datadoghq.com/tracing/trace_collection/custom_instrumentation/android?tab=kotlin |
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 @@ | ||
fun okhttp3.Request.Builder.addParentSpan(io.opentelemetry.api.trace.Span): okhttp3.Request.Builder |
4 changes: 4 additions & 0 deletions
4
integrations/dd-sdk-android-okhttp-otel/api/dd-sdk-android-okhttp-otel.api
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,4 @@ | ||
public final class com/datadog/android/okhttp/otel/OkHttpExtKt { | ||
public static final fun addParentSpan (Lokhttp3/Request$Builder;Lio/opentelemetry/api/trace/Span;)Lokhttp3/Request$Builder; | ||
} | ||
|
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,68 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
import com.datadog.gradle.config.androidLibraryConfig | ||
import com.datadog.gradle.config.dependencyUpdateConfig | ||
import com.datadog.gradle.config.javadocConfig | ||
import com.datadog.gradle.config.junitConfig | ||
import com.datadog.gradle.config.kotlinConfig | ||
import com.datadog.gradle.config.publishingConfig | ||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget | ||
|
||
plugins { | ||
// Build | ||
id("com.android.library") | ||
kotlin("android") | ||
|
||
// Publish | ||
`maven-publish` | ||
signing | ||
id("org.jetbrains.dokka") | ||
|
||
// Analysis tools | ||
id("com.github.ben-manes.versions") | ||
|
||
// Tests | ||
id("org.jetbrains.kotlinx.kover") | ||
|
||
// Internal Generation | ||
id("thirdPartyLicences") | ||
id("apiSurface") | ||
id("transitiveDependencies") | ||
id("binary-compatibility-validator") | ||
} | ||
|
||
android { | ||
namespace = "com.datadog.android.okhttp.otel" | ||
} | ||
|
||
dependencies { | ||
implementation(project(":integrations:dd-sdk-android-okhttp")) | ||
implementation(project(":features:dd-sdk-android-trace-otel")) | ||
implementation(libs.okHttp) | ||
implementation(libs.kotlin) | ||
|
||
testImplementation(project(":tools:unit")) { | ||
attributes { | ||
attribute( | ||
com.android.build.api.attributes.ProductFlavorAttr.of("platform"), | ||
objects.named("jvm") | ||
) | ||
} | ||
} | ||
testImplementation(libs.bundles.jUnit5) | ||
testImplementation(libs.bundles.testTools) | ||
testImplementation(libs.okHttpMock) | ||
} | ||
|
||
kotlinConfig(jvmBytecodeTarget = JvmTarget.JVM_11) | ||
androidLibraryConfig() | ||
junitConfig() | ||
javadocConfig() | ||
dependencyUpdateConfig() | ||
publishingConfig( | ||
"An OkHttp collection of extensions to be used in conjunction with OpenTelemetry Datadog SDK." | ||
) |
25 changes: 25 additions & 0 deletions
25
...s/dd-sdk-android-okhttp-otel/src/main/kotlin/com/datadog/android/okhttp/otel/OkHttpExt.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,25 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.android.okhttp.otel | ||
|
||
import com.datadog.android.okhttp.TraceContext | ||
import com.datadog.trace.api.sampling.PrioritySampling | ||
import io.opentelemetry.api.trace.Span | ||
import okhttp3.Request | ||
|
||
/** | ||
* Add the current span context as the parent span for the distributed trace created around the Request. | ||
* @param span the parent span to add to the request. | ||
* @return the modified Request.Builder instance | ||
*/ | ||
fun Request.Builder.addParentSpan(span: Span): Request.Builder { | ||
val context = span.spanContext | ||
val prioritySampling = if (context.isSampled) PrioritySampling.USER_KEEP.toInt() else PrioritySampling.UNSET.toInt() | ||
@Suppress("UnsafeThirdPartyFunctionCall") // the context will always be a TraceContext | ||
tag(TraceContext::class.java, TraceContext(context.traceId, context.spanId, prioritySampling)) | ||
return this | ||
} |
80 changes: 80 additions & 0 deletions
80
...-sdk-android-okhttp-otel/src/test/kotlin/com/datadog/android/okhttp/otel/OkHttpExtTest.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,80 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.android.okhttp.otel | ||
|
||
import com.datadog.android.okhttp.TraceContext | ||
import com.datadog.tools.unit.forge.BaseConfigurator | ||
import com.datadog.trace.api.sampling.PrioritySampling | ||
import fr.xgouchet.elmyr.annotation.BoolForgery | ||
import fr.xgouchet.elmyr.annotation.StringForgery | ||
import fr.xgouchet.elmyr.junit5.ForgeConfiguration | ||
import fr.xgouchet.elmyr.junit5.ForgeExtension | ||
import io.opentelemetry.api.trace.Span | ||
import io.opentelemetry.api.trace.SpanContext | ||
import okhttp3.Request | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.junit.jupiter.api.extension.Extensions | ||
import org.mockito.Mock | ||
import org.mockito.junit.jupiter.MockitoExtension | ||
import org.mockito.junit.jupiter.MockitoSettings | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
import org.mockito.quality.Strictness | ||
|
||
@Extensions( | ||
ExtendWith(MockitoExtension::class), | ||
ExtendWith(ForgeExtension::class) | ||
) | ||
@MockitoSettings(strictness = Strictness.LENIENT) | ||
@ForgeConfiguration(BaseConfigurator::class) | ||
internal class OkHttpExtTest { | ||
|
||
@StringForgery(regex = "[a-d0-9]{16}") | ||
internal lateinit var fakeSpanId: String | ||
|
||
@StringForgery(regex = "[a-d0-9]{32}") | ||
internal lateinit var fakeTraceId: String | ||
|
||
@BoolForgery | ||
internal var fakeIsSampled: Boolean = false | ||
|
||
@StringForgery(regex = "http://[a-z0-9_]{8}\\.[a-z]{3}/") | ||
internal lateinit var fakeUrl: String | ||
|
||
@Mock | ||
internal lateinit var mockSpan: Span | ||
|
||
private var expectedPrioritySampling: Int = 0 | ||
|
||
@BeforeEach | ||
fun `set up`() { | ||
val spanContext: SpanContext = mock { | ||
on { spanId }.thenReturn(fakeSpanId) | ||
on { traceId }.thenReturn(fakeTraceId) | ||
on { isSampled }.thenReturn(fakeIsSampled) | ||
} | ||
expectedPrioritySampling = | ||
if (fakeIsSampled) PrioritySampling.USER_KEEP.toInt() else PrioritySampling.UNSET.toInt() | ||
whenever(mockSpan.spanContext).thenReturn(spanContext) | ||
} | ||
|
||
@Test | ||
fun `M set the parentSpan through the Request builder W addParentSpan`() { | ||
// When | ||
val request = Request.Builder().url(fakeUrl).addParentSpan(mockSpan).build() | ||
|
||
// Then | ||
val taggedContext = request.tag(TraceContext::class.java) | ||
checkNotNull(taggedContext) | ||
assertThat(taggedContext.spanId).isEqualTo(fakeSpanId) | ||
assertThat(taggedContext.traceId).isEqualTo(fakeTraceId) | ||
assertThat(taggedContext.samplingPriority).isEqualTo(expectedPrioritySampling) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...k-android-okhttp-otel/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker
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 @@ | ||
mock-maker-inline |
14 changes: 14 additions & 0 deletions
14
integrations/dd-sdk-android-okhttp-otel/transitiveDependencies
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,14 @@ | ||
Dependencies List | ||
|
||
com.squareup.okhttp3:okhttp:4.11.0 : 768 Kb | ||
com.squareup.okio:okio-jvm:3.2.0 : 337 Kb | ||
io.opentelemetry:opentelemetry-api:1.4.0 : 78 Kb | ||
io.opentelemetry:opentelemetry-context:1.4.0 : 42 Kb | ||
org.jetbrains.kotlin:kotlin-stdlib-common:1.8.22 : 216 Kb | ||
org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.22 : 963 b | ||
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.22 : 969 b | ||
org.jetbrains.kotlin:kotlin-stdlib:1.8.22 : 1631 Kb | ||
org.jetbrains:annotations:13.0 : 17 Kb | ||
|
||
Total transitive dependencies size : 3 Mb | ||
|
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
28 changes: 28 additions & 0 deletions
28
...grations/dd-sdk-android-okhttp/src/main/kotlin/com/datadog/android/okhttp/TraceContext.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,28 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.android.okhttp | ||
|
||
import com.datadog.android.lint.InternalApi | ||
|
||
/** | ||
* The context of a trace to be propagated through the OkHttp requests for Datadog tracing. | ||
*/ | ||
@InternalApi | ||
data class TraceContext( | ||
/** | ||
* The trace id. | ||
*/ | ||
val traceId: String, | ||
/** | ||
* The span id. | ||
*/ | ||
val spanId: String, | ||
/** | ||
* The sampling priority. | ||
*/ | ||
val samplingPriority: Int | ||
) |
40 changes: 40 additions & 0 deletions
40
...ndroid-okhttp/src/main/kotlin/com/datadog/android/okhttp/internal/otel/TraceContextExt.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,40 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.android.okhttp.internal.otel | ||
|
||
import com.datadog.android.okhttp.TraceContext | ||
import com.datadog.opentracing.propagation.ExtractedContext | ||
import io.opentracing.SpanContext | ||
import java.math.BigInteger | ||
|
||
private const val BASE_16_RADIX = 16 | ||
|
||
internal fun TraceContext.toOpenTracingContext(): SpanContext { | ||
val traceIdAsBigInteger = parseToBigInteger(traceId) | ||
val spanIdAsBigInteger = parseToBigInteger(spanId) | ||
return ExtractedContext( | ||
traceIdAsBigInteger, | ||
spanIdAsBigInteger, | ||
samplingPriority, | ||
null, | ||
emptyMap(), | ||
emptyMap() | ||
) | ||
} | ||
|
||
@Suppress("SwallowedException") | ||
private fun parseToBigInteger(value: String): BigInteger { | ||
// just in case but theoretically it should never happen as we are controlling the way the ID is | ||
// generated in the CoreTracer. | ||
return try { | ||
BigInteger(value, BASE_16_RADIX) | ||
} catch (e: NumberFormatException) { | ||
BigInteger.ZERO | ||
} catch (e: ArithmeticException) { | ||
BigInteger.ZERO | ||
} | ||
} |
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
Oops, something went wrong.