Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix message when writer is NoOp #1963

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.datadog.android.api.context.DatadogContext
import com.datadog.android.api.feature.Feature
import com.datadog.android.api.feature.FeatureSdkCore
import com.datadog.android.api.storage.DataWriter
import com.datadog.android.api.storage.NoOpDataWriter
import com.datadog.android.rum.GlobalRumMonitor
import com.datadog.android.rum.internal.monitor.AdvancedRumMonitor

Expand Down Expand Up @@ -44,20 +45,29 @@ internal class WriteOperation(
fun submit() {
sdkCore.getFeature(Feature.RUM_FEATURE_NAME)
?.withWriteContext { datadogContext, eventBatchWriter ->
try {
val event = eventSource(datadogContext)
if (rumDataWriter is NoOpDataWriter) {
sdkCore.internalLogger.log(
level = InternalLogger.Level.INFO,
target = InternalLogger.Target.USER,
messageBuilder = { WRITE_OPERATION_IGNORED }
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: we probably should also call onSuccess/onError here (don't know which one makes more sense), because at least for the RumViewScope until sum(pending) == 0 it won't be marked as complete and thus garbage collected. We may have the case when there is NoOpDataWriter here and RUM pipeline working if session is not tracked, I believe.

advancedRumMonitor?.let { onError(it) }
} else {
try {
val event = eventSource(datadogContext)

@Suppress("ThreadSafety") // called in a worker thread context
val isSuccess = rumDataWriter.write(eventBatchWriter, event)
if (isSuccess) {
advancedRumMonitor?.let {
onSuccess(it)
@Suppress("ThreadSafety") // called in a worker thread context
val isSuccess = rumDataWriter.write(eventBatchWriter, event)
if (isSuccess) {
advancedRumMonitor?.let {
onSuccess(it)
}
} else {
notifyEventWriteFailure()
}
} else {
notifyEventWriteFailure()
} catch (@Suppress("TooGenericExceptionCaught") e: Exception) {
notifyEventWriteFailure(e)
}
} catch (@Suppress("TooGenericExceptionCaught") e: Exception) {
notifyEventWriteFailure(e)
}
}
}
Expand Down Expand Up @@ -88,6 +98,7 @@ internal class WriteOperation(
}

internal companion object {
const val WRITE_OPERATION_IGNORED = "Write operation ignored, session is expired or RUM feature is disabled."
const val WRITE_OPERATION_FAILED_ERROR = "Write operation failed."
const val NO_ERROR_CALLBACK_PROVIDED_WARNING =
"Write operation failed, but no onError callback was provided."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.datadog.android.api.feature.Feature
import com.datadog.android.api.feature.FeatureScope
import com.datadog.android.api.storage.DataWriter
import com.datadog.android.api.storage.EventBatchWriter
import com.datadog.android.api.storage.NoOpDataWriter
import com.datadog.android.rum.utils.config.GlobalRumMonitorTestConfiguration
import com.datadog.android.rum.utils.forge.Configurator
import com.datadog.tools.unit.annotations.TestConfigurationsProvider
Expand All @@ -22,6 +23,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.api.extension.Extensions
import org.mockito.Mock
import org.mockito.Mockito.mock
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.junit.jupiter.MockitoSettings
import org.mockito.kotlin.any
Expand Down Expand Up @@ -218,6 +220,31 @@ internal class SdkCoreExtTest {
)
}

@Test
fun `𝕄 do nothing 𝕎 submit() { noop writer}`() {
// Given
var errorInvoked = false
var successInvoked = false
val fakeEvent = Any()
mockWriter = mock<NoOpDataWriter<Any>>()

// When
mockSdkCore.newRumEventWriteOperation(mockWriter) { fakeEvent }
.onError { errorInvoked = true }
.onSuccess { successInvoked = true }
.submit()

// Then
mockInternalLogger.verifyLog(
level = InternalLogger.Level.INFO,
target = InternalLogger.Target.USER,
message = WriteOperation.WRITE_OPERATION_IGNORED
)
assertThat(errorInvoked).isTrue()
assertThat(successInvoked).isFalse()
verifyNoInteractions(mockWriter)
}

companion object {
val rumMonitor = GlobalRumMonitorTestConfiguration()

Expand Down