Skip to content

Add instrumented Django management command base class #627

@beniwohli

Description

@beniwohli

We should make it easy to instrument Django management commands. It is not a lot of work now, but it could be made even easier by providing a base class that calls the right APIs

Currently, the user would need to do something like

from django.core.management.base import BaseCommand, CommandError

import elasticapm
from elasticapm.contrib.django.client import get_client

class Command(BaseCommand):
    def handle(self, *args, **options):
        elasticapm.instrument()
        client = get_client()
        client.begin_transaction(transaction_type="command")
        try:
            # do some work
            status = "success"
        except Exception as e:
            client.capture_exception()
            status = "failure"
            raise e  # Python 3
            # for Python 2, use this instead:
            #
            # import sys
            # t, v, tb = sys.exc_info()
            # raise t, v, tb
        finally:
            # use module name as transaction name
            client.end_transaction(name=__name__, result=status)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions