Skip to content

Commit

Permalink
Add log.record.uid to all logs generated (#785)
Browse files Browse the repository at this point in the history
## Goal

Add `log.record.uid` to every OTel log written that uses `LogWriterImpl`, similar to how we add other common attributes.

Kept the existing logic in JVM crashes and Embrace logs because of some extra things they are doing - limiting the size of the change and will consolidate that later.

## Testing
Add test to LogWriterImpl
  • Loading branch information
bidetofevil authored Apr 19, 2024
2 parents 7cc639a + 55dfc57 commit c8c4d2b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package io.embrace.android.embracesdk.arch.destination

import io.embrace.android.embracesdk.capture.metadata.MetadataService
import io.embrace.android.embracesdk.internal.spans.toOtelSeverity
import io.embrace.android.embracesdk.internal.utils.Uuid
import io.embrace.android.embracesdk.opentelemetry.embSessionId
import io.embrace.android.embracesdk.opentelemetry.embState
import io.embrace.android.embracesdk.opentelemetry.logRecordUid
import io.embrace.android.embracesdk.session.id.SessionIdTracker
import io.opentelemetry.api.common.AttributeKey
import io.opentelemetry.api.logs.Logger
Expand All @@ -28,6 +30,8 @@ internal class LogWriterImpl(
.setSeverity(logEventData.severity.toOtelSeverity())
.setSeverityText(logEventData.severity.name)

builder.setAttribute(logRecordUid, Uuid.getEmbUuid())

sessionIdTracker.getActiveSessionId()?.let { sessionId ->
builder.setAttribute(embSessionId.attributeKey, sessionId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import io.embrace.android.embracesdk.fakes.FakeConfigService
import io.embrace.android.embracesdk.fakes.FakeMetadataService
import io.embrace.android.embracesdk.fakes.FakeOpenTelemetryLogger
import io.embrace.android.embracesdk.fakes.FakeSessionIdTracker
import io.embrace.android.embracesdk.internal.spans.getAttribute
import io.embrace.android.embracesdk.internal.spans.hasFixedAttribute
import io.embrace.android.embracesdk.opentelemetry.embSessionId
import io.embrace.android.embracesdk.opentelemetry.embState
import io.embrace.android.embracesdk.opentelemetry.logRecordUid
import io.embrace.android.embracesdk.payload.AppExitInfoData
import io.embrace.android.embracesdk.session.id.SessionIdTracker
import io.opentelemetry.api.logs.Severity
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -72,7 +76,9 @@ internal class LogWriterImplTest {
assertEquals("test", body)
assertEquals(Severity.ERROR, severity)
assertEquals(Severity.ERROR.name, severity.name)
assertEquals("session-id", attributes[embSessionId.name])
assertEquals("session-id", attributes.getAttribute(embSessionId))
assertNotNull(attributes.getAttribute(embState))
assertNotNull(attributes.getAttribute(logRecordUid))
assertTrue(attributes.hasFixedAttribute(PrivateSpan))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import io.embrace.android.embracesdk.internal.utils.toUTF8String
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger
import io.embrace.android.embracesdk.opentelemetry.embCrashNumber
import io.embrace.android.embracesdk.opentelemetry.embSessionId
import io.embrace.android.embracesdk.opentelemetry.logRecordUid
import io.embrace.android.embracesdk.payload.NativeCrashDataError
import io.embrace.android.embracesdk.session.id.SessionIdTracker
import io.embrace.android.embracesdk.session.properties.EmbraceSessionProperties
Expand Down Expand Up @@ -81,6 +82,7 @@ internal class NativeCrashDataSourceImplTest {
with(otelLogger.builders.single()) {
assertEquals(1, emitCalled)
assertTrue(attributes.hasFixedAttribute(EmbType.System.NativeCrash))
assertNotNull(attributes.getAttribute(logRecordUid))
assertEquals(testNativeCrashData.sessionId, attributes.getAttribute(embSessionId))
assertEquals("1", attributes.getAttribute(embCrashNumber))
assertEquals(testNativeCrashData.crash, attributes.getAttribute(embNativeCrashException))
Expand Down

0 comments on commit c8c4d2b

Please sign in to comment.