Appearance
OpenTelemetry
Send OpenTelemetry traces to a Rundial agent run with OTLP over HTTP.
Start Rundial and create an API key with the Quickstart. Keep the stack running and set RUNDIAL_API_KEY.
1. Configure the exporter
Install the exporter and SDK:
bash
pip install rundial opentelemetry-sdk opentelemetry-exporter-otlp-proto-httpSave this as otel_example.py and run python otel_example.py.
python
import os
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
provider = TracerProvider(resource=Resource.create({
"service.name": "support-agent", "session.id": "demo-session",
}))
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter(
endpoint="http://127.0.0.1:8787/api/v1/ingest/otlp/v1/traces",
headers={"Authorization": "Bearer " + os.environ["RUNDIAL_API_KEY"]},
)))
trace.set_tracer_provider(provider)
with trace.get_tracer("example").start_as_current_span("answer") as span:
span.set_attribute("gen_ai.request.model", "example-model")
span.set_attribute("gen_ai.usage.input_tokens", 13)
span.set_attribute("gen_ai.usage.output_tokens", 21)
span.set_attribute("gen_ai.usage.cost_usd", 0.002)
provider.force_flush()
provider.shutdown()Open Default Project → Train, select Filter, choose agent under Kind, and select support-agent demo-session. Open Traces.
Select a span to see its attributes. Use these attribute names to record model usage:
| Record | Attribute names |
|---|---|
| Model | gen_ai.request.model, llm.model_name |
| Provider | gen_ai.system |
| Input tokens | gen_ai.usage.input_tokens, llm.token_count.prompt |
| Output tokens | gen_ai.usage.output_tokens, llm.token_count.completion |
| Cost in USD | gen_ai.usage.cost_usd, llm.cost.total |
2. Try the LangChain example
Install the extra packages, then run the example from a clone of the repository:
bash
pip install langchain-core langchain-community openinference-instrumentation-langchain
python docs/recipes/otel_langchain_agent.pyThe script uses a local fake model and the same API key and endpoint as the first example. Open Default Project → Train, select Filter, choose agent under Kind, and select langchain-agent demo-session. Open Traces. See the OpenTelemetry Python documentation for other exporters and producers.