Skip to content

Commit b820571

Browse files
committed
Merge remote-tracking branch 'origin/feature/v3' into feature/v3
2 parents 2263592 + 8e0a4f7 commit b820571

File tree

182 files changed

+7753
-4432
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

182 files changed

+7753
-4432
lines changed

dd-sdk-android-core/api/apiSurface

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,23 @@ interface com.datadog.android.api.feature.FeatureEventReceiver
116116
fun onReceive(Any)
117117
interface com.datadog.android.api.feature.FeatureScope
118118
val dataStore: com.datadog.android.api.storage.datastore.DataStoreHandler
119-
fun withWriteContext(Boolean = false, (com.datadog.android.api.context.DatadogContext, com.datadog.android.api.storage.EventBatchWriter) -> Unit)
119+
fun withWriteContext(Set<String> = emptySet(), (com.datadog.android.api.context.DatadogContext) -> Unit)
120+
fun withContext(Set<String> = emptySet(), (com.datadog.android.api.context.DatadogContext) -> Unit)
121+
fun getWriteContextSync(Set<String> = emptySet()): Pair<com.datadog.android.api.context.DatadogContext, EventWriteScope>?
120122
fun sendEvent(Any)
121123
fun <T: Feature> unwrap(): T
124+
typealias EventWriteScope = ((com.datadog.android.api.storage.EventBatchWriter) -> Unit) -> Unit
122125
fun <R: Any?> com.datadog.android.api.InternalLogger.measureMethodCallPerf(Class<*>, String, Float = 100f, () -> R): R
123126
interface com.datadog.android.api.feature.FeatureSdkCore : com.datadog.android.api.SdkCore
124127
val internalLogger: com.datadog.android.api.InternalLogger
125128
fun registerFeature(Feature)
126129
fun getFeature(String): FeatureScope?
127-
fun updateFeatureContext(String, (MutableMap<String, Any?>) -> Unit)
128-
fun getFeatureContext(String): Map<String, Any?>
130+
fun updateFeatureContext(String, Boolean = true, (MutableMap<String, Any?>) -> Unit)
131+
fun getFeatureContext(String, Boolean = true): Map<String, Any?>
129132
fun setEventReceiver(String, FeatureEventReceiver)
130-
fun setContextUpdateReceiver(String, FeatureContextUpdateReceiver)
131-
fun removeContextUpdateReceiver(String, FeatureContextUpdateReceiver)
132133
fun removeEventReceiver(String)
134+
fun setContextUpdateReceiver(FeatureContextUpdateReceiver)
135+
fun removeContextUpdateReceiver(FeatureContextUpdateReceiver)
133136
fun createSingleThreadExecutorService(String): java.util.concurrent.ExecutorService
134137
fun createScheduledExecutorService(String): java.util.concurrent.ScheduledExecutorService
135138
fun setAnonymousId(java.util.UUID?)
@@ -196,7 +199,7 @@ interface com.datadog.android.core.InternalSdkCore : com.datadog.android.api.fea
196199
fun writeLastFatalAnrSent(Long)
197200
fun getPersistenceExecutorService(): java.util.concurrent.ExecutorService
198201
fun getAllFeatures(): List<com.datadog.android.api.feature.FeatureScope>
199-
fun getDatadogContext(): com.datadog.android.api.context.DatadogContext?
202+
fun getDatadogContext(Set<String> = emptySet()): com.datadog.android.api.context.DatadogContext?
200203
class com.datadog.android.core.SdkReference
201204
constructor(String? = null, (com.datadog.android.api.SdkCore) -> Unit = {})
202205
fun get(): com.datadog.android.api.SdkCore?
@@ -294,6 +297,8 @@ fun Collection<ByteArray>.join(ByteArray, ByteArray = ByteArray(0), ByteArray =
294297
fun java.util.concurrent.Executor.executeSafe(String, com.datadog.android.api.InternalLogger, Runnable)
295298
fun java.util.concurrent.ScheduledExecutorService.scheduleSafe(String, Long, java.util.concurrent.TimeUnit, com.datadog.android.api.InternalLogger, Runnable): java.util.concurrent.ScheduledFuture<*>?
296299
fun java.util.concurrent.ExecutorService.submitSafe(String, com.datadog.android.api.InternalLogger, Runnable): java.util.concurrent.Future<*>?
300+
fun <T> java.util.concurrent.ExecutorService.submitSafe(String, com.datadog.android.api.InternalLogger, java.util.concurrent.Callable<T>): java.util.concurrent.Future<T>?
301+
fun <T> java.util.concurrent.Future<T>?.getSafe(String, com.datadog.android.api.InternalLogger): T?
297302
object com.datadog.android.core.internal.utils.JsonSerializer
298303
fun toJsonElement(Any?): com.google.gson.JsonElement
299304
fun Map<String, Any?>.safeMapValuesToJson(com.datadog.android.api.InternalLogger): Map<String, com.google.gson.JsonElement>

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,17 @@ public abstract interface class com/datadog/android/api/feature/FeatureEventRece
371371

372372
public abstract interface class com/datadog/android/api/feature/FeatureScope {
373373
public abstract fun getDataStore ()Lcom/datadog/android/api/storage/datastore/DataStoreHandler;
374+
public abstract fun getWriteContextSync (Ljava/util/Set;)Lkotlin/Pair;
374375
public abstract fun sendEvent (Ljava/lang/Object;)V
375376
public abstract fun unwrap ()Lcom/datadog/android/api/feature/Feature;
376-
public abstract fun withWriteContext (ZLkotlin/jvm/functions/Function2;)V
377+
public abstract fun withContext (Ljava/util/Set;Lkotlin/jvm/functions/Function1;)V
378+
public abstract fun withWriteContext (Ljava/util/Set;Lkotlin/jvm/functions/Function2;)V
377379
}
378380

379381
public final class com/datadog/android/api/feature/FeatureScope$DefaultImpls {
380-
public static synthetic fun withWriteContext$default (Lcom/datadog/android/api/feature/FeatureScope;ZLkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
382+
public static synthetic fun getWriteContextSync$default (Lcom/datadog/android/api/feature/FeatureScope;Ljava/util/Set;ILjava/lang/Object;)Lkotlin/Pair;
383+
public static synthetic fun withContext$default (Lcom/datadog/android/api/feature/FeatureScope;Ljava/util/Set;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
384+
public static synthetic fun withWriteContext$default (Lcom/datadog/android/api/feature/FeatureScope;Ljava/util/Set;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
381385
}
382386

383387
public final class com/datadog/android/api/feature/FeatureScopeExtKt {
@@ -389,15 +393,20 @@ public abstract interface class com/datadog/android/api/feature/FeatureSdkCore :
389393
public abstract fun createScheduledExecutorService (Ljava/lang/String;)Ljava/util/concurrent/ScheduledExecutorService;
390394
public abstract fun createSingleThreadExecutorService (Ljava/lang/String;)Ljava/util/concurrent/ExecutorService;
391395
public abstract fun getFeature (Ljava/lang/String;)Lcom/datadog/android/api/feature/FeatureScope;
392-
public abstract fun getFeatureContext (Ljava/lang/String;)Ljava/util/Map;
396+
public abstract fun getFeatureContext (Ljava/lang/String;Z)Ljava/util/Map;
393397
public abstract fun getInternalLogger ()Lcom/datadog/android/api/InternalLogger;
394398
public abstract fun registerFeature (Lcom/datadog/android/api/feature/Feature;)V
395-
public abstract fun removeContextUpdateReceiver (Ljava/lang/String;Lcom/datadog/android/api/feature/FeatureContextUpdateReceiver;)V
399+
public abstract fun removeContextUpdateReceiver (Lcom/datadog/android/api/feature/FeatureContextUpdateReceiver;)V
396400
public abstract fun removeEventReceiver (Ljava/lang/String;)V
397401
public abstract fun setAnonymousId (Ljava/util/UUID;)V
398-
public abstract fun setContextUpdateReceiver (Ljava/lang/String;Lcom/datadog/android/api/feature/FeatureContextUpdateReceiver;)V
402+
public abstract fun setContextUpdateReceiver (Lcom/datadog/android/api/feature/FeatureContextUpdateReceiver;)V
399403
public abstract fun setEventReceiver (Ljava/lang/String;Lcom/datadog/android/api/feature/FeatureEventReceiver;)V
400-
public abstract fun updateFeatureContext (Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V
404+
public abstract fun updateFeatureContext (Ljava/lang/String;ZLkotlin/jvm/functions/Function1;)V
405+
}
406+
407+
public final class com/datadog/android/api/feature/FeatureSdkCore$DefaultImpls {
408+
public static synthetic fun getFeatureContext$default (Lcom/datadog/android/api/feature/FeatureSdkCore;Ljava/lang/String;ZILjava/lang/Object;)Ljava/util/Map;
409+
public static synthetic fun updateFeatureContext$default (Lcom/datadog/android/api/feature/FeatureSdkCore;Ljava/lang/String;ZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
401410
}
402411

403412
public abstract interface class com/datadog/android/api/feature/StorageBackedFeature : com/datadog/android/api/feature/Feature {
@@ -559,7 +568,7 @@ public abstract interface class com/datadog/android/core/InternalSdkCore : com/d
559568
public abstract fun deleteLastViewEvent ()V
560569
public abstract fun getAllFeatures ()Ljava/util/List;
561570
public abstract fun getAppStartTimeNs ()J
562-
public abstract fun getDatadogContext ()Lcom/datadog/android/api/context/DatadogContext;
571+
public abstract fun getDatadogContext (Ljava/util/Set;)Lcom/datadog/android/api/context/DatadogContext;
563572
public abstract fun getFirstPartyHostResolver ()Lcom/datadog/android/core/internal/net/FirstPartyHostHeaderTypeResolver;
564573
public abstract fun getLastFatalAnrSent ()Ljava/lang/Long;
565574
public abstract fun getLastViewEvent ()Lcom/google/gson/JsonObject;
@@ -572,6 +581,10 @@ public abstract interface class com/datadog/android/core/InternalSdkCore : com/d
572581
public abstract fun writeLastViewEvent ([B)V
573582
}
574583

584+
public final class com/datadog/android/core/InternalSdkCore$DefaultImpls {
585+
public static synthetic fun getDatadogContext$default (Lcom/datadog/android/core/InternalSdkCore;Ljava/util/Set;ILjava/lang/Object;)Lcom/datadog/android/api/context/DatadogContext;
586+
}
587+
575588
public final class com/datadog/android/core/SdkReference {
576589
public fun <init> ()V
577590
public fun <init> (Ljava/lang/String;)V
@@ -785,8 +798,10 @@ public final class com/datadog/android/core/internal/utils/ByteArrayExtKt {
785798

786799
public final class com/datadog/android/core/internal/utils/ConcurrencyExtKt {
787800
public static final fun executeSafe (Ljava/util/concurrent/Executor;Ljava/lang/String;Lcom/datadog/android/api/InternalLogger;Ljava/lang/Runnable;)V
801+
public static final fun getSafe (Ljava/util/concurrent/Future;Ljava/lang/String;Lcom/datadog/android/api/InternalLogger;)Ljava/lang/Object;
788802
public static final fun scheduleSafe (Ljava/util/concurrent/ScheduledExecutorService;Ljava/lang/String;JLjava/util/concurrent/TimeUnit;Lcom/datadog/android/api/InternalLogger;Ljava/lang/Runnable;)Ljava/util/concurrent/ScheduledFuture;
789803
public static final fun submitSafe (Ljava/util/concurrent/ExecutorService;Ljava/lang/String;Lcom/datadog/android/api/InternalLogger;Ljava/lang/Runnable;)Ljava/util/concurrent/Future;
804+
public static final fun submitSafe (Ljava/util/concurrent/ExecutorService;Ljava/lang/String;Lcom/datadog/android/api/InternalLogger;Ljava/util/concurrent/Callable;)Ljava/util/concurrent/Future;
790805
}
791806

792807
public final class com/datadog/android/core/internal/utils/JsonSerializer {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ object Datadog {
9494
sdkInstanceName
9595
).apply {
9696
initialize(configuration)
97+
// not pushing to the context thread to have it set already at the
98+
// moment Datadog.initialize is completed
99+
coreFeature.trackingConsentProvider.setConsent(trackingConsent)
97100
}
98-
sdkCore.setTrackingConsent(trackingConsent)
99101
registry.register(sdkInstanceName, sdkCore)
100102

101103
return sdkCore

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ fun interface FeatureContextUpdateReceiver {
1616
/**
1717
* Called when the context for a feature is updated.
1818
* @param featureName the name of the feature
19-
* @param event the updated context
19+
* @param context the updated context
2020
*/
2121
@AnyThread
22-
fun onContextUpdate(featureName: String, event: Map<String, Any?>)
22+
fun onContextUpdate(featureName: String, context: Map<String, Any?>)
2323
}

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

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.annotation.AnyThread
1010
import com.datadog.android.api.context.DatadogContext
1111
import com.datadog.android.api.storage.EventBatchWriter
1212
import com.datadog.android.api.storage.datastore.DataStoreHandler
13+
import com.datadog.android.lint.InternalApi
1314

1415
/**
1516
* Represents a Datadog feature.
@@ -23,19 +24,48 @@ interface FeatureScope {
2324

2425
/**
2526
* Utility to write an event, asynchronously.
26-
* @param forceNewBatch if `true` forces the [EventBatchWriter] to write in a new file and
27-
* not reuse the already existing pending data persistence file. By default it is `false`.
27+
* @param withFeatureContexts Feature contexts ([DatadogContext.featuresContext] property) to include
28+
* in the [DatadogContext] provided. The value should be the feature names as declared by [Feature.name].
29+
* Default is empty, meaning that no feature contexts will be included.
2830
* @param callback an operation called with an up-to-date [DatadogContext]
29-
* and an [EventBatchWriter]. Callback will be executed on a worker thread from I/O pool.
30-
* [DatadogContext] will have a state created at the moment this method is called, before the
31-
* thread switch for the callback invocation.
31+
* and an [EventWriteScope]. Callback will be executed on a single context processing worker thread. Execution of
32+
* [EventWriteScope] will be done on a worker thread from I/O pool.
33+
* [DatadogContext] will have a state created at the moment this method is called.
3234
*/
3335
@AnyThread
3436
fun withWriteContext(
35-
forceNewBatch: Boolean = false,
36-
callback: (DatadogContext, EventBatchWriter) -> Unit
37+
withFeatureContexts: Set<String> = emptySet(),
38+
callback: (datadogContext: DatadogContext, write: EventWriteScope) -> Unit
3739
)
3840

41+
/**
42+
* Utility to read current [DatadogContext], asynchronously.
43+
* @param withFeatureContexts Feature contexts ([DatadogContext.featuresContext] property) to include
44+
* in the [DatadogContext] provided. The value should be the feature names as declared by [Feature.name].
45+
* Default is empty, meaning that no feature contexts will be included.
46+
* @param callback an operation called with an up-to-date [DatadogContext].
47+
* [DatadogContext] will have a state created at the moment this method is called.
48+
*/
49+
@AnyThread
50+
fun withContext(
51+
withFeatureContexts: Set<String> = emptySet(),
52+
callback: (datadogContext: DatadogContext) -> Unit
53+
)
54+
55+
// TODO RUM-9852 Implement better passthrough mechanism for the JVM crash scenario
56+
/**
57+
* Same as [withWriteContext] but will be executed in the blocking manner.
58+
*
59+
* @param withFeatureContexts Feature contexts ([DatadogContext.featuresContext] property) to include
60+
* in the [DatadogContext] provided. The value should be the feature names as declared by [Feature.name].
61+
* Default is empty, meaning that no feature contexts will be included.
62+
*
63+
* **NOTE**: This API is for the internal use only and is not guaranteed to be stable.
64+
*/
65+
@AnyThread
66+
@InternalApi
67+
fun getWriteContextSync(withFeatureContexts: Set<String> = emptySet()): Pair<DatadogContext, EventWriteScope>?
68+
3969
/**
4070
* Send event to a given feature. It will be sent in a synchronous way.
4171
*
@@ -48,3 +78,9 @@ interface FeatureScope {
4878
*/
4979
fun <T : Feature> unwrap(): T
5080
}
81+
82+
/**
83+
* Scope for the event write operation which is invoked on the worker thread from I/O pool, which is different
84+
* from the context processing worker thread used for [FeatureScope.withWriteContext] callback invocation.
85+
*/
86+
typealias EventWriteScope = ((EventBatchWriter) -> Unit) -> Unit

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,30 @@ interface FeatureSdkCore : SdkCore {
4545
* provided [featureName], a new one will be created.
4646
*
4747
* @param featureName Feature name.
48+
* @param useContextThread Whenever update of the context should happen on the context processing thread or not. It
49+
* should be true for most of the cases related to the event processing. Be careful when setting it to false, valid
50+
* use-case can be like updating/reading feature context on the same (or already on the context) thread.
51+
* Defaults to true.
4852
* @param updateCallback Provides current feature context for the update. If there is no feature
4953
* with the given name registered, callback won't be called.
5054
*/
5155
fun updateFeatureContext(
5256
featureName: String,
57+
useContextThread: Boolean = true,
5358
updateCallback: (context: MutableMap<String, Any?>) -> Unit
5459
)
5560

5661
/**
5762
* Retrieves the context for the particular feature.
5863
*
5964
* @param featureName Feature name.
65+
* @param useContextThread Whenever context read should happen on the context processing thread or not. It
66+
* should be true for most of the cases related to the event processing. Be careful when setting it to false, valid
67+
* use-case can be like updating/reading feature context on the same (or already on the context) thread.
68+
* Defaults to true.
6069
* @return Context for the given feature or empty map if feature is not registered.
6170
*/
62-
fun getFeatureContext(featureName: String): Map<String, Any?>
71+
fun getFeatureContext(featureName: String, useContextThread: Boolean = true): Map<String, Any?>
6372

6473
/**
6574
* Sets event receiver for the given feature.
@@ -70,27 +79,26 @@ interface FeatureSdkCore : SdkCore {
7079
fun setEventReceiver(featureName: String, receiver: FeatureEventReceiver)
7180

7281
/**
73-
* Sets context update receiver for the given feature.
82+
* Removes events receive for the given feature.
7483
*
7584
* @param featureName Feature name.
76-
* @param listener Listener to remove.
7785
*/
78-
fun setContextUpdateReceiver(featureName: String, listener: FeatureContextUpdateReceiver)
86+
fun removeEventReceiver(featureName: String)
7987

8088
/**
81-
* Removes context update listener for the given feature.
89+
* Sets feature context update listener. Once subscribed, current context will be emitted
90+
* immdediately if it exists.
8291
*
83-
* @param featureName Feature name.
8492
* @param listener Listener to remove.
8593
*/
86-
fun removeContextUpdateReceiver(featureName: String, listener: FeatureContextUpdateReceiver)
94+
fun setContextUpdateReceiver(listener: FeatureContextUpdateReceiver)
8795

8896
/**
89-
* Removes events receive for the given feature.
97+
* Removes feature context update listener.
9098
*
91-
* @param featureName Feature name.
99+
* @param listener Listener to remove.
92100
*/
93-
fun removeEventReceiver(featureName: String)
101+
fun removeContextUpdateReceiver(listener: FeatureContextUpdateReceiver)
94102

95103
/**
96104
* Returns a new single thread [ExecutorService], set up with backpressure and internal monitoring.

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,11 @@ interface InternalSdkCore : FeatureSdkCore {
116116
fun getAllFeatures(): List<FeatureScope>
117117

118118
/**
119-
* @return the current DatadogContext, or null
119+
* @param withFeatureContexts Feature contexts ([DatadogContext.featuresContext] property) to include
120+
* in the [DatadogContext] provided. The value should be the feature names as declared by [Feature.name].
121+
* Default is empty, meaning that no feature contexts will be included.
122+
* @return the current [DatadogContext], or null
120123
*/
121124
@InternalApi
122-
fun getDatadogContext(): DatadogContext?
125+
fun getDatadogContext(withFeatureContexts: Set<String> = emptySet()): DatadogContext?
123126
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@
77
package com.datadog.android.core.internal
88

99
import com.datadog.android.api.context.DatadogContext
10+
import com.datadog.android.api.feature.Feature
1011

1112
internal interface ContextProvider {
12-
// TODO RUM-3784 getting context may be quite heavy, should it be something non-blocking here?
13-
1413
// TODO RUM-3784 lifecycle checks may be needed for the cases when context is requested
1514
// when datadog is not initialized yet/anymore (case of UploadWorker, other calls site
1615
// should be in sync with lifecycle)
17-
18-
// TODO RUM-3784 can be accessed from different threads
19-
val context: DatadogContext
20-
21-
fun setFeatureContext(feature: String, context: Map<String, Any?>)
22-
23-
fun getFeatureContext(feature: String): Map<String, Any?>
16+
/**
17+
* @param withFeatureContexts Feature contexts ([DatadogContext.featuresContext] property) to include
18+
* in the [DatadogContext] provided. The value should be the feature names as declared by [Feature.name].
19+
* Default is empty, meaning that no feature contexts will be included.
20+
*/
21+
fun getContext(withFeatureContexts: Set<String>): DatadogContext
2422
}

0 commit comments

Comments
 (0)