GraphQL Java Integration

Sentry GraphQL integration provides an integration with GraphQL Java through:

  • SentryDataFetcherExceptionHandler which captures exceptions thrown during data fetcher executions.
  • SentryInstrumentation which creates spans around each data fetcher execution.

Install

Copied
<dependency>
    <groupId>io.sentry</groupId>
    <artifactId>sentry-graphql</artifactId>
    <version>6.18.1</version>
</dependency>

For other dependency managers, check out the central Maven repository.

Capture Exceptions

SentryDataFetcherExceptionHandler captures the exception, sends it to Sentry, and calls the delegate responsible for handling the exception.

Create a new instance of SentryDataFetcherExceptionHandler, pass the delegate that handles the exception, and configure defaultDataFetcherExceptionHandler when building a GraphQL instance:

Copied
import graphql.GraphQL;
import graphql.execution.SimpleDataFetcherExceptionHandler;
import io.sentry.graphql.SentryDataFetcherExceptionHandler;

SimpleDataFetcherExceptionHandler defaultExceptionHandler = new SimpleDataFetcherExceptionHandler();
SentryDataFetcherExceptionHandler sentryExceptionHandler = new SentryDataFetcherExceptionHandler(defaultExceptionHandler);

GraphQL graphQL = GraphQL.newGraphQL(...)
    // ...
    .defaultDataFetcherExceptionHandler(sentryExceptionHandler)
    .build();

Capture Performance

Configure

Create a new instance of SentryInstrumentation and configure instrumentation when building a GraphQL instance:

Copied
import graphql.GraphQL;
import io.sentry.graphql.SentryInstrumentation;

GraphQL graphQL = GraphQL.newGraphQL(...)
    // ...
    .instrumentation(new SentryInstrumentation())
    .build();

Modify or Drop Spans

Spans created around requests can be modified or dropped using SentryInstrumentation.BeforeSpanCallback passed to SentryInstrumentation:

Copied
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;
    }))
    .build();

Using with Netflix DGS

Netflix DGS automatically detects and configures Instrumentation and DataFetcherExceptionHandler beans. To use Sentry GraphQL integration, create SentryDataFetcherExceptionHandler and SentryInstrumentation beans:

Copied
import com.netflix.graphql.dgs.exceptions.DefaultDataFetcherExceptionHandler;
import io.sentry.graphql.SentryDataFetcherExceptionHandler;
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();
  }

  @Bean
  SentryDataFetcherExceptionHandler sentryDataFetcherExceptionHandler() {
    // delegate to default Netflix DGS exception handler
    return new SentryDataFetcherExceptionHandler(new DefaultDataFetcherExceptionHandler());
  }
}
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").