Skip to main content

openai-otel is a library that automatically instrument your OpenAI python code using OpenTelemetry

Project description

OpenAI OTEL

OpenAI OTEL is a library that allows you to instrument your OpenAI Python client using OpenTelemetry.

Installation

pip install openai-otel

Or

poetry add openai-otel

if you are using poetry.

Usage

Make sure that you have OPENAI_API_KEY set in your environment variables.

import openai
from openai_otel import OpenAIAutoInstrumentor, tracer
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor, ConsoleSpanExporter

trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(
    SimpleSpanProcessor(ConsoleSpanExporter())
)

OpenAIAutoInstrumentor().instrument()

resp = openai.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "user", "content": "what's the meaning of life?"},
    ],
)

print(resp.choices[0].message.content)

Examples

Start up tempo using docker compose:

(
    cd examples/tempo
    docker compose up -d
)

Run the example:

(
    cd examples/fastapi
    pip install -r requirements.txt
    fastapi run
)

Then open http://localhost:8000 - you shall get the meaning of life.

Afterwards you can check out the traces on Grafana. You can do it via:

  • Visit http://localhost:3000
  • Go to the explorer view
  • Click TraceQL
  • Run {.service.name="fastapi-demo" && span.create.request.model="gpt-4o"}

More examples

You will get view like this:

alt text

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

openai_otel-0.1.0a3.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

openai_otel-0.1.0a3-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file openai_otel-0.1.0a3.tar.gz.

File metadata

  • Download URL: openai_otel-0.1.0a3.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.6 Linux/6.8.0-1014-azure

File hashes

Hashes for openai_otel-0.1.0a3.tar.gz
Algorithm Hash digest
SHA256 12969c3055b55027aa86b9854de8ccfabb014f55f2b37796d2d2d1672190d6a0
MD5 9f6c77396301ecfc04c8b58536804364
BLAKE2b-256 a4c5d110462a74e08f32549fe32e8802554934bdab719b0636010c22bd0c2cc2

See more details on using hashes here.

File details

Details for the file openai_otel-0.1.0a3-py3-none-any.whl.

File metadata

  • Download URL: openai_otel-0.1.0a3-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.6 Linux/6.8.0-1014-azure

File hashes

Hashes for openai_otel-0.1.0a3-py3-none-any.whl
Algorithm Hash digest
SHA256 1dc52e6d7a4933319bd3650bf7207cc8e819a868d8ed183beda1547f4b417928
MD5 bfd7b07811beea190b534d45e878608d
BLAKE2b-256 76f83c2bd8156843ad5e5a5a4e43ae9e989aa4a01d7f839dda415916c6cc24b1

See more details on using hashes here.

Supported by

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