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

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:

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.1.tar.gz (5.6 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.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

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

Hashes for dimredtracer-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e57b841b5a8b595f079777254559aeee470edd477abb19a1cf94456b2932fe33
MD5 c089febc70b8a113300314e9350be2dc
BLAKE2b-256 0dacf4d995303ba1fc41136ad220678ed3c83284b0f78d1256b9c9e80a6cda5c

See more details on using hashes here.

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

Hashes for dimredtracer-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fe32c080fbea29b0ca6d0e9cd5421047db58af445061065d9f32c604b492a9dd
MD5 3488198e14d436baeaaeddfb8eb1558d
BLAKE2b-256 4d24b6e9fd2332ab04e86628c5d3dcbae390ec2d7270342774a7191b20fa5bdc

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