OpenTelemetry Writer Instrumentation for Traceloop SDK

0.58.0 · active · verified Sat Apr 11

This package provides OpenTelemetry instrumentation for the internal `writer` module within the Traceloop SDK. It helps capture tracing data related to text generation and other 'write' operations performed by the `traceloop-sdk`. It's part of the broader OpenLLMetry project, focusing on observability for LLM applications. Releases are frequent, often several times a week, aligning with the rapid development of the OpenLLMetry ecosystem.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to manually initialize OpenTelemetry, enable the `WriterInstrumentor`, and explains that traces will be generated when the `traceloop-sdk`'s internal `writer` module is used. This instrumentation is typically part of the broader `Traceloop.init()` setup, but can be enabled standalone.

from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor

# NOTE: This instrumentation requires 'traceloop-sdk' to be installed
# and its internal 'writer' components to be used to generate traces.
# You would typically install it via `pip install traceloop-sdk`.

# Optional: Also install a specific LLM library like openai, anthropic, etc.
# e.g., pip install opentelemetry-instrumentation-openai

# 1. Import the specific instrumentation
from opentelemetry.instrumentation.writer import WriterInstrumentor

# 2. Set up a basic OpenTelemetry TracerProvider and Exporter
resource = Resource.create({"service.name": "my-traceloop-writer-app"})
provider = TracerProvider(resource=resource)
processor = SimpleSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)

# 3. Instrument the target library
WriterInstrumentor().instrument()

# 4. Now, if `traceloop-sdk` is used, operations involving its internal
#    'writer' module (e.g., handling LLM inputs/outputs) will generate spans.
#    For example, if you were using `traceloop-sdk` to wrap an OpenAI call:
#
# from traceloop.sdk import Traceloop
# from openai import OpenAI
# Traceloop.init(app_name="my-llm-app", disable_batch=True) # Enables ALL Traceloop instrumentations
# client = OpenAI()
# response = client.chat.completions.create(
#    model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello"}]
# )
# print(response.choices[0].message.content)

print("OpenTelemetry Writer Instrumentation enabled. Traces will appear when traceloop-sdk 'writer' module is invoked.")

view raw JSON →