Skip to content

Commit

Permalink
fix(openai): track client attributes for v1 SDK of OpenAI (traceloop#522
Browse files Browse the repository at this point in the history
)

Co-authored-by: Nir Gazit <nirga@users.noreply.github.com>
  • Loading branch information
tonybaloney and nirga authored Feb 26, 2024
1 parent a81d94c commit bb9b983
Show file tree
Hide file tree
Showing 10 changed files with 534 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,33 @@ def _set_span_attribute(span, name, value):
return


def _set_client_attributes(span, instance):
if not span.is_recording():
return

if not is_openai_v1():
return

try:
client = instance._client # pylint: disable=protected-access
if isinstance(client, (openai.AsyncOpenAI, openai.OpenAI)):
_set_span_attribute(span, OPENAI_API_BASE, str(client.base_url))
if isinstance(client, (openai.AsyncAzureOpenAI, openai.AzureOpenAI)):
_set_span_attribute(span, OPENAI_API_VERSION, client._api_version) # pylint: disable=protected-access

except Exception as ex: # pylint: disable=broad-except
logger.warning(
"Failed to set api attributes for openai v1 span, error: %s", str(ex)
)


def _set_api_attributes(span):
if not span.is_recording():
return

if is_openai_v1():
return

try:
base_url = openai.base_url if hasattr(openai, "base_url") else openai.api_base

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
from opentelemetry.instrumentation.openai.utils import _with_tracer_wrapper
from opentelemetry.instrumentation.openai.shared import (
_set_client_attributes,
_set_request_attributes,
_set_span_attribute,
_set_functions_attributes,
Expand Down Expand Up @@ -39,7 +40,7 @@ def chat_wrapper(tracer, wrapped, instance, args, kwargs):
attributes={SpanAttributes.LLM_REQUEST_TYPE: LLM_REQUEST_TYPE.value},
)

_handle_request(span, kwargs)
_handle_request(span, kwargs, instance)
response = wrapped(*args, **kwargs)

if is_streaming_response(response):
Expand All @@ -62,7 +63,7 @@ async def achat_wrapper(tracer, wrapped, instance, args, kwargs):
kind=SpanKind.CLIENT,
attributes={SpanAttributes.LLM_REQUEST_TYPE: LLM_REQUEST_TYPE.value},
)
_handle_request(span, kwargs)
_handle_request(span, kwargs, instance)
response = await wrapped(*args, **kwargs)

if is_streaming_response(response):
Expand All @@ -75,8 +76,9 @@ async def achat_wrapper(tracer, wrapped, instance, args, kwargs):
return response


def _handle_request(span, kwargs):
def _handle_request(span, kwargs, instance):
_set_request_attributes(span, kwargs)
_set_client_attributes(span, instance)
if should_send_prompts():
_set_prompts(span, kwargs.get("messages"))
_set_functions_attributes(span, kwargs.get("functions"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
from opentelemetry.instrumentation.openai.utils import _with_tracer_wrapper
from opentelemetry.instrumentation.openai.shared import (
_set_client_attributes,
_set_request_attributes,
_set_span_attribute,
_set_functions_attributes,
Expand Down Expand Up @@ -39,7 +40,7 @@ def completion_wrapper(tracer, wrapped, instance, args, kwargs):
attributes={SpanAttributes.LLM_REQUEST_TYPE: LLM_REQUEST_TYPE.value},
)

_handle_request(span, kwargs)
_handle_request(span, kwargs, instance)
response = wrapped(*args, **kwargs)

if is_streaming_response(response):
Expand All @@ -63,7 +64,7 @@ async def acompletion_wrapper(tracer, wrapped, instance, args, kwargs):
attributes={SpanAttributes.LLM_REQUEST_TYPE: LLM_REQUEST_TYPE.value},
)

_handle_request(span, kwargs)
_handle_request(span, kwargs, instance)
response = await wrapped(*args, **kwargs)

if is_streaming_response(response):
Expand All @@ -76,11 +77,12 @@ async def acompletion_wrapper(tracer, wrapped, instance, args, kwargs):
return response


def _handle_request(span, kwargs):
def _handle_request(span, kwargs, instance):
_set_request_attributes(span, kwargs)
if should_send_prompts():
_set_prompts(span, kwargs.get("prompt"))
_set_functions_attributes(span, kwargs.get("functions"))
_set_client_attributes(span, instance)


def _handle_response(response, span):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
start_as_current_span_async,
)
from opentelemetry.instrumentation.openai.shared import (
_set_client_attributes,
_set_request_attributes,
_set_span_attribute,
_set_response_attributes,
Expand Down Expand Up @@ -37,7 +38,7 @@ def embeddings_wrapper(tracer, wrapped, instance, args, kwargs):
kind=SpanKind.CLIENT,
attributes={SpanAttributes.LLM_REQUEST_TYPE: LLM_REQUEST_TYPE.value},
) as span:
_handle_request(span, kwargs)
_handle_request(span, kwargs, instance)
response = wrapped(*args, **kwargs)
_handle_response(response, span)

Expand All @@ -55,17 +56,18 @@ async def aembeddings_wrapper(tracer, wrapped, instance, args, kwargs):
kind=SpanKind.CLIENT,
attributes={SpanAttributes.LLM_REQUEST_TYPE: LLM_REQUEST_TYPE.value},
) as span:
_handle_request(span, kwargs)
_handle_request(span, kwargs, instance)
response = await wrapped(*args, **kwargs)
_handle_response(response, span)

return response


def _handle_request(span, kwargs):
def _handle_request(span, kwargs, instance):
_set_request_attributes(span, kwargs)
if should_send_prompts():
_set_prompts(span, kwargs.get("input"))
_set_client_attributes(span, instance)


def _handle_response(response, span):
Expand Down
Loading

0 comments on commit bb9b983

Please sign in to comment.