Skip to content

Commit 301f9a2

Browse files
committed
Add device and OS attributes to metrics
1 parent d3cadef commit 301f9a2

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

sentry-android-core/src/main/java/io/sentry/android/core/DefaultAndroidEventProcessor.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ public DefaultAndroidEventProcessor(
9393
return event;
9494
}
9595

96+
@Override
97+
public @Nullable SentryMetricsEvent process(@NotNull SentryMetricsEvent event) {
98+
setDevice(event);
99+
setOs(event);
100+
return event;
101+
}
102+
96103
/**
97104
* The last exception is usually used for picking the issue title, but the convention is to send
98105
* inner exceptions first, e.g. [inner, outer] This doesn't work very well on Android, as some
@@ -248,6 +255,34 @@ private void setOs(final @NotNull SentryLogEvent event) {
248255
}
249256
}
250257

258+
private void setDevice(final @NotNull SentryMetricsEvent event) {
259+
try {
260+
event.setAttribute(
261+
"device.brand",
262+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.BRAND));
263+
event.setAttribute(
264+
"device.model",
265+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.MODEL));
266+
event.setAttribute(
267+
"device.family",
268+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, deviceFamily.getValue()));
269+
} catch (Throwable e) {
270+
options.getLogger().log(SentryLevel.ERROR, "Failed to retrieve device info", e);
271+
}
272+
}
273+
274+
private void setOs(final @NotNull SentryMetricsEvent event) {
275+
try {
276+
event.setAttribute(
277+
"os.name", new SentryLogEventAttributeValue(SentryAttributeType.STRING, "Android"));
278+
event.setAttribute(
279+
"os.version",
280+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.VERSION.RELEASE));
281+
} catch (Throwable e) {
282+
options.getLogger().log(SentryLevel.ERROR, "Failed to retrieve os system", e);
283+
}
284+
}
285+
251286
// Data to be applied to events that was created in the running process
252287
private void processNonCachedEvent(
253288
final @NotNull SentryBaseEvent event, final @NotNull Hint hint) {

sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ import android.os.Build
55
import android.os.Looper
66
import androidx.test.core.app.ApplicationProvider
77
import androidx.test.ext.junit.runners.AndroidJUnit4
8+
import io.sentry.DateUtils
89
import io.sentry.DiagnosticLogger
910
import io.sentry.Hint
1011
import io.sentry.IScopes
1112
import io.sentry.SentryEvent
1213
import io.sentry.SentryLevel
14+
import io.sentry.SentryLogEvent
15+
import io.sentry.SentryLogLevel
16+
import io.sentry.SentryMetricsEvent
1317
import io.sentry.SentryTracer
1418
import io.sentry.TransactionContext
1519
import io.sentry.TypeCheckHint.SENTRY_DART_SDK_NAME
1620
import io.sentry.android.core.internal.util.CpuInfoUtils
1721
import io.sentry.protocol.OperatingSystem
1822
import io.sentry.protocol.SdkVersion
1923
import io.sentry.protocol.SentryException
24+
import io.sentry.protocol.SentryId
2025
import io.sentry.protocol.SentryStackFrame
2126
import io.sentry.protocol.SentryStackTrace
2227
import io.sentry.protocol.SentryThread
@@ -619,4 +624,45 @@ class DefaultAndroidEventProcessorTest {
619624
assertEquals("IllegalArgumentException", it.exceptions!![1].type)
620625
}
621626
}
627+
628+
@Test
629+
fun `device and os are set on metric`() {
630+
val sut = fixture.getSut(context)
631+
val processedEvent: SentryMetricsEvent? =
632+
sut.process(
633+
SentryMetricsEvent(
634+
SentryId("5c1f73d39486827b9e60ceb1fc23277a"),
635+
DateUtils.dateToSeconds(DateUtils.getDateTime("2004-04-10T18:24:03.000Z")),
636+
"42e6bd2a-c45e-414d-8066-ed5196fbc686",
637+
"counter",
638+
123.0,
639+
)
640+
)
641+
642+
assertNotNull(processedEvent?.attributes?.get("device.brand"))
643+
assertNotNull(processedEvent?.attributes?.get("device.model"))
644+
assertNotNull(processedEvent?.attributes?.get("device.family"))
645+
assertNotNull(processedEvent?.attributes?.get("os.name"))
646+
assertNotNull(processedEvent?.attributes?.get("os.version"))
647+
}
648+
649+
@Test
650+
fun `device and os are set on log`() {
651+
val sut = fixture.getSut(context)
652+
val processedEvent: SentryLogEvent? =
653+
sut.process(
654+
SentryLogEvent(
655+
SentryId("5c1f73d39486827b9e60ceb1fc23277a"),
656+
DateUtils.dateToSeconds(DateUtils.getDateTime("2004-04-10T18:24:03.000Z")),
657+
"message",
658+
SentryLogLevel.WARN,
659+
)
660+
)
661+
662+
assertNotNull(processedEvent?.attributes?.get("device.brand"))
663+
assertNotNull(processedEvent?.attributes?.get("device.model"))
664+
assertNotNull(processedEvent?.attributes?.get("device.family"))
665+
assertNotNull(processedEvent?.attributes?.get("os.name"))
666+
assertNotNull(processedEvent?.attributes?.get("os.version"))
667+
}
622668
}

0 commit comments

Comments
 (0)