Skip to content

feat(openai): implement emitting events in addition to current behavior #2892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from typing import Callable, Collection, Optional
from typing_extensions import Coroutine

from opentelemetry.instrumentation.instrumentor import BaseInstrumentor

from opentelemetry.instrumentation.openai.shared.config import Config
from opentelemetry.instrumentation.openai.utils import is_openai_v1
from typing_extensions import Coroutine

_instruments = ("openai >= 0.27.0",)

Expand All @@ -22,6 +21,7 @@ def __init__(
Callable[[str, str, str, str], Coroutine[None, None, str]]
] = lambda *args: "",
enable_trace_context_propagation: bool = True,
use_legacy_attributes: bool = True,
):
super().__init__()
Config.enrich_assistant = enrich_assistant
Expand All @@ -30,6 +30,7 @@ def __init__(
Config.get_common_metrics_attributes = get_common_metrics_attributes
Config.upload_base64_image = upload_base64_image
Config.enable_trace_context_propagation = enable_trace_context_propagation
Config.use_legacy_attributes = use_legacy_attributes

def instrumentation_dependencies(self) -> Collection[str]:
return _instruments
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import os
import openai
import json
import types
import logging

import types
from importlib.metadata import version

from opentelemetry import context as context_api
from opentelemetry.trace.propagation import set_span_in_context
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator

from opentelemetry.instrumentation.openai.shared.config import Config
from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import (
GEN_AI_RESPONSE_ID,
)
from opentelemetry.semconv_ai import SpanAttributes
from opentelemetry.instrumentation.openai.utils import (
dont_throw,
is_openai_v1,
should_record_stream_token_usage,
)
from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import (
GEN_AI_RESPONSE_ID,
)
from opentelemetry.semconv_ai import SpanAttributes
from opentelemetry.trace.propagation import set_span_in_context
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator

import openai

OPENAI_LLM_USAGE_TOKEN_TYPES = ["prompt_tokens", "completion_tokens"]
PROMPT_FILTER_KEY = "prompt_filter_results"
Expand All @@ -33,12 +30,6 @@
logger = logging.getLogger(__name__)


def should_send_prompts():
return (
os.getenv("TRACELOOP_TRACE_CONTENT") or "true"
).lower() == "true" or context_api.get_value("override_enable_content_tracing")


def _set_span_attribute(span, name, value):
if value is None or value == "":
return
Expand Down Expand Up @@ -187,7 +178,9 @@ def _set_response_attributes(span, response):
)
prompt_tokens_details = dict(usage.get("prompt_tokens_details", {}))
_set_span_attribute(
span, SpanAttributes.LLM_USAGE_CACHE_READ_INPUT_TOKENS, prompt_tokens_details.get("cached_tokens", 0)
span,
SpanAttributes.LLM_USAGE_CACHE_READ_INPUT_TOKENS,
prompt_tokens_details.get("cached_tokens", 0),
)
return

Expand All @@ -206,17 +199,17 @@ def _set_span_stream_usage(span, prompt_tokens, completion_tokens):
if not span.is_recording():
return

if type(completion_tokens) is int and completion_tokens >= 0:
if isinstance(completion_tokens, int) and completion_tokens >= 0:
_set_span_attribute(
span, SpanAttributes.LLM_USAGE_COMPLETION_TOKENS, completion_tokens
)

if type(prompt_tokens) is int and prompt_tokens >= 0:
if isinstance(prompt_tokens, int) and prompt_tokens >= 0:
_set_span_attribute(span, SpanAttributes.LLM_USAGE_PROMPT_TOKENS, prompt_tokens)

if (
type(prompt_tokens) is int
and type(completion_tokens) is int
isinstance(prompt_tokens, int)
and isinstance(completion_tokens, int)
and completion_tokens + prompt_tokens >= 0
):
_set_span_attribute(
Expand Down
Loading