Skip to main content

otel-metrics

Reusable OpenTelemetry library: simplified wrappers around OTel metric instruments, plus an OTLP tracing pipeline.

Install

pip install otel-metrics

Or from a built wheel:

pip install dist/otel_metrics-0.1.0-py3-none-any.whl

Usage

from enum import auto

from otel_metrics import MetricName, OtelConfig, setup_metrics

# Define your metric names
class MyMetrics(MetricName):
    REQUEST_COUNT = auto()
    REQUEST_DURATION_MS = auto()

# Configure and initialize
config = OtelConfig(endpoint="http://localhost:4317", service_name="my-service")
metrics = setup_metrics(config)

# Create instruments
counter = metrics.counter(MyMetrics.REQUEST_COUNT, description="Total requests")
timer = metrics.timer(MyMetrics.REQUEST_DURATION_MS, description="Request latency")

# Record
counter.inc()

Timing operations

from otel_metrics import TimestampNS

start = TimestampNS.now()
# ... do work ...
timer.record(start)

Configuration

OtelConfig is a pydantic-settings model. Values resolve from constructor args or environment variables:

Field Env var Default
endpoint OTEL_EXPORTER_OTLP_ENDPOINT (required)
service_name OTEL_SERVICE_NAME (required)
protocol OTEL_EXPORTER_OTLP_PROTOCOL grpc
headers OTEL_EXPORTER_OTLP_HEADERS None

Supported protocols: grpc, http/protobuf.

headers accepts either a mapping or the OTLP key=value,key2=value2 string form, and is forwarded to both the metrics and traces exporters. Values may be URL-encoded per the OTLP spec, but plain values are accepted too.

Header values are held as pydantic.SecretStr, because they usually carry an API key. Printing or dumping the config shows the header names but masks the values:

config = OtelConfig(
    endpoint="https://api.braintrust.dev/otel",
    service_name="my-service",
    headers="Authorization=Bearer sk-live-123",
)

print(config.model_dump_json())
# {"endpoint":"...","headers":{"authorization":"**********"}}

config.resolved_headers()
# {'authorization': 'Bearer sk-live-123'}

Call resolved_headers() when you need the real values. Reading config.headers gives SecretStr objects, not plain strings.

Tracing

from otel_metrics import OtelConfig, setup_telemetry

config = OtelConfig(
    endpoint="https://api.braintrust.dev/otel",
    service_name="my-service",
    protocol="http/protobuf",
    headers={"Authorization": "Bearer <key>", "x-bt-parent": "project_id:<id>"},
)
tracer = setup_telemetry(config)

with tracer.start_as_current_span("my-operation"):
    ...

setup_telemetry sets the process-global tracer provider, so call it once at startup. Use build_test_tracer() for a non-exporting tracer in unit tests.

Instrumentors

Pass any BaseInstrumentor as a trailing argument and it is attached to the provider built here -- no need to reach for the global one:

from opentelemetry.instrumentation.langchain import LangchainInstrumentor

setup_telemetry(config, LangchainInstrumentor())

Several are fine, and an existing list can be splatted:

setup_telemetry(config, LangchainInstrumentor(), RequestsInstrumentor())
setup_telemetry(config, *instrumentors)

Testing

Use build_test_metrics() to get a no-op Metrics instance for unit tests:

from otel_metrics import build_test_metrics

metrics = build_test_metrics()

Development

uv sync
uv run pytest
uv run ruff check .

Build

uv build

Produces a wheel and an sdist in dist/.

Releasing

Publishing happens on merge to main. The workflow runs the tests and builds on every merge, but only uploads when the version in pyproject.toml is one PyPI has not seen — so bumping the version is what cuts a release. Merging anything else is a no-op for the index.

To release: bump version, open a PR, merge it.

Uploads use PyPI Trusted Publishing, so no API token lives in this repo. It needs a one-time setup on each index:

  1. On PyPI, go to Your projects → Publishing (or Account → Publishing for a name that has never been published) and add a GitHub publisher:

    Field Value
    Owner venkateshvasuki
    Repository reusable_otel_metric
    Workflow publish.yml
    Environment pypi
  2. In this repo's Settings → Environments, create an environment named pypi. Add required reviewers there if releases should need a human approval.

Repeat with an environment named testpypi on test.pypi.org to use the manual Run workflow button, which defaults to TestPyPI.

Metadata

Release files for otel-metrics 0.3.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 otel-metrics 0.3.0
File Size Uploaded Provenance
otel_metrics-0.3.0.tar.gz 11.5 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for otel-metrics 0.3.0
File Interpreter ABI Platform
otel_metrics-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.0 kB

Release files / otel_metrics-0.3.0.tar.gz

Download URL otel_metrics-0.3.0.tar.gz
Size 11.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6f6ae296f18fc28b8df950cf16938c472ebdda647f54c3c63a1bb10995dfd103
BLAKE2b-256 checksum
How to use checksums
9a5b3d22264639356dd923f65779ba662477d25108401bd48436a10ea8b65146
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

Release files / otel_metrics-0.3.0-py3-none-any.whl

Download URL otel_metrics-0.3.0-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1c6d8c0632d009fcb0fdf009341a52fcffbb9474c724cc55eb1980905d783e04
BLAKE2b-256 checksum
How to use checksums
41bc17171bb33f4bfa104107e54e69c0cc1a3299f9300a140166e81fb6849bf3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

Security for otel-metrics 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for otel_metrics-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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