Skip to content

Commit 72b09b1

Browse files
committed
Add device and OS attributes to logs
1 parent 76ac22b commit 72b09b1

4 files changed

Lines changed: 109 additions & 0 deletions

File tree

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
import io.sentry.EventProcessor;
88
import io.sentry.Hint;
99
import io.sentry.IpAddressUtils;
10+
import io.sentry.SentryAttributeType;
1011
import io.sentry.SentryBaseEvent;
1112
import io.sentry.SentryEvent;
1213
import io.sentry.SentryLevel;
14+
import io.sentry.SentryLogEvent;
15+
import io.sentry.SentryLogEventAttributeValue;
1316
import io.sentry.SentryReplayEvent;
1417
import io.sentry.android.core.internal.util.AndroidThreadChecker;
1518
import io.sentry.android.core.performance.AppStartMetrics;
1619
import io.sentry.android.core.performance.TimeSpan;
1720
import io.sentry.protocol.App;
21+
import io.sentry.protocol.Device;
1822
import io.sentry.protocol.OperatingSystem;
1923
import io.sentry.protocol.SentryException;
2024
import io.sentry.protocol.SentryStackFrame;
@@ -81,6 +85,13 @@ public DefaultAndroidEventProcessor(
8185
return event;
8286
}
8387

88+
@Override
89+
public @Nullable SentryLogEvent process(@NotNull SentryLogEvent event) {
90+
setDevice(event);
91+
setOs(event);
92+
return event;
93+
}
94+
8495
/**
8596
* The last exception is usually used for picking the issue title, but the convention is to send
8697
* inner exceptions first, e.g. [inner, outer] This doesn't work very well on Android, as some
@@ -199,6 +210,37 @@ private void mergeOS(final @NotNull SentryBaseEvent event) {
199210
}
200211
}
201212

213+
private void setDevice(final @NotNull SentryLogEvent event) {
214+
try {
215+
final @NotNull Device device = deviceInfoUtil.get().collectDeviceInformation(false, false);
216+
event.setAttribute(
217+
"device.brand",
218+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, device.getBrand()));
219+
event.setAttribute(
220+
"device.model",
221+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, device.getModel()));
222+
event.setAttribute(
223+
"device.family",
224+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, device.getFamily()));
225+
} catch (Throwable e) {
226+
options.getLogger().log(SentryLevel.ERROR, "Failed to retrieve device info", e);
227+
}
228+
}
229+
230+
private void setOs(final @NotNull SentryLogEvent event) {
231+
try {
232+
final OperatingSystem androidOS = deviceInfoUtil.get().getOperatingSystem();
233+
event.setAttribute(
234+
"os.name",
235+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, androidOS.getName()));
236+
event.setAttribute(
237+
"os.version",
238+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, androidOS.getVersion()));
239+
} catch (Throwable e) {
240+
options.getLogger().log(SentryLevel.ERROR, "Failed to retrieve os system", e);
241+
}
242+
}
243+
202244
// Data to be applied to events that was created in the running process
203245
private void processNonCachedEvent(
204246
final @NotNull SentryBaseEvent event, final @NotNull Hint hint) {

sentry/src/main/java/io/sentry/EventProcessor.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ default SentryReplayEvent process(@NotNull SentryReplayEvent event, @NotNull Hin
4545
return event;
4646
}
4747

48+
/**
49+
* May mutate or drop a SentryLogEvent
50+
*
51+
* @param event the SentryLogEvent
52+
* @return the event itself, a mutated SentryLogEvent or null
53+
*/
54+
@Nullable
55+
default SentryLogEvent process(@NotNull SentryLogEvent event) {
56+
return event;
57+
}
58+
4859
/**
4960
* Controls when this EventProcessor is invoked.
5061
*

sentry/src/main/java/io/sentry/SentryClient.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,38 @@ private SentryEvent processEvent(
470470
return event;
471471
}
472472

473+
@Nullable
474+
private SentryLogEvent processLogEvent(
475+
@NotNull SentryLogEvent event, final @NotNull List<EventProcessor> eventProcessors) {
476+
for (final EventProcessor processor : eventProcessors) {
477+
try {
478+
event = processor.process(event);
479+
} catch (Throwable e) {
480+
options
481+
.getLogger()
482+
.log(
483+
SentryLevel.ERROR,
484+
e,
485+
"An exception occurred while processing log event by processor: %s",
486+
processor.getClass().getName());
487+
}
488+
489+
if (event == null) {
490+
options
491+
.getLogger()
492+
.log(
493+
SentryLevel.DEBUG,
494+
"Log event was dropped by a processor: %s",
495+
processor.getClass().getName());
496+
options
497+
.getClientReportRecorder()
498+
.recordLostEvent(DiscardReason.EVENT_PROCESSOR, DataCategory.LogItem);
499+
break;
500+
}
501+
}
502+
return event;
503+
}
504+
473505
private @Nullable SentryTransaction processTransaction(
474506
@NotNull SentryTransaction transaction,
475507
final @NotNull Hint hint,
@@ -1138,6 +1170,19 @@ public void captureSession(final @NotNull Session session, final @Nullable Hint
11381170
@ApiStatus.Experimental
11391171
@Override
11401172
public void captureLog(@Nullable SentryLogEvent logEvent, @Nullable IScope scope) {
1173+
if (logEvent != null && scope != null) {
1174+
logEvent = processLogEvent(logEvent, scope.getEventProcessors());
1175+
if (logEvent == null) {
1176+
return;
1177+
}
1178+
}
1179+
1180+
if (logEvent != null) {
1181+
logEvent = processLogEvent(logEvent, options.getEventProcessors());
1182+
if (logEvent == null) {
1183+
return;
1184+
}
1185+
}
11411186

11421187
if (logEvent != null) {
11431188
logEvent = executeBeforeSendLog(logEvent);

sentry/src/main/java/io/sentry/SentryLogEvent.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ public void setAttributes(final @Nullable Map<String, SentryLogEventAttributeVal
7373
this.attributes = attributes;
7474
}
7575

76+
public void setAttribute(
77+
final @Nullable String key, final @Nullable SentryLogEventAttributeValue value) {
78+
if (key == null) {
79+
return;
80+
}
81+
if (this.attributes == null) {
82+
this.attributes = new HashMap<>();
83+
}
84+
this.attributes.put(key, value);
85+
}
86+
7687
public @Nullable Integer getSeverityNumber() {
7788
return severityNumber;
7889
}

0 commit comments

Comments
 (0)