Skip to main content

Universal OpenTelemetry bootstrap for Python services with elite-quality defaults, privacy, auto-instrumentation, traces, and metrics.

Project description

OpenTelemetry Instrumentation Python (v1)

Python-first OpenTelemetry bootstrap for backend services, focused on:

  • deterministic startup and shutdown
  • explicit feature toggles for tracing, metrics and instrumentations
  • strict privacy defaults for database statements and user identifiers
  • zero-code startup through the official distro/configurator mechanism
  • log correlation only, with no log export pipeline inside this package

Installation

pip install "elven-opentelemetry-instrumentation-py[full]"

Runtime support

v1 supports runtime="python" only.

Passing runtime="serverless" or runtime="edge" raises an explicit error.

Zero-code

export OTEL_SERVICE_NAME="my-service"
export OTEL_SERVICE_VERSION="1.0.0"
elven-otel-instrument python app.py

Disable wrapper initialization explicitly:

export OTEL_ZERO_CODE=false

Kubernetes Operator

Build the Python auto-instrumentation image expected by the OpenTelemetry Operator:

docker build -f Dockerfile.operator -t your-registry/elven-opentelemetry-instrumentation-py:0.1.3 .

Use that image in the Instrumentation resource:

apiVersion: opentelemetry.io/v1alpha1
kind: Instrumentation
metadata:
  name: instrumentation
spec:
  exporter:
    endpoint: "http://opentelemetrycollector.monitoring.svc.cluster.local:4318"
  propagators:
    - tracecontext
    - baggage
    - b3
    - b3multi
  sampler:
    type: parentbased_traceidratio
    argument: "1"
  python:
    image: docker.io/elvenobservability/python-k8s-operator:latest
    env:
      - name: OTEL_PYTHON_DISTRO
        value: "elven"
      - name: OTEL_PYTHON_CONFIGURATOR
        value: "elven"
      - name: OTEL_PYTHON_LOG_AUTO_INSTRUMENTATION
        value: "false"
      - name: OTEL_LOGS_EXPORTER
        value: "none"
      - name: OTEL_EXPORTER_OTLP_PROTOCOL
        value: "http/protobuf"
      - name: OTEL_METRICS_EXPORTER
        value: "otlp"
      - name: OTEL_TRACES_EXPORTER
        value: "otlp"
      - name: OTEL_EXPORTER_OTLP_ENDPOINT
        value: "http://opentelemetrycollector.monitoring.svc.cluster.local:4318"

This image ships both /autoinstrumentation and /autoinstrumentation-musl, sets OTEL_PYTHON_DISTRO=elven, sets OTEL_PYTHON_CONFIGURATOR=elven, and keeps logs export disabled by default to match the package contract.

If you prefer split endpoints, keep the same http/protobuf protocol and point both variables to the collector HTTP port:

- name: OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
  value: "http://opentelemetrycollector.monitoring.svc.cluster.local:4318/v1/traces"
- name: OTEL_EXPORTER_OTLP_METRICS_ENDPOINT
  value: "http://opentelemetrycollector.monitoring.svc.cluster.local:4318/v1/metrics"

For Alpine-based workloads, add:

metadata:
  annotations:
    instrumentation.opentelemetry.io/otel-python-platform: "musl"

Manual initialization

from opentelemetry.sdk.trace.sampling import Decision

from elven_opentelemetry_instrumentation import (
    MetricsConfig,
    ObservabilityConfig,
    PrivacyConfig,
    SamplingRule,
    ServiceConfig,
    TracingConfig,
    init_observability,
)

handle = init_observability(
    ObservabilityConfig(
        service=ServiceConfig(
            service_name="payment-service",
            service_version="1.0.0",
            deployment_environment="production",
        ),
        tracing=TracingConfig(
            enabled=True,
            rules=[
                SamplingRule(
                    attribute_key="http.route",
                    attribute_value="/healthz",
                    decision=Decision.DROP,
                ),
            ],
        ),
        metrics=MetricsConfig(enabled=True, export_interval_millis=60_000),
        privacy=PrivacyConfig(
            redact_db_statement=True,
            hash_user_id=True,
        ),
    )
)

handle.tracer.with_span("startup", lambda span: span.set_attribute("boot.ready", True))
handle.metrics.increment("jobs.started", 1)
handle.force_flush()
handle.shutdown()

Public API

init_observability(config) returns an ObservabilityHandle with:

  • tracer
  • metrics
  • shutdown()
  • force_flush()
  • is_started()

The initializer is idempotent and protects against duplicate startup.

Feature precedence

For booleans, precedence is:

  1. explicit config
  2. environment variable
  3. package default

Examples:

  • OTEL_TRACES_EXPORTER=none disables traces by default
  • TracingConfig(enabled=True) overrides that default
  • OTEL_INSTR_PSYCOPG=false disables psycopg instrumentation unless explicitly enabled in config

Supported coverage

  • Web: ASGI, WSGI, FastAPI, Flask, Django
  • GraphQL: request-level enrichment plus operation and field spans through graphql-core
  • HTTP clients: requests, httpx, urllib3, aiohttp-client
  • Data: SQLAlchemy, psycopg, psycopg2, pymysql, mysqlclient, Redis, MongoDB
  • Workers and queues: Celery, threading, asyncio, gRPC, Kafka, RabbitMQ/Pika, SQS via boto3
  • Logging: stdlib logging correlation

Instrumentation is activated only when the target library is installed and enabled.

Development

python -m venv .venv
. .venv/bin/activate
pip install -e ".[dev]"
ruff check .
pyright
pytest

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

elven_opentelemetry_instrumentation_py-0.1.4.tar.gz (22.8 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 elven_opentelemetry_instrumentation_py-0.1.4.tar.gz.

File metadata

File hashes

Hashes for elven_opentelemetry_instrumentation_py-0.1.4.tar.gz
Algorithm Hash digest
SHA256 dbbb5e489b8b367ab83b584d4a8217bf9e2cd6ec94e150e5eb95e8ebe8cec31b
MD5 e11b3068425b46b3a986861516fc9617
BLAKE2b-256 e07aa3fd8b4138e76ebe436d51f4c220c04919dfcc4be013e88c2acd92461189

See more details on using hashes here.

File details

Details for the file elven_opentelemetry_instrumentation_py-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for elven_opentelemetry_instrumentation_py-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0cac5543dd6bb84329a322fb2720a42b61d7477555bcab4c41dd5c89721fb102
MD5 8bb19513666c8799238f98ceaeb2d7e7
BLAKE2b-256 08ba47bf8b7ff8dd2875abe84f72701e1cac82993a3baebf320f777066754b79

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