From c19fed7f01c6b7b7e742c09935ab8c170fc4d5cb Mon Sep 17 00:00:00 2001 From: "Xavier F. Gouchet" Date: Mon, 24 Jun 2024 07:40:02 +0200 Subject: [PATCH 1/2] RUM-4822 Allow monitoring internal connection pool --- dd-sdk-android-core/api/apiSurface | 2 ++ dd-sdk-android-core/api/dd-sdk-android-core.api | 13 +++++++++++++ .../datadog/android/core/ConnectionPoolInfo.kt | 17 +++++++++++++++++ .../internal/data/upload/DataOkHttpUploader.kt | 12 ++++++++++++ .../rum/RumResourceAttributesProvider.kt | 2 +- 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/ConnectionPoolInfo.kt diff --git a/dd-sdk-android-core/api/apiSurface b/dd-sdk-android-core/api/apiSurface index 74647c6619..0f424ca832 100644 --- a/dd-sdk-android-core/api/apiSurface +++ b/dd-sdk-android-core/api/apiSurface @@ -151,6 +151,8 @@ data class com.datadog.android.api.storage.RawBatchEvent constructor(ByteArray, ByteArray = EMPTY_BYTE_ARRAY) override fun equals(Any?): Boolean override fun hashCode(): Int +data class com.datadog.android.core.ConnectionPoolInfo + constructor(Int, Int) interface com.datadog.android.core.InternalSdkCore : com.datadog.android.api.feature.FeatureSdkCore val networkInfo: com.datadog.android.api.context.NetworkInfo val trackingConsent: com.datadog.android.privacy.TrackingConsent diff --git a/dd-sdk-android-core/api/dd-sdk-android-core.api b/dd-sdk-android-core/api/dd-sdk-android-core.api index d9e9c3b0d2..4bf910ddbb 100644 --- a/dd-sdk-android-core/api/dd-sdk-android-core.api +++ b/dd-sdk-android-core/api/dd-sdk-android-core.api @@ -462,6 +462,19 @@ public final class com/datadog/android/api/storage/RawBatchEvent { public fun toString ()Ljava/lang/String; } +public final class com/datadog/android/core/ConnectionPoolInfo { + public fun (II)V + public final fun component1 ()I + public final fun component2 ()I + public final fun copy (II)Lcom/datadog/android/core/ConnectionPoolInfo; + public static synthetic fun copy$default (Lcom/datadog/android/core/ConnectionPoolInfo;IIILjava/lang/Object;)Lcom/datadog/android/core/ConnectionPoolInfo; + public fun equals (Ljava/lang/Object;)Z + public final fun getConnectionCount ()I + public final fun getIdleConnectionCount ()I + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + public abstract interface class com/datadog/android/core/InternalSdkCore : com/datadog/android/api/feature/FeatureSdkCore { public abstract fun deleteLastViewEvent ()V public abstract fun getAllFeatures ()Ljava/util/List; diff --git a/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/ConnectionPoolInfo.kt b/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/ConnectionPoolInfo.kt new file mode 100644 index 0000000000..c10bcb99a3 --- /dev/null +++ b/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/ConnectionPoolInfo.kt @@ -0,0 +1,17 @@ +/* + * 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.core + +/** + * Provide information abouth the internal OkHttp Connection pool. + * @property connectionCount the total number of connections in the pool + * @property idleConnectionCount the number of idle connections in the pool + */ +data class ConnectionPoolInfo( + val connectionCount: Int, + val idleConnectionCount: Int +) diff --git a/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/data/upload/DataOkHttpUploader.kt b/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/data/upload/DataOkHttpUploader.kt index 9743e37ca5..c936b45529 100644 --- a/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/data/upload/DataOkHttpUploader.kt +++ b/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/data/upload/DataOkHttpUploader.kt @@ -11,9 +11,11 @@ import com.datadog.android.api.InternalLogger import com.datadog.android.api.context.DatadogContext import com.datadog.android.api.net.RequestFactory import com.datadog.android.api.storage.RawBatchEvent +import com.datadog.android.core.ConnectionPoolInfo import com.datadog.android.core.internal.system.AndroidInfoProvider import okhttp3.Call import okhttp3.MediaType.Companion.toMediaTypeOrNull +import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.RequestBody.Companion.toRequestBody import java.util.Locale @@ -133,6 +135,15 @@ internal class DataOkHttpUploader( builder.addHeader(HEADER_USER_AGENT, userAgent) + if (callFactory is OkHttpClient) { + val stats = ConnectionPoolInfo( + callFactory.connectionPool.connectionCount(), + callFactory.connectionPool.idleConnectionCount() + ) + + builder.tag(ConnectionPoolInfo::class.java, stats) + } + return builder.build() } @@ -165,6 +176,7 @@ internal class DataOkHttpUploader( HTTP_UNAVAILABLE, HTTP_GATEWAY_TIMEOUT, HTTP_INSUFFICIENT_STORAGE -> UploadStatus.HttpServerError(code) + else -> { internalLogger.log( InternalLogger.Level.WARN, diff --git a/features/dd-sdk-android-rum/src/main/kotlin/com/datadog/android/rum/RumResourceAttributesProvider.kt b/features/dd-sdk-android-rum/src/main/kotlin/com/datadog/android/rum/RumResourceAttributesProvider.kt index 53c0ad13b6..0697158af6 100644 --- a/features/dd-sdk-android-rum/src/main/kotlin/com/datadog/android/rum/RumResourceAttributesProvider.kt +++ b/features/dd-sdk-android-rum/src/main/kotlin/com/datadog/android/rum/RumResourceAttributesProvider.kt @@ -15,7 +15,7 @@ import okhttp3.Response * offers a possibility to add custom attributes to the RUM Resource event. */ @NoOpImplementation -interface RumResourceAttributesProvider { +fun interface RumResourceAttributesProvider { /** * Offers a possibility to create custom attributes collection which later will be attached to From 341222a0665f14fba21c7911c442d373d6a3ac40 Mon Sep 17 00:00:00 2001 From: "Xavier F. Gouchet" Date: Mon, 24 Jun 2024 08:35:34 +0200 Subject: [PATCH 2/2] RUM-4822 include connection pool info in sample --- .../com/datadog/android/sample/SampleApplication.kt | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/sample/kotlin/src/main/kotlin/com/datadog/android/sample/SampleApplication.kt b/sample/kotlin/src/main/kotlin/com/datadog/android/sample/SampleApplication.kt index a3901479f5..5a994f1d99 100644 --- a/sample/kotlin/src/main/kotlin/com/datadog/android/sample/SampleApplication.kt +++ b/sample/kotlin/src/main/kotlin/com/datadog/android/sample/SampleApplication.kt @@ -13,6 +13,7 @@ import android.util.Log import androidx.lifecycle.ViewModelProvider import com.datadog.android.Datadog import com.datadog.android.DatadogSite +import com.datadog.android.core.ConnectionPoolInfo import com.datadog.android.core.configuration.BackPressureMitigation import com.datadog.android.core.configuration.BackPressureStrategy import com.datadog.android.core.configuration.BatchSize @@ -272,7 +273,17 @@ class SampleApplication : Application() { ) ) - configBuilder.addNetworkInterceptor(DatadogInterceptor()) + configBuilder.addNetworkInterceptor( + DatadogInterceptor( + rumResourceAttributesProvider = { request, _, _ -> + val connectionPoolInfo = request.tag(ConnectionPoolInfo::class.java) + mapOf( + "connection_pool.connection_count" to connectionPoolInfo?.connectionCount, + "connection_pool.idle_connection_count" to connectionPoolInfo?.idleConnectionCount + ) + } + ) + ) return configBuilder.build() }