Skip to content

Commit 9705052

Browse files
authored
Merge 4128f36 into 58769f0
2 parents 58769f0 + 4128f36 commit 9705052

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
SentryUserFeedbackDialog.Builder(context).create().show()
2626
```
2727
- Add `user.id`, `user.name` and `user.email` to log attributes ([#4486](https://github.com/getsentry/sentry-java/pull/4486))
28+
- Add device (brand, model and family) and OS (name and version) attributes to logs ([#4493](https://github.com/getsentry/sentry-java/pull/4493))
2829
- Serialize `preContext` and `postContext` in `SentryStackFrame` ([#4482](https://github.com/getsentry/sentry-java/pull/4482))
2930

3031
## 8.13.3

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
import android.content.Context;
44
import android.content.pm.PackageInfo;
55
import android.content.pm.PackageManager;
6+
import android.os.Build;
7+
68
import io.sentry.DateUtils;
79
import io.sentry.EventProcessor;
810
import io.sentry.Hint;
911
import io.sentry.IpAddressUtils;
12+
import io.sentry.NoOpLogger;
13+
import io.sentry.SentryAttributeType;
1014
import io.sentry.SentryBaseEvent;
1115
import io.sentry.SentryEvent;
1216
import io.sentry.SentryLevel;
17+
import io.sentry.SentryLogEvent;
18+
import io.sentry.SentryLogEventAttributeValue;
1319
import io.sentry.SentryReplayEvent;
1420
import io.sentry.android.core.internal.util.AndroidThreadChecker;
1521
import io.sentry.android.core.performance.AppStartMetrics;
@@ -23,6 +29,7 @@
2329
import io.sentry.protocol.SentryTransaction;
2430
import io.sentry.protocol.User;
2531
import io.sentry.util.HintUtils;
32+
import io.sentry.util.LazyEvaluator;
2633
import io.sentry.util.Objects;
2734
import java.util.Collections;
2835
import java.util.List;
@@ -42,6 +49,7 @@ final class DefaultAndroidEventProcessor implements EventProcessor {
4249
private final @NotNull BuildInfoProvider buildInfoProvider;
4350
private final @NotNull SentryAndroidOptions options;
4451
private final @NotNull Future<DeviceInfoUtil> deviceInfoUtil;
52+
private final @NotNull LazyEvaluator<String> deviceFamily = new LazyEvaluator<>(() -> ContextUtils.getFamily(NoOpLogger.getInstance()));
4553

4654
public DefaultAndroidEventProcessor(
4755
final @NotNull Context context,
@@ -81,6 +89,13 @@ public DefaultAndroidEventProcessor(
8189
return event;
8290
}
8391

92+
@Override
93+
public @Nullable SentryLogEvent process(@NotNull SentryLogEvent event) {
94+
setDevice(event);
95+
setOs(event);
96+
return event;
97+
}
98+
8499
/**
85100
* The last exception is usually used for picking the issue title, but the convention is to send
86101
* inner exceptions first, e.g. [inner, outer] This doesn't work very well on Android, as some
@@ -199,6 +214,35 @@ private void mergeOS(final @NotNull SentryBaseEvent event) {
199214
}
200215
}
201216

217+
private void setDevice(final @NotNull SentryLogEvent event) {
218+
try {
219+
event.setAttribute(
220+
"device.brand",
221+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.BRAND));
222+
event.setAttribute(
223+
"device.model",
224+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.MODEL));
225+
event.setAttribute(
226+
"device.family",
227+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, deviceFamily.getValue()));
228+
} catch (Throwable e) {
229+
options.getLogger().log(SentryLevel.ERROR, "Failed to retrieve device info", e);
230+
}
231+
}
232+
233+
private void setOs(final @NotNull SentryLogEvent event) {
234+
try {
235+
event.setAttribute(
236+
"os.name",
237+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, "Android"));
238+
event.setAttribute(
239+
"os.version",
240+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, Build.VERSION.RELEASE));
241+
} catch (Throwable e) {
242+
options.getLogger().log(SentryLevel.ERROR, "Failed to retrieve os system", e);
243+
}
244+
}
245+
202246
// Data to be applied to events that was created in the running process
203247
private void processNonCachedEvent(
204248
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/MainEventProcessor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ private void processNonCachedEvent(final @NotNull SentryBaseEvent event) {
142142
return event;
143143
}
144144

145+
@Override
146+
public @Nullable SentryLogEvent process(@NotNull SentryLogEvent event) {
147+
return event;
148+
}
149+
145150
private void setCommons(final @NotNull SentryBaseEvent event) {
146151
setPlatform(event);
147152
}

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)