From e13ebab0d61927dbdb94d707fe048cdb88c75819 Mon Sep 17 00:00:00 2001 From: bidetofevil Date: Sun, 17 Nov 2024 22:01:57 -0800 Subject: [PATCH] Set log timestamp at time of logging if not specified --- .../internal/arch/destination/LogWriterImpl.kt | 6 ++++-- .../internal/injection/EssentialServiceModuleImpl.kt | 3 ++- .../android/embracesdk/internal/payload/LogMapper.kt | 2 +- .../internal/arch/destination/LogWriterImplTest.kt | 10 ++++++++-- .../internal/ndk/NativeCrashDataSourceImplTest.kt | 7 ++++++- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/embrace-android-core/src/main/kotlin/io/embrace/android/embracesdk/internal/arch/destination/LogWriterImpl.kt b/embrace-android-core/src/main/kotlin/io/embrace/android/embracesdk/internal/arch/destination/LogWriterImpl.kt index efd785333e..c02cfec26c 100644 --- a/embrace-android-core/src/main/kotlin/io/embrace/android/embracesdk/internal/arch/destination/LogWriterImpl.kt +++ b/embrace-android-core/src/main/kotlin/io/embrace/android/embracesdk/internal/arch/destination/LogWriterImpl.kt @@ -2,6 +2,7 @@ package io.embrace.android.embracesdk.internal.arch.destination import io.embrace.android.embracesdk.internal.arch.schema.PrivateSpan import io.embrace.android.embracesdk.internal.arch.schema.SchemaType +import io.embrace.android.embracesdk.internal.clock.Clock import io.embrace.android.embracesdk.internal.opentelemetry.embState import io.embrace.android.embracesdk.internal.session.id.SessionIdTracker import io.embrace.android.embracesdk.internal.session.lifecycle.EmbraceProcessStateService.Companion.BACKGROUND_STATE @@ -21,6 +22,7 @@ class LogWriterImpl( private val logger: Logger, private val sessionIdTracker: SessionIdTracker, private val processStateService: ProcessStateService, + private val clock: Clock ) : LogWriter { override fun addLog( @@ -31,12 +33,12 @@ class LogWriterImpl( addCurrentSessionInfo: Boolean, timestampMs: Long?, ) { + val logTimeMs = timestampMs ?: clock.now() val builder = logger.logRecordBuilder() .setBody(message) .setSeverity(severity) .setSeverityText(getSeverityText(severity)) - - timestampMs?.let { ts -> builder.setTimestamp(ts, TimeUnit.MILLISECONDS) } + .setTimestamp(logTimeMs, TimeUnit.MILLISECONDS) builder.setAttribute(LogIncubatingAttributes.LOG_RECORD_UID, Uuid.getEmbUuid()) diff --git a/embrace-android-core/src/main/kotlin/io/embrace/android/embracesdk/internal/injection/EssentialServiceModuleImpl.kt b/embrace-android-core/src/main/kotlin/io/embrace/android/embracesdk/internal/injection/EssentialServiceModuleImpl.kt index f3aa4aec4a..26ec3c0e32 100644 --- a/embrace-android-core/src/main/kotlin/io/embrace/android/embracesdk/internal/injection/EssentialServiceModuleImpl.kt +++ b/embrace-android-core/src/main/kotlin/io/embrace/android/embracesdk/internal/injection/EssentialServiceModuleImpl.kt @@ -147,7 +147,8 @@ class EssentialServiceModuleImpl( LogWriterImpl( logger = openTelemetryModule.logger, sessionIdTracker = sessionIdTracker, - processStateService = processStateService + processStateService = processStateService, + clock = initModule.clock, ) } } diff --git a/embrace-android-core/src/main/kotlin/io/embrace/android/embracesdk/internal/payload/LogMapper.kt b/embrace-android-core/src/main/kotlin/io/embrace/android/embracesdk/internal/payload/LogMapper.kt index b4d30e8ab9..5a33176baa 100644 --- a/embrace-android-core/src/main/kotlin/io/embrace/android/embracesdk/internal/payload/LogMapper.kt +++ b/embrace-android-core/src/main/kotlin/io/embrace/android/embracesdk/internal/payload/LogMapper.kt @@ -9,7 +9,7 @@ fun LogRecordData.toNewPayload(): Log { return Log( traceId = if (isSpanContextValid) spanContext.traceId else null, spanId = if (isSpanContextValid) spanContext.spanId else null, - timeUnixNano = observedTimestampEpochNanos, + timeUnixNano = timestampEpochNanos, severityNumber = severity.severityNumber, severityText = severityText, body = body.asString(), diff --git a/embrace-android-core/src/test/java/io/embrace/android/embracesdk/internal/arch/destination/LogWriterImplTest.kt b/embrace-android-core/src/test/java/io/embrace/android/embracesdk/internal/arch/destination/LogWriterImplTest.kt index d41898bf12..39c246a7c7 100644 --- a/embrace-android-core/src/test/java/io/embrace/android/embracesdk/internal/arch/destination/LogWriterImplTest.kt +++ b/embrace-android-core/src/test/java/io/embrace/android/embracesdk/internal/arch/destination/LogWriterImplTest.kt @@ -1,6 +1,7 @@ package io.embrace.android.embracesdk.internal.arch.destination import io.embrace.android.embracesdk.assertions.findAttributeValue +import io.embrace.android.embracesdk.fakes.FakeClock import io.embrace.android.embracesdk.fakes.FakeClock.Companion.DEFAULT_FAKE_CURRENT_TIME import io.embrace.android.embracesdk.fakes.FakeConfigService import io.embrace.android.embracesdk.fakes.FakeOpenTelemetryLogger @@ -30,16 +31,19 @@ internal class LogWriterImplTest { private lateinit var sessionIdTracker: FakeSessionIdTracker private lateinit var logWriterImpl: LogWriterImpl private lateinit var processStateService: FakeProcessStateService + private lateinit var clock: FakeClock @Before fun setup() { sessionIdTracker = FakeSessionIdTracker() logger = FakeOpenTelemetryLogger() processStateService = FakeProcessStateService() + clock = FakeClock() logWriterImpl = LogWriterImpl( logger = logger, sessionIdTracker = sessionIdTracker, processStateService = processStateService, + clock = clock, ) } @@ -64,7 +68,8 @@ internal class LogWriterImplTest { assertNotNull(attributes.getAttribute(embState)) assertNotNull(attributes.getAttribute(LogIncubatingAttributes.LOG_RECORD_UID)) assertTrue(attributes.hasFixedAttribute(PrivateSpan)) - assertEquals(0, timestampEpochNanos) + assertEquals(clock.nowInNanos(), timestampEpochNanos) + assertEquals(0, observedTimestampEpochNanos) } } @@ -156,7 +161,8 @@ internal class LogWriterImplTest { ) with(logger.builders.last()) { - assertEquals(timestampEpochNanos, fakeTimeMs.millisToNanos()) + assertEquals(fakeTimeMs.millisToNanos(), timestampEpochNanos) + assertEquals(0, observedTimestampEpochNanos) } } diff --git a/embrace-android-features/src/test/java/io/embrace/android/embracesdk/internal/ndk/NativeCrashDataSourceImplTest.kt b/embrace-android-features/src/test/java/io/embrace/android/embracesdk/internal/ndk/NativeCrashDataSourceImplTest.kt index a8a3d46968..6965feab01 100644 --- a/embrace-android-features/src/test/java/io/embrace/android/embracesdk/internal/ndk/NativeCrashDataSourceImplTest.kt +++ b/embrace-android-features/src/test/java/io/embrace/android/embracesdk/internal/ndk/NativeCrashDataSourceImplTest.kt @@ -1,5 +1,6 @@ package io.embrace.android.embracesdk.internal.ndk +import io.embrace.android.embracesdk.fakes.FakeClock import io.embrace.android.embracesdk.fakes.FakeConfigService import io.embrace.android.embracesdk.fakes.FakeMetadataService import io.embrace.android.embracesdk.fakes.FakeNdkService @@ -53,6 +54,7 @@ internal class NativeCrashDataSourceImplTest { private lateinit var metadataService: FakeMetadataService private lateinit var processStateService: FakeProcessStateService private lateinit var nativeCrashDataSource: NativeCrashDataSourceImpl + private lateinit var clock: FakeClock @Before fun setUp() { @@ -63,11 +65,13 @@ internal class NativeCrashDataSourceImplTest { sessionIdTracker = FakeSessionIdTracker().apply { setActiveSession("currentSessionId", true) } metadataService = FakeMetadataService() processStateService = FakeProcessStateService() + clock = FakeClock() otelLogger = FakeOpenTelemetryLogger() logWriter = LogWriterImpl( sessionIdTracker = sessionIdTracker, processStateService = processStateService, - logger = otelLogger + logger = otelLogger, + clock = clock ) configService = FakeConfigService() serializer = EmbraceSerializer() @@ -90,6 +94,7 @@ internal class NativeCrashDataSourceImplTest { with(otelLogger.builders.single()) { assertEquals(1, emitCalled) assertEquals(testNativeCrashData.timestamp, timestampEpochNanos.nanosToMillis()) + assertEquals(0, observedTimestampEpochNanos.nanosToMillis()) assertEquals(testNativeCrashData.appState, attributes.getAttribute(embState)) assertTrue(attributes.hasFixedAttribute(EmbType.System.NativeCrash)) assertNotNull(attributes.getAttribute(LogIncubatingAttributes.LOG_RECORD_UID))