write a custom decorator to create a dagster op and add sentry logging to it in python

main.py
from dagster import lambda_solid, ModeDefinition, default_executors, pipeline
from raven import Client

def with_sentry_logging(client):
    def decorator(function):
        def wrapper(*args, **kwargs):
            try:
                return function(*args, **kwargs)
            except Exception as e:
                client.captureException()
                raise e
        return wrapper
    return decorator

# Create a raven client instance
client = Client('YOUR_SENTRY_DSN')

@with_sentry_logging(client)
def my_dagster_op():
    return "Hello, Dagster!"

@pipeline(mode_defs=[ModeDefinition(executors=default_executors)])
def my_pipeline():
    my_dagster_op()

672 chars
26 lines

In the example above, we define a custom decorator called with_sentry_logging that takes a raven client as a parameter. This decorator wraps a function and captures any exceptions that occur within the function using client.captureException(). We then apply this decorator to our my_dagster_op function.

related categories

gistlibby LogSnag