Metadata-Version: 2.5
Name: dagger-pytest-otel
Version: 0.3.0
Summary: Pytest plugin for OpenTelemetry instrumentation with Dagger
Project-URL: Homepage, https://dagger.io
Project-URL: Repository, https://github.com/dagger/python/tree/main/pytest/dagger_pytest_otel
Project-URL: Issues, https://github.com/dagger/python/issues
Author-email: Dagger <hello@dagger.io>
License-Expression: Apache-2.0
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: Pytest
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: opentelemetry-api>=1.39.1
Requires-Dist: opentelemetry-exporter-otlp-proto-http>=1.39.0
Requires-Dist: opentelemetry-sdk>=1.39.1
Requires-Dist: opentelemetry-semantic-conventions>=0.60b1
Requires-Dist: pluggy>=1.2.0
Requires-Dist: pytest>=7.0.0
Description-Content-Type: text/markdown

# dagger-pytest-otel

A pytest plugin for OpenTelemetry instrumentation with Dagger.

This plugin automatically instruments pytest tests with OpenTelemetry spans, enabling test visibility in Dagger's TUI and Dagger Cloud.

## Features

- **Zero configuration**: Automatically creates spans for each test
- **TRACEPARENT propagation**: Inherits trace context from Dagger
- **Session spans**: Creates a parent span for the entire test session
- **Test hierarchy**: Captures module, class, and function information
- **Outcome tracking**: Records test pass/fail/skip status
- **Log capture**: Forwards Python logs to OpenTelemetry
- **Exception recording**: Records test exceptions on spans

## Installation

```bash
pip install dagger-pytest-otel
```

## Usage

Once installed, the plugin is automatically enabled. No code changes are required in your tests.

### With Dagger

When running tests through Dagger, the `TRACEPARENT` environment variable is automatically set, and test spans will appear in the Dagger TUI and Dagger Cloud.

The [pytest module](https://github.com/dagger/python/tree/main/pytest) bundles this plugin. A module that builds its own test container installs it:

```python
# In your Dagger module
@function
async def test(self, source: dagger.Directory) -> str:
    return await (
        dag.container()
        .from_("python:3.11-slim")
        .with_directory("/src", source)
        .with_workdir("/src")
        .with_exec(["pip", "install", "pytest", "dagger-pytest-otel"])
        .with_exec(["pytest", "-v"])
        .stdout()
    )
```

### Standalone

You can also use the plugin standalone with any OTLP-compatible backend:

```bash
export OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4318"
export TRACEPARENT="00-abc123...-def456...-01"  # Optional parent trace
pytest
```

### Disabling

To disable instrumentation:

```bash
pytest --no-otel
```

## Span Attributes

Each test span includes the following attributes:

| Attribute | Description |
|-----------|-------------|
| `dagger.io/ui.boundary` | Prevents log bubbling in Dagger TUI |
| `test.case.name` | Fully qualified pytest test case name |
| `test.case.result.status` | Test result (`pass`/`fail`/`skipped`) |
| `test.suite.name` | Pytest suite containing the test case |

Session spans also include `test.suite.run.status`.

## Test output

Captured stdout and stderr output is exported as OpenTelemetry logs on the active
test span using Dagger's `stdio.stream` attribute. Python logging records are
also forwarded as OpenTelemetry logs and mapped to stdout or stderr by severity.

## Development

```bash
cd pytest/dagger_pytest_otel
uv run pytest
```

## License

Apache-2.0
