Skip to content

Commit

Permalink
Set log timestamp at time of logging if not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
bidetofevil committed Nov 18, 2024
1 parent eebbae2 commit e13ebab
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ class EssentialServiceModuleImpl(
LogWriterImpl(
logger = openTelemetryModule.logger,
sessionIdTracker = sessionIdTracker,
processStateService = processStateService
processStateService = processStateService,
clock = initModule.clock,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
)
}

Expand All @@ -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)
}
}

Expand Down Expand Up @@ -156,7 +161,8 @@ internal class LogWriterImplTest {
)

with(logger.builders.last()) {
assertEquals(timestampEpochNanos, fakeTimeMs.millisToNanos())
assertEquals(fakeTimeMs.millisToNanos(), timestampEpochNanos)
assertEquals(0, observedTimestampEpochNanos)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -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()
Expand All @@ -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))
Expand Down

0 comments on commit e13ebab

Please sign in to comment.