|
| 1 | +package io.sentry.graphql; |
| 2 | + |
| 3 | +import graphql.ExecutionResult; |
| 4 | +import graphql.execution.instrumentation.InstrumentationContext; |
| 5 | +import graphql.execution.instrumentation.InstrumentationState; |
| 6 | +import graphql.execution.instrumentation.SimpleInstrumentation; |
| 7 | +import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters; |
| 8 | +import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters; |
| 9 | +import graphql.schema.DataFetcher; |
| 10 | +import graphql.schema.DataFetchingEnvironment; |
| 11 | +import graphql.schema.GraphQLNonNull; |
| 12 | +import graphql.schema.GraphQLObjectType; |
| 13 | +import graphql.schema.GraphQLOutputType; |
| 14 | +import io.sentry.HubAdapter; |
| 15 | +import io.sentry.IHub; |
| 16 | +import io.sentry.ISpan; |
| 17 | +import io.sentry.SpanStatus; |
| 18 | +import io.sentry.util.Objects; |
| 19 | +import java.util.concurrent.CompletableFuture; |
| 20 | +import org.jetbrains.annotations.NotNull; |
| 21 | +import org.jetbrains.annotations.Nullable; |
| 22 | + |
| 23 | +public final class SentryInstrumentation extends SimpleInstrumentation { |
| 24 | + private final @NotNull IHub hub; |
| 25 | + private final @Nullable BeforeSpanCallback beforeSpan; |
| 26 | + |
| 27 | + public SentryInstrumentation( |
| 28 | + final @NotNull IHub hub, final @Nullable BeforeSpanCallback beforeSpan) { |
| 29 | + this.hub = Objects.requireNonNull(hub, "hub is required"); |
| 30 | + this.beforeSpan = beforeSpan; |
| 31 | + } |
| 32 | + |
| 33 | + public SentryInstrumentation(final @NotNull IHub hub) { |
| 34 | + this(hub, null); |
| 35 | + } |
| 36 | + |
| 37 | + public SentryInstrumentation() { |
| 38 | + this(HubAdapter.getInstance()); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public @NotNull InstrumentationState createState() { |
| 43 | + return new TracingState(); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public @NotNull InstrumentationContext<ExecutionResult> beginExecution( |
| 48 | + final @NotNull InstrumentationExecutionParameters parameters) { |
| 49 | + final TracingState tracingState = parameters.getInstrumentationState(); |
| 50 | + tracingState.setTransaction(hub.getSpan()); |
| 51 | + return super.beginExecution(parameters); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + @SuppressWarnings("FutureReturnValueIgnored") |
| 56 | + public @NotNull DataFetcher<?> instrumentDataFetcher( |
| 57 | + final @NotNull DataFetcher<?> dataFetcher, |
| 58 | + final @NotNull InstrumentationFieldFetchParameters parameters) { |
| 59 | + // We only care about user code |
| 60 | + if (parameters.isTrivialDataFetcher()) { |
| 61 | + return dataFetcher; |
| 62 | + } |
| 63 | + |
| 64 | + return environment -> { |
| 65 | + final TracingState tracingState = parameters.getInstrumentationState(); |
| 66 | + final ISpan transaction = tracingState.getTransaction(); |
| 67 | + if (transaction != null) { |
| 68 | + final ISpan span = transaction.startChild(findDataFetcherTag(parameters)); |
| 69 | + try { |
| 70 | + final Object result = dataFetcher.get(environment); |
| 71 | + if (result instanceof CompletableFuture) { |
| 72 | + ((CompletableFuture<?>) result) |
| 73 | + .whenComplete( |
| 74 | + (r, ex) -> { |
| 75 | + if (ex != null) { |
| 76 | + span.setThrowable(ex); |
| 77 | + span.setStatus(SpanStatus.INTERNAL_ERROR); |
| 78 | + } else { |
| 79 | + span.setStatus(SpanStatus.OK); |
| 80 | + } |
| 81 | + finish(span, environment, r); |
| 82 | + }); |
| 83 | + } else { |
| 84 | + span.setStatus(SpanStatus.OK); |
| 85 | + finish(span, environment, result); |
| 86 | + } |
| 87 | + return result; |
| 88 | + } catch (Exception e) { |
| 89 | + span.setThrowable(e); |
| 90 | + span.setStatus(SpanStatus.INTERNAL_ERROR); |
| 91 | + finish(span, environment); |
| 92 | + throw e; |
| 93 | + } |
| 94 | + } else { |
| 95 | + return dataFetcher.get(environment); |
| 96 | + } |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + private void finish( |
| 101 | + final @NotNull ISpan span, |
| 102 | + final @NotNull DataFetchingEnvironment environment, |
| 103 | + final @Nullable Object result) { |
| 104 | + if (beforeSpan != null) { |
| 105 | + final ISpan newSpan = beforeSpan.execute(span, environment, result); |
| 106 | + if (newSpan == null) { |
| 107 | + // span is dropped |
| 108 | + span.getSpanContext().setSampled(false); |
| 109 | + } else { |
| 110 | + newSpan.finish(); |
| 111 | + } |
| 112 | + } else { |
| 113 | + span.finish(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private void finish( |
| 118 | + final @NotNull ISpan span, final @NotNull DataFetchingEnvironment environment) { |
| 119 | + finish(span, environment, null); |
| 120 | + } |
| 121 | + |
| 122 | + private @NotNull String findDataFetcherTag( |
| 123 | + final @NotNull InstrumentationFieldFetchParameters parameters) { |
| 124 | + final GraphQLOutputType type = parameters.getExecutionStepInfo().getParent().getType(); |
| 125 | + GraphQLObjectType parent; |
| 126 | + if (type instanceof GraphQLNonNull) { |
| 127 | + parent = (GraphQLObjectType) ((GraphQLNonNull) type).getWrappedType(); |
| 128 | + } else { |
| 129 | + parent = (GraphQLObjectType) type; |
| 130 | + } |
| 131 | + |
| 132 | + return parent.getName() + "." + parameters.getExecutionStepInfo().getPath().getSegmentName(); |
| 133 | + } |
| 134 | + |
| 135 | + static final class TracingState implements InstrumentationState { |
| 136 | + private @Nullable ISpan transaction; |
| 137 | + |
| 138 | + public @Nullable ISpan getTransaction() { |
| 139 | + return transaction; |
| 140 | + } |
| 141 | + |
| 142 | + public void setTransaction(final @Nullable ISpan transaction) { |
| 143 | + this.transaction = transaction; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @FunctionalInterface |
| 148 | + public interface BeforeSpanCallback { |
| 149 | + @Nullable |
| 150 | + ISpan execute( |
| 151 | + @NotNull ISpan span, @NotNull DataFetchingEnvironment environment, @Nullable Object result); |
| 152 | + } |
| 153 | +} |
0 commit comments