Skip to main content

OpenTelemetry integration for HawkAPI — plugin + middleware

Project description

hawkapi-otel

PyPI Python License CI Downloads

OpenTelemetry integration for HawkAPI — a plugin that initialises the OTel SDK on startup and a middleware that creates a server span per request, with full W3C trace-context propagation.

Works out of the box with a local OTLP collector. One-liner configs for Honeycomb, Grafana Cloud, Datadog, and Jaeger are listed below.


Quickstart

pip install hawkapi-otel
from hawkapi import HawkAPI
from hawkapi_otel import OTelPlugin, OTelMiddleware

app = HawkAPI()

app.add_plugin(
    OTelPlugin(
        service_name="my-app",
        service_version="1.0.0",
        endpoint="http://localhost:4317",
        protocol="grpc",
        insecure=True,
    )
)
app.add_middleware(OTelMiddleware)

Every request gets a server span with HTTP semantic-convention attributes. Unhandled exceptions are recorded on the current span.


OTelPlugin parameter reference

Parameter Type Default Description
service_name str "unknown_service" service.name resource attribute.
service_version str | None None service.version resource attribute.
endpoint str "http://localhost:4317" OTLP collector endpoint.
protocol str "grpc" "grpc" or "http/protobuf".
insecure bool True Disable TLS (useful for local collectors).
headers dict[str, str] | None None Extra headers — API keys for Honeycomb, Grafana Cloud, etc.
resource_attributes dict[str, str] | None None Arbitrary extra resource attributes.
traces_sampler str "parentbased_always_on" Sampler name. See sampler reference below.
traces_sampler_arg float | None None Ratio for traceidratio / parentbased_traceidratio.
enable_metrics bool True Build a MeterProvider with OTLP metric export.
enable_logs bool False Attach LoggingHandler to root logger + OTLP log export.
console_exporter bool False Also add a ConsoleSpanExporter (debug).
record_exceptions bool True Call span.record_exception() in on_exception.

Sampler reference

traces_sampler value Behaviour
"always_on" Sample every trace.
"always_off" Sample nothing.
"traceidratio" Sample traces_sampler_arg fraction (e.g. 0.1 = 10%).
"parentbased_always_on" Respect parent; default to always-on. (default)
"parentbased_always_off" Respect parent; default to always-off.
"parentbased_traceidratio" Respect parent; default to ratio sampling.

Vendor recipes

Honeycomb

OTelPlugin(
    service_name="my-app",
    endpoint="https://api.honeycomb.io",
    protocol="http/protobuf",
    insecure=False,
    headers={"x-honeycomb-team": "YOUR_API_KEY"},
)

Grafana Cloud

OTelPlugin(
    service_name="my-app",
    endpoint="https://otlp-gateway-prod-eu-west-0.grafana.net/otlp",
    protocol="http/protobuf",
    insecure=False,
    headers={"authorization": "Basic BASE64_ENCODED_INSTANCE_ID:TOKEN"},
)

Datadog (OTLP intake)

OTelPlugin(
    service_name="my-app",
    endpoint="https://api.datadoghq.com",
    protocol="http/protobuf",
    insecure=False,
    headers={"dd-api-key": "YOUR_DD_API_KEY"},
)

Jaeger (local)

OTelPlugin(
    service_name="my-app",
    endpoint="http://localhost:4317",
    protocol="grpc",
    insecure=True,
)

Development

# Clone and install in editable mode with dev extras
git clone https://github.com/ashimov/hawkapi-otel.git
cd hawkapi-otel
uv sync --extra dev

# Run tests
uv run pytest tests/ -q

# Lint
uv run ruff check .
uv run ruff format .

# Type-check
uv run pyright src/

License

MIT — Copyright (c) 2026 HawkAPI Contributors.

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

hawkapi_otel-0.1.0.tar.gz (37.7 kB view details)

Uploaded Source

Built Distribution

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

hawkapi_otel-0.1.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file hawkapi_otel-0.1.0.tar.gz.

File metadata

  • Download URL: hawkapi_otel-0.1.0.tar.gz
  • Upload date:
  • Size: 37.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for hawkapi_otel-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1ccf47e323a00ed2e9cb79c157c8a56aa5b37239defae80ff3fa4b5696f5472a
MD5 f0176c3b882f138a4890c7fc4ed13341
BLAKE2b-256 b04c48f24dc96f30fcc37e1c618d07ba2138b9e735eb18e0a331100c69f399d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for hawkapi_otel-0.1.0.tar.gz:

Publisher: release.yml on ashimov/hawkapi-otel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hawkapi_otel-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: hawkapi_otel-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for hawkapi_otel-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 782eb1574cfe81a2089228af3a70204be950e15389383c4dd5f7fd17dfd2a34c
MD5 ac9306c5b81fa05fb18030988bacbc52
BLAKE2b-256 323f88e05a71825e4e1b77b512ba1438894d81eab5f4333742d5c6d11271ef4a

See more details on using hashes here.

Provenance

The following attestation bundles were made for hawkapi_otel-0.1.0-py3-none-any.whl:

Publisher: release.yml on ashimov/hawkapi-otel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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