Skip to main content

Opentelemetry auto instrumentation for django-stomp

Maintainability Rating Coverage Quality Gate Status Code style: black PyPI version GitHub

This library will help you to use opentelemetry traces and metrics on Django STOMP usage library.

Django stomp instrumentation

Installation

pip install opentelemetry-instrumentation-django-stomp

How to use ?

You can use the DjangoStompInstrumentor().instrument() for example in manage.py file.

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
import typing

from opentelemetry_instrumentation_django_stomp import DjangoStompInstrumentor

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
from opentelemetry.trace.span import Span


def publisher_hook(span: Span, body: typing.Dict, headers: typing.Dict):
    # Custom code in your project here we can see span attributes and make custom logic with then.
    pass


def consumer_hook(span: Span, body: typing.Dict, headers: typing.Dict):
    # Custom code in your project here we can see span attributes and make custom logic with then.
    pass


provider = TracerProvider()
trace.set_tracer_provider(provider)
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))

def main():
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "application.settings")
    DjangoStompInstrumentor().instrument(
        trace_provider=trace,
        publisher_hook=publisher_hook,
        consumer_hook=consumer_hook,
    )
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == "__main__":
    main()

The code above will create telemetry wrappers inside django-stomp code and creates automatic spans with broker data.

The DjangoStompInstrumentor can receive three optional parameters:

  • trace_provider: The tracer provider to use in open-telemetry spans.
  • publisher_hook: The callable function on publisher action to call before the original function call, use this to override, enrich the span or get span information in the main project.
  • consumer_hook: The callable function on consumer action to call before the original function call, use this to override, enrich the span or get span information in the main project.

⚠️ The hook function will not raise an exception when an error occurs inside hook function, only a warning log is generated

Span Generated

send {destination}

With the django-stomp, we can publish a message to a broker using publisher.send and the instrumentator can include a span with telemetry data in this function utilization.

    from uuid import uuid4
    from django_stomp.builder import build_publisher
    publisher = build_publisher(f"publisher-unique-name-{uuid4()}")
    publisher.send(
        queue='/queue/a-destination',
        body={"a": "random","body": "message"},
    )

The publisher span had send {destination} name.

publisher example

CONSUMER

With the django-stomp, we create a simple consumer using pubsub command and the instrumentator can include a span with telemetry data in this function utilization.

   python manage.py pubsub QUEUE_NAME callback_function_to_consume_message

Consumer spans can generate up to three types:

  • process {destination} process trace
  • ack {destination} ack trace
  • nack {destination} nack trace

Supress django-stomp traces and metrics

When the flag OTEL_PYTHON_DJANGO_STOMP_INSTRUMENT has False value traces and metrics will not be generated. Use this to supress the django-stomp instrumentation.

HOW TO CONTRIBUTE ?

Look the contributing specs

Metadata

Release files for opentelemetry-instrumentation-django-stomp 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for opentelemetry-instrumentation-django-stomp 0.5.0
File Size Uploaded
opentelemetry_instrumentation_django_stomp-0.5.0.tar.gz 9.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for opentelemetry-instrumentation-django-stomp 0.5.0
File Interpreter ABI Platform
opentelemetry_instrumentation_django_stomp-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.5 kB

Release files / opentelemetry_instrumentation_django_stomp-0.5.0.tar.gz

Download URL opentelemetry_instrumentation_django_stomp-0.5.0.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
4f93ce50a457afd9b70dd556bbcbed9989b6c86533e40fbaba4dea596c4875cd
BLAKE2b-256 checksum
How to use checksums
85523435f28af6f662139c11d3ecb8fec0f100e6fff63fd2084e8b4ac6700e87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.12.13 Linux/6.17.0-1018-azure

Release files / opentelemetry_instrumentation_django_stomp-0.5.0-py3-none-any.whl

Download URL opentelemetry_instrumentation_django_stomp-0.5.0-py3-none-any.whl
Size 12.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f1d47bc2982bc9fef9758c3b471cbde3ac2a89039883b08214222c1822e60847
BLAKE2b-256 checksum
How to use checksums
a59250ecf832c4eb14b7f429f3f5f818f4ebba784beaf68687c27dad9654e3d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.12.13 Linux/6.17.0-1018-azure

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page