diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 51d7499fe7..3ec1c14c78 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -46,10 +46,11 @@ The whole project is covered by a set of static analysis tools, linters and test ./gradlew instrumentTestAll # launches the detekt static analysis for all modules -# the detekt client needs to be installed on your machine via Homebrew -# the configuration files are stored in the dd-source repository -detekt --config {dd-source}/domains/mobile/config/android/gitlab/detekt/detekt-common.yml +# the detekt client needs to be installed on your machine as stated in the official documentation +# https://detekt.dev/docs/gettingstarted/cli +# the configuration files are stored in Datadog's dd-source repository detekt --config {dd-source}/domains/mobile/config/android/gitlab/detekt/detekt-common.yml +detekt --config {dd-source}/domains/mobile/config/android/gitlab/detekt/detekt-public-api.yml # launches the ktlint check and formatter for all Kotlin files # the ktlint client needs to be installed on your machine diff --git a/dd-sdk-android/src/main/kotlin/com/datadog/android/DatadogInterceptor.kt b/dd-sdk-android/src/main/kotlin/com/datadog/android/DatadogInterceptor.kt index e4dd9c7b47..08f35e4022 100644 --- a/dd-sdk-android/src/main/kotlin/com/datadog/android/DatadogInterceptor.kt +++ b/dd-sdk-android/src/main/kotlin/com/datadog/android/DatadogInterceptor.kt @@ -12,8 +12,8 @@ import com.datadog.android.core.internal.net.FirstPartyHostDetector import com.datadog.android.core.internal.net.identifyRequest import com.datadog.android.core.internal.sampling.RateBasedSampler import com.datadog.android.core.internal.sampling.Sampler -import com.datadog.android.core.internal.utils.HUNDRED import com.datadog.android.core.internal.utils.devLogger +import com.datadog.android.core.internal.utils.percent import com.datadog.android.core.internal.utils.sdkLogger import com.datadog.android.rum.GlobalRum import com.datadog.android.rum.NoOpRumResourceAttributesProvider @@ -104,7 +104,6 @@ internal constructor( * be kept, `100.0` means all traces will be kept (default value is `20.0`). */ @JvmOverloads - @Suppress("MagicNumber") constructor( firstPartyHosts: List, tracedRequestListener: TracedRequestListener = NoOpTracedRequestListener(), @@ -116,7 +115,7 @@ internal constructor( tracedRequestListener = tracedRequestListener, firstPartyHostDetector = getGlobalFirstPartyHostDetector(), rumResourceAttributesProvider = rumResourceAttributesProvider, - traceSampler = RateBasedSampler(traceSamplingRate / HUNDRED), + traceSampler = RateBasedSampler(traceSamplingRate.percent()), localTracerFactory = { AndroidTracer.Builder().build() } ) @@ -143,7 +142,7 @@ internal constructor( tracedRequestListener = tracedRequestListener, firstPartyHostDetector = getGlobalFirstPartyHostDetector(), rumResourceAttributesProvider = rumResourceAttributesProvider, - traceSampler = RateBasedSampler(traceSamplingRate / HUNDRED), + traceSampler = RateBasedSampler(traceSamplingRate.percent()), localTracerFactory = { AndroidTracer.Builder().build() } ) diff --git a/dd-sdk-android/src/main/kotlin/com/datadog/android/core/internal/persistence/file/batch/BatchFileDataWriter.kt b/dd-sdk-android/src/main/kotlin/com/datadog/android/core/internal/persistence/file/batch/BatchFileDataWriter.kt index 0aead7f837..71a7c427cf 100644 --- a/dd-sdk-android/src/main/kotlin/com/datadog/android/core/internal/persistence/file/batch/BatchFileDataWriter.kt +++ b/dd-sdk-android/src/main/kotlin/com/datadog/android/core/internal/persistence/file/batch/BatchFileDataWriter.kt @@ -43,7 +43,7 @@ internal open class BatchFileDataWriter( /** * Called whenever data is written successfully. * @param data the data written - * @param rawData the data written (as the actual ByteArray written on disk) + * @param rawData the data written (as the actual [ByteArray] written on disk) */ @WorkerThread internal open fun onDataWritten(data: T, rawData: ByteArray) {} diff --git a/dd-sdk-android/src/main/kotlin/com/datadog/android/core/internal/utils/NumberExt.kt b/dd-sdk-android/src/main/kotlin/com/datadog/android/core/internal/utils/NumberExt.kt index 616cff13ba..0c90bbfa58 100644 --- a/dd-sdk-android/src/main/kotlin/com/datadog/android/core/internal/utils/NumberExt.kt +++ b/dd-sdk-android/src/main/kotlin/com/datadog/android/core/internal/utils/NumberExt.kt @@ -18,3 +18,5 @@ internal fun Long.toHexString() = toString(HEX_RADIX) internal fun BigInteger.toHexString(): String { return toLong().toHexString() } + +internal fun Float.percent() = this / HUNDRED diff --git a/dd-sdk-android/src/main/kotlin/com/datadog/android/rum/RumMonitor.kt b/dd-sdk-android/src/main/kotlin/com/datadog/android/rum/RumMonitor.kt index 2f4db5bfd6..70caa503bc 100644 --- a/dd-sdk-android/src/main/kotlin/com/datadog/android/rum/RumMonitor.kt +++ b/dd-sdk-android/src/main/kotlin/com/datadog/android/rum/RumMonitor.kt @@ -13,8 +13,8 @@ import androidx.annotation.FloatRange import androidx.fragment.app.Fragment import com.datadog.android.Datadog import com.datadog.android.core.internal.sampling.RateBasedSampler -import com.datadog.android.core.internal.utils.HUNDRED import com.datadog.android.core.internal.utils.devLogger +import com.datadog.android.core.internal.utils.percent import com.datadog.android.rum.internal.monitor.DatadogRumMonitor import com.datadog.android.telemetry.internal.TelemetryEventHandler import com.datadog.android.v2.core.DatadogCore @@ -308,7 +308,7 @@ interface RumMonitor { telemetryEventHandler = TelemetryEventHandler( sdkCore = datadogCore, coreFeature.timeProvider, - RateBasedSampler(rumFeature.telemetrySamplingRate / HUNDRED) + RateBasedSampler(rumFeature.telemetrySamplingRate.percent()) ), firstPartyHostDetector = coreFeature.firstPartyHostDetector, cpuVitalMonitor = rumFeature.cpuVitalMonitor, diff --git a/dd-sdk-android/src/main/kotlin/com/datadog/android/rum/internal/domain/scope/RumSessionScope.kt b/dd-sdk-android/src/main/kotlin/com/datadog/android/rum/internal/domain/scope/RumSessionScope.kt index 9ceb125f75..1b7f12a120 100644 --- a/dd-sdk-android/src/main/kotlin/com/datadog/android/rum/internal/domain/scope/RumSessionScope.kt +++ b/dd-sdk-android/src/main/kotlin/com/datadog/android/rum/internal/domain/scope/RumSessionScope.kt @@ -10,7 +10,7 @@ import androidx.annotation.WorkerThread import com.datadog.android.core.internal.net.FirstPartyHostDetector import com.datadog.android.core.internal.system.BuildSdkVersionProvider import com.datadog.android.core.internal.system.DefaultBuildSdkVersionProvider -import com.datadog.android.core.internal.utils.HUNDRED +import com.datadog.android.core.internal.utils.percent import com.datadog.android.rum.RumSessionListener import com.datadog.android.rum.internal.RumFeature import com.datadog.android.rum.internal.domain.RumContext @@ -144,7 +144,7 @@ internal class RumSessionScope( } private fun renewSession(nanoTime: Long) { - val keepSession = (random.nextFloat() * HUNDRED) < samplingRate + val keepSession = random.nextFloat() < samplingRate.percent() sessionState = if (keepSession) State.TRACKED else State.NOT_TRACKED sessionId = UUID.randomUUID().toString() sessionStartNs.set(nanoTime) diff --git a/dd-sdk-android/src/main/kotlin/com/datadog/android/tracing/TracingInterceptor.kt b/dd-sdk-android/src/main/kotlin/com/datadog/android/tracing/TracingInterceptor.kt index bf1b270dfc..5db9419d2e 100644 --- a/dd-sdk-android/src/main/kotlin/com/datadog/android/tracing/TracingInterceptor.kt +++ b/dd-sdk-android/src/main/kotlin/com/datadog/android/tracing/TracingInterceptor.kt @@ -13,9 +13,9 @@ import com.datadog.android.core.configuration.Configuration import com.datadog.android.core.internal.net.FirstPartyHostDetector import com.datadog.android.core.internal.sampling.RateBasedSampler import com.datadog.android.core.internal.sampling.Sampler -import com.datadog.android.core.internal.utils.HUNDRED import com.datadog.android.core.internal.utils.devLogger import com.datadog.android.core.internal.utils.loggableStackTrace +import com.datadog.android.core.internal.utils.percent import com.datadog.android.core.internal.utils.sdkLogger import com.datadog.android.log.internal.utils.warningWithTelemetry import com.datadog.android.v2.core.DatadogCore @@ -105,7 +105,7 @@ internal constructor( tracedRequestListener, getGlobalFirstPartyHostDetector(), null, - RateBasedSampler(traceSamplingRate / HUNDRED), + RateBasedSampler(traceSamplingRate.percent()), { AndroidTracer.Builder().build() } ) @@ -126,7 +126,7 @@ internal constructor( tracedRequestListener, getGlobalFirstPartyHostDetector(), null, - RateBasedSampler(traceSamplingRate / HUNDRED), + RateBasedSampler(traceSamplingRate.percent()), { AndroidTracer.Builder().build() } ) diff --git a/dd-sdk-android/src/test/kotlin/com/datadog/android/core/internal/persistence/file/batch/BatchFileOrchestratorTest.kt b/dd-sdk-android/src/test/kotlin/com/datadog/android/core/internal/persistence/file/batch/BatchFileOrchestratorTest.kt index 3740eac8a7..7b873f2eae 100644 --- a/dd-sdk-android/src/test/kotlin/com/datadog/android/core/internal/persistence/file/batch/BatchFileOrchestratorTest.kt +++ b/dd-sdk-android/src/test/kotlin/com/datadog/android/core/internal/persistence/file/batch/BatchFileOrchestratorTest.kt @@ -340,7 +340,7 @@ internal class BatchFileOrchestratorTest { assumeTrue(fakeRootDir.listFiles().isNullOrEmpty()) var previousFile = testedOrchestrator.getWritableFile() - repeat(5) { + repeat(4) { checkNotNull(previousFile) val previousData = forge.aList(MAX_ITEM_PER_BATCH) { diff --git a/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/Constants.kt b/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/Constants.kt index 326b73971f..a1f24f11fe 100644 --- a/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/Constants.kt +++ b/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/Constants.kt @@ -7,6 +7,5 @@ package com.datadog.android.nightly.activities internal const val HUNDRED_PERCENT = 100f -internal const val DATA_SIZE = 200L -internal const val REPEATED_TESTS = 100 +internal const val FAKE_RESOURCE_DOWNLOADED_BYTES = 200L internal const val CRASH_DELAY_MS = 1000L diff --git a/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/ResourceTrackingTraceSamplingActivity.kt b/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/ResourceTrackingTraceSamplingActivity.kt index d9aff3a913..30764068d5 100644 --- a/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/ResourceTrackingTraceSamplingActivity.kt +++ b/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/ResourceTrackingTraceSamplingActivity.kt @@ -48,9 +48,9 @@ internal class ResourceTrackingTraceSamplingActivity : AppCompatActivity() { override fun onResume() { super.onResume() - val countDownLatch = CountDownLatch(REPEATED_TESTS) + val countDownLatch = CountDownLatch(REQUEST_COUNT) localServer.start { it.respond(HttpStatusCode.OK, "{}") } - repeat(REPEATED_TESTS) { + repeat(REQUEST_COUNT) { okHttpClient .newCall( Request.Builder() @@ -76,4 +76,8 @@ internal class ResourceTrackingTraceSamplingActivity : AppCompatActivity() { localServer.stop() super.onPause() } + + companion object { + internal const val REQUEST_COUNT = 100 + } } diff --git a/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/UserInteractionCustomTargetActivity.kt b/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/UserInteractionCustomTargetActivity.kt index acdc4215c9..202b67a511 100644 --- a/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/UserInteractionCustomTargetActivity.kt +++ b/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/UserInteractionCustomTargetActivity.kt @@ -29,7 +29,7 @@ internal class UserInteractionCustomTargetActivity : AppCompatActivity() { GlobalRum.get().stopResource( key, HttpURLConnection.HTTP_OK, - DATA_SIZE, + FAKE_RESOURCE_DOWNLOADED_BYTES, RumResourceKind.IMAGE, emptyMap() ) diff --git a/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/UserInteractionTrackingActivity.kt b/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/UserInteractionTrackingActivity.kt index 1a8463021a..b7cc7d94df 100644 --- a/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/UserInteractionTrackingActivity.kt +++ b/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/UserInteractionTrackingActivity.kt @@ -29,7 +29,7 @@ internal class UserInteractionTrackingActivity : AppCompatActivity() { GlobalRum.get().stopResource( key, HttpURLConnection.HTTP_OK, - DATA_SIZE, + FAKE_RESOURCE_DOWNLOADED_BYTES, RumResourceKind.IMAGE, emptyMap() ) diff --git a/library/dd-sdk-android-session-replay/clone_session_replay_schema.gradle.kts b/library/dd-sdk-android-session-replay/clone_session_replay_schema.gradle.kts index 1dbb9f3ee5..4ed50f9599 100644 --- a/library/dd-sdk-android-session-replay/clone_session_replay_schema.gradle.kts +++ b/library/dd-sdk-android-session-replay/clone_session_replay_schema.gradle.kts @@ -5,13 +5,13 @@ */ import com.datadog.gradle.plugin.gitclone.GitCloneDependenciesTask -val sessionReplayRepo = "https://github.com/DataDog/rum-events-format.git" -val sessionReplayVersion = "master" +val eventFormatRepo = "https://github.com/DataDog/rum-events-format.git" +val eventFormatVersion = "master" tasks.register("cloneSessionReplayRootSchemas") { extension.apply { clone( - sessionReplayRepo, + eventFormatRepo, "schemas/", destinationFolder = "src/main/json/schemas", excludedPrefixes = listOf( @@ -22,7 +22,7 @@ tasks.register("cloneSessionReplayRootSchemas") { "session-replay-schema", "session-replay-browser-schema" ), - ref = sessionReplayVersion + ref = eventFormatVersion ) } } @@ -30,10 +30,10 @@ tasks.register("cloneSessionReplayRootSchemas") { tasks.register("cloneSessionReplayMobileSchemas") { extension.apply { clone( - sessionReplayRepo, + eventFormatRepo, "schemas/session-replay/mobile", destinationFolder = "src/main/json/schemas/session-replay/mobile", - ref = sessionReplayVersion + ref = eventFormatVersion ) } } @@ -41,10 +41,10 @@ tasks.register("cloneSessionReplayMobileSchemas") { tasks.register("cloneSessionReplayCommonSchemas") { extension.apply { clone( - sessionReplayRepo, + eventFormatRepo, "schemas/session-replay/common", destinationFolder = "src/main/json/schemas/session-replay/common", - ref = sessionReplayVersion + ref = eventFormatVersion ) } } diff --git a/tools/unit/src/main/kotlin/com/datadog/tools/unit/ConditionWatcher.kt b/tools/unit/src/main/kotlin/com/datadog/tools/unit/ConditionWatcher.kt index bef95adc11..6d12e20830 100644 --- a/tools/unit/src/main/kotlin/com/datadog/tools/unit/ConditionWatcher.kt +++ b/tools/unit/src/main/kotlin/com/datadog/tools/unit/ConditionWatcher.kt @@ -63,7 +63,7 @@ class ConditionWatcher( } private fun reportTimeout(timeoutMs: Long, assertionError: AssertionError?) { - val message = "Waiting took more than $timeoutMs millisseconds. Test stopped." + val message = "Waiting took more than $timeoutMs milliseconds. Test stopped." if (assertionError == null) { throw TimeoutException(message) } else {