OpenTelemetry tracer for DimRed observability platform
Project description
dimredtracer
A minimal Python SDK that wraps OpenTelemetry and OpenInference for seamless LLM observability.
Features
- Simple API: Just three methods:
set_attribute(),start_span(), andforce_flush() - Environment-driven configuration: No hardcoded endpoints
- OpenInference integration: Pass any OpenInference instrumentor during initialization
- Works with async/await: Full support for async OpenAI clients
- Production-ready: Idempotent initialization, fail-soft error handling
Installation
pip install dimredtracer
Quick Start
1. Set environment variables
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="http://your-collector:4318/v1/traces"
export OTEL_SERVICE_NAME="my-service"
export DIMRED_TENANT_ID="your-tenant-id" # optional
export OPENAI_API_KEY="sk-..." # for OpenAI examples
2. Basic usage
from dimredtracer import Tracer
from openinference.instrumentation.openai import OpenAIInstrumentor
from openai import AsyncOpenAI
# Initialize tracer with OpenInference instrumentation
tracer = Tracer(OpenAIInstrumentor())
client = AsyncOpenAI()
# Set attributes on the current span
tracer.set_attribute("experiment", "demo-1")
tracer.set_attribute("user.id", "user-123")
# Make LLM call (auto-instrumented by OpenInference)
response = await client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hello!"}],
)
# Add post-call attributes
tracer.set_attribute("response.length", len(response.choices[0].message.content))
# Ensure spans are exported
tracer.force_flush()
3. Custom spans
# Create custom spans for non-LLM operations
with tracer.start_span("preprocessing"):
tracer.set_attribute("chunks_processed", 42)
# ... your preprocessing logic ...
with tracer.start_span("postprocessing"):
tracer.set_attribute("validation_passed", True)
# ... your postprocessing logic ...
API Reference
Tracer(instrumentation=None, name="dimred.tracer")
Initialize the tracer with optional OpenInference instrumentation.
Parameters:
instrumentation(optional): An OpenInference instrumentor instance (e.g.,OpenAIInstrumentor())name(str): Logical name for the OTEL tracer
tracer.set_attribute(key: str, value: Any)
Attach an attribute to the current active span.
tracer.start_span(name: str)
Context manager that creates a custom span and makes it current.
tracer.force_flush(timeout_millis: int = 30000)
Force flush buffered spans to the collector.
Environment Variables
| Variable | Required | Description | Example |
|---|---|---|---|
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT |
Yes | Full OTLP HTTP endpoint | http://your-collector:4318/v1/traces |
OTEL_SERVICE_NAME |
No | Service identifier | my-llm-service (default: dimredtracer-service) |
DIMRED_TENANT_ID |
No | Tenant ID for multi-tenancy | acme-corp |
Examples
See the examples/ directory for complete examples:
- openai_async_example.py: Async OpenAI with OpenInference instrumentation
Requirements
- Python 3.10+
- OpenTelemetry SDK 1.22.0+
- (Optional) OpenInference instrumentation packages
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dimredtracer-0.1.1.tar.gz.
File metadata
- Download URL: dimredtracer-0.1.1.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e57b841b5a8b595f079777254559aeee470edd477abb19a1cf94456b2932fe33
|
|
| MD5 |
c089febc70b8a113300314e9350be2dc
|
|
| BLAKE2b-256 |
0dacf4d995303ba1fc41136ad220678ed3c83284b0f78d1256b9c9e80a6cda5c
|
File details
Details for the file dimredtracer-0.1.1-py3-none-any.whl.
File metadata
- Download URL: dimredtracer-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe32c080fbea29b0ca6d0e9cd5421047db58af445061065d9f32c604b492a9dd
|
|
| MD5 |
3488198e14d436baeaaeddfb8eb1558d
|
|
| BLAKE2b-256 |
4d24b6e9fd2332ab04e86628c5d3dcbae390ec2d7270342774a7191b20fa5bdc
|