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"}

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.0a1.tar.gz (2.6 kB view details)

Uploaded Source

Built Distribution

openai_otel-0.1.0a1-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: openai_otel-0.1.0a1.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.5.0-1025-azure

File hashes

Hashes for openai_otel-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 ad76150cf20900875893bf80a7cf5225f63b9bc9bdd1699eba477f51f245ba62
MD5 838cdeea7668ed55285106b8a480fd56
BLAKE2b-256 29f291070c5b49e36456da6189698931c4cb691d538073994a6f320ea25a1156

See more details on using hashes here.

File details

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

File metadata

  • Download URL: openai_otel-0.1.0a1-py3-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.5.0-1025-azure

File hashes

Hashes for openai_otel-0.1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 b899465b655a366c7169b40cdb156132feb89f36d0936f6af6dedc6b507d2f48
MD5 81121e109fad096b4d26e99e7f2880b9
BLAKE2b-256 66764edba413c3cf5264db1e6455a79ac90e6fa5c7446b59221766f4b52eb3a4

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