-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RUMM-2172 Implement SDKv2 SingleStorage
- Loading branch information
Showing
16 changed files
with
1,253 additions
and
334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
dd-sdk-android/src/main/kotlin/com/datadog/android/v2/api/BatchWriterListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* 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.v2.api | ||
|
||
/** | ||
* A Listener to be notified when an event is actually written in the storage, | ||
* or when a write operation failed. | ||
*/ | ||
internal interface BatchWriterListener { | ||
/** | ||
* Called whenever data is written successfully. | ||
* @param eventId the id of the written event | ||
*/ | ||
fun onDataWritten(eventId: String) | ||
|
||
/** | ||
* Called whenever data failed to be written. | ||
* @param eventId the id of the event that failed | ||
*/ | ||
fun onDataWriteFailed(eventId: String) | ||
} |
31 changes: 31 additions & 0 deletions
31
dd-sdk-android/src/main/kotlin/com/datadog/android/v2/api/EventBatchWriter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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.v2.api | ||
|
||
/** | ||
* Utility allowing [FeatureScope] to write events in the storage to be uploaded asynchronously. | ||
*/ | ||
interface EventBatchWriter { | ||
|
||
/** | ||
* @return the metadata of the current writeable file | ||
*/ | ||
fun currentMetadata(): ByteArray? | ||
|
||
/** | ||
* Writes the content of the event to the current available batch. | ||
* @param event the event to write | ||
* @param eventId a unique identifier for the event (used to identify events in | ||
* the [BatchWriterListener] callbacks). | ||
* @param newMetadata the updated metadata | ||
*/ | ||
fun write( | ||
event: ByteArray, | ||
eventId: String, | ||
newMetadata: ByteArray | ||
) | ||
} |
51 changes: 51 additions & 0 deletions
51
dd-sdk-android/src/main/kotlin/com/datadog/android/v2/api/InternalLogger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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.v2.api | ||
|
||
import com.datadog.tools.annotation.NoOpImplementation | ||
|
||
/** | ||
* A Logger used to log messages from the internal implementation of the Datadog SDKs. | ||
*/ | ||
@NoOpImplementation | ||
interface InternalLogger { | ||
|
||
/** | ||
* The severity level of a logged message. | ||
*/ | ||
enum class Level { | ||
DEBUG, | ||
INFO, | ||
WARN, | ||
ERROR; | ||
} | ||
|
||
/** | ||
* The target handler for a log message. | ||
*/ | ||
enum class Target { | ||
USER, | ||
MAINTAINER, | ||
TELEMETRY | ||
} | ||
|
||
/** | ||
* Logs a message from the internal implementation. | ||
* @param level the severity level of the log | ||
* @param target the target handler for the log | ||
* @param message the log message | ||
* @param error an optional throwable error | ||
* @param attributes an optional map of custom attributes | ||
*/ | ||
fun log( | ||
level: Level, | ||
target: Target, | ||
message: String, | ||
error: Throwable?, | ||
attributes: Map<String, Any> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...android/src/main/kotlin/com/datadog/android/v2/core/internal/storage/BatchConfirmation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* 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.v2.core.internal.storage | ||
|
||
internal fun interface BatchConfirmation { | ||
fun markAsRead(deleteBatch: Boolean) | ||
} |
29 changes: 29 additions & 0 deletions
29
dd-sdk-android/src/main/kotlin/com/datadog/android/v2/core/internal/storage/BatchId.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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.v2.core.internal.storage | ||
|
||
import java.io.File | ||
|
||
internal data class BatchId( | ||
val id: String | ||
) { | ||
|
||
fun matchesFile(file: File): Boolean { | ||
return file.extractFileId() == id | ||
} | ||
|
||
companion object { | ||
|
||
fun fromFile(file: File): BatchId { | ||
return BatchId(file.extractFileId()) | ||
} | ||
|
||
private fun File.extractFileId(): String { | ||
return absolutePath | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
dd-sdk-android/src/main/kotlin/com/datadog/android/v2/core/internal/storage/BatchReader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* 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.v2.core.internal.storage | ||
|
||
internal fun interface BatchReader { | ||
|
||
fun read(batchId: BatchId): List<ByteArray> | ||
} |
11 changes: 11 additions & 0 deletions
11
dd-sdk-android/src/main/kotlin/com/datadog/android/v2/core/internal/storage/BatchWriter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* 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.v2.core.internal.storage | ||
|
||
import com.datadog.android.v2.api.EventBatchWriter | ||
|
||
internal interface BatchWriter : EventBatchWriter |
111 changes: 111 additions & 0 deletions
111
...droid/src/main/kotlin/com/datadog/android/v2/core/internal/storage/ConsentAwareStorage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* 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.v2.core.internal.storage | ||
|
||
import com.datadog.android.core.internal.persistence.file.FileHandler | ||
import com.datadog.android.core.internal.persistence.file.FileOrchestrator | ||
import com.datadog.android.privacy.TrackingConsent | ||
import com.datadog.android.v2.api.BatchWriterListener | ||
import com.datadog.android.v2.api.InternalLogger | ||
import com.datadog.android.v2.api.context.DatadogContext | ||
import java.io.File | ||
import java.util.Locale | ||
|
||
internal class ConsentAwareStorage( | ||
private val grantedOrchestrator: FileOrchestrator, | ||
private val pendingOrchestrator: FileOrchestrator, | ||
private val handler: FileHandler, | ||
private val listener: BatchWriterListener, | ||
private val internalLogger: InternalLogger | ||
) : Storage { | ||
|
||
/** | ||
* Keeps track of files currently being read. | ||
*/ | ||
private val lockedFiles: MutableSet<File> = mutableSetOf() | ||
|
||
/** @inheritdoc */ | ||
override fun writeCurrentBatch( | ||
datadogContext: DatadogContext, | ||
callback: (BatchWriter) -> Unit | ||
) { | ||
val orchestrator = when (datadogContext.trackingConsent) { | ||
TrackingConsent.GRANTED -> grantedOrchestrator | ||
TrackingConsent.PENDING -> pendingOrchestrator | ||
TrackingConsent.NOT_GRANTED -> null | ||
} | ||
|
||
val writer = object : BatchWriter { | ||
override fun currentMetadata(): ByteArray? { | ||
// TODO RUMM-2186 handle writing/updating batch metadata in separate file | ||
return null | ||
} | ||
|
||
override fun write(event: ByteArray, eventId: String, newMetadata: ByteArray) { | ||
// prevent useless operation for empty event / null orchestrator | ||
if (event.isEmpty() || orchestrator == null) { | ||
listener.onDataWritten(eventId) | ||
return | ||
} | ||
|
||
val file = orchestrator.getWritableFile(event.size) | ||
|
||
if (file != null && handler.writeData(file, event, true)) { | ||
listener.onDataWritten(eventId) | ||
} else { | ||
listener.onDataWriteFailed(eventId) | ||
} | ||
} | ||
} | ||
callback.invoke(writer) | ||
} | ||
|
||
/** @inheritdoc */ | ||
override fun readNextBatch( | ||
datadogContext: DatadogContext, | ||
callback: (BatchId, BatchReader) -> Unit | ||
) { | ||
val batchFile = synchronized(lockedFiles) { | ||
grantedOrchestrator.getReadableFile(lockedFiles)?.also { | ||
lockedFiles.add(it) | ||
} ?: return | ||
} | ||
|
||
val batchId = BatchId.fromFile(batchFile) | ||
val reader = BatchReader { handler.readData(batchFile) } | ||
callback(batchId, reader) | ||
} | ||
|
||
/** @inheritdoc */ | ||
override fun confirmBatchRead(batchId: BatchId, callback: (BatchConfirmation) -> Unit) { | ||
val batchFile = synchronized(lockedFiles) { | ||
lockedFiles.firstOrNull { batchId.matchesFile(it) } | ||
} ?: return | ||
val confirmation = BatchConfirmation { delete -> | ||
if (delete) { | ||
val result = handler.delete(batchFile) | ||
if (!result) { | ||
internalLogger.log( | ||
InternalLogger.Level.WARN, | ||
InternalLogger.Target.MAINTAINER, | ||
WARNING_DELETE_FAILED.format(Locale.US, batchFile.path), | ||
null, | ||
emptyMap() | ||
) | ||
} | ||
} | ||
synchronized(lockedFiles) { | ||
lockedFiles.remove(batchFile) | ||
} | ||
} | ||
callback(confirmation) | ||
} | ||
|
||
companion object { | ||
internal const val WARNING_DELETE_FAILED = "Unable to delete file: %s" | ||
} | ||
} |
Oops, something went wrong.