Metadata-Version: 2.5
Name: conviva-agent-tracing
Version: 1.2.0
Summary: Conviva tracing SDK for AI agents — a thin wrapper over OpenLLMetry that exports OTLP http/protobuf traces to the Conviva agent gateway, with Conviva's span processors and baggage connector built in.
Author: Conviva Inc.
License: Apache-2.0
Keywords: agent,conviva,llm,observability,openllmetry,opentelemetry,otlp,traceloop,tracing
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: System :: Monitoring
Requires-Python: >=3.10
Requires-Dist: conviva-agent-traces-connector>=1.0.0
Requires-Dist: conviva-otlp-exporter>=1.2.0
Requires-Dist: httpx>=0.23.0
Requires-Dist: opentelemetry-api>=1.20.0
Requires-Dist: opentelemetry-sdk>=1.20.0
Requires-Dist: requests>=2.25.0
Requires-Dist: traceloop-sdk<1.0,>=0.62.0
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == 'dev'
Description-Content-Type: text/markdown

# conviva-agent-tracing

OpenLLMetry auto-instrumentation, exporting to Conviva. Wraps
[`traceloop-sdk`](https://github.com/traceloop/openllmetry) so the LLM,
vector-DB and agent-framework libraries it supports are instrumented
automatically, and routes the resulting spans to the Conviva agent gateway with
`cvx.conversation_id` and `cnv.project_id` on every span. Traces only — no
metrics or logs are exported.

It is built from two Conviva packages, both installed as dependencies and both
usable on their own:

| Package | Role here |
|---|---|
| [`conviva-otlp-exporter`](../conviva-otlp-exporter) | The OTLP exporter to `https://<customer_key>.agw.conviva.com/v1/traces`, and the span processors that stamp `cvx.conversation_id` and `cnv.project_id`. |
| [`conviva-agent-traces-connector`](../conviva-agent-traces-connector) | Copies the Conviva client's `c3.*` W3C baggage — client id, conversation id, message id — onto every span. |

The Python mirror of [`@convivainc/agent-tracing`](../../js/conviva-agent-tracing)
— same shape, `snake_case` names.

If you already have an instrumentation library and only want the Conviva
transport, use [`conviva-otlp-exporter`](../conviva-otlp-exporter) directly; it
depends on no vendor SDK.

## Install

```bash
pip install conviva-agent-tracing
```

## Use

```python
import conviva_agent_tracing

conviva_agent_tracing.init(app_name="flight-booking")
```

Call `init()` **before** importing the libraries you want instrumented —
OpenLLMetry patches modules at import time.

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

| | |
|---|---|
| **customer key** | `customer_key=` or `CONVIVA_CUSTOMER_KEY`. Becomes the subdomain: `https://<customer_key>.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. |

Both are resolved by `conviva-otlp-exporter`, so the endpoint precedence and
header rules documented there apply unchanged.

## API

| | |
|---|---|
| `init(...) -> ResolvedConfig` | Initialize. Idempotent: a second call warns and returns the cached config. Raises `ConvivaTracingError` on bad configuration, before touching any global state. |
| `get_config()` | The active `ResolvedConfig`, or `None` before `init()`. |
| `force_flush()` | Drain the batch processor. Call before a short-lived process exits. |
| `shutdown()` | Flush pending spans — see the caveat below. |
| `set_association_properties({...})` | Attach properties to the current trace. `session_id` is the one Conviva reads back as `cvx.conversation_id`. |
| `BaggageSpanProcessor`, `BaggageConfig`, `ensure_baggage_propagator` | The connector, re-exported. `init()` already installs it. |

OpenLLMetry's manual-annotation decorators — `workflow`, `task`, `agent`,
`tool`, `conversation` and the `a`-prefixed async variants — are re-exported,
so an application depends on this package alone.

Any keyword `init()` does not name is passed straight through to
`Traceloop.init`: `instruments`, `block_instruments`, `sampler`, `propagator`,
`resource_attributes` and the rest.

```python
conviva_agent_tracing.init(
    app_name="flight-booking",
    conversation_id="conv-42",           # or let the processor resolve it
    attributes={"tenant": "acme"},       # stamped on every span
    span_processors=[MyProcessor()],     # composed after Conviva's
    disable_batch=True,                  # export each span as it ends
    trace_content=False,                 # or CONVIVA_TRACE_CONTENT=false
    baggage_config=BaggageConfig(        # which baggage reaches spans
        allowlist=("c3.", "tenant"),
    ),
)
```

## Conviva client baggage

The Conviva client sensor sends `c3.client_id`, `c3.conv_id` and `c3.msg_id`
in the W3C `baggage` header. `init()` installs the connector's
`BaggageSpanProcessor` ahead of the Conviva processors and merges the W3C
baggage propagator into the global one, so every span started while that
baggage is in context carries those entries as attributes under their own
names. Your HTTP server instrumentation still has to extract the header into
context — that part is not this package's job.

## Conversation ids

`ConversationIdSpanProcessor` resolves one from, in order: the `conversation_id`
argument → span attributes (`traceloop.association.properties.session_id`,
`session.id`, `gen_ai.conversation.id`) → baggage (`c3.conv_id`,
`conversation_id`, `session_id`, `langfuse_session_id`). Ids are only
extracted, never generated: a span with none of these carries no
`cvx.conversation_id`. The usual path is the second:

```python
conviva_agent_tracing.set_association_properties({"session_id": conversation_id})
```

Override the source lists with `conversation_id_sources=` and
`conversation_id_baggage_keys=` if your application names them differently.

## Two things to know

**`shutdown()` only flushes.** OpenLLMetry keeps its tracer provider private
and exposes no shutdown hook, so instrumentation stays installed and tracing
cannot be restarted in the same process. The JS wrapper has the same
limitation.

**Every span is exported, not just LLM spans.** OpenLLMetry installs no scope
filter, so spans from any HTTP or database instrumentation on the global
provider go to Conviva too. That is what auto-collection means here, but it is
a louder default than a scoped exporter.

## Traces only

Nothing but traces goes to Conviva. `init()` sets `TRACELOOP_METRICS_ENABLED`
and `TRACELOOP_LOGGING_ENABLED` to `false` unless you have set them yourself.
If you turn OpenLLMetry's logging on without a `logging_exporter` or
`TRACELOOP_LOGGING_ENDPOINT` of your own, `init()` turns it back off with a
warning — OpenLLMetry would otherwise send the logs to the gateway. Metrics
and logs you route to your own destination are left alone.

## Test

```bash
uv run pytest packages/python/conviva-agent-tracing
```

## 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. |
