-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #487 from embrace-io/add-custom-breadcrumb-data-so…
…urce Add custom breadcrumb data source
- Loading branch information
Showing
4 changed files
with
121 additions
and
20 deletions.
There are no files selected for viewing
54 changes: 35 additions & 19 deletions
54
.../src/main/java/io/embrace/android/embracesdk/capture/crumbs/CustomBreadcrumbDataSource.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 |
---|---|---|
@@ -1,32 +1,48 @@ | ||
package io.embrace.android.embracesdk.capture.crumbs | ||
|
||
import android.text.TextUtils | ||
import io.embrace.android.embracesdk.arch.DataCaptureService | ||
import io.embrace.android.embracesdk.arch.datasource.DataSourceImpl | ||
import io.embrace.android.embracesdk.arch.destination.SessionSpanWriter | ||
import io.embrace.android.embracesdk.arch.destination.SpanEventData | ||
import io.embrace.android.embracesdk.arch.destination.SpanEventMapper | ||
import io.embrace.android.embracesdk.arch.limits.UpToLimitStrategy | ||
import io.embrace.android.embracesdk.config.ConfigService | ||
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger | ||
import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger | ||
import io.embrace.android.embracesdk.internal.clock.millisToNanos | ||
import io.embrace.android.embracesdk.payload.CustomBreadcrumb | ||
|
||
/** | ||
* Captures custom breadcrumbs. | ||
*/ | ||
internal class CustomBreadcrumbDataSource( | ||
private val configService: ConfigService, | ||
private val store: BreadcrumbDataStore<CustomBreadcrumb> = BreadcrumbDataStore { | ||
configService.breadcrumbBehavior.getCustomBreadcrumbLimit() | ||
}, | ||
private val logger: InternalEmbraceLogger = InternalStaticEmbraceLogger.logger | ||
) : DataCaptureService<List<CustomBreadcrumb>> by store { | ||
configService: ConfigService, | ||
writer: SessionSpanWriter | ||
) : DataSourceImpl<SessionSpanWriter>( | ||
destination = writer, | ||
limitStrategy = UpToLimitStrategy(configService.breadcrumbBehavior::getCustomBreadcrumbLimit) | ||
), | ||
SpanEventMapper<CustomBreadcrumb> { | ||
|
||
companion object { | ||
internal const val TYPE_NAME = "system.breadcrumb" | ||
internal const val EVENT_NAME = "custom-breadcrumb" | ||
internal const val ATTR_KEY_MESSAGE = "message" | ||
} | ||
|
||
fun logCustom(message: String, timestamp: Long) { | ||
if (TextUtils.isEmpty(message)) { | ||
logger.logWarning("Breadcrumb message must not be blank") | ||
return | ||
} | ||
try { | ||
store.tryAddBreadcrumb(CustomBreadcrumb(message, timestamp)) | ||
} catch (ex: Exception) { | ||
logger.logError("Failed to log custom breadcrumb with message $message", ex) | ||
} | ||
alterSessionSpan( | ||
inputValidation = { | ||
message.isNotEmpty() | ||
}, | ||
captureAction = { | ||
val crumb = CustomBreadcrumb(message, timestamp) | ||
addEvent(crumb, ::toSpanEventData) | ||
} | ||
) | ||
} | ||
|
||
override fun toSpanEventData(obj: CustomBreadcrumb) = SpanEventData( | ||
TYPE_NAME, | ||
EVENT_NAME, | ||
obj.timestamp.millisToNanos(), | ||
mapOf("message" to (obj.message ?: "")) | ||
) | ||
} |
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
32 changes: 32 additions & 0 deletions
32
...ain/java/io/embrace/android/embracesdk/capture/crumbs/LegacyCustomBreadcrumbDataSource.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,32 @@ | ||
package io.embrace.android.embracesdk.capture.crumbs | ||
|
||
import android.text.TextUtils | ||
import io.embrace.android.embracesdk.arch.DataCaptureService | ||
import io.embrace.android.embracesdk.config.ConfigService | ||
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger | ||
import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger | ||
import io.embrace.android.embracesdk.payload.CustomBreadcrumb | ||
|
||
/** | ||
* Captures custom breadcrumbs. | ||
*/ | ||
internal class LegacyCustomBreadcrumbDataSource( | ||
private val configService: ConfigService, | ||
private val store: BreadcrumbDataStore<CustomBreadcrumb> = BreadcrumbDataStore { | ||
configService.breadcrumbBehavior.getCustomBreadcrumbLimit() | ||
}, | ||
private val logger: InternalEmbraceLogger = InternalStaticEmbraceLogger.logger | ||
) : DataCaptureService<List<CustomBreadcrumb>> by store { | ||
|
||
fun logCustom(message: String, timestamp: Long) { | ||
if (TextUtils.isEmpty(message)) { | ||
logger.logWarning("Breadcrumb message must not be blank") | ||
return | ||
} | ||
try { | ||
store.tryAddBreadcrumb(CustomBreadcrumb(message, timestamp)) | ||
} catch (ex: Exception) { | ||
logger.logError("Failed to log custom breadcrumb with message $message", ex) | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
.../test/java/io/embrace/android/embracesdk/capture/crumbs/CustomBreadcrumbDataSourceTest.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,53 @@ | ||
package io.embrace.android.embracesdk.capture.crumbs | ||
|
||
import io.embrace.android.embracesdk.fakes.FakeConfigService | ||
import io.embrace.android.embracesdk.fakes.FakeCurrentSessionSpan | ||
import io.embrace.android.embracesdk.internal.clock.millisToNanos | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
internal class CustomBreadcrumbDataSourceTest { | ||
|
||
private lateinit var source: CustomBreadcrumbDataSource | ||
private lateinit var writer: FakeCurrentSessionSpan | ||
|
||
@Before | ||
fun setUp() { | ||
writer = FakeCurrentSessionSpan() | ||
source = CustomBreadcrumbDataSource( | ||
FakeConfigService(), | ||
writer | ||
) | ||
} | ||
|
||
@Test | ||
fun `add invalid breadcrumb`() { | ||
source.logCustom("", 0) | ||
assertEquals(0, writer.addedEvents.size) | ||
} | ||
|
||
@Test | ||
fun `add breadcrumb`() { | ||
source.logCustom("Hello, world!", 15000000000) | ||
with(writer.addedEvents.single()) { | ||
assertEquals("custom-breadcrumb", spanName) | ||
assertEquals(15000000000.millisToNanos(), spanStartTimeMs) | ||
assertEquals( | ||
mapOf( | ||
"emb.type" to "system.breadcrumb", | ||
"message" to "Hello, world!" | ||
), | ||
attributes | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun `limit not exceeded`() { | ||
repeat(150) { k -> | ||
source.logCustom("Crumb #$k", 15000000000) | ||
} | ||
assertEquals(100, writer.addedEvents.size) | ||
} | ||
} |