-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add otel log data source for internal errors
- Loading branch information
1 parent
c74d8cc
commit d4e6744
Showing
7 changed files
with
126 additions
and
2 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
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
5 changes: 5 additions & 0 deletions
5
...ain/java/io/embrace/android/embracesdk/capture/internal/errors/InternalErrorDataSource.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,5 @@ | ||
package io.embrace.android.embracesdk.capture.internal.errors | ||
|
||
import io.embrace.android.embracesdk.arch.datasource.LogDataSource | ||
|
||
internal interface InternalErrorDataSource : LogDataSource, InternalErrorHandler |
33 changes: 33 additions & 0 deletions
33
...java/io/embrace/android/embracesdk/capture/internal/errors/InternalErrorDataSourceImpl.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,33 @@ | ||
package io.embrace.android.embracesdk.capture.internal.errors | ||
|
||
import io.embrace.android.embracesdk.Severity | ||
import io.embrace.android.embracesdk.arch.datasource.LogDataSourceImpl | ||
import io.embrace.android.embracesdk.arch.datasource.NoInputValidation | ||
import io.embrace.android.embracesdk.arch.destination.LogEventData | ||
import io.embrace.android.embracesdk.arch.destination.LogWriter | ||
import io.embrace.android.embracesdk.arch.limits.UpToLimitStrategy | ||
import io.embrace.android.embracesdk.arch.schema.SchemaType | ||
import io.embrace.android.embracesdk.logging.EmbLogger | ||
|
||
/** | ||
* Tracks internal errors & sends them as OTel logs. | ||
*/ | ||
internal class InternalErrorDataSourceImpl( | ||
logWriter: LogWriter, | ||
logger: EmbLogger, | ||
) : InternalErrorDataSource, | ||
LogDataSourceImpl( | ||
destination = logWriter, | ||
logger = logger, | ||
limitStrategy = UpToLimitStrategy { 10 }, | ||
) { | ||
|
||
override fun handleInternalError(throwable: Throwable) { | ||
alterSessionSpan(NoInputValidation) { | ||
this.addLog(throwable, true) { | ||
val schemaType = SchemaType.InternalError(throwable) | ||
LogEventData(schemaType, Severity.ERROR, "") | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...c/main/java/io/embrace/android/embracesdk/capture/internal/errors/InternalErrorHandler.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,5 @@ | ||
package io.embrace.android.embracesdk.capture.internal.errors | ||
|
||
internal interface InternalErrorHandler { | ||
fun handleInternalError(throwable: Throwable) | ||
} |
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
69 changes: 69 additions & 0 deletions
69
.../test/java/io/embrace/android/embracesdk/capture/crash/InternalErrorDataSourceImplTest.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,69 @@ | ||
package io.embrace.android.embracesdk.capture.crash | ||
|
||
import io.embrace.android.embracesdk.Severity | ||
import io.embrace.android.embracesdk.arch.destination.LogEventData | ||
import io.embrace.android.embracesdk.arch.schema.EmbType | ||
import io.embrace.android.embracesdk.capture.internal.errors.InternalErrorDataSourceImpl | ||
import io.embrace.android.embracesdk.fakes.FakeLogWriter | ||
import io.embrace.android.embracesdk.logging.EmbLogger | ||
import io.embrace.android.embracesdk.logging.EmbLoggerImpl | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNotNull | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
internal class InternalErrorDataSourceImplTest { | ||
|
||
private lateinit var dataSource: InternalErrorDataSourceImpl | ||
private lateinit var logWriter: FakeLogWriter | ||
private lateinit var logger: EmbLogger | ||
|
||
@Before | ||
fun setUp() { | ||
logWriter = FakeLogWriter() | ||
logger = EmbLoggerImpl() | ||
dataSource = InternalErrorDataSourceImpl( | ||
logWriter, | ||
logger | ||
) | ||
} | ||
|
||
@Test | ||
fun `handle throwable with no message`() { | ||
dataSource.handleInternalError(IllegalStateException()) | ||
val data = logWriter.logEvents.single() | ||
val attrs = assertInternalErrorLogged(data) | ||
assertEquals("java.lang.IllegalStateException", attrs["exception.type"]) | ||
assertEquals("", attrs["exception.message"]) | ||
assertNotNull(attrs["exception.stacktrace"]) | ||
} | ||
|
||
@Test | ||
fun `handle throwable with message`() { | ||
dataSource.handleInternalError(IllegalArgumentException("Whoops!")) | ||
val data = logWriter.logEvents.single() | ||
val attrs = assertInternalErrorLogged(data) | ||
assertEquals("java.lang.IllegalArgumentException", attrs["exception.type"]) | ||
assertEquals("Whoops!", attrs["exception.message"]) | ||
assertNotNull(attrs["exception.stacktrace"]) | ||
} | ||
|
||
@Test | ||
fun `limit not exceeded`() { | ||
repeat(15) { | ||
dataSource.handleInternalError(IllegalStateException()) | ||
} | ||
assertEquals(10, logWriter.logEvents.size) | ||
} | ||
|
||
private fun assertInternalErrorLogged(data: LogEventData): Map<String, String> { | ||
assertEquals(Severity.ERROR, data.severity) | ||
assertEquals("", data.message) | ||
assertEquals(EmbType.System.InternalError, data.schemaType.telemetryType) | ||
assertEquals("internal-error", data.schemaType.fixedObjectName) | ||
|
||
val attrs = data.schemaType.attributes() | ||
assertEquals("sys.internal", attrs["emb.type"]) | ||
return attrs | ||
} | ||
} |