Skip to main content

Django tool for instrumenting

Project description

Instrumenting

install

  • install with your favorite Python package manager
pip3 install shopcloud-django-instrumenting

LogRequestMiddleware

add additional Information from request in AppEngine to Log

usage

add to MIDDLEWARE in django-app settings.py:

MIDDLEWARE = [
    ...
    'shopcloud_django_instrumenting.middleware.LogRequestMiddleware',
]

tracing

from shopcloud_django_instrumenting import tracing

tr = tracing.Tracer('name_of_service', 'name_of_operation')
with tr.start_span('event.processing') as span:
    pass

data = tr.close()

error management

A special layer on top of the API is the error boundary within the with blocks of spans. This layer catches exceptions and prints them. The pattern here is to catch all runtime errors and return a 422 status, indicating that an unexpected error has occurred, not a validation error. If you need to return validation data to the user, you must return a Result object on your own.

@action(detail=True, methods=["post"], url_path="my-action")
def my_action(self, request, pk):
    tr = tracing.DjangoAPITracer(request)

    serializer = serializers.ProductSyncSerializer(data=request.data)
    serializer.is_valid(raise_exception=True)

    with tr.start_span('proceed') as span:
        raise Exception('something unhandle')

    trace_data = tr.close()
    return Response({
        'trace': trace_data,
    }, status=status.HTTP_201_CREATED if tr.is_success else status.HTTP_422_UNPROCESSABLE_ENTITY)

Another pattern to note is that every with block should contain a repeatable operation. You can create structures where you can run blocks side by side and only repeat some of them. However, be careful: while the with block catches exceptions, it may not guarantee that all variables are set.

tr = tracing.DjangoAPITracer(request)

is_a_success = False
is_b_success = False
is_c_success = False

with tr.start_span('A') as span_a:
    is_a_success = False

with tr.start_span('B') as span_b:
    is_b_success = True

with tr.start_span('C') as span_c:
    if is_a_success:
        pass

develop

$ pytest
$ pip3 install coverage
# shell report
$ coverage run -m pytest  && coverage report --show-missing
# html report
$ coverage run -m pytest  && coverage html
$ cd htmlcov
$ python3 -m http.server

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shopcloud_django_instrumenting-1.5.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file shopcloud_django_instrumenting-1.5.0.tar.gz.

File metadata

File hashes

Hashes for shopcloud_django_instrumenting-1.5.0.tar.gz
Algorithm Hash digest
SHA256 ed940575112fc9b92a9599722be824776b3aa373bc16146b4498e0395fcb5f82
MD5 66baadf95390f734844664672ff72668
BLAKE2b-256 75955960b158f0ac4dcbd0aea596a324050a67cdbcd831bff94e3e36ef807582

See more details on using hashes here.

File details

Details for the file shopcloud_django_instrumenting-1.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for shopcloud_django_instrumenting-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 41784ee0d549ee38cf2b2e8833f6d31e1328cd4c913d39eda10005537336d102
MD5 a86a7f950d20973a4d26cf2c7c0ff08d
BLAKE2b-256 e9678a36bb5f12b1063244d5a8e747a169ab54ea8d0303dfdb7c7bc777fe9724

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page