-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathSentryInstrumentation.java
More file actions
158 lines (142 loc) · 6.73 KB
/
SentryInstrumentation.java
File metadata and controls
158 lines (142 loc) · 6.73 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
package io.sentry.graphql;
import graphql.ExecutionResult;
import graphql.execution.instrumentation.InstrumentationContext;
import graphql.execution.instrumentation.InstrumentationState;
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 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;
@SuppressWarnings("deprecation")
public final class SentryInstrumentation
extends graphql.execution.instrumentation.SimpleInstrumentation {
static {
SentryIntegrationPackageStorage.getInstance()
.addPackage("maven:io.sentry:sentry-graphql", 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.graphql";
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");
SentryIntegrationPackageStorage.getInstance()
.addPackage("maven:io.sentry:sentry-graphql", 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() {
return instrumentation.createState();
}
@Override
public @NotNull InstrumentationContext<ExecutionResult> beginExecution(
final @NotNull InstrumentationExecutionParameters parameters) {
final SentryGraphqlInstrumentation.TracingState tracingState =
parameters.getInstrumentationState();
instrumentation.beginExecution(parameters, tracingState);
return super.beginExecution(parameters);
}
@Override
public CompletableFuture<ExecutionResult> instrumentExecutionResult(
ExecutionResult executionResult, InstrumentationExecutionParameters parameters) {
return super.instrumentExecutionResult(executionResult, parameters)
.whenComplete(
(result, exception) -> {
instrumentation.instrumentExecutionResultComplete(parameters, result, exception);
});
}
@Override
public @NotNull InstrumentationContext<ExecutionResult> beginExecuteOperation(
final @NotNull InstrumentationExecuteOperationParameters parameters) {
instrumentation.beginExecuteOperation(parameters);
return super.beginExecuteOperation(parameters);
}
@Override
@SuppressWarnings({"FutureReturnValueIgnored", "deprecation"})
public @NotNull DataFetcher<?> instrumentDataFetcher(
final @NotNull DataFetcher<?> dataFetcher,
final @NotNull InstrumentationFieldFetchParameters parameters) {
final SentryGraphqlInstrumentation.TracingState tracingState =
parameters.getInstrumentationState();
return instrumentation.instrumentDataFetcher(dataFetcher, parameters, tracingState);
}
/**
* @deprecated please use {@link SentryGraphqlInstrumentation.BeforeSpanCallback}
*/
@Deprecated
@FunctionalInterface
public interface BeforeSpanCallback extends SentryGraphqlInstrumentation.BeforeSpanCallback {}
}