Skip to main content

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(), and force_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

From source

pip install -r requirements.txt

From GitHub

pip install "git+https://github.com/DimRedLabs/dimred-python-sdk.git@main"
pip install -r requirements.txt

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:

Requirements

  • Python 3.10+
  • OpenTelemetry SDK 1.22.0+
  • (Optional) OpenInference instrumentation packages

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dimredtracer-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dimredtracer-0.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file dimredtracer-0.1.0.tar.gz.

File metadata

  • Download URL: dimredtracer-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.12

File hashes

Hashes for dimredtracer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b8eea9858809245873067632e42e1b5c6d03742e0b1ab286beeddf28eefc465f
MD5 7010d30ead55972a160a8ae8f72eb3b7
BLAKE2b-256 71a0ad78686184fadb2152f826d89c05777a2c93b7f958564014ce20732a0ab0

See more details on using hashes here.

File details

Details for the file dimredtracer-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dimredtracer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.12

File hashes

Hashes for dimredtracer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 433cbaf3825d8732e1ef4702c2300b6c411373dbd985dd05b6dd4f0c0d69d1ab
MD5 fbfa2fde1272523e14a6eef6d58723cf
BLAKE2b-256 16d0f38436e43f25de8eb1975d1f89c11596a3fc3fcb8405e07f7a3489d52322

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page