Skip to content
Merged
2 changes: 1 addition & 1 deletion sentry-jdbc/api/sentry-jdbc.api
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public final class io/sentry/jdbc/DatabaseUtils$DatabaseDetails {

public class io/sentry/jdbc/SentryJdbcEventListener : com/p6spy/engine/event/SimpleJdbcEventListener {
public fun <init> ()V
public fun <init> (Lio/sentry/IHub;)V
public fun <init> (Lio/sentry/IScopes;)V
public fun onAfterAnyExecute (Lcom/p6spy/engine/common/StatementInformation;JLjava/sql/SQLException;)V
public fun onBeforeAnyExecute (Lcom/p6spy/engine/common/StatementInformation;)V
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import com.jakewharton.nopen.annotation.Open;
import com.p6spy.engine.common.StatementInformation;
import com.p6spy.engine.event.SimpleJdbcEventListener;
import io.sentry.HubAdapter;
import io.sentry.IHub;
import io.sentry.IScopes;
import io.sentry.ISpan;
import io.sentry.ScopesAdapter;
import io.sentry.SentryIntegrationPackageStorage;
import io.sentry.Span;
import io.sentry.SpanStatus;
Expand All @@ -21,24 +21,24 @@
@Open
public class SentryJdbcEventListener extends SimpleJdbcEventListener {
private static final String TRACE_ORIGIN = "auto.db.jdbc";
private final @NotNull IHub hub;
private final @NotNull IScopes scopes;
private static final @NotNull ThreadLocal<ISpan> CURRENT_SPAN = new ThreadLocal<>();

private volatile @Nullable DatabaseUtils.DatabaseDetails cachedDatabaseDetails = null;
private final @NotNull Object databaseDetailsLock = new Object();

public SentryJdbcEventListener(final @NotNull IHub hub) {
this.hub = Objects.requireNonNull(hub, "hub is required");
public SentryJdbcEventListener(final @NotNull IScopes scopes) {
this.scopes = Objects.requireNonNull(scopes, "scopes are required");
addPackageAndIntegrationInfo();
}

public SentryJdbcEventListener() {
this(HubAdapter.getInstance());
this(ScopesAdapter.getInstance());
}

@Override
public void onBeforeAnyExecute(final @NotNull StatementInformation statementInformation) {
final ISpan parent = hub.getSpan();
final ISpan parent = scopes.getSpan();
if (parent != null && !parent.isNoOp()) {
final ISpan span = parent.startChild("db.query", statementInformation.getSql());
CURRENT_SPAN.set(span);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.sentry.jdbc

import com.p6spy.engine.common.StatementInformation
import com.p6spy.engine.spy.P6DataSource
import io.sentry.IHub
import io.sentry.IScopes
import io.sentry.SentryOptions
import io.sentry.SentryTracer
import io.sentry.SpanDataConvention.DB_NAME_KEY
Expand All @@ -26,7 +26,7 @@ import kotlin.test.assertTrue
class SentryJdbcEventListenerTest {

class Fixture {
val hub = mock<IHub>().apply {
val scopes = mock<IScopes>().apply {
whenever(options).thenReturn(
SentryOptions().apply {
sdkVersion = SdkVersion("test", "1.2.3")
Expand All @@ -37,9 +37,9 @@ class SentryJdbcEventListenerTest {
val actualDataSource = JDBCDataSource()

fun getSut(withRunningTransaction: Boolean = true, existingRow: Int? = null): DataSource {
tx = SentryTracer(TransactionContext("name", "op"), hub)
tx = SentryTracer(TransactionContext("name", "op"), scopes)
if (withRunningTransaction) {
whenever(hub.span).thenReturn(tx)
whenever(scopes.span).thenReturn(tx)
}
actualDataSource.setURL("jdbc:hsqldb:mem:testdb")

Expand All @@ -54,7 +54,7 @@ class SentryJdbcEventListenerTest {
}
}

val sentryQueryExecutionListener = SentryJdbcEventListener(hub)
val sentryQueryExecutionListener = SentryJdbcEventListener(scopes)
val p6spyDataSource = P6DataSource(actualDataSource)
p6spyDataSource.setJdbcEventListenerFactory { sentryQueryExecutionListener }
return p6spyDataSource
Expand Down Expand Up @@ -131,9 +131,9 @@ class SentryJdbcEventListenerTest {
@Test
fun `sets SDKVersion Info`() {
val sut = fixture.getSut()
assertNotNull(fixture.hub.options.sdkVersion)
assert(fixture.hub.options.sdkVersion!!.integrationSet.contains("JDBC"))
val packageInfo = fixture.hub.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-jdbc" }
assertNotNull(fixture.scopes.options.sdkVersion)
assert(fixture.scopes.options.sdkVersion!!.integrationSet.contains("JDBC"))
val packageInfo = fixture.scopes.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-jdbc" }
assertNotNull(packageInfo)
assert(packageInfo.version == BuildConfig.VERSION_NAME)
}
Expand Down
8 changes: 4 additions & 4 deletions sentry-kotlin-extensions/api/sentry-kotlin-extensions.api
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
public final class io/sentry/kotlin/SentryContext : kotlin/coroutines/AbstractCoroutineContextElement, kotlinx/coroutines/CopyableThreadContextElement {
public fun <init> ()V
public fun <init> (Lio/sentry/IHub;)V
public synthetic fun <init> (Lio/sentry/IHub;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lio/sentry/IScopes;)V
public synthetic fun <init> (Lio/sentry/IScopes;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun copyForChild ()Lkotlinx/coroutines/CopyableThreadContextElement;
public fun fold (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
public fun get (Lkotlin/coroutines/CoroutineContext$Key;)Lkotlin/coroutines/CoroutineContext$Element;
public fun mergeForChild (Lkotlin/coroutines/CoroutineContext$Element;)Lkotlin/coroutines/CoroutineContext;
public fun minusKey (Lkotlin/coroutines/CoroutineContext$Key;)Lkotlin/coroutines/CoroutineContext;
public fun plus (Lkotlin/coroutines/CoroutineContext;)Lkotlin/coroutines/CoroutineContext;
public fun restoreThreadContext (Lkotlin/coroutines/CoroutineContext;Lio/sentry/IHub;)V
public fun restoreThreadContext (Lkotlin/coroutines/CoroutineContext;Lio/sentry/IScopes;)V
public synthetic fun restoreThreadContext (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Object;)V
public fun updateThreadContext (Lkotlin/coroutines/CoroutineContext;)Lio/sentry/IHub;
public fun updateThreadContext (Lkotlin/coroutines/CoroutineContext;)Lio/sentry/IScopes;
public synthetic fun updateThreadContext (Lkotlin/coroutines/CoroutineContext;)Ljava/lang/Object;
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.sentry.kotlin

import io.sentry.IHub
import io.sentry.IScopes
import io.sentry.Sentry
import kotlinx.coroutines.CopyableThreadContextElement
import kotlin.coroutines.AbstractCoroutineContextElement
Expand All @@ -9,26 +9,32 @@ import kotlin.coroutines.CoroutineContext
/**
* Sentry context element for [CoroutineContext].
*/
public class SentryContext(private val hub: IHub = Sentry.getCurrentHub().clone()) :
CopyableThreadContextElement<IHub>, AbstractCoroutineContextElement(Key) {
@SuppressWarnings("deprecation")
// TODO fork instead
public class SentryContext(private val scopes: IScopes = Sentry.getCurrentScopes().clone()) :
CopyableThreadContextElement<IScopes>, AbstractCoroutineContextElement(Key) {

private companion object Key : CoroutineContext.Key<SentryContext>

override fun copyForChild(): CopyableThreadContextElement<IHub> {
return SentryContext(hub.clone())
@SuppressWarnings("deprecation")
override fun copyForChild(): CopyableThreadContextElement<IScopes> {
// TODO fork instead
return SentryContext(scopes.clone())
}

@SuppressWarnings("deprecation")
override fun mergeForChild(overwritingElement: CoroutineContext.Element): CoroutineContext {
return overwritingElement[Key] ?: SentryContext(hub.clone())
// TODO fork instead?
return overwritingElement[Key] ?: SentryContext(scopes.clone())
}

override fun updateThreadContext(context: CoroutineContext): IHub {
val oldState = Sentry.getCurrentHub()
Sentry.setCurrentHub(hub)
override fun updateThreadContext(context: CoroutineContext): IScopes {
val oldState = Sentry.getCurrentScopes()
Sentry.setCurrentScopes(scopes)
return oldState
}

override fun restoreThreadContext(context: CoroutineContext, oldState: IHub) {
Sentry.setCurrentHub(oldState)
override fun restoreThreadContext(context: CoroutineContext, oldState: IScopes) {
Sentry.setCurrentScopes(oldState)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class SentryContextTest {

val c2 = launch(
SentryContext(
Sentry.getCurrentHub().clone().also {
Sentry.getCurrentScopes().clone().also {
it.setTag("cloned", "clonedValue")
}
)
Expand All @@ -145,7 +145,7 @@ class SentryContextTest {
@Test
fun `mergeForChild returns copy of initial context if Key not present`() {
val initialContextElement = SentryContext(
Sentry.getCurrentHub().clone().also {
Sentry.getCurrentScopes().clone().also {
it.setTag("cloned", "clonedValue")
}
)
Expand All @@ -158,7 +158,7 @@ class SentryContextTest {
@Test
fun `mergeForChild returns passed context`() {
val initialContextElement = SentryContext(
Sentry.getCurrentHub().clone().also {
Sentry.getCurrentScopes().clone().also {
it.setTag("cloned", "clonedValue")
}
)
Expand Down
4 changes: 2 additions & 2 deletions sentry-openfeign/api/sentry-openfeign.api
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
public final class io/sentry/openfeign/SentryCapability : feign/Capability {
public fun <init> ()V
public fun <init> (Lio/sentry/IHub;Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V
public fun <init> (Lio/sentry/IScopes;Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V
public fun <init> (Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V
public fun enrich (Lfeign/Client;)Lfeign/Client;
}

public final class io/sentry/openfeign/SentryFeignClient : feign/Client {
public fun <init> (Lfeign/Client;Lio/sentry/IHub;Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V
public fun <init> (Lfeign/Client;Lio/sentry/IScopes;Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V
public fun execute (Lfeign/Request;Lfeign/Request$Options;)Lfeign/Response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,34 @@

import feign.Capability;
import feign.Client;
import io.sentry.HubAdapter;
import io.sentry.IHub;
import io.sentry.IScopes;
import io.sentry.ScopesAdapter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/** Adds Sentry tracing capability to Feign clients. */
public final class SentryCapability implements Capability {

private final @NotNull IHub hub;
private final @NotNull IScopes scopes;
private final @Nullable SentryFeignClient.BeforeSpanCallback beforeSpan;

public SentryCapability(
final @NotNull IHub hub, final @Nullable SentryFeignClient.BeforeSpanCallback beforeSpan) {
this.hub = hub;
final @NotNull IScopes scopes,
final @Nullable SentryFeignClient.BeforeSpanCallback beforeSpan) {
this.scopes = scopes;
this.beforeSpan = beforeSpan;
}

public SentryCapability(final @Nullable SentryFeignClient.BeforeSpanCallback beforeSpan) {
this(HubAdapter.getInstance(), beforeSpan);
this(ScopesAdapter.getInstance(), beforeSpan);
}

public SentryCapability() {
this(HubAdapter.getInstance(), null);
this(ScopesAdapter.getInstance(), null);
}

@Override
public @NotNull Client enrich(final @NotNull Client client) {
return new SentryFeignClient(client, hub, beforeSpan);
return new SentryFeignClient(client, scopes, beforeSpan);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.sentry.BaggageHeader;
import io.sentry.Breadcrumb;
import io.sentry.Hint;
import io.sentry.IHub;
import io.sentry.IScopes;
import io.sentry.ISpan;
import io.sentry.SpanDataConvention;
import io.sentry.SpanStatus;
Expand All @@ -30,15 +30,15 @@
public final class SentryFeignClient implements Client {
private static final String TRACE_ORIGIN = "auto.http.openfeign";
private final @NotNull Client delegate;
private final @NotNull IHub hub;
private final @NotNull IScopes scopes;
private final @Nullable BeforeSpanCallback beforeSpan;

public SentryFeignClient(
final @NotNull Client delegate,
final @NotNull IHub hub,
final @NotNull IScopes scopes,
final @Nullable BeforeSpanCallback beforeSpan) {
this.delegate = Objects.requireNonNull(delegate, "delegate is required");
this.hub = Objects.requireNonNull(hub, "hub is required");
this.scopes = Objects.requireNonNull(scopes, "scopes are required");
this.beforeSpan = beforeSpan;
}

Expand All @@ -47,7 +47,7 @@ public Response execute(final @NotNull Request request, final @NotNull Request.O
throws IOException {
Response response = null;
try {
final ISpan activeSpan = hub.getSpan();
final ISpan activeSpan = scopes.getSpan();

if (activeSpan == null) {
final @NotNull Request modifiedRequest = maybeAddTracingHeaders(request, null);
Expand Down Expand Up @@ -102,7 +102,7 @@ public Response execute(final @NotNull Request request, final @NotNull Request.O

final @Nullable TracingUtils.TracingHeaders tracingHeaders =
TracingUtils.traceIfAllowed(
hub,
scopes,
request.url(),
(requestBaggageHeaders != null ? new ArrayList<>(requestBaggageHeaders) : null),
span);
Expand Down Expand Up @@ -139,7 +139,7 @@ private void addBreadcrumb(final @NotNull Request request, final @Nullable Respo
hint.set(OPEN_FEIGN_RESPONSE, response);
}

hub.addBreadcrumb(breadcrumb, hint);
scopes.addBreadcrumb(breadcrumb, hint);
}

static final class RequestWrapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import feign.HeaderMap
import feign.RequestLine
import io.sentry.BaggageHeader
import io.sentry.Breadcrumb
import io.sentry.IHub
import io.sentry.IScopes
import io.sentry.Scope
import io.sentry.ScopeCallback
import io.sentry.SentryOptions
Expand Down Expand Up @@ -37,7 +37,7 @@ import kotlin.test.fail
class SentryFeignClientTest {

class Fixture {
val hub = mock<IHub>()
val scopes = mock<IScopes>()
val server = MockWebServer()
val sentryTracer: SentryTracer
val sentryOptions = SentryOptions().apply {
Expand All @@ -46,9 +46,9 @@ class SentryFeignClientTest {
val scope = Scope(sentryOptions)

init {
whenever(hub.options).thenReturn(sentryOptions)
doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any())
sentryTracer = SentryTracer(TransactionContext("name", "op"), hub)
whenever(scopes.options).thenReturn(sentryOptions)
doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any())
sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes)
}

fun getSut(
Expand All @@ -59,7 +59,7 @@ class SentryFeignClientTest {
beforeSpan: SentryFeignClient.BeforeSpanCallback? = null
): MockApi {
if (isSpanActive) {
whenever(hub.span).thenReturn(sentryTracer)
whenever(scopes.span).thenReturn(sentryTracer)
}
server.enqueue(
MockResponse()
Expand All @@ -70,12 +70,12 @@ class SentryFeignClientTest {

return if (!networkError) {
Feign.builder()
.addCapability(SentryCapability(hub, beforeSpan))
.addCapability(SentryCapability(scopes, beforeSpan))
} else {
val mockClient = mock<Client>()
whenever(mockClient.execute(any(), any())).thenThrow(RuntimeException::class.java)
Feign.builder()
.client(SentryFeignClient(mockClient, hub, beforeSpan))
.client(SentryFeignClient(mockClient, scopes, beforeSpan))
}.target(MockApi::class.java, server.url("/").toUrl().toString())
}
}
Expand Down Expand Up @@ -201,7 +201,7 @@ class SentryFeignClientTest {
fun `adds breadcrumb when http calls succeeds`() {
val sut = fixture.getSut(responseBody = "response body")
sut.postWithBody("request-body")
verify(fixture.hub).addBreadcrumb(
verify(fixture.scopes).addBreadcrumb(
check<Breadcrumb> {
assertEquals("http", it.type)
assertEquals(13, it.data["response_body_size"])
Expand All @@ -215,7 +215,7 @@ class SentryFeignClientTest {
fun `adds breadcrumb when http calls succeeds even though response body is null`() {
val sut = fixture.getSut(responseBody = "")
sut.postWithBody("request-body")
verify(fixture.hub).addBreadcrumb(
verify(fixture.scopes).addBreadcrumb(
check<Breadcrumb> {
assertEquals("http", it.type)
assertEquals(0, it.data["response_body_size"])
Expand All @@ -236,7 +236,7 @@ class SentryFeignClientTest {
} catch (e: Exception) {
// ignore me
}
verify(fixture.hub).addBreadcrumb(
verify(fixture.scopes).addBreadcrumb(
check<Breadcrumb> {
assertEquals("http", it.type)
},
Expand Down
Loading