Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
adding scope methods to sentry static class (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Dec 3, 2019
1 parent cee0f0a commit 2b51fbd
Show file tree
Hide file tree
Showing 10 changed files with 504 additions and 61 deletions.
159 changes: 143 additions & 16 deletions sentry-core/src/main/java/io/sentry/core/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import static io.sentry.core.ILogger.logIfNotNull;

import io.sentry.core.protocol.SentryId;
import io.sentry.core.protocol.User;
import io.sentry.core.util.Objects;
import java.io.Closeable;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;
import org.jetbrains.annotations.NotNull;
Expand All @@ -29,7 +31,7 @@ private static final class StackItem {
private volatile @NotNull SentryId lastEventId;
private final @NotNull SentryOptions options;
private volatile boolean isEnabled;
private final Deque<StackItem> stack = new LinkedBlockingDeque<>();
private final @NotNull Deque<StackItem> stack = new LinkedBlockingDeque<>();

public Hub(@NotNull SentryOptions options) {
this(options, createRootStackItem(options));
Expand Down Expand Up @@ -74,9 +76,8 @@ public boolean isEnabled() {
return isEnabled;
}

@NotNull
@Override
public SentryId captureEvent(SentryEvent event, @Nullable Object hint) {
public @NotNull SentryId captureEvent(@NotNull SentryEvent event, @Nullable Object hint) {
SentryId sentryId = SentryId.EMPTY_ID;
if (!isEnabled()) {
logIfNotNull(
Expand Down Expand Up @@ -107,9 +108,8 @@ public SentryId captureEvent(SentryEvent event, @Nullable Object hint) {
return sentryId;
}

@NotNull
@Override
public SentryId captureMessage(String message, SentryLevel level) {
public @NotNull SentryId captureMessage(@NotNull String message, @NotNull SentryLevel level) {
SentryId sentryId = SentryId.EMPTY_ID;
if (!isEnabled()) {
logIfNotNull(
Expand Down Expand Up @@ -137,9 +137,8 @@ public SentryId captureMessage(String message, SentryLevel level) {
return sentryId;
}

@NotNull
@Override
public SentryId captureException(Throwable throwable, @Nullable Object hint) {
public @NotNull SentryId captureException(@NotNull Throwable throwable, @Nullable Object hint) {
SentryId sentryId = SentryId.EMPTY_ID;
if (!isEnabled()) {
logIfNotNull(
Expand Down Expand Up @@ -201,7 +200,7 @@ public void close() {
}

@Override
public void addBreadcrumb(Breadcrumb breadcrumb, @Nullable Object hint) {
public void addBreadcrumb(@NotNull Breadcrumb breadcrumb, @Nullable Object hint) {
if (!isEnabled()) {
logIfNotNull(
options.getLogger(),
Expand All @@ -227,9 +226,139 @@ public void addBreadcrumb(Breadcrumb breadcrumb, @Nullable Object hint) {
}
}

private Breadcrumb executeBeforeBreadcrumb(
SentryOptions.BeforeBreadcrumbCallback callback,
Breadcrumb breadcrumb,
@Override
public void setLevel(@Nullable SentryLevel level) {
if (!isEnabled()) {
logIfNotNull(
options.getLogger(),
SentryLevel.WARNING,
"Instance is disabled and this 'setLevel' call is a no-op.");
} else {
StackItem item = stack.peek();
if (item != null) {
item.scope.setLevel(level);
} else {
logIfNotNull(options.getLogger(), SentryLevel.FATAL, "Stack peek was null when setLevel");
}
}
}

@Override
public void setTransaction(@Nullable String transaction) {
if (!isEnabled()) {
logIfNotNull(
options.getLogger(),
SentryLevel.WARNING,
"Instance is disabled and this 'setTransaction' call is a no-op.");
} else {
StackItem item = stack.peek();
if (item != null) {
item.scope.setTransaction(transaction);
} else {
logIfNotNull(
options.getLogger(), SentryLevel.FATAL, "Stack peek was null when setTransaction");
}
}
}

@Override
public void setUser(@Nullable User user) {
if (!isEnabled()) {
logIfNotNull(
options.getLogger(),
SentryLevel.WARNING,
"Instance is disabled and this 'setUser' call is a no-op.");
} else {
StackItem item = stack.peek();
if (item != null) {
item.scope.setUser(user);
} else {
logIfNotNull(options.getLogger(), SentryLevel.FATAL, "Stack peek was null when setUser");
}
}
}

@Override
public void setFingerprint(@NotNull List<String> fingerprint) {
if (!isEnabled()) {
logIfNotNull(
options.getLogger(),
SentryLevel.WARNING,
"Instance is disabled and this 'setFingerprint' call is a no-op.");
} else if (fingerprint == null) {
logIfNotNull(
options.getLogger(), SentryLevel.WARNING, "setFingerprint called with null parameter.");
} else {
StackItem item = stack.peek();
if (item != null) {
item.scope.setFingerprint(fingerprint);
} else {
logIfNotNull(
options.getLogger(), SentryLevel.FATAL, "Stack peek was null when setFingerprint");
}
}
}

@Override
public void clearBreadcrumbs() {
if (!isEnabled()) {
logIfNotNull(
options.getLogger(),
SentryLevel.WARNING,
"Instance is disabled and this 'clearBreadcrumbs' call is a no-op.");
} else {
StackItem item = stack.peek();
if (item != null) {
item.scope.clearBreadcrumbs();
} else {
logIfNotNull(
options.getLogger(), SentryLevel.FATAL, "Stack peek was null when clearBreadcrumbs");
}
}
}

@Override
public void setTag(@NotNull String key, @NotNull String value) {
if (!isEnabled()) {
logIfNotNull(
options.getLogger(),
SentryLevel.WARNING,
"Instance is disabled and this 'setTag' call is a no-op.");
} else if (key == null || value == null) {
logIfNotNull(options.getLogger(), SentryLevel.WARNING, "setTag called with null parameter.");
} else {
StackItem item = stack.peek();
if (item != null) {
item.scope.setTag(key, value);
} else {
logIfNotNull(options.getLogger(), SentryLevel.FATAL, "Stack peek was null when setTag");
}
}
}

@Override
public void setExtra(@NotNull String key, @NotNull String value) {
if (!isEnabled()) {
logIfNotNull(
options.getLogger(),
SentryLevel.WARNING,
"Instance is disabled and this 'setExtra' call is a no-op.");
} else if (key == null || value == null) {
logIfNotNull(
options.getLogger(), SentryLevel.WARNING, "setExtra called with null parameter.");
} else {
StackItem item = stack.peek();
if (item != null) {
item.scope.setExtra(key, value);
} else {
logIfNotNull(options.getLogger(), SentryLevel.FATAL, "Stack peek was null when setExtra");
}
}
}

private @Nullable Breadcrumb executeBeforeBreadcrumb(
@NotNull SentryOptions.BeforeBreadcrumbCallback callback,
@NotNull Breadcrumb breadcrumb,
@Nullable Object hint) {
try {
breadcrumb = callback.execute(breadcrumb, hint);
Expand All @@ -253,9 +382,8 @@ private Breadcrumb executeBeforeBreadcrumb(
return breadcrumb;
}

@NotNull
@Override
public SentryId getLastEventId() {
public @NotNull SentryId getLastEventId() {
return lastEventId;
}

Expand Down Expand Up @@ -356,7 +484,7 @@ public void configureScope(@NotNull ScopeCallback callback) {
}

@Override
public void bindClient(ISentryClient client) {
public void bindClient(@NotNull ISentryClient client) {
if (!isEnabled()) {
logIfNotNull(
options.getLogger(),
Expand Down Expand Up @@ -399,9 +527,8 @@ public void flush(long timeoutMills) {
}
}

@NotNull
@Override
public IHub clone() {
public @NotNull IHub clone() {
if (!isEnabled()) {
logIfNotNull(options.getLogger(), SentryLevel.WARNING, "Disabled Hub cloned.");
}
Expand Down
16 changes: 16 additions & 0 deletions sentry-core/src/main/java/io/sentry/core/IHub.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.sentry.core;

import io.sentry.core.protocol.SentryId;
import io.sentry.core.protocol.User;
import java.util.List;
import org.jetbrains.annotations.Nullable;

public interface IHub {
Expand Down Expand Up @@ -33,6 +35,20 @@ default void addBreadcrumb(Breadcrumb breadcrumb) {
addBreadcrumb(breadcrumb, null);
}

void setLevel(SentryLevel level);

void setTransaction(String transaction);

void setUser(User user);

void setFingerprint(List<String> fingerprint);

void clearBreadcrumbs();

void setTag(String key, String value);

void setExtra(String key, String value);

SentryId getLastEventId();

void pushScope();
Expand Down
23 changes: 23 additions & 0 deletions sentry-core/src/main/java/io/sentry/core/NoOpHub.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.sentry.core;

import io.sentry.core.protocol.SentryId;
import io.sentry.core.protocol.User;
import java.util.List;
import org.jetbrains.annotations.Nullable;

final class NoOpHub implements IHub {
Expand Down Expand Up @@ -39,6 +41,27 @@ public void close() {}
@Override
public void addBreadcrumb(Breadcrumb breadcrumb, @Nullable Object hint) {}

@Override
public void setLevel(SentryLevel level) {}

@Override
public void setTransaction(String transaction) {}

@Override
public void setUser(User user) {}

@Override
public void setFingerprint(List<String> fingerprint) {}

@Override
public void clearBreadcrumbs() {}

@Override
public void setTag(String key, String value) {}

@Override
public void setExtra(String key, String value) {}

@Override
public SentryId getLastEventId() {
return SentryId.EMPTY_ID;
Expand Down

0 comments on commit 2b51fbd

Please sign in to comment.