Skip to content

Commit

Permalink
Merge pull request #2105 from DataDog/xgouchet/RUM-4822/monitor_conne…
Browse files Browse the repository at this point in the history
…ction_pool

RUM-4822 Allow monitoring internal connection pool
  • Loading branch information
xgouchet authored Jun 24, 2024
2 parents c2572e7 + 341222a commit ded9b52
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 2 deletions.
2 changes: 2 additions & 0 deletions dd-sdk-android-core/api/apiSurface
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions dd-sdk-android-core/api/dd-sdk-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 <init> (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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -165,6 +176,7 @@ internal class DataOkHttpUploader(
HTTP_UNAVAILABLE,
HTTP_GATEWAY_TIMEOUT,
HTTP_INSUFFICIENT_STORAGE -> UploadStatus.HttpServerError(code)

else -> {
internalLogger.log(
InternalLogger.Level.WARN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit ded9b52

Please sign in to comment.