Skip to main content

A set of functions to reduce the boilerplate required to add observability to your service of module

Project description

CI Coverage PyPI License

observability_utils

A set of functions and decorators to reduce the boilerplate required to add OpenTelemetry based observability to your Python service or module.

The decorators allow spans to be initialised which automatically capture identified parameters of the decorated method as Span Attributes. The context propagation helpers provide a pair of standard functions that can be used as parameters to streamline addition of this functionality.

In the initial version the following utils are provided:

  • setup_tracing(name) - Sets up basic tracing using a standardised naming convebstion so that the application is easily identifiable in visualisation tools.
  • set_console_exporter() - Turns on output of the capturesd traces in a local console/terminal to allow viewing of it without the need for an observability backend such as Jaeger or Promethus. Useful for debugging and testing.
  • get_tracer(name) - Retrieves the currently active Tracer object and labels is using a standard naming convention so that traces it produces are consistent across applications.
  • get_context_propagator - Retrieves the current observability context info in a form suitable for propagating.
  • propagate_context_in_stomp_headers(headers, context) - Simplfies the propagation of the Tracing Context between services that support STOMP communication over a message bus.
  • retrieve_context_from_stomp_headers(frame) - Simplifies th reception of the Tracing Context by services that support STOMP communication over a message bus.
  • add_span_attributes - Simplifies the addition of named attributes to the current span.
  • JsonObjectSpanExporter - A custom SpanExporter that allows the span content to be examined for use in tests.
  • asserting_span_exporter - A contextmanager that makes use of JsonObjectSpanExporter to allow functions under test to be checked by enclosing them in a with block.
Source https://github.com/DiamondLightSource/observability-utils
PyPI pip install observability-utils
Releases https://github.com/DiamondLightSource/observability-utils/releases

Usage examples:

from fastapi import FastAPI
from observability_utils.decorators import start_as_current_span
from observability_utils.tracing import setup_tracing, get_tracer
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor

setup_tracing("my_rest_app")

app = FastAPI(
    docs_url="/docs",
    on_shutdown=[teardown_handler],
    title="My Rest App",
    lifespan=lifespan,
    version=REST_API_VERSION,
)

FastAPIInstrumentor().instrument_app(app)

TRACER = get_tracer("my_rest_app")

start_as_current_span(TRACER, "fruit", "fruit.colour", "amount")
def my_func(fruit : Fruit = "apple", amount : int = 0):
    #function body

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

observability_utils-0.1.5.tar.gz (33.0 kB view details)

Uploaded Source

Built Distribution

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

observability_utils-0.1.5-py3-none-any.whl (18.4 kB view details)

Uploaded Python 3

File details

Details for the file observability_utils-0.1.5.tar.gz.

File metadata

  • Download URL: observability_utils-0.1.5.tar.gz
  • Upload date:
  • Size: 33.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for observability_utils-0.1.5.tar.gz
Algorithm Hash digest
SHA256 ce9b0052f4ef1d9b19b8ae6fac9d0ad0e5c6451a095bed4038f277bbabeeea53
MD5 dd4d054206eba51a12bb8594f54a0ad8
BLAKE2b-256 c15eed9eaaa95dc32e215c15a87a708f8644ce16f0bbdcfd93c96cbcc2369406

See more details on using hashes here.

File details

Details for the file observability_utils-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for observability_utils-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 eb6b125b94de96593b3704bfc845357410ee0074c4a5f758c6505b8506e85161
MD5 9b84b55fcb9805ec4cf14c24d0bbd80e
BLAKE2b-256 906ca7a997fbfb7fac5aa51f2d2a0b9812941921dafa659ebec26ede6e629f9c

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