How to Monitor Python GraphQL API With Sentry

As engineering teams are adopting CI/CD practices, software products are getting delivered faster than ever. Error monitoring comes in handy as it provides developers the confidence to deploy faster while knowing there’s a safety net watching their deployments.

On top of that, having error monitoring helps developers to fix and debug errors faster, in order words, developers can spend more quality time building new high-quality features.

The goal of this post is to document and share some of the issues and tips that I have learned while I was working on setting up Sentry monitoring with Django Graphene projects.


How to:

  • Hide Traceback (most recent call last) logger errors
  • Log Sentry errors with Django Graphene using middleware
  • Monitor your GraphQL query performance on Sentry
  • Log each GraphQL operation correctly on Sentry

Getting Started With Django

For starters, you can easily set up your Django projects with Sentry by following the official documentation here.

Here’s an example of a with Sentry SDK:

import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.logging import ignore_logger


Check out Sentry’s documentation to understand what these configuration options mean.

Traceback Error Spams


As I was using the default setup while working with Graphene, I ran into issues where my Django Graphene project was getting spammed with this Sentry issue:

Traceback error on Sentry
Sentry was getting spammed with Traceback (most recent call last) error

Looking from the Sentry issue, as this error message isn’t particularly meaningful, nor actionable — which can cause error fatigue in the long run, we chose to ignore it.


In order to get around this, simply ignore this by using ignore_logger from sentry_sdk .

In this case, we would be ignoring the graphql.execution.utils , which is the source of the Traceback errors.

from sentry_sdk.integrations.logging import ignore_logger


You can easily ignore other loggers with this method too!

Error Monitoring


As we try to forward exceptions to Sentry, there was an apparent issue where Graphene Django would swallow everything, especially after ignoring the graphql.execution.utils logger.


One of the recommended solutions here is to use create and use middleware inside your Graphene settings.

Start by creating a simple SentryMiddeware to capture all the exceptions thrown.

class SentryMiddleware(object):
    def on_error(self, error):
        raise error
    def resolve(self, next, root, info: ResolveInfo, **args):
        return next(root, info, **args).catch(self.on_error)

Next, simply add the newly created SentryMiddleware to your GRAPHENE setting inside your .

    'SCHEMA': 'project.schema.schema',
    'SCHEMA_OUTPUT': 'schema.graphql',
        # ...

Issue Example on Sentry
Yay! All errors are logged properly now on the Sentry issue.

While this is much better than just Traceback error, the operation name /graphql is still rather inaccurate.

Ideally, this should be named as the operation name of your GraphQL query or mutation, i.e. updateArticle , createPublication etc. We will work on naming each query and mutation correctly next.

Performance Monitoring

Besides error monitoring, Sentry is able to track your software performance by measuring metrics such as throughput and latency. Here’s the official documentation to set up performance monitoring for your Django project.

Example of Performance monitoring on Sentry
Query performance log with SQL statement breakdown

To set this up with your Django Graphene project, you would need to create a custom GraphQLView.

from sentry_sdk.api import start_transaction
from graphene_django.views import GraphQLView

class CustomGraphQLView(GraphQLView):
    def execute_graphql_request(
        request: HttpRequest,
        with start_transaction():
            return super().execute_graphql_request(request, data, query, variables, operation_name, show_graphiql)

GraphQL Performance example
Our GraphQL query will not be logged on Sentry performance


From the code snippet above, each of your GraphQL requests will now be logged under the performance tab on Sentry. Though now we have another problem — all our transactions are being named as /graphql , while the operation is always http.server

We don’t have a clear idea of what we’re looking at or what we’re tracking.


Let’s get the operation type and operation name from Graphene.

class CustomGraphQLView(GraphQLView):
    def execute_graphql_request(
        request: HttpRequest,
        operation_type = (
            .document_from_string(self.schema, query)
        with start_transaction(op=operation_type, name=operation_name):
            return super().execute_graphql_request(
                request, data, query, variables, operation_name, show_graphiql

Let’s run our GraphQL query/mutation again:

mutation updatePublication($input: UpdatePublicationInput!) { # NOTE 
  updatePublication(input: $input) {
    publication {

# NOTE: We need to make sure that we name this mutation accordingly
Awesome! Now our transactions are being named correctly, with the right operation name and type too!
Sentry Issue with Correct Name
Even the errors are being named correctly now! Yay!

Closing Remark

Error monitoring improves your applications’ health by providing useful insights into the stability of your software.

On top of that, developers can use these insights for addressing existing technical debts and make better decisions around building new features versus fixing bugs.

I had a lot of fun integrating Sentry with my Django Graphene project, feel free to look at my public project on GitHub as a reference. Thanks for reading!

Hosted on Digital Ocean.