Package: opentelemetry-instrumentation-django-stomp
Opentelemetry auto instrumentation for django-stompThis library will help you to use opentelemetry traces and metrics onDjango STOMPusage library.Installationpip installopentelemetry-instrumentation-django-stompHow to use ?You can use theDjangoStompInstrumentor().instrument()for example inmanage.pyfile.#!/usr/bin/env python"""Django's command-line utility for administrative tasks."""importosimportsysimporttypingfromopentelemetry_instrumentation_django_stompimportDjangoStompInstrumentorfromopentelemetryimporttracefromopentelemetry.sdk.traceimportTracerProviderfromopentelemetry.sdk.trace.exportimportBatchSpanProcessorfromopentelemetry.sdk.trace.exportimportConsoleSpanExporterfromopentelemetry.trace.spanimportSpandefpublisher_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.passdefconsumer_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.passprovider=TracerProvider()trace.set_tracer_provider(provider)trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))defmain():os.environ.setdefault("DJANGO_SETTINGS_MODULE","application.settings")DjangoStompInstrumentor().instrument(trace_provider=trace,publisher_hook=publisher_hook,consumer_hook=consumer_hook,)try:fromdjango.core.managementimportexecute_from_command_lineexceptImportErrorasexc:raiseImportError("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?")fromexcexecute_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.TheDjangoStompInstrumentorcan 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.:warning: The hook function will not raise an exception when an error occurs inside hook function, only a warning log is generatedPUBLISHER exampleWith the django-stomp, we can publish a message to a broker usingpublisher.sendand the instrumentator
can include a span with telemetry data in this function utilization.fromuuidimportuuid4fromdjango_stomp.builderimportbuild_publisherpublisher=build_publisher(f"publisher-unique-name-{uuid4()}")publisher.send(queue='/queue/a-destination',body={"a":"random","body":"message"},)The publisher span had "PUBLISHER" name.CONSUMER exampleWith 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.pythonmanage.pypubsubQUEUE_NAMEcallback_function_to_consume_messageConsumer spans can generate up to three types:CONSUMERACKNACKSupress django-stomp traces and metricsWhen the flagOTEL_PYTHON_DJANGO_STOMP_INSTRUMENThasFalsevalue traces and metrics will not be generated.
Use this to supress the django-stomp instrumentation.HOW TO CONTRIBUTE ?Look thecontributingspecs