Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import java.util.*;

public class DefaultAndroidEventProcessor implements EventProcessor {
Context context;
SentryOptions options;
final Context context;
final SentryOptions options;

// it could also be a parameter and get from Sentry.init(...)
private static final Date appStartTime = DateUtils.getCurrentDateTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

/** Sentry SDK internal diagnostic logger. */
public class DiagnosticLogger implements ILogger {
private SentryOptions options;
private ILogger logger;
private final SentryOptions options;
private final ILogger logger;

/**
* Creates a new instance of DiagnosticLogger with the wrapped ILogger.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ SentryEnvelopeItemHeader.class, new SentryEnvelopeItemHeaderAdapter())

public @Nullable SentryEnvelope read(InputStream stream) throws IOException {
byte[] buffer = new byte[1024];
int currentLength = 0;
int currentLength;
int streamOffset = 0;
// Offset of the line break defining the end of the envelope header
int envelopeEndHeaderOffset = -1;
Expand Down
85 changes: 70 additions & 15 deletions sentry-core/src/main/java/io/sentry/core/Hub.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.sentry.core;

import static io.sentry.core.ILogger.log;

import io.sentry.core.protocol.SentryId;
import io.sentry.core.util.Nullable;
import java.util.Deque;
Expand Down Expand Up @@ -40,8 +42,7 @@ private Hub(SentryOptions options, @Nullable StackItem rootStackItem) {
static StackItem createRootStackItem(SentryOptions options) {
Scope scope = new Scope();
ISentryClient client = new SentryClient(options);
StackItem item = new StackItem(client, scope);
return item;
return new StackItem(client, scope);
}

@Override
Expand All @@ -53,16 +54,26 @@ public boolean isEnabled() {
public SentryId captureEvent(SentryEvent event) {
SentryId sentryId;
StackItem item = stack.peek();
sentryId = item.client.captureEvent(event, item.scope);
this.lastEventId = sentryId;
if (item != null) {
sentryId = item.client.captureEvent(event, item.scope);
} else {
log(options.getLogger(), SentryLevel.FATAL, "Stack peek was NULL when captureEvent");
sentryId = SentryId.EMPTY_ID;
}
this.lastEventId = event.getEventId();
return sentryId;
}

@Override
public SentryId captureMessage(String message) {
SentryId sentryId;
StackItem item = stack.peek();
sentryId = item.client.captureMessage(message, item.scope);
if (item != null) {
sentryId = item.client.captureMessage(message, item.scope);
} else {
log(options.getLogger(), SentryLevel.FATAL, "Stack peek was NULL when captureMessage");
sentryId = SentryId.EMPTY_ID;
}
this.lastEventId = sentryId;
return sentryId;
}
Expand All @@ -71,7 +82,12 @@ public SentryId captureMessage(String message) {
public SentryId captureException(Throwable throwable) {
SentryId sentryId;
StackItem item = stack.peek();
sentryId = item.client.captureException(throwable, item.scope);
if (item != null) {
sentryId = item.client.captureException(throwable, item.scope);
} else {
log(options.getLogger(), SentryLevel.FATAL, "Stack peek was NULL when captureException");
sentryId = SentryId.EMPTY_ID;
}
this.lastEventId = sentryId;
return sentryId;
}
Expand All @@ -80,14 +96,22 @@ public SentryId captureException(Throwable throwable) {
public void close() {
// Close the top-most client
StackItem item = stack.peek();
item.client.close();
if (item != null) {
item.client.close();
} else {
log(options.getLogger(), SentryLevel.FATAL, "Stack peek was NULL when closing Hub");
}
isEnabled = false;
}

@Override
public void addBreadcrumb(Breadcrumb breadcrumb) {
StackItem item = stack.peek();
item.scope.addBreadcrumb(breadcrumb);
if (item != null) {
item.scope.addBreadcrumb(breadcrumb);
} else {
log(options.getLogger(), SentryLevel.FATAL, "Stack peek was NULL when addBreadcrumb");
}
}

@Override
Expand All @@ -98,9 +122,24 @@ public SentryId getLastEventId() {
@Override
public void pushScope() {
StackItem item = stack.peek();
Scope clone = item.scope.clone();
StackItem newItem = new StackItem(item.client, clone);
stack.push(newItem);
if (item != null) {
Scope clone = null;
try {
clone = item.scope.clone();
} catch (CloneNotSupportedException e) {
log(
options.getLogger(),
SentryLevel.ERROR,
"An error has occurred when cloning a Scope",
e);
}
if (clone != null) {
StackItem newItem = new StackItem(item.client, clone);
stack.push(newItem);
}
} else {
log(options.getLogger(), SentryLevel.FATAL, "Stack peek was NULL when pushScope");
}
}

@Override
Expand All @@ -118,7 +157,11 @@ public void withScope(ScopeCallback callback) {
pushScope();
try {
StackItem item = stack.peek();
callback.run(item.scope);
if (item != null) {
callback.run(item.scope);
} else {
log(options.getLogger(), SentryLevel.FATAL, "Stack peek was NULL when withScope");
}
} finally {
popScope();
}
Expand All @@ -127,19 +170,31 @@ public void withScope(ScopeCallback callback) {
@Override
public void configureScope(ScopeCallback callback) {
StackItem item = stack.peek();
callback.run(item.scope);
if (item != null) {
callback.run(item.scope);
} else {
log(options.getLogger(), SentryLevel.FATAL, "Stack peek was NULL when configureScope");
}
}

@Override
public void bindClient(SentryClient client) {
StackItem item = stack.peek();
item.client = client;
if (item != null) {
item.client = client != null ? client : NoOpSentryClient.getInstance();
} else {
log(options.getLogger(), SentryLevel.FATAL, "Stack peek was NULL when bindClient");
}
}

@Override
public void flush(long timeoutMills) {
StackItem item = stack.peek();
item.client.flush(timeoutMills);
if (item != null) {
item.client.flush(timeoutMills);
} else {
log(options.getLogger(), SentryLevel.FATAL, "Stack peek was NULL when flush");
}
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion sentry-core/src/main/java/io/sentry/core/NoOpLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/** No-op implementation of ILogger */
class NoOpLogger implements ILogger {

private static NoOpLogger instance = new NoOpLogger();
private static final NoOpLogger instance = new NoOpLogger();

public static NoOpLogger getInstance() {
return instance;
Expand Down
5 changes: 2 additions & 3 deletions sentry-core/src/main/java/io/sentry/core/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public void setExtra(String key, String value) {
}

@Override
protected Scope clone() {
// TODO: clone me
return new Scope();
protected Scope clone() throws CloneNotSupportedException {
return (Scope) super.clone();
}
}
2 changes: 1 addition & 1 deletion sentry-core/src/main/java/io/sentry/core/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public final class Sentry {

private Sentry() {}

private static ThreadLocal<IHub> currentHub = new ThreadLocal<>();
private static final ThreadLocal<IHub> currentHub = new ThreadLocal<>();

private static volatile IHub mainHub = NoOpHub.getInstance();

Expand Down
31 changes: 31 additions & 0 deletions sentry-core/src/test/java/io/sentry/core/HubTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,40 @@ package io.sentry.core

import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import io.sentry.core.protocol.User
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNotSame

class HubTest {

@Test
fun `when cloning Scope it returns the same values`() {
val scope = Scope()
scope.extra["test"] = "test"
val breadcrumb = Breadcrumb()
breadcrumb.message = "test"
scope.breadcrumbs.add(breadcrumb)
scope.level = SentryLevel.DEBUG
scope.transaction = "test"
scope.fingerprint.add("test")
scope.tags["test"] = "test"
val user = User()
user.email = "[email protected]"
scope.user = user

val clone = scope.clone()
assertNotNull(clone)
assertNotSame(scope, clone)
assertEquals("test", clone.extra["test"])
assertEquals("test", clone.breadcrumbs[0].message)
assertEquals("test", scope.transaction)
assertEquals("test", scope.fingerprint[0])
assertEquals("test", clone.tags["test"])
assertEquals("[email protected]", clone.user.email)
}

@Test
fun `when hub is initialized, integrations are registed`() {
val integrationMock = mock<Integration>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import java.io.InputStream
import java.nio.charset.Charset
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNotNull
import kotlin.test.assertNull

class SentryEnvelopeTest {
private val UTF_8 = Charset.forName("UTF-8")

@Test
fun `deserialize sample envelope with event and two attachments`() {
Expand Down Expand Up @@ -46,7 +44,7 @@ class SentryEnvelopeTest {
@Test
fun `when envelope is empty, reader throws illegal argument`() {
val envelopeReader = EnvelopeReader()
var stream = mock<InputStream>()
val stream = mock<InputStream>()
whenever(stream.read(any())).thenReturn(-1)
val exception = assertFailsWith<IllegalArgumentException> { envelopeReader.read(stream) }
assertEquals("Empty stream.", exception.message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlin.test.assertTrue
class HttpTransportTest {

private class Fixture {
var dsn = URI.create("http://key@localhost/proj").toURL()
val dsn: URL = URI.create("http://key@localhost/proj").toURL()
val serializer = mock<ISerializer>()
var proxy: Proxy? = null
var requestUpdater = IConnectionConfigurator {}
Expand Down
7 changes: 7 additions & 0 deletions sentry-sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ android {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}

tasks.all {
if (this.name == "signingConfigWriterDebugAndroidTest") {
this.enabled = false
println("${this.name} is SKIPPED")
}
}

}

dependencies {
Expand Down