Skip to content

Commit 784341e

Browse files
authored
Hubs/Scopes Merge 7 - Replace IHub with IScopes in GraphQL integration (getsentry#3303)
* Introduce `IScopes` interface. * Replace `IHub` with `IScopes` in core * Replace `IHub` with `IScopes` in android core * Replace `IHub` with `IScopes` in android integrations * Replace `IHub` with `IScopes` in apollo integrations * Replace `IHub` with `IScopes` in okhttp integration * Replace `IHub` with `IScopes` in graphql integration
1 parent c1840cf commit 784341e

12 files changed

+132
-117
lines changed

sentry-graphql/api/sentry-graphql.api

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ public final class io/sentry/graphql/ExceptionReporter {
99
}
1010

1111
public final class io/sentry/graphql/ExceptionReporter$ExceptionDetails {
12-
public fun <init> (Lio/sentry/IHub;Lgraphql/execution/instrumentation/parameters/InstrumentationExecutionParameters;Z)V
13-
public fun <init> (Lio/sentry/IHub;Lgraphql/schema/DataFetchingEnvironment;Z)V
14-
public fun getHub ()Lio/sentry/IHub;
12+
public fun <init> (Lio/sentry/IScopes;Lgraphql/execution/instrumentation/parameters/InstrumentationExecutionParameters;Z)V
13+
public fun <init> (Lio/sentry/IScopes;Lgraphql/schema/DataFetchingEnvironment;Z)V
14+
public fun getHub ()Lio/sentry/IScopes;
1515
public fun getQuery ()Ljava/lang/String;
16+
public fun getScopes ()Lio/sentry/IScopes;
1617
public fun getVariables ()Ljava/util/Map;
1718
public fun isSubscription ()Z
1819
}
@@ -26,19 +27,19 @@ public final class io/sentry/graphql/GraphqlStringUtils {
2627

2728
public final class io/sentry/graphql/NoOpSubscriptionHandler : io/sentry/graphql/SentrySubscriptionHandler {
2829
public static fun getInstance ()Lio/sentry/graphql/NoOpSubscriptionHandler;
29-
public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IHub;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object;
30+
public fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IScopes;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object;
3031
}
3132

3233
public final class io/sentry/graphql/SentryDataFetcherExceptionHandler : graphql/execution/DataFetcherExceptionHandler {
3334
public fun <init> (Lgraphql/execution/DataFetcherExceptionHandler;)V
34-
public fun <init> (Lio/sentry/IHub;Lgraphql/execution/DataFetcherExceptionHandler;)V
35+
public fun <init> (Lio/sentry/IScopes;Lgraphql/execution/DataFetcherExceptionHandler;)V
3536
public fun handleException (Lgraphql/execution/DataFetcherExceptionHandlerParameters;)Ljava/util/concurrent/CompletableFuture;
3637
public fun onException (Lgraphql/execution/DataFetcherExceptionHandlerParameters;)Lgraphql/execution/DataFetcherExceptionHandlerResult;
3738
}
3839

3940
public final class io/sentry/graphql/SentryGenericDataFetcherExceptionHandler : graphql/execution/DataFetcherExceptionHandler {
4041
public fun <init> (Lgraphql/execution/DataFetcherExceptionHandler;)V
41-
public fun <init> (Lio/sentry/IHub;Lgraphql/execution/DataFetcherExceptionHandler;)V
42+
public fun <init> (Lio/sentry/IScopes;Lgraphql/execution/DataFetcherExceptionHandler;)V
4243
public fun handleException (Lgraphql/execution/DataFetcherExceptionHandlerParameters;)Ljava/util/concurrent/CompletableFuture;
4344
public fun onException (Lgraphql/execution/DataFetcherExceptionHandlerParameters;)Lgraphql/execution/DataFetcherExceptionHandlerResult;
4445
}
@@ -51,9 +52,10 @@ public final class io/sentry/graphql/SentryGraphqlExceptionHandler {
5152
public final class io/sentry/graphql/SentryInstrumentation : graphql/execution/instrumentation/SimpleInstrumentation {
5253
public static final field SENTRY_EXCEPTIONS_CONTEXT_KEY Ljava/lang/String;
5354
public static final field SENTRY_HUB_CONTEXT_KEY Ljava/lang/String;
55+
public static final field SENTRY_SCOPES_CONTEXT_KEY Ljava/lang/String;
5456
public fun <init> ()V
55-
public fun <init> (Lio/sentry/IHub;)V
56-
public fun <init> (Lio/sentry/IHub;Lio/sentry/graphql/SentryInstrumentation$BeforeSpanCallback;)V
57+
public fun <init> (Lio/sentry/IScopes;)V
58+
public fun <init> (Lio/sentry/IScopes;Lio/sentry/graphql/SentryInstrumentation$BeforeSpanCallback;)V
5759
public fun <init> (Lio/sentry/graphql/SentryInstrumentation$BeforeSpanCallback;)V
5860
public fun <init> (Lio/sentry/graphql/SentryInstrumentation$BeforeSpanCallback;Lio/sentry/graphql/SentrySubscriptionHandler;Lio/sentry/graphql/ExceptionReporter;Ljava/util/List;)V
5961
public fun <init> (Lio/sentry/graphql/SentryInstrumentation$BeforeSpanCallback;Lio/sentry/graphql/SentrySubscriptionHandler;Z)V
@@ -71,6 +73,6 @@ public abstract interface class io/sentry/graphql/SentryInstrumentation$BeforeSp
7173
}
7274

7375
public abstract interface class io/sentry/graphql/SentrySubscriptionHandler {
74-
public abstract fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IHub;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object;
76+
public abstract fun onSubscriptionResult (Ljava/lang/Object;Lio/sentry/IScopes;Lio/sentry/graphql/ExceptionReporter;Lgraphql/execution/instrumentation/parameters/InstrumentationFieldFetchParameters;)Ljava/lang/Object;
7577
}
7678

sentry-graphql/src/main/java/io/sentry/graphql/ExceptionReporter.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import graphql.language.AstPrinter;
66
import graphql.schema.DataFetchingEnvironment;
77
import io.sentry.Hint;
8-
import io.sentry.IHub;
8+
import io.sentry.IScopes;
99
import io.sentry.SentryEvent;
1010
import io.sentry.SentryLevel;
1111
import io.sentry.SentryOptions;
@@ -33,7 +33,7 @@ public void captureThrowable(
3333
final @NotNull Throwable throwable,
3434
final @NotNull ExceptionDetails exceptionDetails,
3535
final @Nullable ExecutionResult result) {
36-
final @NotNull IHub hub = exceptionDetails.getHub();
36+
final @NotNull IScopes scopes = exceptionDetails.getScopes();
3737
final @NotNull Mechanism mechanism = new Mechanism();
3838
mechanism.setType(MECHANISM_TYPE);
3939
mechanism.setHandled(false);
@@ -43,44 +43,44 @@ public void captureThrowable(
4343
event.setLevel(SentryLevel.FATAL);
4444

4545
final @NotNull Hint hint = new Hint();
46-
setRequestDetailsOnEvent(hub, exceptionDetails, event);
46+
setRequestDetailsOnEvent(scopes, exceptionDetails, event);
4747

48-
if (result != null && isAllowedToAttachBody(hub)) {
48+
if (result != null && isAllowedToAttachBody(scopes)) {
4949
final @NotNull Response response = new Response();
5050
final @NotNull Map<String, Object> responseBody = result.toSpecification();
5151
response.setData(responseBody);
5252
event.getContexts().setResponse(response);
5353
}
5454

55-
hub.captureEvent(event, hint);
55+
scopes.captureEvent(event, hint);
5656
}
5757

58-
private boolean isAllowedToAttachBody(final @NotNull IHub hub) {
59-
final @NotNull SentryOptions options = hub.getOptions();
58+
private boolean isAllowedToAttachBody(final @NotNull IScopes scopes) {
59+
final @NotNull SentryOptions options = scopes.getOptions();
6060
return options.isSendDefaultPii()
6161
&& !SentryOptions.RequestSize.NONE.equals(options.getMaxRequestBodySize());
6262
}
6363

6464
private void setRequestDetailsOnEvent(
65-
final @NotNull IHub hub,
65+
final @NotNull IScopes scopes,
6666
final @NotNull ExceptionDetails exceptionDetails,
6767
final @NotNull SentryEvent event) {
68-
hub.configureScope(
68+
scopes.configureScope(
6969
(scope) -> {
7070
final @Nullable Request scopeRequest = scope.getRequest();
7171
final @NotNull Request request = scopeRequest == null ? new Request() : scopeRequest;
72-
setDetailsOnRequest(hub, exceptionDetails, request);
72+
setDetailsOnRequest(scopes, exceptionDetails, request);
7373
event.setRequest(request);
7474
});
7575
}
7676

7777
private void setDetailsOnRequest(
78-
final @NotNull IHub hub,
78+
final @NotNull IScopes scopes,
7979
final @NotNull ExceptionDetails exceptionDetails,
8080
final @NotNull Request request) {
8181
request.setApiTarget("graphql");
8282

83-
if (isAllowedToAttachBody(hub)
83+
if (isAllowedToAttachBody(scopes)
8484
&& (exceptionDetails.isSubscription() || captureRequestBodyForNonSubscriptions)) {
8585
final @NotNull Map<String, Object> data = new HashMap<>();
8686

@@ -99,27 +99,27 @@ private void setDetailsOnRequest(
9999

100100
public static final class ExceptionDetails {
101101

102-
private final @NotNull IHub hub;
102+
private final @NotNull IScopes scopes;
103103
private final @Nullable InstrumentationExecutionParameters instrumentationExecutionParameters;
104104
private final @Nullable DataFetchingEnvironment dataFetchingEnvironment;
105105

106106
private final boolean isSubscription;
107107

108108
public ExceptionDetails(
109-
final @NotNull IHub hub,
109+
final @NotNull IScopes scopes,
110110
final @Nullable InstrumentationExecutionParameters instrumentationExecutionParameters,
111111
final boolean isSubscription) {
112-
this.hub = hub;
112+
this.scopes = scopes;
113113
this.instrumentationExecutionParameters = instrumentationExecutionParameters;
114114
dataFetchingEnvironment = null;
115115
this.isSubscription = isSubscription;
116116
}
117117

118118
public ExceptionDetails(
119-
final @NotNull IHub hub,
119+
final @NotNull IScopes scopes,
120120
final @Nullable DataFetchingEnvironment dataFetchingEnvironment,
121121
final boolean isSubscription) {
122-
this.hub = hub;
122+
this.scopes = scopes;
123123
this.dataFetchingEnvironment = dataFetchingEnvironment;
124124
instrumentationExecutionParameters = null;
125125
this.isSubscription = isSubscription;
@@ -149,8 +149,13 @@ public boolean isSubscription() {
149149
return isSubscription;
150150
}
151151

152-
public @NotNull IHub getHub() {
153-
return hub;
152+
@Deprecated
153+
public @NotNull IScopes getHub() {
154+
return scopes;
155+
}
156+
157+
public @NotNull IScopes getScopes() {
158+
return scopes;
154159
}
155160
}
156161
}

sentry-graphql/src/main/java/io/sentry/graphql/NoOpSubscriptionHandler.java

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

33
import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters;
4-
import io.sentry.IHub;
4+
import io.sentry.IScopes;
55
import org.jetbrains.annotations.NotNull;
66

77
public final class NoOpSubscriptionHandler implements SentrySubscriptionHandler {
@@ -17,7 +17,7 @@ private NoOpSubscriptionHandler() {}
1717
@Override
1818
public @NotNull Object onSubscriptionResult(
1919
@NotNull Object result,
20-
@NotNull IHub hub,
20+
@NotNull IScopes scopes,
2121
@NotNull ExceptionReporter exceptionReporter,
2222
@NotNull InstrumentationFieldFetchParameters parameters) {
2323
return result;

sentry-graphql/src/main/java/io/sentry/graphql/SentryDataFetcherExceptionHandler.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import graphql.execution.DataFetcherExceptionHandlerParameters;
77
import graphql.execution.DataFetcherExceptionHandlerResult;
88
import io.sentry.Hint;
9-
import io.sentry.HubAdapter;
10-
import io.sentry.IHub;
9+
import io.sentry.IScopes;
10+
import io.sentry.ScopesAdapter;
1111
import io.sentry.SentryIntegrationPackageStorage;
1212
import io.sentry.util.Objects;
1313
import java.util.concurrent.CompletableFuture;
@@ -24,18 +24,18 @@
2424
*/
2525
@Deprecated
2626
public final class SentryDataFetcherExceptionHandler implements DataFetcherExceptionHandler {
27-
private final @NotNull IHub hub;
27+
private final @NotNull IScopes scopes;
2828
private final @NotNull DataFetcherExceptionHandler delegate;
2929

3030
public SentryDataFetcherExceptionHandler(
31-
final @NotNull IHub hub, final @NotNull DataFetcherExceptionHandler delegate) {
32-
this.hub = Objects.requireNonNull(hub, "hub is required");
31+
final @NotNull IScopes scopes, final @NotNull DataFetcherExceptionHandler delegate) {
32+
this.scopes = Objects.requireNonNull(scopes, "scopes are required");
3333
this.delegate = Objects.requireNonNull(delegate, "delegate is required");
3434
SentryIntegrationPackageStorage.getInstance().addIntegration("GrahQLLegacyExceptionHandler");
3535
}
3636

3737
public SentryDataFetcherExceptionHandler(final @NotNull DataFetcherExceptionHandler delegate) {
38-
this(HubAdapter.getInstance(), delegate);
38+
this(ScopesAdapter.getInstance(), delegate);
3939
}
4040

4141
@Override
@@ -44,7 +44,7 @@ public CompletableFuture<DataFetcherExceptionHandlerResult> handleException(
4444
final Hint hint = new Hint();
4545
hint.set(GRAPHQL_HANDLER_PARAMETERS, handlerParameters);
4646

47-
hub.captureException(handlerParameters.getException(), hint);
47+
scopes.captureException(handlerParameters.getException(), hint);
4848
return delegate.handleException(handlerParameters);
4949
}
5050

sentry-graphql/src/main/java/io/sentry/graphql/SentryGenericDataFetcherExceptionHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import graphql.execution.DataFetcherExceptionHandler;
44
import graphql.execution.DataFetcherExceptionHandlerParameters;
55
import graphql.execution.DataFetcherExceptionHandlerResult;
6-
import io.sentry.IHub;
6+
import io.sentry.IScopes;
77
import java.util.concurrent.CompletableFuture;
88
import java.util.concurrent.ExecutionException;
99
import org.jetbrains.annotations.NotNull;
@@ -17,7 +17,7 @@ public final class SentryGenericDataFetcherExceptionHandler implements DataFetch
1717
private final @NotNull SentryGraphqlExceptionHandler handler;
1818

1919
public SentryGenericDataFetcherExceptionHandler(
20-
final @Nullable IHub hub, final @NotNull DataFetcherExceptionHandler delegate) {
20+
final @Nullable IScopes scopes, final @NotNull DataFetcherExceptionHandler delegate) {
2121
this.handler = new SentryGraphqlExceptionHandler(delegate);
2222
}
2323

sentry-graphql/src/main/java/io/sentry/graphql/SentryInstrumentation.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
import graphql.schema.GraphQLObjectType;
1919
import graphql.schema.GraphQLOutputType;
2020
import io.sentry.Breadcrumb;
21-
import io.sentry.IHub;
21+
import io.sentry.IScopes;
2222
import io.sentry.ISpan;
23-
import io.sentry.NoOpHub;
23+
import io.sentry.NoOpScopes;
2424
import io.sentry.Sentry;
2525
import io.sentry.SentryIntegrationPackageStorage;
2626
import io.sentry.SpanStatus;
@@ -46,7 +46,11 @@ public final class SentryInstrumentation
4646
"INTERNAL", // Netflix DGS
4747
"DataFetchingException" // raw graphql-java
4848
);
49-
public static final @NotNull String SENTRY_HUB_CONTEXT_KEY = "sentry.hub";
49+
public static final @NotNull String SENTRY_SCOPES_CONTEXT_KEY = "sentry.scopes";
50+
51+
@Deprecated
52+
public static final @NotNull String SENTRY_HUB_CONTEXT_KEY = SENTRY_SCOPES_CONTEXT_KEY;
53+
5054
public static final @NotNull String SENTRY_EXCEPTIONS_CONTEXT_KEY = "sentry.exceptions";
5155
private static final String TRACE_ORIGIN = "auto.graphql.graphql";
5256
private final @Nullable BeforeSpanCallback beforeSpan;
@@ -70,7 +74,7 @@ public SentryInstrumentation() {
7074
*/
7175
@Deprecated
7276
@SuppressWarnings("InlineMeSuggester")
73-
public SentryInstrumentation(final @Nullable IHub hub) {
77+
public SentryInstrumentation(final @Nullable IScopes scopes) {
7478
this(null, NoOpSubscriptionHandler.getInstance(), true);
7579
}
7680

@@ -89,7 +93,7 @@ public SentryInstrumentation(final @Nullable BeforeSpanCallback beforeSpan) {
8993
@Deprecated
9094
@SuppressWarnings("InlineMeSuggester")
9195
public SentryInstrumentation(
92-
final @Nullable IHub hub, final @Nullable BeforeSpanCallback beforeSpan) {
96+
final @Nullable IScopes scopes, final @Nullable BeforeSpanCallback beforeSpan) {
9397
this(beforeSpan, NoOpSubscriptionHandler.getInstance(), true);
9498
}
9599

@@ -172,9 +176,9 @@ public SentryInstrumentation(
172176
public @NotNull InstrumentationContext<ExecutionResult> beginExecution(
173177
final @NotNull InstrumentationExecutionParameters parameters) {
174178
final TracingState tracingState = parameters.getInstrumentationState();
175-
final @NotNull IHub currentHub = Sentry.getCurrentHub();
176-
tracingState.setTransaction(currentHub.getSpan());
177-
parameters.getGraphQLContext().put(SENTRY_HUB_CONTEXT_KEY, currentHub);
179+
final @NotNull IScopes currentScopes = Sentry.getCurrentScopes();
180+
tracingState.setTransaction(currentScopes.getSpan());
181+
parameters.getGraphQLContext().put(SENTRY_SCOPES_CONTEXT_KEY, currentScopes);
178182
return super.beginExecution(parameters);
179183
}
180184

@@ -195,7 +199,7 @@ public CompletableFuture<ExecutionResult> instrumentExecutionResult(
195199
exceptionReporter.captureThrowable(
196200
throwable,
197201
new ExceptionReporter.ExceptionDetails(
198-
hubFromContext(graphQLContext), parameters, false),
202+
scopesFromContext(graphQLContext), parameters, false),
199203
result);
200204
}
201205
}
@@ -207,7 +211,7 @@ public CompletableFuture<ExecutionResult> instrumentExecutionResult(
207211
exceptionReporter.captureThrowable(
208212
new RuntimeException(error.getMessage()),
209213
new ExceptionReporter.ExceptionDetails(
210-
hubFromContext(graphQLContext), parameters, false),
214+
scopesFromContext(graphQLContext), parameters, false),
211215
result);
212216
}
213217
}
@@ -217,7 +221,7 @@ public CompletableFuture<ExecutionResult> instrumentExecutionResult(
217221
exceptionReporter.captureThrowable(
218222
exception,
219223
new ExceptionReporter.ExceptionDetails(
220-
hubFromContext(parameters.getGraphQLContext()), parameters, false),
224+
scopesFromContext(parameters.getGraphQLContext()), parameters, false),
221225
null);
222226
}
223227
});
@@ -262,7 +266,7 @@ private boolean isIgnored(final @Nullable String errorType) {
262266
operationDefinition.getOperation();
263267
final @Nullable String operationType =
264268
operation == null ? null : operation.name().toLowerCase(Locale.ROOT);
265-
hubFromContext(parameters.getExecutionContext().getGraphQLContext())
269+
scopesFromContext(parameters.getExecutionContext().getGraphQLContext())
266270
.addBreadcrumb(
267271
Breadcrumb.graphqlOperation(
268272
operationDefinition.getName(),
@@ -273,11 +277,11 @@ private boolean isIgnored(final @Nullable String errorType) {
273277
return super.beginExecuteOperation(parameters);
274278
}
275279

276-
private @NotNull IHub hubFromContext(final @Nullable GraphQLContext context) {
280+
private @NotNull IScopes scopesFromContext(final @Nullable GraphQLContext context) {
277281
if (context == null) {
278-
return NoOpHub.getInstance();
282+
return NoOpScopes.getInstance();
279283
}
280-
return context.getOrDefault(SENTRY_HUB_CONTEXT_KEY, NoOpHub.getInstance());
284+
return context.getOrDefault(SENTRY_SCOPES_CONTEXT_KEY, NoOpScopes.getInstance());
281285
}
282286

283287
@Override
@@ -293,7 +297,7 @@ private boolean isIgnored(final @Nullable String errorType) {
293297
return environment -> {
294298
final @Nullable ExecutionStepInfo executionStepInfo = environment.getExecutionStepInfo();
295299
if (executionStepInfo != null) {
296-
hubFromContext(parameters.getExecutionContext().getGraphQLContext())
300+
scopesFromContext(parameters.getExecutionContext().getGraphQLContext())
297301
.addBreadcrumb(
298302
Breadcrumb.graphqlDataFetcher(
299303
StringUtils.toString(executionStepInfo.getPath()),
@@ -351,7 +355,7 @@ private boolean isIgnored(final @Nullable String errorType) {
351355
environment.getOperationDefinition().getOperation())) {
352356
return subscriptionHandler.onSubscriptionResult(
353357
tmpResult,
354-
hubFromContext(environment.getGraphQlContext()),
358+
scopesFromContext(environment.getGraphQlContext()),
355359
exceptionReporter,
356360
parameters);
357361
}

0 commit comments

Comments
 (0)