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