Metadata-Version: 2.5
Name: conviva-otlp-exporter
Version: 1.0.0
Summary: OTLP trace exporter and span processors that send OpenTelemetry traces to the Conviva agent gateway.
Author: Conviva Inc.
License: Apache-2.0
Keywords: conviva,observability,opentelemetry,otel,otlp,tracing
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: System :: Monitoring
Requires-Python: >=3.9
Requires-Dist: opentelemetry-api>=1.20.0
Requires-Dist: opentelemetry-exporter-otlp-proto-http>=1.20.0
Requires-Dist: opentelemetry-sdk>=1.20.0
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == 'dev'
Description-Content-Type: text/markdown

# conviva-otlp-exporter

The Conviva transport layer for Python. Resolves the gateway destination and
routing headers, builds the OTLP http/protobuf exporter, and supplies the span
processors that stamp the ids Conviva correlates on.

It depends on no vendor SDK — no OpenLLMetry, no Langfuse — so you can drop it
underneath whichever instrumentation library you already have. If you want
auto-instrumentation as well, use
[`conviva-agent-tracing`](../conviva-agent-tracing), which is this package plus
OpenLLMetry.

## Install

```bash
pip install conviva-otlp-exporter
```

## Configure

Two values, and nothing else — the second is optional:

| | |
|---|---|
| **customer key** | `customer_key=` or `CONVIVA_CUSTOMER_KEY`. Becomes the subdomain: `https://<customerKey>.agw.conviva.com`. |
| **project id** | `project_id=` or `CONVIVA_PROJECT_ID`. Sent as the `X-Conviva-Project-Id` header and written onto every span as `cnv.project_id`. Optional: without it, traces go to the account's default project. |

## Use

```python
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

from conviva_otlp_exporter import (
    CompositeSpanProcessor,
    create_conviva_span_processors,
    create_trace_exporter,
    resolve_config,
)

config = resolve_config()  # reads CONVIVA_CUSTOMER_KEY and CONVIVA_PROJECT_ID

provider = TracerProvider()
provider.add_span_processor(BatchSpanProcessor(create_trace_exporter(config)))
for processor in create_conviva_span_processors(config):
    provider.add_span_processor(processor)
```

Order matters. The exporting processor goes **first**: the Conviva processors
read attributes the instrumentation wrote during `on_start`, so they have to
run after whatever put them there.

For an API that accepts only one processor — `Traceloop.init(processor=...)`
is the common case — wrap the stack:

```python
processor = CompositeSpanProcessor(*create_conviva_span_processors(config))
```

## What the processors do

| Processor | Writes | Notes |
|---|---|---|
| `ConversationIdSpanProcessor` | `cvx.conversation_id`, `gen_ai.conversation.id` | Resolves from an explicit value, then span attributes (`traceloop.association.properties.session_id`, `session.id`, `gen_ai.conversation.id`), then baggage (`c3.conv_id`, `conversation_id`, `session_id`, `langfuse_session_id`). Never overwrites an existing value. |
| `ProjectIdSpanProcessor` | `cnv.project_id` | Not redundant with the header. Header plus resource attribute alone was observed to return HTTP 200 with nothing visible downstream; only the per-span attribute worked. |
| `AttributeSpanProcessor` | whatever you give it | Drops `None` and `""` once at construction. |
| `CompositeSpanProcessor` | — | Runs several as one, in argument order. |

All of them mutate in `on_start`. By `on_end` the span is read-only.

## Endpoint precedence

Highest first:

1. the `endpoint=` argument
2. `CONVIVA_OTLP_ENDPOINT`
3. `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`
4. `OTEL_EXPORTER_OTLP_ENDPOINT`
5. derived — `https://<customer_key>.agw.conviva.com`

Rungs 3 and 4 let a stock OpenTelemetry operator point telemetry at a local
collector without knowing any Conviva-specific variable name. A configured
endpoint may include the signal path or not; `/v1/traces` is stripped and
re-appended either way.

## Traces only

This package exports traces to `https://<customer_key>.agw.conviva.com/v1/traces`
and nothing else: there is no metric or log exporter. A configured endpoint
that ends in `/v1/metrics` or `/v1/logs` is still accepted and reduced to its
base URL, so a value copied from a generic OTLP setup resolves correctly.

## The contract

Every constant here is authored in
[`contract/config-contract.json`](../../../contract/config-contract.json) at
the component root, and asserted by `tests/test_contract.py` — alongside the
JS exporter and the coding-agent hook engine, which read the same file. Three
implementations, one source of truth.

## Test

```bash
uv run pytest packages/python/conviva-otlp-exporter
```

## Documentation

| | |
|---|---|
| [README.md](README.md) | What the package does and its API. |
| [INTEGRATION.md](INTEGRATION.md) | Step-by-step integration into an application. |
| [AGENTS.md](AGENTS.md) | Guidance for AI coding agents working on or with this package. |
