GraphQL Java Integration
Learn how to capture exceptions and about the performance of queries executed with GraphQL Java.
Sentry's GraphQL Java integration is provided through:
SentryGenericDataFetcherExceptionHandler
, which checks for exceptions thrown during data fetcher executions and then passes them toSentryInstrumentation
.SentryInstrumentation
, which creates spans around each data fetcher execution, captures exceptions, and adds breadcrumbs.
Our GraphQL integration can be configured automatically if you're using spring-graphql
with either the sentry-spring-boot-starter
or the sentry-spring-boot-jakarta-starter
integration.
To install use:
plugins {
id "io.sentry.jvm.gradle" version "4.14.0"
}
For other dependency managers, check out the central Maven repository.
When building a GraphQL
instance:
- set
defaultDataFetcherExceptionHandler
to an instance ofSentryGenericDataFetcherExceptionHandler
and pass the delegate that handles the exception to the constructor - set
instrumentation
to an instance ofSentryInstrumentation
You may want to filter some of the errors by using beforeSend
or an EventProcessor
(read more about Filters).
import graphql.GraphQL;
import graphql.execution.SimpleDataFetcherExceptionHandler;
import io.sentry.graphql.SentryGenericDataFetcherExceptionHandler;
import io.sentry.graphql.SentryInstrumentation;
SimpleDataFetcherExceptionHandler defaultExceptionHandler = new SimpleDataFetcherExceptionHandler();
SentryGenericDataFetcherExceptionHandler sentryExceptionHandler = new SentryGenericDataFetcherExceptionHandler(defaultExceptionHandler);
GraphQL graphQL = GraphQL.newGraphQL(...)
// ...
.defaultDataFetcherExceptionHandler(sentryExceptionHandler)
.instrumentation(new SentryInstrumentation(
// If you're not using our Spring integration, please provide NoOpSubscriptionHandler.getInstance() instead.
new SentrySpringSubscriptionHandler(),
// Set this to false when using Spring WebMVC
true
))
.build();
The SentryDataFetcherExceptionHandler
has been deprecated. Please upgrade to SentryGenericDataFetcherExceptionHandler
and make sure SentryInstrumentation
is configured to have more exceptions captured, more detailed exceptions, breadcrumbs, and better hub propagation. You may want to filter the errors by using beforeSend
or an EventProcessor
(read more about Filters).
To be able to capture transactions, you have to first set up tracing.
Spans created around requests can be modified by returning a modified Span, or dropped by returning null
, using SentryInstrumentation.BeforeSpanCallback
passed to SentryInstrumentation
:
import io.sentry.graphql.SentryInstrumentation;
import graphql.GraphQL;
GraphQL graphQL = GraphQL.newGraphQL()
// ...
.instrumentation(new SentryInstrumentation((span, environment, result) -> {
if ("/shows".equals(environment.getExecutionStepInfo().getPath().segmentToString())) {
span.setTag("tag-name", "tag-value");
}
return span;
}, new SentrySpringSubscriptionHandler(), true))
.build();
Netflix DGS automatically detects and configures Instrumentation
and DataFetcherExceptionHandler
beans. To use the Sentry GraphQL integration, create SentryGenericDataFetcherExceptionHandler
and SentryInstrumentation
beans:
import com.netflix.graphql.dgs.exceptions.DefaultDataFetcherExceptionHandler;
import io.sentry.graphql.SentryGenericDataFetcherExceptionHandler;
import io.sentry.graphql.SentryInstrumentation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
class SentryConfiguration {
@Bean
SentryInstrumentation sentryInstrumentation() {
return new SentryInstrumentation(new SentryDgsSubscriptionHandler(), true);
}
@Bean
SentryGenericDataFetcherExceptionHandler sentryDataFetcherExceptionHandler() {
// delegate to default Netflix DGS exception handler
return new SentryGenericDataFetcherExceptionHandler(new DefaultDataFetcherExceptionHandler());
}
}
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").