-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathSentryInstrumentation.java
More file actions
167 lines (151 loc) · 7.26 KB
/
SentryInstrumentation.java
File metadata and controls
167 lines (151 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package io.sentry.graphql22;
import graphql.ExecutionResult;
import graphql.execution.instrumentation.InstrumentationContext;
import graphql.execution.instrumentation.InstrumentationState;
import graphql.execution.instrumentation.parameters.InstrumentationCreateStateParameters;
import graphql.execution.instrumentation.parameters.InstrumentationExecuteOperationParameters;
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters;
import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters;
import graphql.schema.DataFetcher;
import io.sentry.SentryIntegrationPackageStorage;
import io.sentry.graphql.ExceptionReporter;
import io.sentry.graphql.SentryGraphqlInstrumentation;
import io.sentry.graphql.SentrySubscriptionHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
public final class SentryInstrumentation
extends graphql.execution.instrumentation.SimplePerformantInstrumentation {
static {
SentryIntegrationPackageStorage.getInstance()
.addPackage("maven:io.sentry:sentry-graphql-22", BuildConfig.VERSION_NAME);
}
/**
* @deprecated please use {@link SentryGraphqlInstrumentation#SENTRY_SCOPES_CONTEXT_KEY}
*/
@Deprecated
public static final @NotNull String SENTRY_SCOPES_CONTEXT_KEY =
SentryGraphqlInstrumentation.SENTRY_SCOPES_CONTEXT_KEY;
/**
* @deprecated please use {@link SentryGraphqlInstrumentation#SENTRY_EXCEPTIONS_CONTEXT_KEY}
*/
@Deprecated
public static final @NotNull String SENTRY_EXCEPTIONS_CONTEXT_KEY =
SentryGraphqlInstrumentation.SENTRY_EXCEPTIONS_CONTEXT_KEY;
private static final String TRACE_ORIGIN = "auto.graphql.graphql22";
private final @NotNull SentryGraphqlInstrumentation instrumentation;
/**
* @param beforeSpan callback when a span is created
* @param subscriptionHandler can report subscription errors
* @param captureRequestBodyForNonSubscriptions false if request bodies should not be captured by
* this integration for query and mutation operations. This can be used to prevent unnecessary
* work by not adding the request body when another integration will add it anyways, as is the
* case with our spring integration for WebMVC.
*/
public SentryInstrumentation(
final @Nullable SentryGraphqlInstrumentation.BeforeSpanCallback beforeSpan,
final @NotNull SentrySubscriptionHandler subscriptionHandler,
final boolean captureRequestBodyForNonSubscriptions) {
this(
beforeSpan,
subscriptionHandler,
new ExceptionReporter(captureRequestBodyForNonSubscriptions),
new ArrayList<>());
}
/**
* @param beforeSpan callback when a span is created
* @param subscriptionHandler can report subscription errors
* @param captureRequestBodyForNonSubscriptions false if request bodies should not be captured by
* this integration for query and mutation operations. This can be used to prevent unnecessary
* work by not adding the request body when another integration will add it anyways, as is the
* case with our spring integration for WebMVC.
* @param ignoredErrorTypes list of error types that should not be captured and sent to Sentry
*/
public SentryInstrumentation(
final @Nullable SentryGraphqlInstrumentation.BeforeSpanCallback beforeSpan,
final @NotNull SentrySubscriptionHandler subscriptionHandler,
final boolean captureRequestBodyForNonSubscriptions,
final @NotNull List<String> ignoredErrorTypes) {
this(
beforeSpan,
subscriptionHandler,
new ExceptionReporter(captureRequestBodyForNonSubscriptions),
ignoredErrorTypes);
}
@TestOnly
public SentryInstrumentation(
final @Nullable SentryGraphqlInstrumentation.BeforeSpanCallback beforeSpan,
final @NotNull SentrySubscriptionHandler subscriptionHandler,
final @NotNull ExceptionReporter exceptionReporter,
final @NotNull List<String> ignoredErrorTypes) {
this.instrumentation =
new SentryGraphqlInstrumentation(
beforeSpan, subscriptionHandler, exceptionReporter, ignoredErrorTypes, TRACE_ORIGIN);
SentryIntegrationPackageStorage.getInstance().addIntegration("GraphQL-v22");
SentryIntegrationPackageStorage.getInstance()
.addPackage("maven:io.sentry:sentry-graphql-22", BuildConfig.VERSION_NAME);
}
/**
* @param subscriptionHandler can report subscription errors
* @param captureRequestBodyForNonSubscriptions false if request bodies should not be captured by
* this integration for query and mutation operations. This can be used to prevent unnecessary
* work by not adding the request body when another integration will add it anyways, as is the
* case with our spring integration for WebMVC.
*/
public SentryInstrumentation(
final @NotNull SentrySubscriptionHandler subscriptionHandler,
final boolean captureRequestBodyForNonSubscriptions) {
this(null, subscriptionHandler, captureRequestBodyForNonSubscriptions);
}
@Override
public @NotNull InstrumentationState createState(
final @NotNull InstrumentationCreateStateParameters parameters) {
return instrumentation.createState();
}
@Override
public @Nullable InstrumentationContext<ExecutionResult> beginExecution(
final @NotNull InstrumentationExecutionParameters parameters,
final @NotNull InstrumentationState state) {
final SentryGraphqlInstrumentation.TracingState tracingState =
InstrumentationState.ofState(state);
instrumentation.beginExecution(parameters, tracingState);
return super.beginExecution(parameters, state);
}
@Override
public @NotNull CompletableFuture<ExecutionResult> instrumentExecutionResult(
final @NotNull ExecutionResult executionResult,
final @NotNull InstrumentationExecutionParameters parameters,
final @NotNull InstrumentationState state) {
return super.instrumentExecutionResult(executionResult, parameters, state)
.whenComplete(
(result, exception) -> {
instrumentation.instrumentExecutionResultComplete(parameters, result, exception);
});
}
@Override
public @Nullable InstrumentationContext<ExecutionResult> beginExecuteOperation(
final @NotNull InstrumentationExecuteOperationParameters parameters,
final @NotNull InstrumentationState state) {
instrumentation.beginExecuteOperation(parameters);
return super.beginExecuteOperation(parameters, state);
}
@Override
@SuppressWarnings({"FutureReturnValueIgnored"})
public @NotNull DataFetcher<?> instrumentDataFetcher(
final @NotNull DataFetcher<?> dataFetcher,
final @NotNull InstrumentationFieldFetchParameters parameters,
final @NotNull InstrumentationState state) {
final SentryGraphqlInstrumentation.TracingState tracingState =
InstrumentationState.ofState(state);
return instrumentation.instrumentDataFetcher(dataFetcher, parameters, tracingState);
}
/**
* @deprecated please use {@link SentryGraphqlInstrumentation.BeforeSpanCallback}
*/
@Deprecated
@FunctionalInterface
public interface BeforeSpanCallback extends SentryGraphqlInstrumentation.BeforeSpanCallback {}
}