Skip to main content

pytest-otel-capture

Capture and inspect OpenTelemetry logs, spans, and metrics in Python tests. Requires Python 3.12 or newer.

pip install pytest-otel-capture

This first release is a helper library, not an auto-loaded pytest plugin. Your fixture owns SDK providers and instrumentation; no application framework, exporter backend, or pytest runtime dependency is required.

from opentelemetry.sdk import trace
import pytest_otel_capture as capture

collected = capture.OTelMocker(
    capture.InMemoryLogRecordExporter(),
    capture.InMemorySpanExporter(),
    capture.InMemoryMetricReader(),
)
provider = trace.TracerProvider()
provider.add_span_processor(collected.span_processor)
try:
    with provider.get_tracer("example").start_as_current_span("work"):
        pass
    span = capture.get_span(collected.get_finished_spans(), "work")
    assert span is not None
    assert span.scope.name == "example"
finally:
    provider.shutdown()

Capture ownership and selectors

Inject span_processor into a TracerProvider, log_processor into a LoggerProvider, and metric_reader into a MeterProvider. Shut down each provider in your fixture's finally block. Local SDK providers avoid changing the process-global providers and keep independent tests isolated.

  • get_finished_spans() and get_finished_logs() drain their exporters.
  • get_metrics_data() collects a metric snapshot; it does not clear instruments.
  • find_spans, find_logs, and find_metrics return all matches. Attribute matching is a subset match; scope matches the instrumentation scope name.
  • get_span, get_log, and get_metric return None for no match and raise ValueError for ambiguous matches.
  • latest_metric_data returns the selected metric's last data point and checks its type. Attribute filtering selects the metric, not an individual data point.
  • assert_trace_id and assert_trace_header check trace identity. The header helper checks only the version/trace-ID prefix, not full W3C header validity.

OTelMocker retains a legacy context manager that resets the private global trace/metric provider-once guards on exit. It does not restore or shut down providers. The exported reset_otel_once() has the same limits. Prefer direct object use with local SDK providers; global-provider replacement is unsuitable for concurrent tests in one process.

SDK support is deliberately limited to the tested 1.43 minor series because provider-reset and logging support depend on SDK internals. Broaden the range only after compatibility testing. Framework-specific pytest fixtures stay in the application that owns their settings and instrumentation.

Development

nix develop
pytest -q
basedpyright
nix fmt
nix flake check
nix build

See RELEASING.md for repository setup and PyPI trusted publishing.

License

Licensed under the Apache License 2.0.

Metadata

Release files for pytest-otel-capture 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-otel-capture 0.1.1
File Size Uploaded
pytest_otel_capture-0.1.1.tar.gz 53.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-otel-capture 0.1.1
File Interpreter ABI Platform
pytest_otel_capture-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 64.6 kB

Release files / pytest_otel_capture-0.1.1.tar.gz

Download URL pytest_otel_capture-0.1.1.tar.gz
Size 53.5 kB
Tags Source
SHA-256 checksum
How to use checksums
bd2332f3f367cf055288c32cadb7994b9b20499d9974c66a184bd504aef02c5d
BLAKE2b-256 checksum
How to use checksums
6c4231332c67cc1cc7363dd4e6b5761c1f8f703449a83ce7a6d698a42f60c10e
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 5, 2026.

Transparency log

Release files / pytest_otel_capture-0.1.1-py3-none-any.whl

Download URL pytest_otel_capture-0.1.1-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0d9cec4dde8ab50e7d256be535afb91a6d07c92cf40baa48f0cf6518aabed0e9
BLAKE2b-256 checksum
How to use checksums
3323b4febb9d2b54ca7682632b6430b49f5cf1d128a6689a15dd2a882e5efde9
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 5, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

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