Skip to content

Commit ff3aa9c

Browse files
authored
[Logs 12] Logs review feedback (#4392)
* Add Log feature to Java SDK * Rate limit for log items * Add options for logs * Add batch processor for logs * Use a separate ExecutorService for log batching * Reduce locking when log event is created * Add system tests for Logs * Separate enum for SentryLogLevel * Remove logsSampleRate option * Move logs options out of experimental namespace * Add severity_number to SentryLogItem * Logs review feedback * mark captureBatchedLogEvents internal * remove hint for logs * Allow null for log event attribute value
1 parent 87690fc commit ff3aa9c

File tree

11 files changed

+35
-65
lines changed

11 files changed

+35
-65
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class SessionTrackingIntegrationTest {
192192
TODO("Not yet implemented")
193193
}
194194

195-
override fun captureLog(event: SentryLogEvent, scope: IScope?, hint: Hint?) {
195+
override fun captureLog(event: SentryLogEvent, scope: IScope?) {
196196
TODO("Not yet implemented")
197197
}
198198

sentry/api/sentry.api

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ public abstract interface class io/sentry/ISentryClient {
10041004
public fun captureException (Ljava/lang/Throwable;Lio/sentry/IScope;)Lio/sentry/protocol/SentryId;
10051005
public fun captureException (Ljava/lang/Throwable;Lio/sentry/IScope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId;
10061006
public abstract fun captureFeedback (Lio/sentry/protocol/Feedback;Lio/sentry/Hint;Lio/sentry/IScope;)Lio/sentry/protocol/SentryId;
1007-
public abstract fun captureLog (Lio/sentry/SentryLogEvent;Lio/sentry/IScope;Lio/sentry/Hint;)V
1007+
public abstract fun captureLog (Lio/sentry/SentryLogEvent;Lio/sentry/IScope;)V
10081008
public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;)Lio/sentry/protocol/SentryId;
10091009
public fun captureMessage (Ljava/lang/String;Lio/sentry/SentryLevel;Lio/sentry/IScope;)Lio/sentry/protocol/SentryId;
10101010
public abstract fun captureProfileChunk (Lio/sentry/ProfileChunk;Lio/sentry/IScope;)Lio/sentry/protocol/SentryId;
@@ -2738,7 +2738,7 @@ public final class io/sentry/SentryClient : io/sentry/ISentryClient {
27382738
public fun captureEnvelope (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId;
27392739
public fun captureEvent (Lio/sentry/SentryEvent;Lio/sentry/IScope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId;
27402740
public fun captureFeedback (Lio/sentry/protocol/Feedback;Lio/sentry/Hint;Lio/sentry/IScope;)Lio/sentry/protocol/SentryId;
2741-
public fun captureLog (Lio/sentry/SentryLogEvent;Lio/sentry/IScope;Lio/sentry/Hint;)V
2741+
public fun captureLog (Lio/sentry/SentryLogEvent;Lio/sentry/IScope;)V
27422742
public fun captureProfileChunk (Lio/sentry/ProfileChunk;Lio/sentry/IScope;)Lio/sentry/protocol/SentryId;
27432743
public fun captureReplayEvent (Lio/sentry/SentryReplayEvent;Lio/sentry/IScope;Lio/sentry/Hint;)Lio/sentry/protocol/SentryId;
27442744
public fun captureSession (Lio/sentry/Session;Lio/sentry/Hint;)V
@@ -3464,7 +3464,7 @@ public final class io/sentry/SentryOptions$Logs {
34643464
}
34653465

34663466
public abstract interface class io/sentry/SentryOptions$Logs$BeforeSendLogCallback {
3467-
public abstract fun execute (Lio/sentry/SentryLogEvent;Lio/sentry/Hint;)Lio/sentry/SentryLogEvent;
3467+
public abstract fun execute (Lio/sentry/SentryLogEvent;)Lio/sentry/SentryLogEvent;
34683468
}
34693469

34703470
public abstract interface class io/sentry/SentryOptions$ProfilesSamplerCallback {
@@ -4703,7 +4703,7 @@ public abstract interface class io/sentry/logger/ILoggerApi {
47034703
public abstract fun error (Ljava/lang/String;[Ljava/lang/Object;)V
47044704
public abstract fun fatal (Ljava/lang/String;[Ljava/lang/Object;)V
47054705
public abstract fun info (Ljava/lang/String;[Ljava/lang/Object;)V
4706-
public abstract fun log (Lio/sentry/SentryLogLevel;Lio/sentry/SentryDate;Ljava/lang/String;Lio/sentry/Hint;[Ljava/lang/Object;)V
4706+
public abstract fun log (Lio/sentry/SentryLogLevel;Lio/sentry/SentryDate;Ljava/lang/String;[Ljava/lang/Object;)V
47074707
public abstract fun log (Lio/sentry/SentryLogLevel;Ljava/lang/String;[Ljava/lang/Object;)V
47084708
public abstract fun trace (Ljava/lang/String;[Ljava/lang/Object;)V
47094709
public abstract fun warn (Ljava/lang/String;[Ljava/lang/Object;)V
@@ -4720,7 +4720,7 @@ public final class io/sentry/logger/LoggerApi : io/sentry/logger/ILoggerApi {
47204720
public fun error (Ljava/lang/String;[Ljava/lang/Object;)V
47214721
public fun fatal (Ljava/lang/String;[Ljava/lang/Object;)V
47224722
public fun info (Ljava/lang/String;[Ljava/lang/Object;)V
4723-
public fun log (Lio/sentry/SentryLogLevel;Lio/sentry/SentryDate;Ljava/lang/String;Lio/sentry/Hint;[Ljava/lang/Object;)V
4723+
public fun log (Lio/sentry/SentryLogLevel;Lio/sentry/SentryDate;Ljava/lang/String;[Ljava/lang/Object;)V
47244724
public fun log (Lio/sentry/SentryLogLevel;Ljava/lang/String;[Ljava/lang/Object;)V
47254725
public fun trace (Ljava/lang/String;[Ljava/lang/Object;)V
47264726
public fun warn (Ljava/lang/String;[Ljava/lang/Object;)V
@@ -4740,7 +4740,7 @@ public final class io/sentry/logger/NoOpLoggerApi : io/sentry/logger/ILoggerApi
47404740
public fun fatal (Ljava/lang/String;[Ljava/lang/Object;)V
47414741
public static fun getInstance ()Lio/sentry/logger/NoOpLoggerApi;
47424742
public fun info (Ljava/lang/String;[Ljava/lang/Object;)V
4743-
public fun log (Lio/sentry/SentryLogLevel;Lio/sentry/SentryDate;Ljava/lang/String;Lio/sentry/Hint;[Ljava/lang/Object;)V
4743+
public fun log (Lio/sentry/SentryLogLevel;Lio/sentry/SentryDate;Ljava/lang/String;[Ljava/lang/Object;)V
47444744
public fun log (Lio/sentry/SentryLogLevel;Ljava/lang/String;[Ljava/lang/Object;)V
47454745
public fun trace (Ljava/lang/String;[Ljava/lang/Object;)V
47464746
public fun warn (Ljava/lang/String;[Ljava/lang/Object;)V

sentry/src/main/java/io/sentry/ISentryClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,9 @@ SentryId captureProfileChunk(
305305
SentryId captureCheckIn(@NotNull CheckIn checkIn, @Nullable IScope scope, @Nullable Hint hint);
306306

307307
@ApiStatus.Experimental
308-
void captureLog(@NotNull SentryLogEvent logEvent, @Nullable IScope scope, @Nullable Hint hint);
308+
void captureLog(@NotNull SentryLogEvent logEvent, @Nullable IScope scope);
309309

310-
@ApiStatus.Experimental
310+
@ApiStatus.Internal
311311
void captureBatchedLogEvents(@NotNull SentryLogEvents logEvents);
312312

313313
@ApiStatus.Internal

sentry/src/main/java/io/sentry/NoOpSentryClient.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,11 @@ public SentryId captureEnvelope(@NotNull SentryEnvelope envelope, @Nullable Hint
8686

8787
@ApiStatus.Experimental
8888
@Override
89-
public void captureLog(
90-
@NotNull SentryLogEvent logEvent, @Nullable IScope scope, @Nullable Hint hint) {
89+
public void captureLog(@NotNull SentryLogEvent logEvent, @Nullable IScope scope) {
9190
// do nothing
9291
}
9392

94-
@ApiStatus.Experimental
93+
@ApiStatus.Internal
9594
@Override
9695
public void captureBatchedLogEvents(@NotNull SentryLogEvents logEvents) {
9796
// do nothing

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,26 +1132,10 @@ public void captureSession(final @NotNull Session session, final @Nullable Hint
11321132

11331133
@ApiStatus.Experimental
11341134
@Override
1135-
public void captureLog(
1136-
@Nullable SentryLogEvent logEvent, @Nullable IScope scope, @Nullable Hint hint) {
1137-
if (hint == null) {
1138-
hint = new Hint();
1139-
}
1140-
1141-
// @Nullable TraceContext traceContext = null;
1142-
// if (scope != null) {
1143-
// final @Nullable ITransaction transaction = scope.getTransaction();
1144-
// if (transaction != null) {
1145-
// traceContext = transaction.traceContext();
1146-
// } else {
1147-
// final @NotNull PropagationContext propagationContext =
1148-
// TracingUtils.maybeUpdateBaggage(scope, options);
1149-
// traceContext = propagationContext.traceContext();
1150-
// }
1151-
// }
1135+
public void captureLog(@Nullable SentryLogEvent logEvent, @Nullable IScope scope) {
11521136

11531137
if (logEvent != null) {
1154-
logEvent = executeBeforeSendLog(logEvent, hint);
1138+
logEvent = executeBeforeSendLog(logEvent);
11551139

11561140
if (logEvent == null) {
11571141
options.getLogger().log(SentryLevel.DEBUG, "Log Event was dropped by beforeSendLog");
@@ -1163,10 +1147,9 @@ public void captureLog(
11631147

11641148
loggerBatchProcessor.add(logEvent);
11651149
}
1166-
1167-
hint.clear();
11681150
}
11691151

1152+
@ApiStatus.Internal
11701153
@Override
11711154
public void captureBatchedLogEvents(final @NotNull SentryLogEvents logEvents) {
11721155
try {
@@ -1453,13 +1436,12 @@ private void sortBreadcrumbsByDate(
14531436
return event;
14541437
}
14551438

1456-
private @Nullable SentryLogEvent executeBeforeSendLog(
1457-
@NotNull SentryLogEvent event, final @NotNull Hint hint) {
1439+
private @Nullable SentryLogEvent executeBeforeSendLog(@NotNull SentryLogEvent event) {
14581440
final SentryOptions.Logs.BeforeSendLogCallback beforeSendLog =
14591441
options.getLogs().getBeforeSend();
14601442
if (beforeSendLog != null) {
14611443
try {
1462-
event = beforeSendLog.execute(event, hint);
1444+
event = beforeSendLog.execute(event);
14631445
} catch (Throwable e) {
14641446
options
14651447
.getLogger()

sentry/src/main/java/io/sentry/SentryLogEventAttributeValue.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
public final class SentryLogEventAttributeValue implements JsonUnknown, JsonSerializable {
1111

1212
private @NotNull String type;
13-
private @NotNull Object value;
13+
private @Nullable Object value;
1414
private @Nullable Map<String, Object> unknown;
1515

16-
public SentryLogEventAttributeValue(final @NotNull String type, final @NotNull Object value) {
16+
public SentryLogEventAttributeValue(final @NotNull String type, final @Nullable Object value) {
1717
this.type = type;
1818
this.value = value;
1919
}
@@ -88,13 +88,6 @@ public static final class Deserializer implements JsonDeserializer<SentryLogEven
8888
throw exception;
8989
}
9090

91-
if (value == null) {
92-
String message = "Missing required field \"" + JsonKeys.VALUE + "\"";
93-
Exception exception = new IllegalStateException(message);
94-
logger.log(SentryLevel.ERROR, message, exception);
95-
throw exception;
96-
}
97-
9891
final SentryLogEventAttributeValue logEvent = new SentryLogEventAttributeValue(type, value);
9992

10093
logEvent.setUnknown(unknown);

sentry/src/main/java/io/sentry/SentryOptions.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3430,11 +3430,10 @@ public interface BeforeSendLogCallback {
34303430
* Mutates or drop a log event before being sent
34313431
*
34323432
* @param event the event
3433-
* @param hint the hints
34343433
* @return the original log event or the mutated event or null if event was dropped
34353434
*/
34363435
@Nullable
3437-
SentryLogEvent execute(@NotNull SentryLogEvent event, @NotNull Hint hint);
3436+
SentryLogEvent execute(@NotNull SentryLogEvent event);
34383437
}
34393438
}
34403439

sentry/src/main/java/io/sentry/logger/ILoggerApi.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.sentry.logger;
22

3-
import io.sentry.Hint;
43
import io.sentry.SentryDate;
54
import io.sentry.SentryLogLevel;
65
import org.jetbrains.annotations.ApiStatus;
@@ -28,6 +27,5 @@ void log(
2827
@NotNull SentryLogLevel level,
2928
@Nullable SentryDate timestamp,
3029
@Nullable String message,
31-
final @Nullable Hint hint,
3230
@Nullable Object... args);
3331
}

sentry/src/main/java/io/sentry/logger/LoggerApi.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.sentry.logger;
22

3-
import io.sentry.Hint;
3+
import io.sentry.IScope;
44
import io.sentry.ISpan;
55
import io.sentry.PropagationContext;
66
import io.sentry.Scopes;
@@ -13,6 +13,7 @@
1313
import io.sentry.SpanId;
1414
import io.sentry.protocol.SdkVersion;
1515
import io.sentry.protocol.SentryId;
16+
import io.sentry.util.TracingUtils;
1617
import java.util.HashMap;
1718
import org.jetbrains.annotations.ApiStatus;
1819
import org.jetbrains.annotations.NotNull;
@@ -29,8 +30,7 @@ public LoggerApi(final @NotNull Scopes scopes) {
2930

3031
@Override
3132
public void trace(final @Nullable String message, final @Nullable Object... args) {
32-
// TODO SentryLevel.TRACE does not exists yet so we just report it as DEBUG for now
33-
log(SentryLogLevel.DEBUG, message, args);
33+
log(SentryLogLevel.TRACE, message, args);
3434
}
3535

3636
@Override
@@ -63,24 +63,22 @@ public void log(
6363
final @NotNull SentryLogLevel level,
6464
final @Nullable String message,
6565
final @Nullable Object... args) {
66-
log(level, null, message, null, args);
66+
log(level, null, message, args);
6767
}
6868

6969
@Override
7070
public void log(
7171
final @NotNull SentryLogLevel level,
7272
final @Nullable SentryDate timestamp,
7373
final @Nullable String message,
74-
final @Nullable Hint hint,
7574
final @Nullable Object... args) {
76-
captureLog(level, timestamp, hint, message, args);
75+
captureLog(level, timestamp, message, args);
7776
}
7877

7978
@SuppressWarnings("AnnotateFormatMethod")
8079
private void captureLog(
8180
final @NotNull SentryLogLevel level,
8281
final @Nullable SentryDate timestamp,
83-
final @Nullable Hint hint,
8482
final @Nullable String message,
8583
final @Nullable Object... args) {
8684
final @NotNull SentryOptions options = scopes.getOptions();
@@ -106,9 +104,13 @@ private void captureLog(
106104
final @NotNull SentryDate timestampToUse =
107105
timestamp == null ? options.getDateProvider().now() : timestamp;
108106
final @NotNull String messageToUse = args == null ? message : String.format(message, args);
109-
final @NotNull PropagationContext propagationContext =
110-
scopes.getCombinedScopeView().getPropagationContext();
111-
final @Nullable ISpan span = scopes.getCombinedScopeView().getSpan();
107+
108+
final @NotNull IScope combinedScope = scopes.getCombinedScopeView();
109+
final @NotNull PropagationContext propagationContext = combinedScope.getPropagationContext();
110+
final @Nullable ISpan span = combinedScope.getSpan();
111+
if (span == null) {
112+
TracingUtils.maybeUpdateBaggage(combinedScope, options);
113+
}
112114
final @NotNull SentryId traceId =
113115
span == null ? propagationContext.getTraceId() : span.getSpanContext().getTraceId();
114116
final @NotNull SpanId spanId =
@@ -118,7 +120,7 @@ private void captureLog(
118120
logEvent.setAttributes(createAttributes(message, spanId, args));
119121
logEvent.setSeverityNumber(level.getSeverityNumber());
120122

121-
scopes.getClient().captureLog(logEvent, scopes.getCombinedScopeView(), hint);
123+
scopes.getClient().captureLog(logEvent, combinedScope);
122124
} catch (Throwable e) {
123125
options.getLogger().log(SentryLevel.ERROR, "Error while capturing log event", e);
124126
}

sentry/src/main/java/io/sentry/logger/LoggerBatchProcessor.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,9 @@ private void flush() {
8787
}
8888

8989
private void flushInternal() {
90-
flushBatch();
91-
if (queue.size() >= MAX_BATCH_SIZE) {
92-
flushInternal();
93-
}
90+
do {
91+
flushBatch();
92+
} while (queue.size() >= MAX_BATCH_SIZE);
9493
}
9594

9695
private void flushBatch() {

0 commit comments

Comments
 (0)