Skip to content

Commit a8bb138

Browse files
committed
RUM-11854: Post-review fixes - supporting sync get in TracingInterceptor for DatadogContext
1 parent 3b18e73 commit a8bb138

File tree

28 files changed

+824
-616
lines changed

28 files changed

+824
-616
lines changed

dd-sdk-android-core/api/apiSurface

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ interface com.datadog.android.api.feature.FeatureScope
127127
fun <T: Feature> unwrap(): T
128128
typealias EventWriteScope = ((com.datadog.android.api.storage.EventBatchWriter) -> Unit) -> Unit
129129
fun <R: Any?> com.datadog.android.api.InternalLogger.measureMethodCallPerf(Class<*>, String, Float = 100f, () -> R): R
130+
fun FeatureScope.getContextFuture(Set<String> = emptySet()): java.util.concurrent.Future<com.datadog.android.api.context.DatadogContext?>?
130131
interface com.datadog.android.api.feature.FeatureSdkCore : com.datadog.android.api.SdkCore
131132
val internalLogger: com.datadog.android.api.InternalLogger
132133
fun registerFeature(Feature)

dd-sdk-android-core/api/dd-sdk-android-core.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ public final class com/datadog/android/api/feature/FeatureScope$DefaultImpls {
404404
}
405405

406406
public final class com/datadog/android/api/feature/FeatureScopeExtKt {
407+
public static final fun getContextFuture (Lcom/datadog/android/api/feature/FeatureScope;Ljava/util/Set;)Ljava/util/concurrent/Future;
408+
public static synthetic fun getContextFuture$default (Lcom/datadog/android/api/feature/FeatureScope;Ljava/util/Set;ILjava/lang/Object;)Ljava/util/concurrent/Future;
407409
public static final fun measureMethodCallPerf (Lcom/datadog/android/api/InternalLogger;Ljava/lang/Class;Ljava/lang/String;FLkotlin/jvm/functions/Function0;)Ljava/lang/Object;
408410
public static synthetic fun measureMethodCallPerf$default (Lcom/datadog/android/api/InternalLogger;Ljava/lang/Class;Ljava/lang/String;FLkotlin/jvm/functions/Function0;ILjava/lang/Object;)Ljava/lang/Object;
409411
}

dd-sdk-android-core/src/main/kotlin/com/datadog/android/api/feature/FeatureScopeExt.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66

77
package com.datadog.android.api.feature
88

9+
import androidx.annotation.AnyThread
910
import com.datadog.android.api.InternalLogger
11+
import com.datadog.android.api.context.DatadogContext
12+
import com.datadog.android.core.internal.SdkFeature
1013
import com.datadog.android.core.metrics.TelemetryMetricType
1114
import com.datadog.android.lint.InternalApi
15+
import java.util.concurrent.Future
1216

1317
/**
1418
* Measures the execution time for the given block and report it as a MethodCall telemetry metric.
@@ -39,3 +43,22 @@ fun <R : Any?> InternalLogger.measureMethodCallPerf(
3943

4044
return result
4145
}
46+
47+
/**
48+
* Utility to read current [DatadogContext], asynchronously.
49+
* @param withFeatureContexts Feature contexts ([DatadogContext.featuresContext] property) to include
50+
* in the [DatadogContext] provided. The value should be the feature names as declared by [Feature.name].
51+
* Default is empty, meaning that no feature contexts will be included.
52+
*
53+
* Returns future that will contain [DatadogContext] in the state that it has at the moment of call.
54+
*/
55+
@AnyThread
56+
@InternalApi
57+
fun FeatureScope.getContextFuture(
58+
withFeatureContexts: Set<String> = emptySet()
59+
): Future<DatadogContext?>? {
60+
return when (this) {
61+
is SdkFeature -> getContextFuture(withFeatureContexts)
62+
else -> null
63+
}
64+
}

dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/SdkFeature.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import com.datadog.android.privacy.TrackingConsentProviderCallback
6161
import com.datadog.android.security.Encryption
6262
import java.util.Locale
6363
import java.util.concurrent.Callable
64+
import java.util.concurrent.Future
6465
import java.util.concurrent.atomic.AtomicBoolean
6566
import java.util.concurrent.atomic.AtomicReference
6667
import java.util.concurrent.locks.ReadWriteLock
@@ -196,6 +197,20 @@ internal class SdkFeature(
196197
}
197198
}
198199

200+
internal fun getContextFuture(withFeatureContexts: Set<String>): Future<DatadogContext?>? {
201+
return coreFeature.contextExecutorService.submitSafe(
202+
"getContextFuture-${wrappedFeature.name}",
203+
internalLogger,
204+
Callable {
205+
if (coreFeature.initialized.get()) {
206+
contextProvider.getContext(withFeatureContexts)
207+
} else {
208+
null
209+
}
210+
}
211+
)
212+
}
213+
199214
override fun getWriteContextSync(
200215
withFeatureContexts: Set<String>
201216
): Pair<DatadogContext, EventWriteScope>? {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
* Copyright 2016-Present Datadog, Inc.
5+
*/
6+
7+
package com.datadog.android.api.feature
8+
9+
import com.datadog.android.api.context.DatadogContext
10+
import com.datadog.android.core.internal.SdkFeature
11+
import org.mockito.kotlin.any
12+
import org.mockito.kotlin.doReturn
13+
import org.mockito.kotlin.mock
14+
import java.util.concurrent.Future
15+
16+
object SdkFeatureMock {
17+
/**
18+
* This method is a trick that allows to mock FeatureScope.getContextFuture extension method.
19+
*/
20+
fun create(future: Future<DatadogContext?>? = null): FeatureScope = mock<SdkFeature> {
21+
on { getContextFuture(any()) } doReturn future
22+
}
23+
}

dd-sdk-android-internal/api/apiSurface

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ class com.datadog.android.internal.collections.EvictingQueue<T> : java.util.Queu
2222
override fun add(T): Boolean
2323
override fun offer(T): Boolean
2424
override fun addAll(Collection<T>): Boolean
25-
class com.datadog.android.internal.concurrent.CompletableFuture<T: Any>
26-
var value: T
27-
fun isComplete(): Boolean
28-
fun complete(T)
2925
enum com.datadog.android.internal.network.GraphQLHeaders
3026
constructor(String)
3127
- DD_GRAPHQL_NAME_HEADER

dd-sdk-android-internal/api/dd-sdk-android-internal.api

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ public final class com/datadog/android/internal/collections/EvictingQueue : java
5454
public fun toArray ([Ljava/lang/Object;)[Ljava/lang/Object;
5555
}
5656

57-
public final class com/datadog/android/internal/concurrent/CompletableFuture {
58-
public fun <init> ()V
59-
public final fun complete (Ljava/lang/Object;)V
60-
public final fun getValue ()Ljava/lang/Object;
61-
public final fun isComplete ()Z
62-
}
63-
6457
public final class com/datadog/android/internal/network/GraphQLHeaders : java/lang/Enum {
6558
public static final field DD_GRAPHQL_NAME_HEADER Lcom/datadog/android/internal/network/GraphQLHeaders;
6659
public static final field DD_GRAPHQL_PAYLOAD_HEADER Lcom/datadog/android/internal/network/GraphQLHeaders;

dd-sdk-android-internal/src/main/java/com/datadog/android/internal/concurrent/CompletableFuture.kt

Lines changed: 0 additions & 25 deletions
This file was deleted.

dd-sdk-android-internal/src/test/java/com/datadog/android/internal/concurrent/CompletableFutureTest.kt

Lines changed: 0 additions & 112 deletions
This file was deleted.

detekt_custom_unsafe_calls.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ datadog:
107107
- "java.util.concurrent.ExecutorService.execute(java.lang.Runnable?):java.util.concurrent.RejectedExecutionException,java.lang.NullPointerException"
108108
- "java.util.concurrent.ExecutorService.submit(java.lang.Runnable?):java.util.concurrent.RejectedExecutionException,java.lang.NullPointerException"
109109
- "java.util.concurrent.Future.get():java.lang.InterruptedException,java.util.concurrent.CancellationException,java.util.concurrent.ExecutionException"
110+
- "java.util.concurrent.Future.get(kotlin.Long, java.util.concurrent.TimeUnit?):java.lang.InterruptedException,java.util.concurrent.CancellationException,java.util.concurrent.ExecutionException"
110111
- "java.util.concurrent.LinkedBlockingQueue.offer(kotlin.Any?):java.lang.NullPointerException"
111112
- "java.util.concurrent.LinkedBlockingQueue.offer(kotlin.Any?, kotlin.Long, java.util.concurrent.TimeUnit?):java.lang.NullPointerException"
112113
- "java.util.concurrent.ScheduledExecutorService.schedule(java.lang.Runnable, kotlin.Long, java.util.concurrent.TimeUnit):java.util.concurrent.RejectedExecutionException,java.lang.NullPointerException"

0 commit comments

Comments
 (0)