Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

[Feat]: Refactor Langchain OpenTelemetry Instrumentation (#809)

Patcherf0267edd8ca2
diff --git a/sdk/python/pyproject.toml b/sdk/python/pyproject.toml
index 03dd48fd..bcfebd18 100644
--- a/sdk/python/pyproject.toml
+++ b/sdk/python/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openlit"
-version = "1.34.30"
+version = "1.34.31"
description = "OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications and GPUs, facilitating the integration of observability into your GenAI-driven projects"
authors = ["OpenLIT"]
license = "Apache-2.0"
@@ -24,6 +24,7 @@ opentelemetry-instrumentation = ">=0.52b0,<1.0.0"
openai = "^1.1.1"
anthropic = ">=0.42.0,<1.0.0"
openai-agents = ">=0.0.3"
+langchain = "^0.3.15"
[build-system]
requires = ["poetry-core>=1.1.0"]
diff --git a/sdk/python/src/openlit/__helpers.py b/sdk/python/src/openlit/__helpers.py
index 2d64c0f2..d5ea7046 100644
--- a/sdk/python/src/openlit/__helpers.py
+++ b/sdk/python/src/openlit/__helpers.py
@@ -2,6 +2,7 @@
"""
This module has functions to calculate model costs based on tokens and to fetch pricing information.
"""
+
import os
import json
import logging
@@ -9,7 +10,11 @@ from urllib.parse import urlparse
from typing import Any, Dict, List, Tuple
import math
import requests
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from opentelemetry.trace import Status, StatusCode
from opentelemetry._events import Event
from openlit.semcov import SemanticConvention
@@ -17,6 +22,7 @@ from openlit.semcov import SemanticConvention
# Set up logging
logger = logging.getLogger(__name__)
+
def response_as_dict(response):
"""
Return parsed response as a dict
@@ -25,13 +31,14 @@ def response_as_dict(response):
# pylint: disable=no-else-return
if isinstance(response, dict):
return response
- if hasattr(response, 'model_dump'):
+ if hasattr(response, "model_dump"):
return response.model_dump()
- elif hasattr(response, 'parse'):
+ elif hasattr(response, "parse"):
return response_as_dict(response.parse())
else:
return response
+
def get_env_variable(name, arg_value, error_message):
"""
Retrieve an environment variable if the argument is not provided
@@ -45,6 +52,7 @@ def get_env_variable(name, arg_value, error_message):
raise RuntimeError(error_message)
return value
+
def general_tokens(text):
"""
Calculate the number of tokens a given text would take up.
@@ -52,40 +60,45 @@ def general_tokens(text):
return math.ceil(len(text) / 2)
+
def get_chat_model_cost(model, pricing_info, prompt_tokens, completion_tokens):
"""
Retrieve the cost of processing for a given model based on prompt and tokens.
"""
try:
- cost = ((prompt_tokens / 1000) * pricing_info['chat'][model]['promptPrice']) + \
- ((completion_tokens / 1000) * pricing_info['chat'][model]['completionPrice'])
+ cost = ((prompt_tokens / 1000) * pricing_info["chat"][model]["promptPrice"]) + (
+ (completion_tokens / 1000) * pricing_info["chat"][model]["completionPrice"]
+ )
except:
cost = 0
return cost
+
def get_embed_model_cost(model, pricing_info, prompt_tokens):
"""
Retrieve the cost of processing for a given model based on prompt tokens.
"""
try:
- cost = (prompt_tokens / 1000) * pricing_info['embeddings'][model]
+ cost = (prompt_tokens / 1000) * pricing_info["embeddings"][model]
except:
cost = 0
return cost
+
def get_image_model_cost(model, pricing_info, size, quality):
"""
Retrieve the cost of processing for a given model based on image size and quailty.
"""
try:
- cost = pricing_info['images'][model][quality][size]
+ cost = pricing_info["images"][model][quality][size]
except:
cost = 0
return cost
+
def get_audio_model_cost(model, pricing_info, prompt, duration=None):
"""
Retrieve the cost of processing for a given model based on prompt.
@@ -93,52 +106,59 @@ def get_audio_model_cost(model, pricing_info, prompt, duration=None):
try:
if prompt:
- cost = (len(prompt) / 1000) * pricing_info['audio'][model]
+ cost = (len(prompt) / 1000) * pricing_info["audio"][model]
else:
- cost = duration * pricing_info['audio'][model]
+ cost = duration * pricing_info["audio"][model]
except:
cost = 0
return cost
+
def fetch_pricing_info(pricing_json=None):
"""
Fetches pricing information from a specified URL or File Path.
"""
if pricing_json:
- is_url = urlparse(pricing_json).scheme != ''
+ is_url = urlparse(pricing_json).scheme != ""
if is_url:
pricing_url = pricing_json
else:
try:
- with open(pricing_json, mode='r', encoding='utf-8') as f:
+ with open(pricing_json, mode="r", encoding="utf-8") as f:
return json.load(f)
except FileNotFoundError:
- logger.error('Pricing information file not found: %s', pricing_json)
+ logger.error("Pricing information file not found: %s", pricing_json)
except json.JSONDecodeError:
- logger.error('Error decoding JSON from file: %s', pricing_json)
+ logger.error("Error decoding JSON from file: %s", pricing_json)
except Exception as file_err:
- logger.error('Unexpected error occurred while reading file: %s', file_err)
+ logger.error(
+ "Unexpected error occurred while reading file: %s", file_err
+ )
return {}
else:
- pricing_url = 'https://raw.githubusercontent.com/openlit/openlit/main/assets/pricing.json'
+ pricing_url = (
+ "https://raw.githubusercontent.com/openlit/openlit/main/assets/pricing.json"
+ )
try:
# Set a timeout of 10 seconds for both the connection and the read
response = requests.get(pricing_url, timeout=20)
response.raise_for_status()
return response.json()
except requests.HTTPError as http_err:
- logger.error('HTTP error occured while fetching pricing info: %s', http_err)
+ logger.error("HTTP error occured while fetching pricing info: %s", http_err)
except Exception as err:
- logger.error('Unexpected error occurred while fetching pricing info: %s', err)
+ logger.error("Unexpected error occurred while fetching pricing info: %s", err)
return {}
-def handle_exception(span,e):
+
+def handle_exception(span, e):
"""Handles Exception when LLM Function fails or trace creation fails."""
span.record_exception(e)
span.set_status(Status(StatusCode.ERROR))
+
def calculate_ttft(timestamps: List[float], start_time: float) -> float:
"""
Calculate the time to the first tokens.
@@ -148,16 +168,20 @@ def calculate_ttft(timestamps: List[float], start_time: float) -> float:
return timestamps[0] - start_time
return 0.0
+
def calculate_tbt(timestamps: List[float]) -> float:
"""
Calculate the average time between tokens.
"""
if len(timestamps) > 1:
- time_diffs = [timestamps[i] - timestamps[i - 1] for i in range(1, len(timestamps))]
+ time_diffs = [
+ timestamps[i] - timestamps[i - 1] for i in range(1, len(timestamps))
+ ]
return sum(time_diffs) / len(time_diffs)
return 0.0
+
def create_metrics_attributes(
service_name: str,
deployment_environment: str,
@@ -173,7 +197,7 @@ def create_metrics_attributes(
"""
return {
- TELEMETRY_SDK_NAME: 'openlit',
+ TELEMETRY_SDK_NAME: "openlit",
SERVICE_NAME: service_name,
DEPLOYMENT_ENVIRONMENT: deployment_environment,
SemanticConvention.GEN_AI_OPERATION: operation,
@@ -181,39 +205,41 @@ def create_metrics_attributes(
SemanticConvention.GEN_AI_REQUEST_MODEL: request_model,
SemanticConvention.SERVER_ADDRESS: server_address,
SemanticConvention.SERVER_PORT: server_port,
- SemanticConvention.GEN_AI_RESPONSE_MODEL: response_model
+ SemanticConvention.GEN_AI_RESPONSE_MODEL: response_model,
}
-def set_server_address_and_port(client_instance: Any,
- default_server_address: str, default_server_port: int) -> Tuple[str, int]:
+
+def set_server_address_and_port(
+ client_instance: Any, default_server_address: str, default_server_port: int
+) -> Tuple[str, int]:
"""
Determines and returns the server address and port based on the provided client's `base_url`,
using defaults if none found or values are None.
"""
# Try getting base_url from multiple potential attributes
- base_client = getattr(client_instance, '_client', None)
- base_url = getattr(base_client, 'base_url', None)
+ base_client = getattr(client_instance, "_client", None)
+ base_url = getattr(base_client, "base_url", None)
if not base_url:
# Attempt to get endpoint from instance._config.endpoint if base_url is not set
- config = getattr(client_instance, '_config', None)
- base_url = getattr(config, 'endpoint', None)
+ config = getattr(client_instance, "_config", None)
+ base_url = getattr(config, "endpoint", None)
if not base_url:
# Attempt to get server_url from instance.sdk_configuration.server_url
- config = getattr(client_instance, 'sdk_configuration', None)
- base_url = getattr(config, 'server_url', None)
+ config = getattr(client_instance, "sdk_configuration", None)
+ base_url = getattr(config, "server_url", None)
if not base_url:
# Attempt to get host from instance.config.host (used by Pinecone and other vector DBs)
- config = getattr(client_instance, 'config', None)
- base_url = getattr(config, 'host', None)
+ config = getattr(client_instance, "config", None)
+ base_url = getattr(config, "host", None)
if base_url:
if isinstance(base_url, str):
# Check if it's a full URL or just a hostname
- if base_url.startswith(('http://', 'https://')):
+ if base_url.startswith(("http://", "https://")):
url = urlparse(base_url)
server_address = url.hostname or default_server_address
server_port = url.port if url.port is not None else default_server_port
@@ -222,8 +248,8 @@ def set_server_address_and_port(client_instance: Any,
server_address = base_url
server_port = default_server_port
else: # base_url might not be a str; handle as an object.
- server_address = getattr(base_url, 'host', None) or default_server_address
- port_attr = getattr(base_url, 'port', None)
+ server_address = getattr(base_url, "host", None) or default_server_address
+ port_attr = getattr(base_url, "port", None)
server_port = port_attr if port_attr is not None else default_server_port
else: # no base_url or endpoint provided; use defaults.
server_address = default_server_address
@@ -231,6 +257,7 @@ def set_server_address_and_port(client_instance: Any,
return server_address, server_port
+
def otel_event(name, attributes, body):
"""
Returns an OpenTelemetry Event object
@@ -242,28 +269,31 @@ def otel_event(name, attributes, body):
body=body,
)
+
def extract_and_format_input(messages):
"""
Process a list of messages to extract content and categorize
them into fixed roles like 'user', 'assistant', 'system', 'tool'.
"""
- fixed_roles = ['user', 'assistant', 'system', 'tool', 'developer']
- formatted_messages = {role_key: {'role': '', 'content': ''} for role_key in fixed_roles}
+ fixed_roles = ["user", "assistant", "system", "tool", "developer"]
+ formatted_messages = {
+ role_key: {"role": "", "content": ""} for role_key in fixed_roles
+ }
# Check if input is a simple string
if isinstance(messages, str):
- formatted_messages['user'] = {'role': 'user', 'content': messages}
+ formatted_messages["user"] = {"role": "user", "content": messages}
return formatted_messages
for message in messages:
message = response_as_dict(message)
- role = message.get('role')
+ role = message.get("role")
if role not in fixed_roles:
continue
- content = message.get('content', '')
+ content = message.get("content", "")
# Prepare content as a string, handling both list and str
if isinstance(content, list):
@@ -272,27 +302,29 @@ def extract_and_format_input(messages):
content_str = content
# Set the role in the formatted message and concatenate content
- if not formatted_messages[role]['role']:
- formatted_messages[role]['role'] = role
+ if not formatted_messages[role]["role"]:
+ formatted_messages[role]["role"] = role
- if formatted_messages[role]['content']:
- formatted_messages[role]['content'] += ' ' + content_str
+ if formatted_messages[role]["content"]:
+ formatted_messages[role]["content"] += " " + content_str
else:
- formatted_messages[role]['content'] = content_str
+ formatted_messages[role]["content"] = content_str
return formatted_messages
+
# To be removed one the change to log events (from span events) is complete
def concatenate_all_contents(formatted_messages):
"""
Concatenate all 'content' fields into a single strin
"""
- return ' '.join(
- message_data['content']
+ return " ".join(
+ message_data["content"]
for message_data in formatted_messages.values()
- if message_data['content']
+ if message_data["content"]
)
+
def format_and_concatenate(messages):
"""
Process a list of messages to extract content, categorize them by role,
@@ -303,20 +335,22 @@ def format_and_concatenate(messages):
# Check if input is a simple string
if isinstance(messages, str):
- formatted_messages['user'] = {'role': 'user', 'content': messages}
+ formatted_messages["user"] = {"role": "user", "content": messages}
elif isinstance(messages, list) and all(isinstance(m, str) for m in messages):
# If it's a list of strings, each string is 'user' input
- user_content = ' '.join(messages)
- formatted_messages['user'] = {'role': 'user', 'content': user_content}
+ user_content = " ".join(messages)
+ formatted_messages["user"] = {"role": "user", "content": user_content}
else:
for message in messages:
message = response_as_dict(message)
- role = message.get('role', 'unknown') # Default to 'unknown' if no role is specified
- content = message.get('content', '')
+ role = message.get(
+ "role", "unknown"
+ ) # Default to 'unknown' if no role is specified
+ content = message.get("content", "")
# Initialize role in formatted messages if not present
if role not in formatted_messages:
- formatted_messages[role] = {'role': role, 'content': ''}
+ formatted_messages[role] = {"role": role, "content": ""}
# Handle list of dictionaries in content
if isinstance(content, list):
@@ -324,8 +358,8 @@ def format_and_concatenate(messages):
for item in content:
if isinstance(item, dict):
# Collect text or other attributes as needed
- text = item.get('text', '')
- image_url = item.get('image_url', '')
+ text = item.get("text", "")
+ image_url = item.get("image_url", "")
content_str.append(text)
content_str.append(image_url)
content_str = ", ".join(filter(None, content_str))
@@ -333,20 +367,34 @@ def format_and_concatenate(messages):
content_str = content
# Concatenate content
- if formatted_messages[role]['content']:
- formatted_messages[role]['content'] += ' ' + content_str
+ if formatted_messages[role]["content"]:
+ formatted_messages[role]["content"] += " " + content_str
else:
- formatted_messages[role]['content'] = content_str
+ formatted_messages[role]["content"] = content_str
# Concatenate role and content for all messages
- return ' '.join(
+ return " ".join(
f"{message_data['role']}: {message_data['content']}"
for message_data in formatted_messages.values()
- if message_data['content']
+ if message_data["content"]
)
-def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_address, server_port,
- request_model, response_model, environment, application_name, is_stream, tbt, ttft, version):
+
+def common_span_attributes(
+ scope,
+ gen_ai_operation,
+ gen_ai_system,
+ server_address,
+ server_port,
+ request_model,
+ response_model,
+ environment,
+ application_name,
+ is_stream,
+ tbt,
+ ttft,
+ version,
+):
"""
Set common span attributes for both chat and RAG operations.
"""
@@ -365,9 +413,25 @@ def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_addres
scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft)
scope._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
-def record_completion_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
- request_model, response_model, environment, application_name, start_time, end_time, cost,
- input_tokens, output_tokens, tbt, ttft):
+
+def record_completion_metrics(
+ metrics,
+ gen_ai_operation,
+ gen_ai_system,
+ server_address,
+ server_port,
+ request_model,
+ response_model,
+ environment,
+ application_name,
+ start_time,
+ end_time,
+ cost,
+ input_tokens,
+ output_tokens,
+ tbt,
+ ttft,
+):
"""
Record completion metrics for the operation.
"""
@@ -382,7 +446,9 @@ def record_completion_metrics(metrics, gen_ai_operation, gen_ai_system, server_a
service_name=application_name,
deployment_environment=environment,
)
- metrics["genai_client_usage_tokens"].record(input_tokens + output_tokens, attributes)
+ metrics["genai_client_usage_tokens"].record(
+ input_tokens + output_tokens, attributes
+ )
metrics["genai_client_operation_duration"].record(end_time - start_time, attributes)
metrics["genai_server_tbt"].record(tbt, attributes)
metrics["genai_server_ttft"].record(ttft, attributes)
@@ -391,9 +457,22 @@ def record_completion_metrics(metrics, gen_ai_operation, gen_ai_system, server_a
metrics["genai_prompt_tokens"].add(input_tokens, attributes)
metrics["genai_cost"].record(cost, attributes)
-def record_embedding_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
- request_model, response_model, environment, application_name, start_time, end_time,
- input_tokens, cost):
+
+def record_embedding_metrics(
+ metrics,
+ gen_ai_operation,
+ gen_ai_system,
+ server_address,
+ server_port,
+ request_model,
+ response_model,
+ environment,
+ application_name,
+ start_time,
+ end_time,
+ input_tokens,
+ cost,
+):
"""
Record embedding-specific metrics for the operation.
"""
@@ -414,8 +493,21 @@ def record_embedding_metrics(metrics, gen_ai_operation, gen_ai_system, server_ad
metrics["genai_prompt_tokens"].add(input_tokens, attributes)
metrics["genai_cost"].record(cost, attributes)
-def record_audio_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
- request_model, response_model, environment, application_name, start_time, end_time, cost):
+
+def record_audio_metrics(
+ metrics,
+ gen_ai_operation,
+ gen_ai_system,
+ server_address,
+ server_port,
+ request_model,
+ response_model,
+ environment,
+ application_name,
+ start_time,
+ end_time,
+ cost,
+):
"""
Record audio-specific metrics for the operation.
"""
@@ -434,8 +526,21 @@ def record_audio_metrics(metrics, gen_ai_operation, gen_ai_system, server_addres
metrics["genai_requests"].add(1, attributes)
metrics["genai_cost"].record(cost, attributes)
-def record_image_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
- request_model, response_model, environment, application_name, start_time, end_time, cost):
+
+def record_image_metrics(
+ metrics,
+ gen_ai_operation,
+ gen_ai_system,
+ server_address,
+ server_port,
+ request_model,
+ response_model,
+ environment,
+ application_name,
+ start_time,
+ end_time,
+ cost,
+):
"""
Record image-specific metrics for the operation.
"""
@@ -454,14 +559,25 @@ def record_image_metrics(metrics, gen_ai_operation, gen_ai_system, server_addres
metrics["genai_requests"].add(1, attributes)
metrics["genai_cost"].record(cost, attributes)
-def common_db_span_attributes(scope, db_system, server_address, server_port,
- environment, application_name, version):
+
+def common_db_span_attributes(
+ scope,
+ db_system,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ version,
+):
"""
Set common span attributes for database operations.
"""
scope._span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- scope._span.set_attribute(SemanticConvention.GEN_AI_OPERATION, SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB,
+ )
scope._span.set_attribute(SemanticConvention.DB_SYSTEM_NAME, db_system)
scope._span.set_attribute(SemanticConvention.SERVER_ADDRESS, server_address)
scope._span.set_attribute(SemanticConvention.SERVER_PORT, server_port)
@@ -469,8 +585,18 @@ def common_db_span_attributes(scope, db_system, server_address, server_port,
scope._span.set_attribute(SERVICE_NAME, application_name)
scope._span.set_attribute(SemanticConvention.DB_SDK_VERSION, version)
-def common_framework_span_attributes(scope, framework_system, server_address, server_port,
- environment, application_name, version, endpoint, instance=None):
+
+def common_framework_span_attributes(
+ scope,
+ framework_system,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ version,
+ endpoint,
+ instance=None,
+):
"""
Set common span attributes for GenAI framework operations.
"""
@@ -479,17 +605,31 @@ def common_framework_span_attributes(scope, framework_system, server_address, se
scope._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
scope._span.set_attribute(SemanticConvention.GEN_AI_SYSTEM, framework_system)
scope._span.set_attribute(SemanticConvention.GEN_AI_OPERATION, endpoint)
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- getattr(instance, "model_name", "unknown") if instance else "unknown")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MODEL,
+ getattr(instance, "model_name", "unknown") if instance else "unknown",
+ )
scope._span.set_attribute(SemanticConvention.SERVER_ADDRESS, server_address)
scope._span.set_attribute(SemanticConvention.SERVER_PORT, server_port)
scope._span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
scope._span.set_attribute(SERVICE_NAME, application_name)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_OPERATION_DURATION,
- scope._end_time - scope._start_time)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_OPERATION_DURATION,
+ scope._end_time - scope._start_time,
+ )
-def record_framework_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
- environment, application_name, start_time, end_time):
+
+def record_framework_metrics(
+ metrics,
+ gen_ai_operation,
+ gen_ai_system,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ start_time,
+ end_time,
+):
"""
Record basic framework metrics for the operation (only gen_ai_requests counter).
"""
@@ -507,8 +647,17 @@ def record_framework_metrics(metrics, gen_ai_operation, gen_ai_system, server_ad
metrics["genai_requests"].add(1, attributes)
metrics["genai_client_operation_duration"].record(end_time - start_time, attributes)
-def record_db_metrics(metrics, db_system, server_address, server_port,
- environment, application_name, start_time, end_time):
+
+def record_db_metrics(
+ metrics,
+ db_system,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ start_time,
+ end_time,
+):
"""
Record database-specific metrics for the operation.
"""
diff --git a/sdk/python/src/openlit/__init__.py b/sdk/python/src/openlit/__init__.py
index abc9aba2..18b28050 100644
--- a/sdk/python/src/openlit/__init__.py
+++ b/sdk/python/src/openlit/__init__.py
@@ -137,7 +137,9 @@ def module_exists(module_name):
return True
-def instrument_if_available(instrumentor_name, instrumentor_instance, config, disabled_instrumentors):
+def instrument_if_available(
+ instrumentor_name, instrumentor_instance, config, disabled_instrumentors
+):
"""Instruments the specified instrumentor if its library is available."""
if instrumentor_name in disabled_instrumentors:
logger.info("Instrumentor %s is disabled", instrumentor_name)
@@ -242,13 +244,13 @@ def init(
# Setup events based on the provided or default configuration.
event_provider = setup_events(
- application_name=application_name,
- environment=environment,
- event_logger=event_logger,
- otlp_endpoint=None,
- otlp_headers=None,
- disable_batch=disable_batch,
- )
+ application_name=application_name,
+ environment=environment,
+ event_logger=event_logger,
+ otlp_endpoint=None,
+ otlp_headers=None,
+ disable_batch=disable_batch,
+ )
if not event_provider:
logger.error("OpenLIT events setup failed. Events will not be available")
@@ -268,8 +270,11 @@ def init(
)
return
- if os.getenv("OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT", "").lower == "false":
- capture_message_content=False
+ if (
+ os.getenv("OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT", "").lower
+ == "false"
+ ):
+ capture_message_content = False
# Update global configuration with the provided settings.
config.update_config(
@@ -462,9 +467,7 @@ def trace(wrapped):
SERVICE_NAME,
OpenlitConfig.application_name,
)
- span.set_attribute(
- DEPLOYMENT_ENVIRONMENT, OpenlitConfig.environment
- )
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, OpenlitConfig.environment)
except Exception as meta_exception:
logging.error(
"Failed to set metadata for %s: %s",
diff --git a/sdk/python/src/openlit/_instrumentors.py b/sdk/python/src/openlit/_instrumentors.py
index 72294fcf..1f0b9032 100644
--- a/sdk/python/src/openlit/_instrumentors.py
+++ b/sdk/python/src/openlit/_instrumentors.py
@@ -1,6 +1,7 @@
"""
OpenLIT Instrumentors
"""
+
import importlib
# Mapping of instrumentor names to their required Python packages
@@ -49,7 +50,7 @@ MODULE_NAME_MAP = {
"letta": "letta",
"together": "together",
"openai-agents": "agents",
- "pydantic_ai": "pydantic_ai"
+ "pydantic_ai": "pydantic_ai",
}
# Dictionary mapping instrumentor names to their full module paths
diff --git a/sdk/python/src/openlit/evals/all.py b/sdk/python/src/openlit/evals/all.py
index b106bddd..73ff9113 100644
--- a/sdk/python/src/openlit/evals/all.py
+++ b/sdk/python/src/openlit/evals/all.py
@@ -11,11 +11,14 @@ from openlit.evals.utils import (
llm_response,
parse_llm_response,
eval_metrics,
- eval_metric_attributes
+ eval_metric_attributes,
)
-def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None,
- threshold_score: Optional[float] = 0.5) -> str:
+
+def get_system_prompt(
+ custom_categories: Optional[Dict[str, str]] = None,
+ threshold_score: Optional[float] = 0.5,
+) -> str:
"""
Returns the system prompt used for LLM analysis, including custom categories if provided.
@@ -51,8 +54,12 @@ def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None,
"""
if custom_categories:
- custom_categories_str = "\n".join([f"- {key}: {value}" for key, value in custom_categories.items()])
- base_prompt += f"\n Additional Hallucination Categories:\n{custom_categories_str}"
+ custom_categories_str = "\n".join(
+ [f"- {key}: {value}" for key, value in custom_categories.items()]
+ )
+ base_prompt += (
+ f"\n Additional Hallucination Categories:\n{custom_categories_str}"
+ )
base_prompt += """
@@ -96,6 +103,7 @@ def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None,
"""
return base_prompt
+
class All:
"""
A class to detect Bias, Hallucination and Toxicity in AI responses using LLM or custom categories.
@@ -108,11 +116,16 @@ class All:
custom_categories (Optional[Dict[str, str]]): Additional categories for prompt injections.
"""
- def __init__(self, provider: Optional[str] = "openai", api_key: Optional[str] = None,
- model: Optional[str] = None, base_url: Optional[str] = None,
- custom_categories: Optional[Dict[str, str]] = None,
- collect_metrics: Optional[bool] = False,
- threshold_score: Optional[float] = 0.5):
+ def __init__(
+ self,
+ provider: Optional[str] = "openai",
+ api_key: Optional[str] = None,
+ model: Optional[str] = None,
+ base_url: Optional[str] = None,
+ custom_categories: Optional[Dict[str, str]] = None,
+ collect_metrics: Optional[bool] = False,
+ threshold_score: Optional[float] = 0.5,
+ ):
"""
Initializes the All Evals detector with specified LLM settings, custom rules, and categories.
@@ -130,15 +143,22 @@ class All:
self.provider = provider
if self.provider is None:
raise ValueError("An LLM provider must be specified evaluation.")
- self.api_key, self.model, self.base_url = setup_provider(provider, api_key, model, base_url)
+ self.api_key, self.model, self.base_url = setup_provider(
+ provider, api_key, model, base_url
+ )
self.collect_metrics = collect_metrics
self.custom_categories = custom_categories
self.threshold_score = threshold_score
- self.system_prompt = get_system_prompt(self.custom_categories, self.threshold_score)
-
- def measure(self, prompt: Optional[str] = "",
- contexts: Optional[List[str]] = None,
- text: Optional[str] = None) -> JsonOutput:
+ self.system_prompt = get_system_prompt(
+ self.custom_categories, self.threshold_score
+ )
+
+ def measure(
+ self,
+ prompt: Optional[str] = "",
+ contexts: Optional[List[str]] = None,
+ text: Optional[str] = None,
+ ) -> JsonOutput:
"""
Detects toxicity in AI output using LLM or custom rules.
@@ -156,14 +176,23 @@ class All:
llm_result = parse_llm_response(response)
result_verdict = "yes" if llm_result.score > self.threshold_score else "no"
- result = JsonOutput(score=llm_result.score, evaluation=llm_result.evaluation,
- classification=llm_result.classification,
- explanation=llm_result.explanation, verdict=result_verdict)
+ result = JsonOutput(
+ score=llm_result.score,
+ evaluation=llm_result.evaluation,
+ classification=llm_result.classification,
+ explanation=llm_result.explanation,
+ verdict=result_verdict,
+ )
if self.collect_metrics:
eval_counter = eval_metrics()
- attributes = eval_metric_attributes(result_verdict, result.score, result.evaluation,
- result.classification, result.explanation)
+ attributes = eval_metric_attributes(
+ result_verdict,
+ result.score,
+ result.evaluation,
+ result.classification,
+ result.explanation,
+ )
eval_counter.add(1, attributes)
return result
diff --git a/sdk/python/src/openlit/evals/bias_detection.py b/sdk/python/src/openlit/evals/bias_detection.py
index 0aed2540..cfae70c3 100644
--- a/sdk/python/src/openlit/evals/bias_detection.py
+++ b/sdk/python/src/openlit/evals/bias_detection.py
@@ -11,11 +11,14 @@ from openlit.evals.utils import (
llm_response,
parse_llm_response,
eval_metrics,
- eval_metric_attributes
+ eval_metric_attributes,
)
-def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None,
- threshold_score: Optional[float] = 0.5) -> str:
+
+def get_system_prompt(
+ custom_categories: Optional[Dict[str, str]] = None,
+ threshold_score: Optional[float] = 0.5,
+) -> str:
"""
Returns the system prompt used for LLM analysis, including custom categories if provided.
@@ -55,7 +58,9 @@ def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None,
"""
if custom_categories:
- custom_categories_str = "\n".join([f"- {key}: {value}" for key, value in custom_categories.items()])
+ custom_categories_str = "\n".join(
+ [f"- {key}: {value}" for key, value in custom_categories.items()]
+ )
base_prompt += f"\n Additional Bias Categories:\n{custom_categories_str}"
base_prompt += """
@@ -100,6 +105,7 @@ def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None,
"""
return base_prompt
+
class BiasDetector:
"""
A class to detect Bias in AI responses using LLM or custom categories.
@@ -112,11 +118,16 @@ class BiasDetector:
custom_categories (Optional[Dict[str, str]]): Additional categories for prompt injections.
"""
- def __init__(self, provider: Optional[str] = "openai", api_key: Optional[str] = None,
- model: Optional[str] = None, base_url: Optional[str] = None,
- custom_categories: Optional[Dict[str, str]] = None,
- collect_metrics: Optional[bool] = False,
- threshold_score: Optional[float] = 0.5):
+ def __init__(
+ self,
+ provider: Optional[str] = "openai",
+ api_key: Optional[str] = None,
+ model: Optional[str] = None,
+ base_url: Optional[str] = None,
+ custom_categories: Optional[Dict[str, str]] = None,
+ collect_metrics: Optional[bool] = False,
+ threshold_score: Optional[float] = 0.5,
+ ):
"""
Initializes the Bias detector with specified LLM settings, custom rules, and categories.
@@ -134,15 +145,22 @@ class BiasDetector:
self.provider = provider
if self.provider is None:
raise ValueError("An LLM provider must be specified for Bias detection.")
- self.api_key, self.model, self.base_url = setup_provider(provider, api_key, model, base_url)
+ self.api_key, self.model, self.base_url = setup_provider(
+ provider, api_key, model, base_url
+ )
self.collect_metrics = collect_metrics
self.custom_categories = custom_categories
self.threshold_score = threshold_score
- self.system_prompt = get_system_prompt(self.custom_categories, self.threshold_score)
-
- def measure(self, prompt: Optional[str] = "",
- contexts: Optional[List[str]] = None,
- text: Optional[str] = None) -> JsonOutput:
+ self.system_prompt = get_system_prompt(
+ self.custom_categories, self.threshold_score
+ )
+
+ def measure(
+ self,
+ prompt: Optional[str] = "",
+ contexts: Optional[List[str]] = None,
+ text: Optional[str] = None,
+ ) -> JsonOutput:
"""
Detects toxicity in AI output using LLM or custom rules.
@@ -160,14 +178,23 @@ class BiasDetector:
llm_result = parse_llm_response(response)
result_verdict = "yes" if llm_result.score > self.threshold_score else "no"
- result = JsonOutput(score=llm_result.score, evaluation=llm_result.evaluation,
- classification=llm_result.classification,
- explanation=llm_result.explanation, verdict=result_verdict)
+ result = JsonOutput(
+ score=llm_result.score,
+ evaluation=llm_result.evaluation,
+ classification=llm_result.classification,
+ explanation=llm_result.explanation,
+ verdict=result_verdict,
+ )
if self.collect_metrics:
eval_counter = eval_metrics()
- attributes = eval_metric_attributes(result_verdict, result.score, result.evaluation,
- result.classification, result.explanation)
+ attributes = eval_metric_attributes(
+ result_verdict,
+ result.score,
+ result.evaluation,
+ result.classification,
+ result.explanation,
+ )
eval_counter.add(1, attributes)
return result
diff --git a/sdk/python/src/openlit/evals/hallucination.py b/sdk/python/src/openlit/evals/hallucination.py
index c5f667ee..5ba406d5 100644
--- a/sdk/python/src/openlit/evals/hallucination.py
+++ b/sdk/python/src/openlit/evals/hallucination.py
@@ -11,12 +11,15 @@ from openlit.evals.utils import (
llm_response,
parse_llm_response,
eval_metrics,
- eval_metric_attributes
+ eval_metric_attributes,
)
+
# pylint: disable=unused-argument
-def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None,
- threshold_score: Optional[float] = 0.5) -> str:
+def get_system_prompt(
+ custom_categories: Optional[Dict[str, str]] = None,
+ threshold_score: Optional[float] = 0.5,
+) -> str:
"""
Returns the system prompt used for LLM analysis, including custom categories if provided.
@@ -49,8 +52,12 @@ def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None,
"""
if custom_categories:
- custom_categories_str = "\n".join([f"- {key}: {value}" for key, value in custom_categories.items()])
- base_prompt += f"\n Additional Hallucination Categories:\n{custom_categories_str}"
+ custom_categories_str = "\n".join(
+ [f"- {key}: {value}" for key, value in custom_categories.items()]
+ )
+ base_prompt += (
+ f"\n Additional Hallucination Categories:\n{custom_categories_str}"
+ )
base_prompt += """
@@ -98,6 +105,7 @@ def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None,
return base_prompt
+
class Hallucination:
"""
A class to detect hallucinations in AI responses using LLM or custom categories.
@@ -110,11 +118,16 @@ class Hallucination:
custom_categories (Optional[Dict[str, str]]): Additional categories for prompt injections.
"""
- def __init__(self, provider: Optional[str] = "openai", api_key: Optional[str] = None,
- model: Optional[str] = None, base_url: Optional[str] = None,
- custom_categories: Optional[Dict[str, str]] = None,
- collect_metrics: Optional[bool] = False,
- threshold_score: Optional[float] = 0.5):
+ def __init__(
+ self,
+ provider: Optional[str] = "openai",
+ api_key: Optional[str] = None,
+ model: Optional[str] = None,
+ base_url: Optional[str] = None,
+ custom_categories: Optional[Dict[str, str]] = None,
+ collect_metrics: Optional[bool] = False,
+ threshold_score: Optional[float] = 0.5,
+ ):
"""
Initializes the Hallucination detector with specified LLM settings, custom rules, and categories.
@@ -131,16 +144,25 @@ class Hallucination:
self.provider = provider
if self.provider is None:
- raise ValueError("An LLM provider must be specified for Hallucination detection.")
- self.api_key, self.model, self.base_url = setup_provider(provider, api_key, model, base_url)
+ raise ValueError(
+ "An LLM provider must be specified for Hallucination detection."
+ )
+ self.api_key, self.model, self.base_url = setup_provider(
+ provider, api_key, model, base_url
+ )
self.collect_metrics = collect_metrics
self.custom_categories = custom_categories
self.threshold_score = threshold_score
- self.system_prompt = get_system_prompt(self.custom_categories, self.threshold_score)
-
- def measure(self, prompt: Optional[str] = "",
- contexts: Optional[List[str]] = None,
- text: Optional[str] = None) -> JsonOutput:
+ self.system_prompt = get_system_prompt(
+ self.custom_categories, self.threshold_score
+ )
+
+ def measure(
+ self,
+ prompt: Optional[str] = "",
+ contexts: Optional[List[str]] = None,
+ text: Optional[str] = None,
+ ) -> JsonOutput:
"""
Detects hallucinations in AI output using LLM or custom rules.
@@ -157,14 +179,23 @@ class Hallucination:
response = llm_response(self.provider, llm_prompt, self.model, self.base_url)
llm_result = parse_llm_response(response)
result_verdict = "yes" if llm_result.score > self.threshold_score else "no"
- result = JsonOutput(score=llm_result.score, evaluation=llm_result.evaluation,
- classification=llm_result.classification,
- explanation=llm_result.explanation, verdict=result_verdict)
+ result = JsonOutput(
+ score=llm_result.score,
+ evaluation=llm_result.evaluation,
+ classification=llm_result.classification,
+ explanation=llm_result.explanation,
+ verdict=result_verdict,
+ )
if self.collect_metrics:
eval_counter = eval_metrics()
- attributes = eval_metric_attributes(result_verdict, result.score, result.evaluation,
- result.classification, result.explanation)
+ attributes = eval_metric_attributes(
+ result_verdict,
+ result.score,
+ result.evaluation,
+ result.classification,
+ result.explanation,
+ )
eval_counter.add(1, attributes)
return result
diff --git a/sdk/python/src/openlit/evals/toxicity.py b/sdk/python/src/openlit/evals/toxicity.py
index 16eca4a5..6d90acd9 100644
--- a/sdk/python/src/openlit/evals/toxicity.py
+++ b/sdk/python/src/openlit/evals/toxicity.py
@@ -11,11 +11,14 @@ from openlit.evals.utils import (
llm_response,
parse_llm_response,
eval_metrics,
- eval_metric_attributes
+ eval_metric_attributes,
)
-def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None,
- threshold_score: Optional[float] = 0.5) -> str:
+
+def get_system_prompt(
+ custom_categories: Optional[Dict[str, str]] = None,
+ threshold_score: Optional[float] = 0.5,
+) -> str:
"""
Returns the system prompt used for LLM analysis, including custom categories if provided.
@@ -50,7 +53,9 @@ def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None,
"""
if custom_categories:
- custom_categories_str = "\n".join([f"- {key}: {value}" for key, value in custom_categories.items()])
+ custom_categories_str = "\n".join(
+ [f"- {key}: {value}" for key, value in custom_categories.items()]
+ )
base_prompt += f"\n Additional Toxicity Categories:\n{custom_categories_str}"
base_prompt += """
@@ -95,6 +100,7 @@ def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None,
"""
return base_prompt
+
class ToxicityDetector:
"""
A class to detect Toxicity in AI responses using LLM or custom categories.
@@ -107,11 +113,16 @@ class ToxicityDetector:
custom_categories (Optional[Dict[str, str]]): Additional categories for prompt injections.
"""
- def __init__(self, provider: Optional[str] = "openai", api_key: Optional[str] = None,
- model: Optional[str] = None, base_url: Optional[str] = None,
- custom_categories: Optional[Dict[str, str]] = None,
- collect_metrics: Optional[bool] = False,
- threshold_score: Optional[float] = 0.5):
+ def __init__(
+ self,
+ provider: Optional[str] = "openai",
+ api_key: Optional[str] = None,
+ model: Optional[str] = None,
+ base_url: Optional[str] = None,
+ custom_categories: Optional[Dict[str, str]] = None,
+ collect_metrics: Optional[bool] = False,
+ threshold_score: Optional[float] = 0.5,
+ ):
"""
Initializes the toxicity detector with specified LLM settings, custom rules, and categories.
@@ -128,16 +139,25 @@ class ToxicityDetector:
self.provider = provider
if self.provider is None:
- raise ValueError("An LLM provider must be specified for toxicity detection.")
- self.api_key, self.model, self.base_url = setup_provider(provider, api_key, model, base_url)
+ raise ValueError(
+ "An LLM provider must be specified for toxicity detection."
+ )
+ self.api_key, self.model, self.base_url = setup_provider(
+ provider, api_key, model, base_url
+ )
self.collect_metrics = collect_metrics
self.custom_categories = custom_categories
self.threshold_score = threshold_score
- self.system_prompt = get_system_prompt(self.custom_categories, self.threshold_score)
-
- def measure(self, prompt: Optional[str] = "",
- contexts: Optional[List[str]] = "",
- text: Optional[str] = None) -> JsonOutput:
+ self.system_prompt = get_system_prompt(
+ self.custom_categories, self.threshold_score
+ )
+
+ def measure(
+ self,
+ prompt: Optional[str] = "",
+ contexts: Optional[List[str]] = "",
+ text: Optional[str] = None,
+ ) -> JsonOutput:
"""
Detects toxicity in AI output using LLM or custom rules.
@@ -155,14 +175,23 @@ class ToxicityDetector:
llm_result = parse_llm_response(response)
result_verdict = "yes" if llm_result.score > self.threshold_score else "no"
- result = JsonOutput(score=llm_result.score, evaluation=llm_result.evaluation,
- classification=llm_result.classification,
- explanation=llm_result.explanation, verdict=result_verdict)
+ result = JsonOutput(
+ score=llm_result.score,
+ evaluation=llm_result.evaluation,
+ classification=llm_result.classification,
+ explanation=llm_result.explanation,
+ verdict=result_verdict,
+ )
if self.collect_metrics:
eval_counter = eval_metrics()
- attributes = eval_metric_attributes(result_verdict, result.score, result.evaluation,
- result.classification, result.explanation)
+ attributes = eval_metric_attributes(
+ result_verdict,
+ result.score,
+ result.evaluation,
+ result.classification,
+ result.explanation,
+ )
eval_counter.add(1, attributes)
return result
diff --git a/sdk/python/src/openlit/evals/utils.py b/sdk/python/src/openlit/evals/utils.py
index d0895176..8792d359 100644
--- a/sdk/python/src/openlit/evals/utils.py
+++ b/sdk/python/src/openlit/evals/utils.py
@@ -15,6 +15,7 @@ from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
+
class JsonOutput(BaseModel):
"""
A model representing the structure of JSON output for prompt injection detection.
@@ -32,9 +33,13 @@ class JsonOutput(BaseModel):
classification: str
explanation: str
-def setup_provider(provider: Optional[str], api_key: Optional[str],
- model: Optional[str],
- base_url: Optional[str]) -> Tuple[Optional[str], Optional[str], Optional[str]]:
+
+def setup_provider(
+ provider: Optional[str],
+ api_key: Optional[str],
+ model: Optional[str],
+ base_url: Optional[str],
+) -> Tuple[Optional[str], Optional[str], Optional[str]]:
"""
Sets up the provider, API key, model, and base URL.
@@ -52,7 +57,7 @@ def setup_provider(provider: Optional[str], api_key: Optional[str],
"""
provider_configs = {
"openai": {"env_var": "OPENAI_API_KEY"},
- "anthropic": {"env_var": "ANTHROPIC_API_KEY"}
+ "anthropic": {"env_var": "ANTHROPIC_API_KEY"},
}
if provider is None:
@@ -72,12 +77,16 @@ def setup_provider(provider: Optional[str], api_key: Optional[str],
if not api_key:
# pylint: disable=line-too-long
- raise ValueError(f"API key required via 'api_key' parameter or '{env_var}' environment variable")
+ raise ValueError(
+ f"API key required via 'api_key' parameter or '{env_var}' environment variable"
+ )
return api_key, model, base_url
-def format_prompt(system_prompt: str, prompt: str, contexts: List[str], text: str) -> str:
+def format_prompt(
+ system_prompt: str, prompt: str, contexts: List[str], text: str
+) -> str:
"""
Format the prompt.
@@ -98,6 +107,7 @@ def format_prompt(system_prompt: str, prompt: str, contexts: List[str], text: st
return formatted_prompt
+
def llm_response(provider: str, prompt: str, model: str, base_url: str) -> str:
"""
Generates an LLM response using the configured provider.
@@ -117,6 +127,7 @@ def llm_response(provider: str, prompt: str, model: str, base_url: str) -> str:
else:
raise ValueError(f"Unsupported provider: {provider}")
+
def llm_response_openai(prompt: str, model: str, base_url: str) -> str:
"""
Interacts with the OpenAI API to get a LLM response.
@@ -142,10 +153,11 @@ def llm_response_openai(prompt: str, model: str, base_url: str) -> str:
{"role": "user", "content": prompt},
],
temperature=0.0,
- response_format=JsonOutput
+ response_format=JsonOutput,
)
return response.choices[0].message.content
+
def llm_response_anthropic(prompt: str, model: str) -> str:
"""
Interacts with the Anthropic API to get a LLM response.
@@ -172,23 +184,33 @@ def llm_response_anthropic(prompt: str, model: str) -> str:
"verdict": {"type": "string", "description": "Evaluation verdict"},
"evaluation": {"type": "string", "description": "Evaluation type"},
"score": {"type": "number", "description": "Evaluation score"},
- "classification": {"type": "string", "description": "Evaluation category"},
- "explanation": {"type": "string", "description": "Evaluation reason"}
+ "classification": {
+ "type": "string",
+ "description": "Evaluation category",
+ },
+ "explanation": {
+ "type": "string",
+ "description": "Evaluation reason",
+ },
},
- "required": ["verdict", "evaluation", "score", "classification", "explanation"]
- }
+ "required": [
+ "verdict",
+ "evaluation",
+ "score",
+ "classification",
+ "explanation",
+ ],
+ },
}
]
response = client.messages.create(
model=model,
- messages=[
- {"role": "user", "content": prompt}
- ],
+ messages=[{"role": "user", "content": prompt}],
max_tokens=2000,
temperature=0.0,
tools=tools,
- stream=False
+ stream=False,
)
for content in response.content:
@@ -198,6 +220,7 @@ def llm_response_anthropic(prompt: str, model: str) -> str:
return response
+
def parse_llm_response(response) -> JsonOutput:
"""
Parses the LLM response into a JsonOutput object.
@@ -220,8 +243,14 @@ def parse_llm_response(response) -> JsonOutput:
return JsonOutput(**data)
except (json.JSONDecodeError, TypeError) as e:
logger.error("Error parsing LLM response: '%s'", e)
- return JsonOutput(score=0, classification="none", explanation="none",
- verdict="no", evaluation="none")
+ return JsonOutput(
+ score=0,
+ classification="none",
+ explanation="none",
+ verdict="no",
+ evaluation="none",
+ )
+
def eval_metrics():
"""
@@ -240,11 +269,12 @@ def eval_metrics():
guard_requests = meter.create_counter(
name=SemanticConvention.EVAL_REQUESTS,
description="Counter for evaluation requests",
- unit="1"
+ unit="1",
)
return guard_requests
+
def eval_metric_attributes(verdict, score, validator, classification, explanation):
"""
Initializes OpenTelemetry attributes for metrics.
@@ -260,16 +290,10 @@ def eval_metric_attributes(verdict, score, validator, classification, explanatio
"""
return {
- TELEMETRY_SDK_NAME:
- "openlit",
- SemanticConvention.EVAL_VERDICT:
- verdict,
- SemanticConvention.EVAL_SCORE:
- score,
- SemanticConvention.EVAL_VALIDATOR:
- validator,
- SemanticConvention.EVAL_CLASSIFICATION:
- classification,
- SemanticConvention.EVAL_EXPLANATION:
- explanation,
+ TELEMETRY_SDK_NAME: "openlit",
+ SemanticConvention.EVAL_VERDICT: verdict,
+ SemanticConvention.EVAL_SCORE: score,
+ SemanticConvention.EVAL_VALIDATOR: validator,
+ SemanticConvention.EVAL_CLASSIFICATION: classification,
+ SemanticConvention.EVAL_EXPLANATION: explanation,
}
diff --git a/sdk/python/src/openlit/guard/all.py b/sdk/python/src/openlit/guard/all.py
index 1ffbf221..d67e95f4 100644
--- a/sdk/python/src/openlit/guard/all.py
+++ b/sdk/python/src/openlit/guard/all.py
@@ -12,10 +12,15 @@ from openlit.guard.utils import (
parse_llm_response,
custom_rule_detection,
guard_metrics,
- guard_metric_attributes
+ guard_metric_attributes,
)
-def get_all_system_prompt(valid_topics: Optional[List[str]] = None, invalid_topics: Optional[List[str]] = None, custom_categories: Optional[Dict[str, str]] = None) -> str:
+
+def get_all_system_prompt(
+ valid_topics: Optional[List[str]] = None,
+ invalid_topics: Optional[List[str]] = None,
+ custom_categories: Optional[Dict[str, str]] = None,
+) -> str:
"""
Returns the system prompt used for LLM analysis to capture prompt injections, valid topics, and sensitive topics.
@@ -68,8 +73,15 @@ def get_all_system_prompt(valid_topics: Optional[List[str]] = None, invalid_topi
# Append custom categories for prompt injection if provided
if custom_categories:
- custom_categories_str = "\n".join([f"- {key}: {description}" for key, description in custom_categories.items()])
- base_prompt += f"\n Additional Prompt Injection Categories:\n{custom_categories_str}"
+ custom_categories_str = "\n".join(
+ [
+ f"- {key}: {description}"
+ for key, description in custom_categories.items()
+ ]
+ )
+ base_prompt += (
+ f"\n Additional Prompt Injection Categories:\n{custom_categories_str}"
+ )
base_prompt += """
@@ -116,8 +128,15 @@ def get_all_system_prompt(valid_topics: Optional[List[str]] = None, invalid_topi
# Append custom categories for sensitive topics if provided
if custom_categories:
- custom_categories_str = "\n".join([f"- {key}: {description}" for key, description in custom_categories.items()])
- base_prompt += f"\n Additional Sensitive Topics Categories:\n{custom_categories_str}"
+ custom_categories_str = "\n".join(
+ [
+ f"- {key}: {description}"
+ for key, description in custom_categories.items()
+ ]
+ )
+ base_prompt += (
+ f"\n Additional Sensitive Topics Categories:\n{custom_categories_str}"
+ )
base_prompt += """
@@ -126,6 +145,7 @@ def get_all_system_prompt(valid_topics: Optional[List[str]] = None, invalid_topi
"""
return base_prompt
+
class All:
"""
A comprehensive class to detect prompt injections, valid/invalid topics, and sensitive topics using LLM or custom rules.
@@ -141,13 +161,18 @@ class All:
invalid_topics (Optional[List[str]]): List of invalid topics.
"""
- def __init__(self, provider: Optional[str] = None, api_key: Optional[str] = None,
- model: Optional[str] = None, base_url: Optional[str] = None,
- custom_rules: Optional[List[dict]] = None,
- custom_categories: Optional[Dict[str, str]] = None,
- valid_topics: Optional[List[str]] = None,
- invalid_topics: Optional[List[str]] = None,
- collect_metrics: Optional[bool] = False):
+ def __init__(
+ self,
+ provider: Optional[str] = None,
+ api_key: Optional[str] = None,
+ model: Optional[str] = None,
+ base_url: Optional[str] = None,
+ custom_rules: Optional[List[dict]] = None,
+ custom_categories: Optional[Dict[str, str]] = None,
+ valid_topics: Optional[List[str]] = None,
+ invalid_topics: Optional[List[str]] = None,
+ collect_metrics: Optional[bool] = False,
+ ):
"""
Initializes the All class with specified LLM settings, custom rules, and categories.
@@ -165,8 +190,12 @@ class All:
ValueError: If provider is not specified.
"""
self.provider = provider
- self.api_key, self.model, self.base_url = setup_provider(provider, api_key, model, base_url)
- self.system_prompt = get_all_system_prompt(valid_topics, invalid_topics, custom_categories)
+ self.api_key, self.model, self.base_url = setup_provider(
+ provider, api_key, model, base_url
+ )
+ self.system_prompt = get_all_system_prompt(
+ valid_topics, invalid_topics, custom_categories
+ )
self.custom_rules = custom_rules or []
self.valid_topics = valid_topics or []
self.invalid_topics = invalid_topics or []
@@ -183,18 +212,31 @@ class All:
JsonOutput: The structured result of the detection.
"""
custom_rule_result = custom_rule_detection(text, self.custom_rules)
- llm_result = JsonOutput(score=0.0, verdict="no", guard="none", classification="none", explanation="none")
+ llm_result = JsonOutput(
+ score=0.0,
+ verdict="no",
+ guard="none",
+ classification="none",
+ explanation="none",
+ )
if self.provider:
prompt = format_prompt(self.system_prompt, text)
- llm_result = parse_llm_response(llm_response(self.provider, prompt, self.model, self.base_url))
+ llm_result = parse_llm_response(
+ llm_response(self.provider, prompt, self.model, self.base_url)
+ )
result = max(custom_rule_result, llm_result, key=lambda x: x.score)
if self.collect_metrics:
guard_counter = guard_metrics()
- attributes = guard_metric_attributes(result.verdict, result.score, result.guard,
- result.classification, result.explanation)
+ attributes = guard_metric_attributes(
+ result.verdict,
+ result.score,
+ result.guard,
+ result.classification,
+ result.explanation,
+ )
guard_counter.add(1, attributes)
return result
diff --git a/sdk/python/src/openlit/guard/prompt_injection.py b/sdk/python/src/openlit/guard/prompt_injection.py
index e9006d6d..e9d26233 100644
--- a/sdk/python/src/openlit/guard/prompt_injection.py
+++ b/sdk/python/src/openlit/guard/prompt_injection.py
@@ -12,9 +12,10 @@ from openlit.guard.utils import (
parse_llm_response,
custom_rule_detection,
guard_metrics,
- guard_metric_attributes
+ guard_metric_attributes,
)
+
def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None) -> str:
"""
Returns the system prompt used for LLM analysis, including custom categories if provided.
@@ -64,7 +65,9 @@ def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None) -> str
"""
if custom_categories:
- custom_categories_str = "\n".join([f"- {key}: {value}" for key, value in custom_categories.items()])
+ custom_categories_str = "\n".join(
+ [f"- {key}: {value}" for key, value in custom_categories.items()]
+ )
base_prompt += f"\n Additional Categories:\n{custom_categories_str}"
base_prompt += """
@@ -73,17 +76,25 @@ def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None) -> str
"""
return base_prompt
+
class PromptInjection:
"""Class to intialize Prompt Injection"""
- def __init__(self, provider: Optional[str] = None, api_key: Optional[str] = None,
- model: Optional[str] = None, base_url: Optional[str] = None,
- custom_rules: Optional[List[dict]] = None,
- custom_categories: Optional[Dict[str, str]] = None,
- threshold_score: float = 0.25,
- collect_metrics: Optional[bool] = False):
+ def __init__(
+ self,
+ provider: Optional[str] = None,
+ api_key: Optional[str] = None,
+ model: Optional[str] = None,
+ base_url: Optional[str] = None,
+ custom_rules: Optional[List[dict]] = None,
+ custom_categories: Optional[Dict[str, str]] = None,
+ threshold_score: float = 0.25,
+ collect_metrics: Optional[bool] = False,
+ ):
self.provider = provider
- self.api_key, self.model, self.base_url = setup_provider(provider, api_key, model, base_url)
+ self.api_key, self.model, self.base_url = setup_provider(
+ provider, api_key, model, base_url
+ )
self.system_prompt = get_system_prompt(custom_categories)
self.custom_rules = custom_rules or []
self.threshold_score = threshold_score
@@ -93,11 +104,19 @@ class PromptInjection:
"""Functon to detect Prompt Injection and jailbreak attempts in input"""
custom_rule_result = custom_rule_detection(text, self.custom_rules)
- llm_result = JsonOutput(score=0, classification="none", explanation="none", verdict="none", guard="none")
+ llm_result = JsonOutput(
+ score=0,
+ classification="none",
+ explanation="none",
+ verdict="none",
+ guard="none",
+ )
if self.provider:
prompt = format_prompt(self.system_prompt, text)
- llm_result = parse_llm_response(llm_response(self.provider, prompt, self.model, self.base_url))
+ llm_result = parse_llm_response(
+ llm_response(self.provider, prompt, self.model, self.base_url)
+ )
result = max(custom_rule_result, llm_result, key=lambda x: x.score)
score = 0 if result.classification == "none" else result.score
@@ -105,8 +124,9 @@ class PromptInjection:
if self.collect_metrics is True:
guard_counter = guard_metrics()
- attributes = guard_metric_attributes(verdict, score, result.guard,
- result.classification, result.explanation)
+ attributes = guard_metric_attributes(
+ verdict, score, result.guard, result.classification, result.explanation
+ )
guard_counter.add(1, attributes)
return JsonOutput(
@@ -114,5 +134,5 @@ class PromptInjection:
guard=result.guard,
verdict=verdict,
classification=result.classification,
- explanation=result.explanation
+ explanation=result.explanation,
)
diff --git a/sdk/python/src/openlit/guard/restrict_topic.py b/sdk/python/src/openlit/guard/restrict_topic.py
index 26477629..1139c924 100644
--- a/sdk/python/src/openlit/guard/restrict_topic.py
+++ b/sdk/python/src/openlit/guard/restrict_topic.py
@@ -11,10 +11,13 @@ from openlit.guard.utils import (
llm_response,
parse_llm_response,
guard_metrics,
- guard_metric_attributes
+ guard_metric_attributes,
)
-def get_system_prompt(valid_topics: Optional[List[str]] = None, invalid_topics: Optional[List[str]] = None) -> str:
+
+def get_system_prompt(
+ valid_topics: Optional[List[str]] = None, invalid_topics: Optional[List[str]] = None
+) -> str:
"""
Returns the system prompt used for LLM analysis, including valid and invalid topics if provided.
@@ -79,6 +82,7 @@ def get_system_prompt(valid_topics: Optional[List[str]] = None, invalid_topics:
return base_prompt
+
class TopicRestriction:
"""
A class to validate if text belongs to valid or invalid topics using LLM.
@@ -92,12 +96,16 @@ class TopicRestriction:
invalid_topics (Optional[List[str]]): List of invalid topics.
"""
- def __init__(self, provider: Optional[str], valid_topics: Optional[List[str]] = None,
- api_key: Optional[str] = None, model: Optional[str] = None,
- base_url: Optional[str] = None,
- invalid_topics: Optional[List[str]] = None,
- collect_metrics: Optional[bool] = False,
- ):
+ def __init__(
+ self,
+ provider: Optional[str],
+ valid_topics: Optional[List[str]] = None,
+ api_key: Optional[str] = None,
+ model: Optional[str] = None,
+ base_url: Optional[str] = None,
+ invalid_topics: Optional[List[str]] = None,
+ collect_metrics: Optional[bool] = False,
+ ):
"""
Initializes the TopicRestriction with specified LLM settings and topics.
@@ -114,12 +122,18 @@ class TopicRestriction:
"""
self.provider = provider
if self.provider is None:
- raise ValueError("An LLM provider must be specified for TopicRestriction Validator")
- self.api_key, self.model, self.base_url = setup_provider(provider, api_key, model, base_url)
+ raise ValueError(
+ "An LLM provider must be specified for TopicRestriction Validator"
+ )
+ self.api_key, self.model, self.base_url = setup_provider(
+ provider, api_key, model, base_url
+ )
self.system_prompt = get_system_prompt(valid_topics, invalid_topics)
self.valid_topics = valid_topics
if self.valid_topics is None:
- raise ValueError("Valid Topics must be specified for TopicRestriction Validator")
+ raise ValueError(
+ "Valid Topics must be specified for TopicRestriction Validator"
+ )
self.invalid_topics = invalid_topics or []
self.collect_metrics = collect_metrics
@@ -139,14 +153,31 @@ class TopicRestriction:
# Adjusted logic for consistency with updated JSON structure
if llm_result.classification == "valid_topic":
- result = JsonOutput(score=0, verdict="no", guard="topic_restriction", classification="valid_topic", explanation="Text fits into a valid topic.")
+ result = JsonOutput(
+ score=0,
+ verdict="no",
+ guard="topic_restriction",
+ classification="valid_topic",
+ explanation="Text fits into a valid topic.",
+ )
else:
- result = JsonOutput(score=1.0, verdict="yes", guard="topic_restriction", classification="invalid_topic", explanation="Text does not match any valid categories.")
+ result = JsonOutput(
+ score=1.0,
+ verdict="yes",
+ guard="topic_restriction",
+ classification="invalid_topic",
+ explanation="Text does not match any valid categories.",
+ )
if self.collect_metrics:
guard_counter = guard_metrics()
- attributes = guard_metric_attributes(result.verdict, result.score, result.guard,
- result.classification, result.explanation)
+ attributes = guard_metric_attributes(
+ result.verdict,
+ result.score,
+ result.guard,
+ result.classification,
+ result.explanation,
+ )
guard_counter.add(1, attributes)
return result
diff --git a/sdk/python/src/openlit/guard/sensitive_topic.py b/sdk/python/src/openlit/guard/sensitive_topic.py
index 1b3701cb..8346d08d 100644
--- a/sdk/python/src/openlit/guard/sensitive_topic.py
+++ b/sdk/python/src/openlit/guard/sensitive_topic.py
@@ -12,9 +12,10 @@ from openlit.guard.utils import (
parse_llm_response,
custom_rule_detection,
guard_metrics,
- guard_metric_attributes
+ guard_metric_attributes,
)
+
def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None) -> str:
"""
Returns the system prompt used for LLM analysis, including custom categories if provided.
@@ -61,7 +62,9 @@ def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None) -> str
"""
if custom_categories:
- custom_categories_str = "\n".join([f"- {key}: {value}" for key, value in custom_categories.items()])
+ custom_categories_str = "\n".join(
+ [f"- {key}: {value}" for key, value in custom_categories.items()]
+ )
base_prompt += f"\n Additional Categories:\n{custom_categories_str}"
base_prompt += """
@@ -71,6 +74,7 @@ def get_system_prompt(custom_categories: Optional[Dict[str, str]] = None) -> str
return base_prompt
+
class SensitiveTopic:
"""
A class to detect sensitive topics using LLM or custom rules.
@@ -84,14 +88,21 @@ class SensitiveTopic:
custom_categories (Optional[Dict[str, str]]): Additional categories for sensitive topics.
"""
- def __init__(self, provider: Optional[str] = None, api_key: Optional[str] = None,
- model: Optional[str] = None, base_url: Optional[str] = None,
- custom_rules: Optional[List[dict]] = None,
- custom_categories: Optional[Dict[str, str]] = None,
- threshold_score: float = 0.25,
- collect_metrics: Optional[bool] = False):
+ def __init__(
+ self,
+ provider: Optional[str] = None,
+ api_key: Optional[str] = None,
+ model: Optional[str] = None,
+ base_url: Optional[str] = None,
+ custom_rules: Optional[List[dict]] = None,
+ custom_categories: Optional[Dict[str, str]] = None,
+ threshold_score: float = 0.25,
+ collect_metrics: Optional[bool] = False,
+ ):
self.provider = provider
- self.api_key, self.model, self.base_url = setup_provider(provider, api_key, model, base_url)
+ self.api_key, self.model, self.base_url = setup_provider(
+ provider, api_key, model, base_url
+ )
self.system_prompt = get_system_prompt(custom_categories)
self.custom_rules = custom_rules or []
self.threshold_score = threshold_score
@@ -101,11 +112,19 @@ class SensitiveTopic:
"""Function to detect sensitive topic in AI response"""
custom_rule_result = custom_rule_detection(text, self.custom_rules)
- llm_result = JsonOutput(score=0, classification="none", explanation="none", verdict="no", guard="none")
+ llm_result = JsonOutput(
+ score=0,
+ classification="none",
+ explanation="none",
+ verdict="no",
+ guard="none",
+ )
if self.provider:
prompt = format_prompt(self.system_prompt, text)
- llm_result = parse_llm_response(llm_response(self.provider, prompt, self.model, self.base_url))
+ llm_result = parse_llm_response(
+ llm_response(self.provider, prompt, self.model, self.base_url)
+ )
result = max(custom_rule_result, llm_result, key=lambda x: x.score)
score = 0 if result.classification == "none" else result.score
@@ -113,8 +132,9 @@ class SensitiveTopic:
if self.collect_metrics:
guard_counter = guard_metrics()
- attributes = guard_metric_attributes(verdict, score, result.guard,
- result.classification, result.explanation)
+ attributes = guard_metric_attributes(
+ verdict, score, result.guard, result.classification, result.explanation
+ )
guard_counter.add(1, attributes)
return JsonOutput(
@@ -122,5 +142,5 @@ class SensitiveTopic:
guard=result.guard,
verdict=verdict,
classification=result.classification,
- explanation=result.explanation
+ explanation=result.explanation,
)
diff --git a/sdk/python/src/openlit/guard/utils.py b/sdk/python/src/openlit/guard/utils.py
index d8c1295f..c8fc43e2 100644
--- a/sdk/python/src/openlit/guard/utils.py
+++ b/sdk/python/src/openlit/guard/utils.py
@@ -16,6 +16,7 @@ from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
+
class JsonOutput(BaseModel):
"""
A model representing the structure of JSON output for prompt injection detection.
@@ -34,13 +35,17 @@ class JsonOutput(BaseModel):
classification: str
explanation: str
-def setup_provider(provider: Optional[str], api_key: Optional[str],
- model: Optional[str],
- base_url: Optional[str]) -> Tuple[Optional[str], Optional[str], Optional[str]]:
+
+def setup_provider(
+ provider: Optional[str],
+ api_key: Optional[str],
+ model: Optional[str],
+ base_url: Optional[str],
+) -> Tuple[Optional[str], Optional[str], Optional[str]]:
"""Function to setup LLM provider"""
provider_configs = {
"openai": {"env_var": "OPENAI_API_KEY"},
- "anthropic": {"env_var": "ANTHROPIC_API_KEY"}
+ "anthropic": {"env_var": "ANTHROPIC_API_KEY"},
}
if provider is None:
@@ -60,7 +65,9 @@ def setup_provider(provider: Optional[str], api_key: Optional[str],
if not api_key:
# pylint: disable=line-too-long
- raise ValueError(f"API key required via 'api_key' parameter or '{env_var}' environment variable")
+ raise ValueError(
+ f"API key required via 'api_key' parameter or '{env_var}' environment variable"
+ )
return api_key, model, base_url
@@ -69,6 +76,7 @@ def format_prompt(system_prompt: str, text: str) -> str:
"""Function to format the prompt"""
return system_prompt.replace("{{prompt}}", text)
+
def llm_response(provider: str, prompt: str, model: str, base_url: str) -> str:
"""Function to get LLM response based on provider"""
# pylint: disable=no-else-return
@@ -79,6 +87,7 @@ def llm_response(provider: str, prompt: str, model: str, base_url: str) -> str:
else:
raise ValueError(f"Unsupported provider: {provider}")
+
def llm_response_openai(prompt: str, model: str, base_url: str) -> str:
"""Function to make LLM call to OpenAI"""
client = OpenAI(base_url=base_url)
@@ -95,10 +104,11 @@ def llm_response_openai(prompt: str, model: str, base_url: str) -> str:
{"role": "user", "content": prompt},
],
temperature=0.0,
- response_format=JsonOutput
+ response_format=JsonOutput,
)
return response.choices[0].message.content
+
def llm_response_anthropic(prompt: str, model: str) -> str:
"""Function to make LLM call to Anthropic"""
client = Anthropic()
@@ -113,26 +123,42 @@ def llm_response_anthropic(prompt: str, model: str) -> str:
"input_schema": {
"type": "object",
"properties": {
- "verdict": {"type": "string", "description": "Verdict of guardrail"},
+ "verdict": {
+ "type": "string",
+ "description": "Verdict of guardrail",
+ },
"guard": {"type": "string", "description": "Type of guard"},
- "score": {"type": "number", "description": "Prompt score from Guard."},
- "classification": {"type": "string", "description": "Incorrect prompt type"},
- "explanation": {"type": "string", "description": "Reason for classification"}
+ "score": {
+ "type": "number",
+ "description": "Prompt score from Guard.",
+ },
+ "classification": {
+ "type": "string",
+ "description": "Incorrect prompt type",
+ },
+ "explanation": {
+ "type": "string",
+ "description": "Reason for classification",
+ },
},
- "required": ["verdict", "guard", "score", "classification", "explanation"]
- }
+ "required": [
+ "verdict",
+ "guard",
+ "score",
+ "classification",
+ "explanation",
+ ],
+ },
}
]
response = client.messages.create(
model=model,
- messages=[
- {"role": "user", "content": prompt}
- ],
+ messages=[{"role": "user", "content": prompt}],
max_tokens=2000,
temperature=0.0,
tools=tools,
- stream=False
+ stream=False,
)
for content in response.content:
@@ -142,6 +168,7 @@ def llm_response_anthropic(prompt: str, model: str) -> str:
return response
+
def parse_llm_response(response) -> JsonOutput:
"""
Parses the LLM response into a JsonOutput object.
@@ -163,8 +190,14 @@ def parse_llm_response(response) -> JsonOutput:
return JsonOutput(**data)
except (json.JSONDecodeError, TypeError) as e:
logger.error("Error parsing LLM response: '%s'", e)
- return JsonOutput(score=0, classification="none", explanation="none",
- verdict="none", guard="none")
+ return JsonOutput(
+ score=0,
+ classification="none",
+ explanation="none",
+ verdict="none",
+ guard="none",
+ )
+
def custom_rule_detection(text: str, custom_rules: list) -> JsonOutput:
"""
@@ -183,10 +216,12 @@ def custom_rule_detection(text: str, custom_rules: list) -> JsonOutput:
guard=rule.get("guard", "prompt_injection"),
score=rule.get("score", 0.5),
classification=rule.get("classification", "custom"),
- explanation=rule.get("explanation", "Matched custom rule pattern.")
+ explanation=rule.get("explanation", "Matched custom rule pattern."),
)
- return JsonOutput(score=0, classification="none", explanation="none",
- verdict="none", guard="none")
+ return JsonOutput(
+ score=0, classification="none", explanation="none", verdict="none", guard="none"
+ )
+
def guard_metrics():
"""
@@ -204,11 +239,12 @@ def guard_metrics():
guard_requests = meter.create_counter(
name=SemanticConvention.GUARD_REQUESTS,
description="Counter for Guard requests",
- unit="1"
+ unit="1",
)
return guard_requests
+
def guard_metric_attributes(verdict, score, validator, classification, explanation):
"""
Initializes OpenTelemetry attributes for metrics.
diff --git a/sdk/python/src/openlit/instrumentation/ag2/__init__.py b/sdk/python/src/openlit/instrumentation/ag2/__init__.py
index 32d295b3..f2f7aa4d 100644
--- a/sdk/python/src/openlit/instrumentation/ag2/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/ag2/__init__.py
@@ -5,15 +5,15 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.ag2.ag2 import (
- conversable_agent, agent_run
-)
+from openlit.instrumentation.ag2.ag2 import conversable_agent, agent_run
from openlit.instrumentation.ag2.async_ag2 import (
- async_conversable_agent, async_agent_run
+ async_conversable_agent,
+ async_agent_run,
)
_instruments = ("ag2 >= 0.3.2",)
+
class AG2Instrumentor(BaseInstrumentor):
"""
An instrumentor for AG2 client library.
@@ -36,16 +36,32 @@ class AG2Instrumentor(BaseInstrumentor):
wrap_function_wrapper(
"autogen.agentchat.conversable_agent",
"ConversableAgent.__init__",
- conversable_agent(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ conversable_agent(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# sync agent run
wrap_function_wrapper(
"autogen.agentchat.conversable_agent",
"ConversableAgent.run",
- agent_run(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ agent_run(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/ag2/ag2.py b/sdk/python/src/openlit/instrumentation/ag2/ag2.py
index db36a3a6..6dc03ca7 100644
--- a/sdk/python/src/openlit/instrumentation/ag2/ag2.py
+++ b/sdk/python/src/openlit/instrumentation/ag2/ag2.py
@@ -4,18 +4,24 @@ Module for monitoring AG2 API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.ag2.utils import (
process_agent_creation,
process_agent_run,
)
from openlit.semcov import SemanticConvention
-def conversable_agent(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def conversable_agent(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for AG2 conversable agent creation.
"""
@@ -25,12 +31,16 @@ def conversable_agent(version, environment, application_name, tracer, pricing_in
Wraps the AG2 conversable agent creation call.
"""
- server_address, server_port = set_server_address_and_port(instance, "127.0.0.1", 80)
+ server_address, server_port = set_server_address_and_port(
+ instance, "127.0.0.1", 80
+ )
agent_name = kwargs.get("name", "NOT_FOUND")
llm_config = kwargs.get("llm_config", {})
system_message = kwargs.get("system_message", "")
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT} {agent_name}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT} {agent_name}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
@@ -51,7 +61,7 @@ def conversable_agent(version, environment, application_name, tracer, pricing_in
span=span,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -61,8 +71,17 @@ def conversable_agent(version, environment, application_name, tracer, pricing_in
return wrapper
-def agent_run(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def agent_run(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for AG2 agent run execution.
"""
@@ -72,7 +91,9 @@ def agent_run(version, environment, application_name, tracer, pricing_info,
Wraps the AG2 agent run execution call.
"""
- server_address, server_port = set_server_address_and_port(instance, "127.0.0.1", 80)
+ server_address, server_port = set_server_address_and_port(
+ instance, "127.0.0.1", 80
+ )
# Extract agent name from instance
agent_name = getattr(instance, "name", "NOT_FOUND")
@@ -103,7 +124,7 @@ def agent_run(version, environment, application_name, tracer, pricing_info,
span=span,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/ag2/async_ag2.py b/sdk/python/src/openlit/instrumentation/ag2/async_ag2.py
index 81058e7a..3f25bc78 100644
--- a/sdk/python/src/openlit/instrumentation/ag2/async_ag2.py
+++ b/sdk/python/src/openlit/instrumentation/ag2/async_ag2.py
@@ -4,18 +4,24 @@ Module for monitoring AG2 API calls (async version).
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.ag2.utils import (
process_agent_creation,
process_agent_run,
)
from openlit.semcov import SemanticConvention
-def async_conversable_agent(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def async_conversable_agent(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for AG2 async conversable agent creation.
"""
@@ -25,12 +31,16 @@ def async_conversable_agent(version, environment, application_name, tracer, pric
Wraps the AG2 async conversable agent creation call.
"""
- server_address, server_port = set_server_address_and_port(instance, "127.0.0.1", 80)
+ server_address, server_port = set_server_address_and_port(
+ instance, "127.0.0.1", 80
+ )
agent_name = kwargs.get("name", "NOT_FOUND")
llm_config = kwargs.get("llm_config", {})
system_message = kwargs.get("system_message", "")
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT} {agent_name}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT} {agent_name}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
@@ -51,7 +61,7 @@ def async_conversable_agent(version, environment, application_name, tracer, pric
span=span,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -61,8 +71,17 @@ def async_conversable_agent(version, environment, application_name, tracer, pric
return wrapper
-def async_agent_run(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def async_agent_run(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for AG2 async agent run execution.
"""
@@ -72,7 +91,9 @@ def async_agent_run(version, environment, application_name, tracer, pricing_info
Wraps the AG2 async agent run execution call.
"""
- server_address, server_port = set_server_address_and_port(instance, "127.0.0.1", 80)
+ server_address, server_port = set_server_address_and_port(
+ instance, "127.0.0.1", 80
+ )
# Extract agent name from instance
agent_name = getattr(instance, "name", "NOT_FOUND")
@@ -103,7 +124,7 @@ def async_agent_run(version, environment, application_name, tracer, pricing_info
span=span,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/ag2/utils.py b/sdk/python/src/openlit/instrumentation/ag2/utils.py
index 4e7c0b0d..9fdb4571 100644
--- a/sdk/python/src/openlit/instrumentation/ag2/utils.py
+++ b/sdk/python/src/openlit/instrumentation/ag2/utils.py
@@ -1,6 +1,7 @@
"""
AG2 OpenTelemetry instrumentation utility functions
"""
+
import time
from opentelemetry.trace import Status, StatusCode
@@ -12,6 +13,7 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def calculate_tokens_and_cost(response, request_model, pricing_info):
"""
Calculate the input, output tokens, and their respective costs from AG2 response.
@@ -21,7 +23,9 @@ def calculate_tokens_and_cost(response, request_model, pricing_info):
# Early return if response doesn't have cost data
if not hasattr(response, "cost") or response.cost is None:
- cost = get_chat_model_cost(request_model, pricing_info, input_tokens, output_tokens)
+ cost = get_chat_model_cost(
+ request_model, pricing_info, input_tokens, output_tokens
+ )
return input_tokens, output_tokens, cost
try:
@@ -34,6 +38,7 @@ def calculate_tokens_and_cost(response, request_model, pricing_info):
cost = get_chat_model_cost(request_model, pricing_info, input_tokens, output_tokens)
return input_tokens, output_tokens, cost
+
def _extract_tokens_from_cost(cost_data):
"""
Extract input and output tokens from AG2 cost data structure.
@@ -52,6 +57,7 @@ def _extract_tokens_from_cost(cost_data):
return input_tokens, output_tokens
+
def format_content(chat_history):
"""
Format the chat history into a string for span events.
@@ -67,32 +73,62 @@ def format_content(chat_history):
return "\n".join(formatted_messages)
-def common_agent_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, operation_type):
+
+def common_agent_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ operation_type,
+):
"""
Process agent request and generate Telemetry
"""
# Common Span Attributes
- common_span_attributes(scope,
- operation_type, SemanticConvention.GEN_AI_SYSTEM_AG2,
- scope._server_address, scope._server_port, scope._request_model, scope._response_model,
- environment, application_name, False, 0, scope._end_time - scope._start_time, version)
+ common_span_attributes(
+ scope,
+ operation_type,
+ SemanticConvention.GEN_AI_SYSTEM_AG2,
+ scope._server_address,
+ scope._server_port,
+ scope._request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ False,
+ 0,
+ scope._end_time - scope._start_time,
+ version,
+ )
# Span Attributes for Agent-specific parameters
scope._span.set_attribute(SemanticConvention.GEN_AI_AGENT_NAME, scope._agent_name)
# Span Attributes for Response parameters
if hasattr(scope, "_input_tokens"):
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens + scope._output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ scope._input_tokens + scope._output_tokens,
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, scope._cost)
# Span Attributes for Content
if capture_message_content and hasattr(scope, "_chat_history"):
chat_content = format_content(scope._chat_history)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, chat_content)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, chat_content
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -104,20 +140,51 @@ def common_agent_logic(scope, pricing_info, environment, application_name, metri
# Set agent description for create agent operation
if hasattr(scope, "_system_message"):
- scope._span.set_attribute(SemanticConvention.GEN_AI_AGENT_DESCRIPTION, scope._system_message)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_DESCRIPTION, scope._system_message
+ )
scope._span.set_status(Status(StatusCode.OK))
# Metrics
if not disable_metrics and hasattr(scope, "_input_tokens"):
- record_completion_metrics(metrics, operation_type, SemanticConvention.GEN_AI_SYSTEM_AG2,
- scope._server_address, scope._server_port, scope._request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, scope._output_tokens,
- scope._cost, 0, scope._end_time - scope._start_time)
-
-def process_agent_creation(agent_name, llm_config, system_message, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+ record_completion_metrics(
+ metrics,
+ operation_type,
+ SemanticConvention.GEN_AI_SYSTEM_AG2,
+ scope._server_address,
+ scope._server_port,
+ scope._request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ scope._output_tokens,
+ scope._cost,
+ 0,
+ scope._end_time - scope._start_time,
+ )
+
+
+def process_agent_creation(
+ agent_name,
+ llm_config,
+ system_message,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process agent creation and generate Telemetry
"""
@@ -134,12 +201,36 @@ def process_agent_creation(agent_name, llm_config, system_message, pricing_info,
scope._system_message = system_message
scope._server_address, scope._server_port = server_address, server_port
- common_agent_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT)
-
-def process_agent_run(response, agent_name, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+ common_agent_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT,
+ )
+
+
+def process_agent_run(
+ response,
+ agent_name,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process agent run and generate Telemetry
"""
@@ -157,19 +248,31 @@ def process_agent_run(response, agent_name, request_model, pricing_info, server_
# Calculate tokens and cost
scope._input_tokens, scope._output_tokens, scope._cost = calculate_tokens_and_cost(
- response, request_model, pricing_info)
+ response, request_model, pricing_info
+ )
# Extract response model from cost data
try:
if hasattr(response, "cost") and response.cost is not None:
cost_data = response.cost.get("usage_including_cached_inference", {})
- scope._response_model = list(cost_data.keys())[1] if len(cost_data) > 1 else request_model
+ scope._response_model = (
+ list(cost_data.keys())[1] if len(cost_data) > 1 else request_model
+ )
else:
scope._response_model = request_model
except (AttributeError, IndexError, KeyError, TypeError):
scope._response_model = request_model
- common_agent_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, SemanticConvention.GEN_AI_OPERATION_TYPE_EXECUTE_AGENT_TASK)
+ common_agent_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EXECUTE_AGENT_TASK,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/ai21/__init__.py b/sdk/python/src/openlit/instrumentation/ai21/__init__.py
index 3f50b39e..5664eb64 100644
--- a/sdk/python/src/openlit/instrumentation/ai21/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/ai21/__init__.py
@@ -5,15 +5,12 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.ai21.ai21 import (
- chat, chat_rag
-)
-from openlit.instrumentation.ai21.async_ai21 import (
- async_chat, async_chat_rag
-)
+from openlit.instrumentation.ai21.ai21 import chat, chat_rag
+from openlit.instrumentation.ai21.async_ai21 import async_chat, async_chat_rag
_instruments = ("ai21 >= 3.0.0",)
+
class AI21Instrumentor(BaseInstrumentor):
"""
An instrumentor for AI21 client library.
@@ -36,32 +33,64 @@ class AI21Instrumentor(BaseInstrumentor):
wrap_function_wrapper(
"ai21.clients.studio.resources.chat.chat_completions",
"ChatCompletions.create",
- chat(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# RAG completions
wrap_function_wrapper(
"ai21.clients.studio.resources.studio_conversational_rag",
"StudioConversationalRag.create",
- chat_rag(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ chat_rag(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# Async chat completions
wrap_function_wrapper(
"ai21.clients.studio.resources.chat.async_chat_completions",
"AsyncChatCompletions.create",
- async_chat(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# Async RAG completions
wrap_function_wrapper(
"ai21.clients.studio.resources.studio_conversational_rag",
"AsyncStudioConversationalRag.create",
- async_chat_rag(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_chat_rag(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/ai21/ai21.py b/sdk/python/src/openlit/instrumentation/ai21/ai21.py
index c60048c4..7d8ed572 100644
--- a/sdk/python/src/openlit/instrumentation/ai21/ai21.py
+++ b/sdk/python/src/openlit/instrumentation/ai21/ai21.py
@@ -12,12 +12,21 @@ from openlit.instrumentation.ai21.utils import (
process_chunk,
process_chat_response,
process_streaming_chat_response,
- process_chat_rag_response
+ process_chat_rag_response,
)
from openlit.semcov import SemanticConvention
-def chat(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -28,15 +37,15 @@ def chat(version, environment, application_name, tracer, pricing_info,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -77,7 +86,9 @@ def chat(version, environment, application_name, tracer, pricing_info,
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(self._span_name, kind=SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -86,7 +97,7 @@ def chat(version, environment, application_name, tracer, pricing_info,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -100,7 +111,9 @@ def chat(version, environment, application_name, tracer, pricing_info,
"""
# Check if streaming is enabled for the API call
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "api.ai21.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.ai21.com", 443
+ )
request_model = kwargs.get("model", "jamba-1.5-mini")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -109,7 +122,9 @@ def chat(version, environment, application_name, tracer, pricing_info,
# Special handling for streaming response
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedSyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
else:
# Handling for non-streaming responses
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -131,7 +146,7 @@ def chat(version, environment, application_name, tracer, pricing_info,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -141,8 +156,17 @@ def chat(version, environment, application_name, tracer, pricing_info,
return wrapper
-def chat_rag(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def chat_rag(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI RAG function call
"""
@@ -151,7 +175,9 @@ def chat_rag(version, environment, application_name, tracer, pricing_info,
"""
Wraps the GenAI RAG function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.ai21.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.ai21.com", 443
+ )
request_model = kwargs.get("model", "jamba-1.5-mini")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -175,7 +201,7 @@ def chat_rag(version, environment, application_name, tracer, pricing_info,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/ai21/async_ai21.py b/sdk/python/src/openlit/instrumentation/ai21/async_ai21.py
index a0d91416..c6520f30 100644
--- a/sdk/python/src/openlit/instrumentation/ai21/async_ai21.py
+++ b/sdk/python/src/openlit/instrumentation/ai21/async_ai21.py
@@ -12,12 +12,21 @@ from openlit.instrumentation.ai21.utils import (
process_chunk,
process_chat_response,
process_streaming_chat_response,
- process_chat_rag_response
+ process_chat_rag_response,
)
from openlit.semcov import SemanticConvention
-def async_chat(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def async_chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -28,15 +37,15 @@ def async_chat(version, environment, application_name, tracer, pricing_info,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -77,7 +86,9 @@ def async_chat(version, environment, application_name, tracer, pricing_info,
return chunk
except StopAsyncIteration:
try:
- with tracer.start_as_current_span(self._span_name, kind=SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -86,7 +97,7 @@ def async_chat(version, environment, application_name, tracer, pricing_info,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -100,7 +111,9 @@ def async_chat(version, environment, application_name, tracer, pricing_info,
"""
# Check if streaming is enabled for the API call
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "api.ai21.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.ai21.com", 443
+ )
request_model = kwargs.get("model", "jamba-1.5-mini")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -109,7 +122,9 @@ def async_chat(version, environment, application_name, tracer, pricing_info,
# Special handling for streaming response
awaited_wrapped = await wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedAsyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedAsyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
else:
# Handling for non-streaming responses
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -131,7 +146,7 @@ def async_chat(version, environment, application_name, tracer, pricing_info,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -141,8 +156,17 @@ def async_chat(version, environment, application_name, tracer, pricing_info,
return wrapper
-def async_chat_rag(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def async_chat_rag(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI RAG function call
"""
@@ -151,7 +175,9 @@ def async_chat_rag(version, environment, application_name, tracer, pricing_info,
"""
Wraps the GenAI RAG function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.ai21.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.ai21.com", 443
+ )
request_model = kwargs.get("model", "jamba-1.5-mini")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -175,7 +201,7 @@ def async_chat_rag(version, environment, application_name, tracer, pricing_info,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/ai21/utils.py b/sdk/python/src/openlit/instrumentation/ai21/utils.py
index 77a36e68..8f78323f 100644
--- a/sdk/python/src/openlit/instrumentation/ai21/utils.py
+++ b/sdk/python/src/openlit/instrumentation/ai21/utils.py
@@ -17,6 +17,7 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def format_content(messages):
"""
Process a list of messages to extract content.
@@ -25,9 +26,15 @@ def format_content(messages):
formatted_messages = []
for message in messages:
# Handle different message formats
- if hasattr(message, "role") and (hasattr(message, "content") or hasattr(message, "text")):
+ if hasattr(message, "role") and (
+ hasattr(message, "content") or hasattr(message, "text")
+ ):
# ChatMessage object (AI21 format)
- role = str(message.role) if hasattr(message.role, 'value') else str(message.role)
+ role = (
+ str(message.role)
+ if hasattr(message.role, "value")
+ else str(message.role)
+ )
content = getattr(message, "content", None) or getattr(message, "text", "")
elif isinstance(message, dict):
# Dictionary format
@@ -36,12 +43,15 @@ def format_content(messages):
else:
# Fallback - try to extract as string
role = str(getattr(message, "role", "unknown"))
- content = str(getattr(message, "content", "") or getattr(message, "text", ""))
+ content = str(
+ getattr(message, "content", "") or getattr(message, "text", "")
+ )
if isinstance(content, list):
content_str = ", ".join(
- f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
- if "type" in item else f'text: {item["text"]}'
+ f"{item['type']}: {item['text'] if 'text' in item else item['image_url']}"
+ if "type" in item
+ else f"text: {item['text']}"
for item in content
)
formatted_messages.append(f"{role}: {content_str}")
@@ -50,6 +60,7 @@ def format_content(messages):
return "\n".join(formatted_messages)
+
def process_chunk(scope, chunk):
"""
Process a chunk of response data and update state.
@@ -66,10 +77,11 @@ def process_chunk(scope, chunk):
chunked = response_as_dict(chunk)
# Collect message IDs and aggregated response from events
- if (len(chunked.get("choices", [])) > 0 and
- "delta" in chunked.get("choices")[0] and
- "content" in chunked.get("choices")[0].get("delta", {})):
-
+ if (
+ len(chunked.get("choices", [])) > 0
+ and "delta" in chunked.get("choices")[0]
+ and "content" in chunked.get("choices")[0].get("delta", {})
+ ):
content = chunked.get("choices")[0].get("delta").get("content")
if content:
scope._llmresponse += content
@@ -81,8 +93,18 @@ def process_chunk(scope, chunk):
scope._finish_reason = chunked.get("choices", [{}])[0].get("finish_reason")
scope._end_time = time.time()
-def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream):
+
+def common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream,
+):
"""
Process chat request and generate Telemetry
"""
@@ -93,44 +115,97 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
prompt = format_content(scope._kwargs.get("messages", []))
request_model = scope._kwargs.get("model", "jamba-1.5-mini")
- cost = get_chat_model_cost(request_model, pricing_info, scope._input_tokens, scope._output_tokens)
+ cost = get_chat_model_cost(
+ request_model, pricing_info, scope._input_tokens, scope._output_tokens
+ )
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_AI21,
- scope._server_address, scope._server_port, request_model, request_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_AI21,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Request parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED, scope._kwargs.get("seed", ""))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY, scope._kwargs.get("frequency_penalty", 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, scope._kwargs.get("max_tokens", -1))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, scope._kwargs.get("presence_penalty", 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get("stop", []))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, scope._kwargs.get("temperature", 0.4))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 1.0))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_SEED, scope._kwargs.get("seed", "")
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
+ scope._kwargs.get("frequency_penalty", 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
+ scope._kwargs.get("max_tokens", -1),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
+ scope._kwargs.get("presence_penalty", 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get("stop", [])
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
+ scope._kwargs.get("temperature", 0.4),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 1.0)
+ )
# Span Attributes for Response parameters
scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ "text" if isinstance(scope._llmresponse, str) else "json",
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens + scope._output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ scope._input_tokens + scope._output_tokens,
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Tools
if scope._tools:
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, scope._tools.get("function", {}).get("name", ""))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get("id", "")))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, str(scope._tools.get("function", {}).get("arguments", "")))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_NAME,
+ scope._tools.get("function", {}).get("name", ""),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get("id", ""))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_ARGS,
+ str(scope._tools.get("function", {}).get("arguments", "")),
+ )
# Span Attributes for Content
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -150,23 +225,69 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
# Metrics
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_AI21,
- scope._server_address, scope._server_port, request_model, request_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, scope._output_tokens,
- cost, scope._tbt, scope._ttft)
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_AI21,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ scope._output_tokens,
+ cost,
+ scope._tbt,
+ scope._ttft,
+ )
-def process_streaming_chat_response(scope, pricing_info, environment, application_name, metrics,
- capture_message_content=False, disable_metrics=False, version=""):
+
+def process_streaming_chat_response(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="",
+):
"""
Process streaming chat request and generate Telemetry
"""
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=True)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=True,
+ )
+
-def process_chat_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+def process_chat_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process chat request and generate Telemetry
"""
@@ -189,21 +310,43 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
scope._ttft, scope._tbt = scope._end_time - scope._start_time, 0
scope._server_address, scope._server_port = server_address, server_port
scope._kwargs = kwargs
- scope._finish_reason = str(response_dict.get("choices", [])[0].get("finish_reason", ""))
+ scope._finish_reason = str(
+ response_dict.get("choices", [])[0].get("finish_reason", "")
+ )
# Handle tool calls
if scope._kwargs.get("tools"):
- scope._tools = response_dict.get("choices", [{}])[0].get("message", {}).get("tool_calls")
+ scope._tools = (
+ response_dict.get("choices", [{}])[0].get("message", {}).get("tool_calls")
+ )
else:
scope._tools = None
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=False)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=False,
+ )
return response
-def common_chat_rag_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version):
+
+def common_chat_rag_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+):
"""
Process RAG chat request and generate Telemetry
"""
@@ -211,40 +354,82 @@ def common_chat_rag_logic(scope, pricing_info, environment, application_name, me
prompt = format_content(scope._kwargs.get("messages", []))
request_model = scope._kwargs.get("model", "jamba-1.5-mini")
- cost = get_chat_model_cost(request_model, pricing_info, scope._input_tokens, scope._output_tokens)
+ cost = get_chat_model_cost(
+ request_model, pricing_info, scope._input_tokens, scope._output_tokens
+ )
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_AI21,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, False, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_AI21,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ False,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# RAG-specific span attributes
- scope._span.set_attribute(SemanticConvention.GEN_AI_RAG_MAX_SEGMENTS, scope._kwargs.get("max_segments", -1))
- scope._span.set_attribute(SemanticConvention.GEN_AI_RAG_STRATEGY, scope._kwargs.get("retrieval_strategy", "segments"))
- scope._span.set_attribute(SemanticConvention.GEN_AI_RAG_MAX_NEIGHBORS, scope._kwargs.get("max_neighbors", -1))
- scope._span.set_attribute(SemanticConvention.GEN_AI_RAG_FILE_IDS, str(scope._kwargs.get("file_ids", "")))
- scope._span.set_attribute(SemanticConvention.GEN_AI_RAG_DOCUMENTS_PATH, scope._kwargs.get("path", ""))
- scope._span.set_attribute(SemanticConvention.GEN_AI_RAG_SIMILARITY_THRESHOLD,
- scope._kwargs.get("retrieval_similarity_threshold", -1))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RAG_MAX_SEGMENTS,
+ scope._kwargs.get("max_segments", -1),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RAG_STRATEGY,
+ scope._kwargs.get("retrieval_strategy", "segments"),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RAG_MAX_NEIGHBORS,
+ scope._kwargs.get("max_neighbors", -1),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RAG_FILE_IDS, str(scope._kwargs.get("file_ids", ""))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RAG_DOCUMENTS_PATH, scope._kwargs.get("path", "")
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RAG_SIMILARITY_THRESHOLD,
+ scope._kwargs.get("retrieval_similarity_threshold", -1),
+ )
# Standard span attributes
scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens + scope._output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ "text" if isinstance(scope._llmresponse, str) else "json",
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ scope._input_tokens + scope._output_tokens,
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Handle tool calls
if scope._kwargs.get("tools"):
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALLS,
- str(scope._choices[0].get("message", {}).get("tool_calls", "")))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_CALLS,
+ str(scope._choices[0].get("message", {}).get("tool_calls", "")),
+ )
# Content attributes
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -264,14 +449,42 @@ def common_chat_rag_logic(scope, pricing_info, environment, application_name, me
# Metrics
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_AI21,
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, scope._output_tokens,
- cost, scope._tbt, scope._ttft)
-
-def process_chat_rag_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_AI21,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ scope._output_tokens,
+ cost,
+ scope._tbt,
+ scope._ttft,
+ )
+
+
+def process_chat_rag_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process RAG chat request and generate Telemetry
"""
@@ -311,7 +524,15 @@ def process_chat_rag_response(response, request_model, pricing_info, server_port
scope._tools = None
scope._choices = choices
- common_chat_rag_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version)
+ common_chat_rag_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/anthropic/__init__.py b/sdk/python/src/openlit/instrumentation/anthropic/__init__.py
index fc8ccf88..3af7cc35 100644
--- a/sdk/python/src/openlit/instrumentation/anthropic/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/anthropic/__init__.py
@@ -10,6 +10,7 @@ from openlit.instrumentation.anthropic.async_anthropic import async_messages
_instruments = ("anthropic >= 0.21.0",)
+
class AnthropicInstrumentor(BaseInstrumentor):
"""
An instrumentor for Anthropic's client library.
@@ -32,16 +33,32 @@ class AnthropicInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"anthropic.resources.messages",
"Messages.create",
- messages(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics),
+ messages(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# async
wrap_function_wrapper(
"anthropic.resources.messages",
"AsyncMessages.create",
- async_messages(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics),
+ async_messages(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/anthropic/anthropic.py b/sdk/python/src/openlit/instrumentation/anthropic/anthropic.py
index 52f2f445..62a80c25 100644
--- a/sdk/python/src/openlit/instrumentation/anthropic/anthropic.py
+++ b/sdk/python/src/openlit/instrumentation/anthropic/anthropic.py
@@ -4,10 +4,7 @@ Module for monitoring Anthropic API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.anthropic.utils import (
process_chunk,
process_chat_response,
@@ -15,7 +12,17 @@ from openlit.instrumentation.anthropic.utils import (
)
from openlit.semcov import SemanticConvention
-def messages(version, environment, application_name, tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def messages(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for Anthropic Messages.create calls.
"""
@@ -26,14 +33,14 @@ def messages(version, environment, application_name, tracer, pricing_info, captu
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -87,7 +94,7 @@ def messages(version, environment, application_name, tracer, pricing_info, captu
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
handle_exception(self._span, e)
@@ -99,7 +106,9 @@ def messages(version, environment, application_name, tracer, pricing_info, captu
"""
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "api.anthropic.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.anthropic.com", 443
+ )
request_model = kwargs.get("model", "claude-3-5-sonnet-latest")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -109,7 +118,9 @@ def messages(version, environment, application_name, tracer, pricing_info, captu
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedSyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
else:
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -131,7 +142,7 @@ def messages(version, environment, application_name, tracer, pricing_info, captu
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/anthropic/async_anthropic.py b/sdk/python/src/openlit/instrumentation/anthropic/async_anthropic.py
index 467def84..b6641da1 100644
--- a/sdk/python/src/openlit/instrumentation/anthropic/async_anthropic.py
+++ b/sdk/python/src/openlit/instrumentation/anthropic/async_anthropic.py
@@ -4,10 +4,7 @@ Module for monitoring Anthropic API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.anthropic.utils import (
process_chunk,
process_chat_response,
@@ -15,7 +12,17 @@ from openlit.instrumentation.anthropic.utils import (
)
from openlit.semcov import SemanticConvention
-def async_messages(version, environment, application_name, tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_messages(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for Anthropic AsyncMessages.create calls.
"""
@@ -26,14 +33,14 @@ def async_messages(version, environment, application_name, tracer, pricing_info,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -87,7 +94,7 @@ def async_messages(version, environment, application_name, tracer, pricing_info,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
handle_exception(self._span, e)
@@ -99,7 +106,9 @@ def async_messages(version, environment, application_name, tracer, pricing_info,
"""
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "api.anthropic.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.anthropic.com", 443
+ )
request_model = kwargs.get("model", "claude-3-5-sonnet-latest")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -109,7 +118,9 @@ def async_messages(version, environment, application_name, tracer, pricing_info,
awaited_wrapped = await wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedAsyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedAsyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
else:
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -131,7 +142,7 @@ def async_messages(version, environment, application_name, tracer, pricing_info,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/anthropic/utils.py b/sdk/python/src/openlit/instrumentation/anthropic/utils.py
index 6e899101..d1a17c5e 100644
--- a/sdk/python/src/openlit/instrumentation/anthropic/utils.py
+++ b/sdk/python/src/openlit/instrumentation/anthropic/utils.py
@@ -1,6 +1,7 @@
"""
Anthropic OpenTelemetry instrumentation utility functions
"""
+
import time
from opentelemetry.trace import Status, StatusCode
@@ -15,6 +16,7 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def format_content(messages):
"""
Format the messages into a string for span events.
@@ -47,6 +49,7 @@ def format_content(messages):
return "\n".join(formatted_messages)
+
def process_chunk(scope, chunk):
"""
Process a chunk of response data and update state.
@@ -87,8 +90,18 @@ def process_chunk(scope, chunk):
scope._output_tokens = chunked.get("usage").get("output_tokens")
scope._finish_reason = chunked.get("delta").get("stop_reason")
-def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream):
+
+def common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream,
+):
"""
Process chat request and generate Telemetry
"""
@@ -100,30 +113,68 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
formatted_messages = format_content(scope._kwargs.get("messages", []))
request_model = scope._kwargs.get("model", "claude-3-5-sonnet-latest")
- cost = get_chat_model_cost(request_model, pricing_info, scope._input_tokens, scope._output_tokens)
+ cost = get_chat_model_cost(
+ request_model, pricing_info, scope._input_tokens, scope._output_tokens
+ )
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_ANTHROPIC,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_ANTHROPIC,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Request parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, scope._kwargs.get("max_tokens", -1))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get("stop_sequences", []))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, scope._kwargs.get("temperature", 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_K, scope._kwargs.get("top_k", 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 1.0))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
+ scope._kwargs.get("max_tokens", -1),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES,
+ scope._kwargs.get("stop_sequences", []),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
+ scope._kwargs.get("temperature", 1.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_K, scope._kwargs.get("top_k", 1.0)
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 1.0)
+ )
# Span Attributes for Response parameters
scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ "text" if isinstance(scope._llmresponse, str) else "json",
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens + scope._output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ scope._input_tokens + scope._output_tokens,
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Handle tool calls if present
@@ -139,8 +190,12 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
# Span Attributes for Content
if capture_message_content:
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, formatted_messages)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_PROMPT, formatted_messages
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -160,13 +215,36 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
# Record metrics
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_ANTHROPIC,
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, scope._output_tokens,
- cost, scope._tbt, scope._ttft)
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_ANTHROPIC,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ scope._output_tokens,
+ cost,
+ scope._tbt,
+ scope._ttft,
+ )
-def process_streaming_chat_response(scope, pricing_info, environment, application_name, metrics,
- capture_message_content=False, disable_metrics=False, version=""):
+
+def process_streaming_chat_response(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="",
+):
"""
Process streaming chat response and generate telemetry.
"""
@@ -175,15 +253,38 @@ def process_streaming_chat_response(scope, pricing_info, environment, applicatio
scope._tool_calls = {
"id": scope._tool_id,
"name": scope._tool_name,
- "input": scope._tool_arguments
+ "input": scope._tool_arguments,
}
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=True)
-
-def process_chat_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time,
- span, capture_message_content=False, disable_metrics=False, version="1.0.0", **kwargs):
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=True,
+ )
+
+
+def process_chat_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process non-streaming chat response and generate telemetry.
"""
@@ -215,11 +316,20 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
scope._tool_calls = {
"id": block.get("id", ""),
"name": block.get("name", ""),
- "input": block.get("input", "")
+ "input": block.get("input", ""),
}
break
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=False)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=False,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/assemblyai/__init__.py b/sdk/python/src/openlit/instrumentation/assemblyai/__init__.py
index 178c45df..b2df2b46 100644
--- a/sdk/python/src/openlit/instrumentation/assemblyai/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/assemblyai/__init__.py
@@ -9,6 +9,7 @@ from openlit.instrumentation.assemblyai.assemblyai import transcribe
_instruments = ("assemblyai >= 0.35.1",)
+
class AssemblyAIInstrumentor(BaseInstrumentor):
"""
An instrumentor for AssemblyAI client library.
@@ -31,8 +32,16 @@ class AssemblyAIInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"assemblyai.transcriber",
"Transcriber.transcribe",
- transcribe(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ transcribe(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/assemblyai/assemblyai.py b/sdk/python/src/openlit/instrumentation/assemblyai/assemblyai.py
index 902d8191..63ca0b03 100644
--- a/sdk/python/src/openlit/instrumentation/assemblyai/assemblyai.py
+++ b/sdk/python/src/openlit/instrumentation/assemblyai/assemblyai.py
@@ -12,8 +12,17 @@ from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def transcribe(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def transcribe(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for AssemblyAI transcribe function call
"""
@@ -23,7 +32,9 @@ def transcribe(version, environment, application_name,
Wraps the AssemblyAI transcribe function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.assemblyai.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.assemblyai.com", 443
+ )
request_model = kwargs.get("speech_model", "best")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO} {request_model}"
@@ -48,7 +59,7 @@ def transcribe(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/assemblyai/utils.py b/sdk/python/src/openlit/instrumentation/assemblyai/utils.py
index 1a412a93..dd5cae0a 100644
--- a/sdk/python/src/openlit/instrumentation/assemblyai/utils.py
+++ b/sdk/python/src/openlit/instrumentation/assemblyai/utils.py
@@ -1,9 +1,14 @@
"""
AssemblyAI OpenTelemetry instrumentation utility functions
"""
+
import time
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from opentelemetry.trace import Status, StatusCode
from openlit.__helpers import (
@@ -12,14 +17,29 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def format_audio_url(audio_url):
"""
Process audio URL input to extract content.
"""
return str(audio_url) if audio_url else ""
-def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_address, server_port,
- request_model, response_model, environment, application_name, is_stream, tbt, ttft, version):
+
+def common_span_attributes(
+ scope,
+ gen_ai_operation,
+ gen_ai_system,
+ server_address,
+ server_port,
+ request_model,
+ response_model,
+ environment,
+ application_name,
+ is_stream,
+ tbt,
+ ttft,
+ version,
+):
"""
Set common span attributes for both chat and RAG operations.
"""
@@ -30,7 +50,9 @@ def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_addres
scope._span.set_attribute(SemanticConvention.SERVER_ADDRESS, server_address)
scope._span.set_attribute(SemanticConvention.SERVER_PORT, server_port)
scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, request_model)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, scope._response_model)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_MODEL, scope._response_model
+ )
scope._span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
scope._span.set_attribute(SERVICE_NAME, application_name)
scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, is_stream)
@@ -38,8 +60,21 @@ def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_addres
scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, scope._ttft)
scope._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
-def record_audio_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
- request_model, response_model, environment, application_name, start_time, end_time, cost):
+
+def record_audio_metrics(
+ metrics,
+ gen_ai_operation,
+ gen_ai_system,
+ server_address,
+ server_port,
+ request_model,
+ response_model,
+ environment,
+ application_name,
+ start_time,
+ end_time,
+ cost,
+):
"""
Record audio metrics for the operation.
"""
@@ -58,8 +93,18 @@ def record_audio_metrics(metrics, gen_ai_operation, gen_ai_system, server_addres
metrics["genai_requests"].add(1, attributes)
metrics["genai_cost"].record(cost, attributes)
-def common_audio_logic(scope, gen_ai_endpoint, pricing_info, environment, application_name,
- metrics, capture_message_content, disable_metrics, version):
+
+def common_audio_logic(
+ scope,
+ gen_ai_endpoint,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+):
"""
Process audio transcription request and generate Telemetry
"""
@@ -69,13 +114,26 @@ def common_audio_logic(scope, gen_ai_endpoint, pricing_info, environment, applic
is_stream = False
# Calculate cost based on audio duration
- cost = get_audio_model_cost(request_model, pricing_info, prompt, scope._response.audio_duration)
+ cost = get_audio_model_cost(
+ request_model, pricing_info, prompt, scope._response.audio_duration
+ )
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO, SemanticConvention.GEN_AI_SYSTEM_ASSEMBLYAI,
- scope._server_address, scope._server_port, request_model, request_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO,
+ SemanticConvention.GEN_AI_SYSTEM_ASSEMBLYAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Response parameters
scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text")
@@ -85,12 +143,16 @@ def common_audio_logic(scope, gen_ai_endpoint, pricing_info, environment, applic
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Audio-specific span attributes
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_DURATION, scope._response.audio_duration)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_AUDIO_DURATION, scope._response.audio_duration
+ )
# Span Attributes for Content
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._response.text)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._response.text
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -110,14 +172,38 @@ def common_audio_logic(scope, gen_ai_endpoint, pricing_info, environment, applic
# Metrics
if not disable_metrics:
- record_audio_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO,
- SemanticConvention.GEN_AI_SYSTEM_ASSEMBLYAI, scope._server_address, scope._server_port,
- request_model, request_model, environment, application_name, scope._start_time,
- scope._end_time, cost)
-
-def process_audio_response(response, gen_ai_endpoint, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+ record_audio_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO,
+ SemanticConvention.GEN_AI_SYSTEM_ASSEMBLYAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ cost,
+ )
+
+
+def process_audio_response(
+ response,
+ gen_ai_endpoint,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process audio transcription request and generate Telemetry
"""
@@ -136,7 +222,16 @@ def process_audio_response(response, gen_ai_endpoint, pricing_info, server_port,
scope._tbt = 0.0
scope._ttft = scope._end_time - scope._start_time
- common_audio_logic(scope, gen_ai_endpoint, pricing_info, environment, application_name,
- metrics, capture_message_content, disable_metrics, version)
+ common_audio_logic(
+ scope,
+ gen_ai_endpoint,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/astra/__init__.py b/sdk/python/src/openlit/instrumentation/astra/__init__.py
index 39b6126c..b7ae3874 100644
--- a/sdk/python/src/openlit/instrumentation/astra/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/astra/__init__.py
@@ -17,16 +17,23 @@ ASTRA_SYNC_OPERATIONS = [
# Database operations
("astrapy.database", "Database.create_collection", "astra.create_collection"),
("astrapy.database", "Database.drop_collection", "astra.drop_collection"),
-
# Collection operations
("astrapy.collection", "Collection.insert_one", "astra.insert_one"),
("astrapy.collection", "Collection.insert_many", "astra.insert_many"),
("astrapy.collection", "Collection.update_one", "astra.update_one"),
("astrapy.collection", "Collection.update_many", "astra.update_many"),
- ("astrapy.collection", "Collection.find_one_and_update", "astra.find_one_and_update"),
+ (
+ "astrapy.collection",
+ "Collection.find_one_and_update",
+ "astra.find_one_and_update",
+ ),
("astrapy.collection", "Collection.find", "astra.find"),
("astrapy.collection", "Collection.replace_one", "astra.replace_one"),
- ("astrapy.collection", "Collection.find_one_and_delete", "astra.find_one_and_delete"),
+ (
+ "astrapy.collection",
+ "Collection.find_one_and_delete",
+ "astra.find_one_and_delete",
+ ),
("astrapy.collection", "Collection.delete_one", "astra.delete_one"),
("astrapy.collection", "Collection.delete_many", "astra.delete_many"),
]
@@ -36,20 +43,28 @@ ASTRA_ASYNC_OPERATIONS = [
# Async Database operations
("astrapy.database", "AsyncDatabase.create_collection", "astra.create_collection"),
("astrapy.database", "AsyncDatabase.drop_collection", "astra.drop_collection"),
-
# Async Collection operations
("astrapy.collection", "AsyncCollection.insert_one", "astra.insert_one"),
("astrapy.collection", "AsyncCollection.insert_many", "astra.insert_many"),
("astrapy.collection", "AsyncCollection.update_one", "astra.update_one"),
("astrapy.collection", "AsyncCollection.update_many", "astra.update_many"),
- ("astrapy.collection", "AsyncCollection.find_one_and_update", "astra.find_one_and_update"),
+ (
+ "astrapy.collection",
+ "AsyncCollection.find_one_and_update",
+ "astra.find_one_and_update",
+ ),
("astrapy.collection", "AsyncCollection.find", "astra.find"),
("astrapy.collection", "AsyncCollection.replace_one", "astra.replace_one"),
- ("astrapy.collection", "AsyncCollection.find_one_and_delete", "astra.find_one_and_delete"),
+ (
+ "astrapy.collection",
+ "AsyncCollection.find_one_and_delete",
+ "astra.find_one_and_delete",
+ ),
("astrapy.collection", "AsyncCollection.delete_one", "astra.delete_one"),
("astrapy.collection", "AsyncCollection.delete_many", "astra.delete_many"),
]
+
class AstraInstrumentor(BaseInstrumentor):
"""
An instrumentor for AstraDB's client library.
@@ -73,8 +88,17 @@ class AstraInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
module,
class_method,
- general_wrap(endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ general_wrap(
+ endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# Wrap async operations
@@ -82,8 +106,17 @@ class AstraInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
module,
class_method,
- async_general_wrap(endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_general_wrap(
+ endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/astra/astra.py b/sdk/python/src/openlit/instrumentation/astra/astra.py
index fa0fa127..7133dcfb 100644
--- a/sdk/python/src/openlit/instrumentation/astra/astra.py
+++ b/sdk/python/src/openlit/instrumentation/astra/astra.py
@@ -12,11 +12,22 @@ from openlit.instrumentation.astra.utils import (
set_server_address_and_port,
)
-def general_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def general_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for AstraDB function calls.
"""
+
def wrapper(wrapped, instance, args, kwargs):
# CRITICAL: Suppression check
if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
@@ -42,9 +53,21 @@ def general_wrap(gen_ai_endpoint, version, environment, application_name,
try:
# Process response and generate telemetry
response = process_astra_response(
- response, db_operation, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version, instance, args, **kwargs
+ response,
+ db_operation,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ args,
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/astra/async_astra.py b/sdk/python/src/openlit/instrumentation/astra/async_astra.py
index 3aa6f6c4..ba814aa7 100644
--- a/sdk/python/src/openlit/instrumentation/astra/async_astra.py
+++ b/sdk/python/src/openlit/instrumentation/astra/async_astra.py
@@ -12,11 +12,22 @@ from openlit.instrumentation.astra.utils import (
set_server_address_and_port,
)
-def async_general_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_general_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for AstraDB async function calls.
"""
+
async def wrapper(wrapped, instance, args, kwargs):
# CRITICAL: Suppression check
if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
@@ -42,9 +53,21 @@ def async_general_wrap(gen_ai_endpoint, version, environment, application_name,
try:
# Process response and generate telemetry
response = process_astra_response(
- response, db_operation, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version, instance, args, **kwargs
+ response,
+ db_operation,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ args,
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/astra/utils.py b/sdk/python/src/openlit/instrumentation/astra/utils.py
index 850ea4f7..04cc29a6 100644
--- a/sdk/python/src/openlit/instrumentation/astra/utils.py
+++ b/sdk/python/src/openlit/instrumentation/astra/utils.py
@@ -30,6 +30,7 @@ DB_OPERATION_MAP = {
"astra.delete_many": SemanticConvention.DB_OPERATION_DELETE,
}
+
def object_count(obj):
"""
Counts length of object if it exists, else returns 0.
@@ -40,6 +41,7 @@ def object_count(obj):
return 1
return 0
+
def set_server_address_and_port(instance):
"""
Extracts server address and port from AstraDB client instance.
@@ -83,9 +85,18 @@ def set_server_address_and_port(instance):
return server_address, server_port
-def common_astra_logic(scope, environment, application_name,
- metrics, capture_message_content, disable_metrics, version,
- instance=None, endpoint=None):
+
+def common_astra_logic(
+ scope,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance=None,
+ endpoint=None,
+):
"""
Process AstraDB request and generate telemetry.
@@ -103,13 +114,22 @@ def common_astra_logic(scope, environment, application_name,
scope._end_time = time.time()
# Set common database span attributes using helper
- common_db_span_attributes(scope, SemanticConvention.DB_SYSTEM_ASTRA,
- scope._server_address, scope._server_port, environment, application_name, version)
+ common_db_span_attributes(
+ scope,
+ SemanticConvention.DB_SYSTEM_ASTRA,
+ scope._server_address,
+ scope._server_port,
+ environment,
+ application_name,
+ version,
+ )
# Set DB operation specific attributes
scope._span.set_attribute(SemanticConvention.DB_OPERATION_NAME, scope._db_operation)
- scope._span.set_attribute(SemanticConvention.DB_CLIENT_OPERATION_DURATION,
- scope._end_time - scope._start_time)
+ scope._span.set_attribute(
+ SemanticConvention.DB_CLIENT_OPERATION_DURATION,
+ scope._end_time - scope._start_time,
+ )
# Get collection name from instance
collection_name = getattr(instance, "name", "unknown")
@@ -117,124 +137,192 @@ def common_astra_logic(scope, environment, application_name,
if scope._db_operation == SemanticConvention.DB_OPERATION_CREATE_COLLECTION:
# Handle create_collection operation
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_DIMENSION,
- scope._kwargs.get("dimension", -1))
- scope._span.set_attribute(SemanticConvention.DB_INDEX_METRIC,
- str(scope._kwargs.get("metric", "")))
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_DIMENSION,
+ scope._kwargs.get("dimension", -1),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_INDEX_METRIC, str(scope._kwargs.get("metric", ""))
+ )
# Set namespace if available in response
if scope._response and hasattr(scope._response, "keyspace"):
- scope._span.set_attribute(SemanticConvention.DB_NAMESPACE, scope._response.keyspace)
+ scope._span.set_attribute(
+ SemanticConvention.DB_NAMESPACE, scope._response.keyspace
+ )
if scope._response and hasattr(scope._response, "name"):
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, scope._response.name)
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, scope._response.name
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
f"dimension={scope._kwargs.get('dimension', 'None')} "
- f"metric={scope._kwargs.get('metric', 'None')}")
+ f"metric={scope._kwargs.get('metric', 'None')}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_DELETE_COLLECTION:
# Handle drop_collection operation
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
- f"{scope._db_operation} {collection_name}")
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_INSERT:
# Handle insert operations (insert_one, insert_many, regular insert)
- documents = scope._args[0] if scope._args else scope._kwargs.get("documents", [])
+ documents = (
+ scope._args[0] if scope._args else scope._kwargs.get("documents", [])
+ )
- scope._span.set_attribute(SemanticConvention.DB_DOCUMENTS_COUNT, object_count(documents))
+ scope._span.set_attribute(
+ SemanticConvention.DB_DOCUMENTS_COUNT, object_count(documents)
+ )
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(documents))
# Response metrics
if scope._response and hasattr(scope._response, "inserted_ids"):
- scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
- len(scope._response.inserted_ids))
+ scope._span.set_attribute(
+ SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
+ len(scope._response.inserted_ids),
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
- f"documents_count={object_count(documents)}")
+ f"documents_count={object_count(documents)}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_UPDATE:
# Handle update operations (update_one, update_many, regular update)
- update_query = scope._args[1] if len(scope._args) > 1 else scope._kwargs.get("update", {})
- filter_query = scope._args[0] if scope._args else scope._kwargs.get("filter", {})
+ update_query = (
+ scope._args[1] if len(scope._args) > 1 else scope._kwargs.get("update", {})
+ )
+ filter_query = (
+ scope._args[0] if scope._args else scope._kwargs.get("filter", {})
+ )
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(update_query))
scope._span.set_attribute(SemanticConvention.DB_FILTER, str(filter_query))
# Response metrics
if scope._response and hasattr(scope._response, "update_info"):
- scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
- scope._response.update_info.get("nModified", 0))
+ scope._span.set_attribute(
+ SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
+ scope._response.update_info.get("nModified", 0),
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
f"filter={str(filter_query)[:100]}... "
- f"update={str(update_query)[:100]}...")
+ f"update={str(update_query)[:100]}...",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_REPLACE:
# Handle replace operations (find_one_and_update, replace_one)
- filter_query = scope._args[0] if scope._args else scope._kwargs.get("filter", {})
+ filter_query = (
+ scope._args[0] if scope._args else scope._kwargs.get("filter", {})
+ )
# Check if it's an upsert operation
if scope._kwargs.get("upsert"):
scope._db_operation = SemanticConvention.DB_OPERATION_UPSERT
- scope._span.set_attribute(SemanticConvention.DB_OPERATION_NAME,
- SemanticConvention.DB_OPERATION_UPSERT)
+ scope._span.set_attribute(
+ SemanticConvention.DB_OPERATION_NAME,
+ SemanticConvention.DB_OPERATION_UPSERT,
+ )
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(filter_query))
scope._span.set_attribute(SemanticConvention.DB_FILTER, str(filter_query))
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
f"filter={str(filter_query)[:100]}... "
- f"upsert={scope._kwargs.get('upsert', False)}")
+ f"upsert={scope._kwargs.get('upsert', False)}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_SELECT:
# Handle find operations
- filter_query = scope._args[0] if scope._args else scope._kwargs.get("filter", {})
+ filter_query = (
+ scope._args[0] if scope._args else scope._kwargs.get("filter", {})
+ )
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(filter_query))
scope._span.set_attribute(SemanticConvention.DB_FILTER, str(filter_query))
# Response metrics
if scope._response and hasattr(scope._response, "__len__"):
- scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
- len(scope._response))
+ scope._span.set_attribute(
+ SemanticConvention.DB_RESPONSE_RETURNED_ROWS, len(scope._response)
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
- f"filter={str(filter_query)[:100]}...")
+ f"filter={str(filter_query)[:100]}...",
+ )
- elif scope._db_operation in [SemanticConvention.DB_OPERATION_DELETE,
- SemanticConvention.DB_OPERATION_FIND_AND_DELETE]:
+ elif scope._db_operation in [
+ SemanticConvention.DB_OPERATION_DELETE,
+ SemanticConvention.DB_OPERATION_FIND_AND_DELETE,
+ ]:
# Handle delete operations (delete_one, delete_many, find_one_and_delete)
- filter_query = scope._args[0] if scope._args else scope._kwargs.get("filter", {})
+ filter_query = (
+ scope._args[0] if scope._args else scope._kwargs.get("filter", {})
+ )
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(filter_query))
scope._span.set_attribute(SemanticConvention.DB_FILTER, str(filter_query))
# Response metrics
if scope._response and hasattr(scope._response, "deleted_count"):
- scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
- scope._response.deleted_count)
+ scope._span.set_attribute(
+ SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
+ scope._response.deleted_count,
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
- f"filter={str(filter_query)[:100]}...")
+ f"filter={str(filter_query)[:100]}...",
+ )
scope._span.set_status(Status(StatusCode.OK))
# Record metrics using helper
if not disable_metrics:
- record_db_metrics(metrics, SemanticConvention.DB_SYSTEM_ASTRA,
- scope._server_address, scope._server_port, environment, application_name,
- scope._start_time, scope._end_time)
-
-def process_astra_response(response, db_operation, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version, instance, args, **kwargs):
+ record_db_metrics(
+ metrics,
+ SemanticConvention.DB_SYSTEM_ASTRA,
+ scope._server_address,
+ scope._server_port,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ )
+
+
+def process_astra_response(
+ response,
+ db_operation,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ args,
+ **kwargs,
+):
"""
Process AstraDB response and generate telemetry.
@@ -271,7 +359,15 @@ def process_astra_response(response, db_operation, server_address, server_port,
scope._args = args
# Process the response using common logic
- common_astra_logic(scope, environment, application_name,
- metrics, capture_message_content, disable_metrics, version, instance)
+ common_astra_logic(
+ scope,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/azure_ai_inference/__init__.py b/sdk/python/src/openlit/instrumentation/azure_ai_inference/__init__.py
index 45a26408..867161f9 100644
--- a/sdk/python/src/openlit/instrumentation/azure_ai_inference/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/azure_ai_inference/__init__.py
@@ -6,15 +6,16 @@ from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
from openlit.instrumentation.azure_ai_inference.azure_ai_inference import (
complete,
- embed
+ embed,
)
from openlit.instrumentation.azure_ai_inference.async_azure_ai_inference import (
async_complete,
- async_embed
+ async_embed,
)
_instruments = ("azure-ai-inference >= 1.0.0b4",)
+
class AzureAIInferenceInstrumentor(BaseInstrumentor):
"""
An instrumentor for azure-ai-inference client library.
@@ -37,32 +38,64 @@ class AzureAIInferenceInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"azure.ai.inference",
"ChatCompletionsClient.complete",
- complete(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ complete(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# async chat completions
wrap_function_wrapper(
"azure.ai.inference.aio",
"ChatCompletionsClient.complete",
- async_complete(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_complete(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# sync embeddings
wrap_function_wrapper(
"azure.ai.inference",
"EmbeddingsClient.embed",
- embed(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ embed(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# async embeddings
wrap_function_wrapper(
"azure.ai.inference.aio",
"EmbeddingsClient.embed",
- async_embed(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_embed(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py b/sdk/python/src/openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py
index 9a87b8bc..35f5d663 100644
--- a/sdk/python/src/openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py
+++ b/sdk/python/src/openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py
@@ -20,8 +20,17 @@ from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def async_complete(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_complete(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -32,15 +41,15 @@ def async_complete(version, environment, application_name,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -71,8 +80,14 @@ def async_complete(version, environment, application_name,
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.__wrapped__.__aexit__(exc_type, exc_val, exc_tb)
process_streaming_chat_response(
- self, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version
+ self,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
)
def __aiter__(self):
@@ -92,7 +107,9 @@ def async_complete(version, environment, application_name,
"""
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "models.github.ai", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "models.github.ai", 443
+ )
request_model = kwargs.get("model", "gpt-4o")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -101,7 +118,9 @@ def async_complete(version, environment, application_name,
awaited_wrapped = await wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedAsyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedAsyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
else:
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -121,15 +140,24 @@ def async_complete(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
return response
return wrapper
-def async_embed(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_embed(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI embedding function call
"""
@@ -139,10 +167,14 @@ def async_embed(version, environment, application_name,
Wraps the GenAI embedding function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "models.github.ai", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "models.github.ai", 443
+ )
request_model = kwargs.get("model", "text-embedding-3-small")
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
@@ -163,7 +195,7 @@ def async_embed(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/azure_ai_inference/azure_ai_inference.py b/sdk/python/src/openlit/instrumentation/azure_ai_inference/azure_ai_inference.py
index 23baa88e..25ff9684 100644
--- a/sdk/python/src/openlit/instrumentation/azure_ai_inference/azure_ai_inference.py
+++ b/sdk/python/src/openlit/instrumentation/azure_ai_inference/azure_ai_inference.py
@@ -20,8 +20,17 @@ from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def complete(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def complete(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -32,15 +41,15 @@ def complete(version, environment, application_name,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -71,8 +80,14 @@ def complete(version, environment, application_name,
def __exit__(self, exc_type, exc_val, exc_tb):
self.__wrapped__.__exit__(exc_type, exc_val, exc_tb)
process_streaming_chat_response(
- self, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version
+ self,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
)
def __iter__(self):
@@ -92,7 +107,9 @@ def complete(version, environment, application_name,
"""
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "models.github.ai", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "models.github.ai", 443
+ )
request_model = kwargs.get("model", "gpt-4o")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -101,7 +118,9 @@ def complete(version, environment, application_name,
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedSyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
else:
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -121,15 +140,24 @@ def complete(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
return response
return wrapper
-def embed(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def embed(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI embedding function call
"""
@@ -139,10 +167,14 @@ def embed(version, environment, application_name,
Wraps the GenAI embedding function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "models.github.ai", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "models.github.ai", 443
+ )
request_model = kwargs.get("model", "text-embedding-3-small")
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
@@ -163,7 +195,7 @@ def embed(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/azure_ai_inference/utils.py b/sdk/python/src/openlit/instrumentation/azure_ai_inference/utils.py
index 321e5725..68711009 100644
--- a/sdk/python/src/openlit/instrumentation/azure_ai_inference/utils.py
+++ b/sdk/python/src/openlit/instrumentation/azure_ai_inference/utils.py
@@ -1,6 +1,7 @@
"""
Azure AI Inference OpenTelemetry instrumentation utility functions
"""
+
import time
from opentelemetry.trace import Status, StatusCode
@@ -17,6 +18,7 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def format_content(messages):
"""
Process a list of messages to extract content.
@@ -29,15 +31,17 @@ def format_content(messages):
if isinstance(content, list):
content_str = ", ".join(
- f'{item["type"]}: {item["text"] if "text" in item else item.get("image_url", "")}'
- if "type" in item else f'text: {item.get("text", "")}'
+ f"{item['type']}: {item['text'] if 'text' in item else item.get('image_url', '')}"
+ if "type" in item
+ else f"text: {item.get('text', '')}"
for item in content
)
- formatted_messages.append(f'{role}: {content_str}')
+ formatted_messages.append(f"{role}: {content_str}")
else:
- formatted_messages.append(f'{role}: {content}')
+ formatted_messages.append(f"{role}: {content}")
+
+ return "\n".join(formatted_messages)
- return '\n'.join(formatted_messages)
def process_chunk(scope, chunk):
"""
@@ -92,25 +96,44 @@ def process_chunk(scope, chunk):
func = tool.get("function", {})
scope._tools[idx] = {
"id": tool["id"],
- "function": {"name": func.get("name", ""), "arguments": func.get("arguments", "")},
- "type": tool.get("type", "function")
+ "function": {
+ "name": func.get("name", ""),
+ "arguments": func.get("arguments", ""),
+ },
+ "type": tool.get("type", "function"),
}
- elif scope._tools[idx] and "function" in tool: # Append args (id is None)
- scope._tools[idx]["function"]["arguments"] += tool["function"].get("arguments", "")
+ elif (
+ scope._tools[idx] and "function" in tool
+ ): # Append args (id is None)
+ scope._tools[idx]["function"]["arguments"] += tool["function"].get(
+ "arguments", ""
+ )
# Handle usage information (typically only in final chunk)
if chunked.get("usage"):
scope._input_tokens = chunked.get("usage").get("prompt_tokens", 0)
scope._output_tokens = chunked.get("usage").get("completion_tokens", 0)
# Handle reasoning tokens if present (optional) - check nested structure
- completion_details = chunked.get("usage", {}).get("completion_tokens_details", {})
+ completion_details = chunked.get("usage", {}).get(
+ "completion_tokens_details", {}
+ )
if "reasoning_tokens" in completion_details:
scope._reasoning_tokens = completion_details.get("reasoning_tokens", 0)
elif "reasoning_tokens" in chunked.get("usage", {}):
scope._reasoning_tokens = chunked.get("usage").get("reasoning_tokens", 0)
-def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream):
+
+def common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream,
+):
"""
Process chat request and generate Telemetry
"""
@@ -121,65 +144,134 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
prompt = format_content(scope._kwargs.get("messages", []))
request_model = scope._kwargs.get("model", "gpt-4o")
- cost = get_chat_model_cost(request_model, pricing_info, scope._input_tokens, scope._output_tokens)
+ cost = get_chat_model_cost(
+ request_model, pricing_info, scope._input_tokens, scope._output_tokens
+ )
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_AZURE_AI_INFERENCE,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_AZURE_AI_INFERENCE,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Request parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY, scope._kwargs.get("frequency_penalty", 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, scope._kwargs.get("max_tokens", -1))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, scope._kwargs.get("presence_penalty", 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get("stop", []))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, scope._kwargs.get("temperature", 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 1.0))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
+ scope._kwargs.get("frequency_penalty", 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
+ scope._kwargs.get("max_tokens", -1),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
+ scope._kwargs.get("presence_penalty", 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get("stop", [])
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
+ scope._kwargs.get("temperature", 1.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 1.0)
+ )
# Span Attributes for Response parameters
scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SERVICE_TIER, scope._response_service_tier)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT, scope._response_service_tier)
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_SERVICE_TIER, scope._response_service_tier
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT,
+ scope._response_service_tier,
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ "text" if isinstance(scope._llmresponse, str) else "json",
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens + scope._output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ scope._input_tokens + scope._output_tokens,
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Reasoning (if present)
if hasattr(scope, "_reasoning_tokens") and scope._reasoning_tokens > 0:
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_REASONING_TOKENS, scope._reasoning_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_REASONING_TOKENS, scope._reasoning_tokens
+ )
# Update total token usage to include reasoning tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
- scope._input_tokens + scope._output_tokens + scope._reasoning_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ scope._input_tokens + scope._output_tokens + scope._reasoning_tokens,
+ )
# Span Attributes for Tools - optimized
if scope._tools:
tools = scope._tools if isinstance(scope._tools, list) else [scope._tools]
- names, ids, args = zip(*[
- (t.get("function", {}).get("name", ""),
- str(t.get("id", "")),
- str(t.get("function", {}).get("arguments", "")))
- for t in tools if isinstance(t, dict) and t
- ]) if tools else ([], [], [])
+ names, ids, args = (
+ zip(
+ *[
+ (
+ t.get("function", {}).get("name", ""),
+ str(t.get("id", "")),
+ str(t.get("function", {}).get("arguments", "")),
+ )
+ for t in tools
+ if isinstance(t, dict) and t
+ ]
+ )
+ if tools
+ else ([], [], [])
+ )
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, ", ".join(filter(None, names)))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, ", ".join(filter(None, ids)))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, ", ".join(filter(None, args)))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_NAME, ", ".join(filter(None, names))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_CALL_ID, ", ".join(filter(None, ids))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_ARGS, ", ".join(filter(None, args))
+ )
# Span Attributes for Content
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
# Add reasoning content if available
if hasattr(scope, "_reasoning_content") and scope._reasoning_content:
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_REASONING, scope._reasoning_content)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_REASONING, scope._reasoning_content
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -199,23 +291,69 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
# Metrics
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
- SemanticConvention.GEN_AI_SYSTEM_AZURE_AI_INFERENCE, scope._server_address, scope._server_port,
- request_model, scope._response_model, environment, application_name, scope._start_time, scope._end_time,
- scope._input_tokens, scope._output_tokens, cost, scope._tbt, scope._ttft)
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_AZURE_AI_INFERENCE,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ scope._output_tokens,
+ cost,
+ scope._tbt,
+ scope._ttft,
+ )
+
-def process_streaming_chat_response(scope, pricing_info, environment, application_name, metrics,
- capture_message_content=False, disable_metrics=False, version=""):
+def process_streaming_chat_response(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="",
+):
"""
Process streaming chat request and generate Telemetry
"""
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=True)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=True,
+ )
+
-def process_chat_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+def process_chat_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process chat request and generate Telemetry
"""
@@ -232,14 +370,20 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
for choice in response_dict.get("choices", [])
)
# Handle reasoning content from non-streaming response
- reasoning_content = response_dict.get("choices", [{}])[0].get("message", {}).get("reasoning_content")
+ reasoning_content = (
+ response_dict.get("choices", [{}])[0]
+ .get("message", {})
+ .get("reasoning_content")
+ )
if reasoning_content:
scope._reasoning_content = reasoning_content
scope._input_tokens = response_dict.get("usage", {}).get("prompt_tokens", 0)
scope._output_tokens = response_dict.get("usage", {}).get("completion_tokens", 0)
# Handle reasoning tokens if present (optional) - check nested structure
- completion_details = response_dict.get("usage", {}).get("completion_tokens_details", {})
+ completion_details = response_dict.get("usage", {}).get(
+ "completion_tokens_details", {}
+ )
if "reasoning_tokens" in completion_details:
scope._reasoning_tokens = completion_details.get("reasoning_tokens", 0)
elif "reasoning_tokens" in response_dict.get("usage", {}):
@@ -248,7 +392,9 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
scope._reasoning_tokens = 0
scope._response_id = response_dict.get("id")
scope._response_model = response_dict.get("model")
- scope._finish_reason = str(response_dict.get("choices", [])[0].get("finish_reason", ""))
+ scope._finish_reason = str(
+ response_dict.get("choices", [])[0].get("finish_reason", "")
+ )
scope._response_service_tier = str(response_dict.get("system_fingerprint", ""))
scope._timestamps = []
scope._ttft, scope._tbt = scope._end_time - scope._start_time, 0
@@ -257,17 +403,37 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
# Handle tool calls
if scope._kwargs.get("tools"):
- scope._tools = response_dict.get("choices", [{}])[0].get("message", {}).get("tool_calls")
+ scope._tools = (
+ response_dict.get("choices", [{}])[0].get("message", {}).get("tool_calls")
+ )
else:
scope._tools = None
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=False)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=False,
+ )
return response
-def common_embedding_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version):
+
+def common_embedding_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+):
"""
Process embedding request and generate Telemetry
"""
@@ -277,29 +443,54 @@ def common_embedding_logic(scope, pricing_info, environment, application_name, m
cost = get_embed_model_cost(request_model, pricing_info, scope._input_tokens)
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_AZURE_AI_INFERENCE,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, False, 0, scope._end_time - scope._start_time, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
+ SemanticConvention.GEN_AI_SYSTEM_AZURE_AI_INFERENCE,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ False,
+ 0,
+ scope._end_time - scope._start_time,
+ version,
+ )
# Span Attributes for Request parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS, [scope._kwargs.get("encoding_format", "float")])
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, scope._kwargs.get("user", ""))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS,
+ [scope._kwargs.get("encoding_format", "float")],
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_USER, scope._kwargs.get("user", "")
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Content
if capture_message_content:
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, str(scope._kwargs.get("input", "")))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_PROMPT,
+ str(scope._kwargs.get("input", "")),
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: str(scope._kwargs.get("input", "")),
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: str(
+ scope._kwargs.get("input", "")
+ ),
},
)
@@ -307,14 +498,39 @@ def common_embedding_logic(scope, pricing_info, environment, application_name, m
# Metrics
if not disable_metrics:
- record_embedding_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
- SemanticConvention.GEN_AI_SYSTEM_AZURE_AI_INFERENCE, scope._server_address, scope._server_port,
- request_model, scope._response_model, environment, application_name, scope._start_time, scope._end_time,
- scope._input_tokens, cost)
-
-def process_embedding_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+ record_embedding_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
+ SemanticConvention.GEN_AI_SYSTEM_AZURE_AI_INFERENCE,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ cost,
+ )
+
+
+def process_embedding_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process embedding request and generate Telemetry
"""
@@ -331,7 +547,15 @@ def process_embedding_response(response, request_model, pricing_info, server_por
scope._server_address, scope._server_port = server_address, server_port
scope._kwargs = kwargs
- common_embedding_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version)
+ common_embedding_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/bedrock/__init__.py b/sdk/python/src/openlit/instrumentation/bedrock/__init__.py
index 23aa5a22..1f8795a3 100644
--- a/sdk/python/src/openlit/instrumentation/bedrock/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/bedrock/__init__.py
@@ -9,6 +9,7 @@ from openlit.instrumentation.bedrock.bedrock import converse, converse_stream
_instruments = ("boto3 >= 1.34.138",)
+
class BedrockInstrumentor(BaseInstrumentor):
"""
An instrumentor for AWS Bedrock client library.
@@ -31,16 +32,32 @@ class BedrockInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"botocore.client",
"ClientCreator.create_client",
- converse(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics),
+ converse(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# streaming
wrap_function_wrapper(
"botocore.client",
"ClientCreator.create_client",
- converse_stream(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics),
+ converse_stream(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/bedrock/bedrock.py b/sdk/python/src/openlit/instrumentation/bedrock/bedrock.py
index b617e325..0467714a 100644
--- a/sdk/python/src/openlit/instrumentation/bedrock/bedrock.py
+++ b/sdk/python/src/openlit/instrumentation/bedrock/bedrock.py
@@ -4,10 +4,7 @@ Module for monitoring Amazon Bedrock API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.bedrock.utils import (
process_chunk,
process_chat_response,
@@ -15,7 +12,17 @@ from openlit.instrumentation.bedrock.utils import (
)
from openlit.semcov import SemanticConvention
-def converse(version, environment, application_name, tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def converse(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for AWS Bedrock converse calls.
"""
@@ -30,10 +37,14 @@ def converse(version, environment, application_name, tracer, pricing_info, captu
Wraps the individual converse method call.
"""
- server_address, server_port = set_server_address_and_port(instance, "aws.amazon.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "aws.amazon.com", 443
+ )
request_model = method_kwargs.get("modelId", "amazon.titan-text-express-v1")
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
@@ -56,7 +67,7 @@ def converse(version, environment, application_name, tracer, pricing_info, captu
disable_metrics=disable_metrics,
version=version,
llm_config=llm_config,
- **method_kwargs
+ **method_kwargs,
)
except Exception as e:
@@ -70,13 +81,25 @@ def converse(version, environment, application_name, tracer, pricing_info, captu
# Replace the original method with the instrumented one
if kwargs.get("service_name") == "bedrock-runtime":
original_invoke_model = client.converse
- client.converse = lambda *args, **kwargs: converse_wrapper(original_invoke_model, *args, **kwargs)
+ client.converse = lambda *args, **kwargs: converse_wrapper(
+ original_invoke_model, *args, **kwargs
+ )
return client
return wrapper
-def converse_stream(version, environment, application_name, tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def converse_stream(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for AWS Bedrock converse_stream calls.
"""
@@ -87,15 +110,15 @@ def converse_stream(version, environment, application_name, tracer, pricing_info
"""
def __init__(
- self,
- wrapped_response,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped_response,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped_response = wrapped_response
# Extract the actual stream iterator from the response
if isinstance(wrapped_response, dict) and "stream" in wrapped_response:
@@ -159,7 +182,9 @@ def converse_stream(version, environment, application_name, tracer, pricing_info
except StopIteration:
try:
llm_config = self._kwargs.get("inferenceConfig", {})
- with tracer.start_as_current_span(self._span_name, kind=SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -169,7 +194,7 @@ def converse_stream(version, environment, application_name, tracer, pricing_info
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- llm_config=llm_config
+ llm_config=llm_config,
)
except Exception as e:
@@ -187,16 +212,27 @@ def converse_stream(version, environment, application_name, tracer, pricing_info
Wraps the individual converse_stream method call.
"""
- server_address, server_port = set_server_address_and_port(instance, "aws.amazon.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "aws.amazon.com", 443
+ )
request_model = method_kwargs.get("modelId", "amazon.titan-text-express-v1")
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
+ )
# Get the streaming response
stream_response = original_method(*method_args, **method_kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(stream_response, span, span_name, method_kwargs, server_address, server_port)
+ return TracedSyncStream(
+ stream_response,
+ span,
+ span_name,
+ method_kwargs,
+ server_address,
+ server_port,
+ )
# Get the original client instance from the wrapper
client = wrapped(*args, **kwargs)
@@ -204,7 +240,9 @@ def converse_stream(version, environment, application_name, tracer, pricing_info
# Replace the original method with the instrumented one
if kwargs.get("service_name") == "bedrock-runtime":
original_stream_model = client.converse_stream
- client.converse_stream = lambda *args, **kwargs: converse_stream_wrapper(original_stream_model, *args, **kwargs)
+ client.converse_stream = lambda *args, **kwargs: converse_stream_wrapper(
+ original_stream_model, *args, **kwargs
+ )
return client
diff --git a/sdk/python/src/openlit/instrumentation/bedrock/utils.py b/sdk/python/src/openlit/instrumentation/bedrock/utils.py
index 65aa429c..c054f9cc 100644
--- a/sdk/python/src/openlit/instrumentation/bedrock/utils.py
+++ b/sdk/python/src/openlit/instrumentation/bedrock/utils.py
@@ -1,6 +1,7 @@
"""
AWS Bedrock OpenTelemetry instrumentation utility functions
"""
+
import time
from opentelemetry.trace import Status, StatusCode
@@ -12,10 +13,11 @@ from openlit.__helpers import (
get_chat_model_cost,
record_completion_metrics,
common_span_attributes,
- handle_exception
+ handle_exception,
)
from openlit.semcov import SemanticConvention
+
def format_content(messages):
"""
Format the messages into a string for span events.
@@ -52,6 +54,7 @@ def format_content(messages):
return "\n".join(formatted_messages)
+
def process_chunk(self, chunk):
"""
Process a chunk of response data and update state.
@@ -92,8 +95,19 @@ def process_chunk(self, chunk):
self._output_tokens = usage.get("outputTokens", 0)
self._end_time = end_time
-def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, llm_config, is_stream):
+
+def common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ llm_config,
+ is_stream,
+):
"""
Process chat request and generate Telemetry
"""
@@ -105,13 +119,26 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
formatted_messages = format_content(scope._kwargs.get("messages", []))
request_model = scope._kwargs.get("modelId", "amazon.titan-text-express-v1")
- cost = get_chat_model_cost(request_model, pricing_info, scope._input_tokens, scope._output_tokens)
+ cost = get_chat_model_cost(
+ request_model, pricing_info, scope._input_tokens, scope._output_tokens
+ )
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_AWS_BEDROCK,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_AWS_BEDROCK,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Bedrock-specific attributes from llm_config
bedrock_attributes = [
@@ -132,19 +159,35 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
# Span Attributes for Response parameters
scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ "text" if isinstance(scope._llmresponse, str) else "json",
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens + scope._output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ scope._input_tokens + scope._output_tokens,
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Content
if capture_message_content:
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, formatted_messages)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_PROMPT, formatted_messages
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -164,13 +207,37 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
# Record metrics
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_AWS_BEDROCK,
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, scope._output_tokens,
- cost, scope._tbt, scope._ttft)
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_AWS_BEDROCK,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ scope._output_tokens,
+ cost,
+ scope._tbt,
+ scope._ttft,
+ )
-def process_streaming_chat_response(scope, pricing_info, environment, application_name, metrics,
- capture_message_content=False, disable_metrics=False, version="", llm_config=None):
+
+def process_streaming_chat_response(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="",
+ llm_config=None,
+):
"""
Process streaming chat response and generate telemetry.
"""
@@ -179,15 +246,40 @@ def process_streaming_chat_response(scope, pricing_info, environment, applicatio
if llm_config is None:
llm_config = {}
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, llm_config, is_stream=True)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ llm_config,
+ is_stream=True,
+ )
except Exception as e:
handle_exception(scope._span, e)
raise
-def process_chat_response(response, request_model, pricing_info, server_port, server_address, environment,
- application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", llm_config=None, **kwargs):
+
+def process_chat_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ llm_config=None,
+ **kwargs,
+):
"""
Process non-streaming chat response and generate telemetry.
"""
@@ -202,8 +294,15 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
scope._start_time = start_time
scope._end_time = time.time()
scope._span = span
- scope._llmresponse = response_dict.get("output", {}).get("message", {}).get("content", [{}])[0].get("text", "")
- scope._response_role = response_dict.get("output", {}).get("message", {}).get("role", "assistant")
+ scope._llmresponse = (
+ response_dict.get("output", {})
+ .get("message", {})
+ .get("content", [{}])[0]
+ .get("text", "")
+ )
+ scope._response_role = (
+ response_dict.get("output", {}).get("message", {}).get("role", "assistant")
+ )
scope._input_tokens = response_dict.get("usage", {}).get("inputTokens", 0)
scope._output_tokens = response_dict.get("usage", {}).get("outputTokens", 0)
scope._response_model = request_model
@@ -214,8 +313,18 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
scope._server_address, scope._server_port = server_address, server_port
scope._kwargs = kwargs
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, llm_config, is_stream=False)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ llm_config,
+ is_stream=False,
+ )
return response
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/chroma/__init__.py b/sdk/python/src/openlit/instrumentation/chroma/__init__.py
index 509877e0..e561ba98 100644
--- a/sdk/python/src/openlit/instrumentation/chroma/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/chroma/__init__.py
@@ -11,6 +11,7 @@ from openlit.instrumentation.chroma.chroma import general_wrap
_instruments = ("chromadb >= 0.4.0",)
+
class ChromaInstrumentor(BaseInstrumentor):
"""
An instrumentor for ChromaDB client library.
@@ -33,57 +34,129 @@ class ChromaInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"chromadb.db",
"DB.create_collection",
- general_wrap("chroma.create_collection", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ general_wrap(
+ "chroma.create_collection",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"chromadb",
"Collection.add",
- general_wrap("chroma.add", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ general_wrap(
+ "chroma.add",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"chromadb",
"Collection.get",
- general_wrap("chroma.get", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ general_wrap(
+ "chroma.get",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"chromadb",
"Collection.peek",
- general_wrap("chroma.peek", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ general_wrap(
+ "chroma.peek",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"chromadb",
"Collection.query",
- general_wrap("chroma.query", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ general_wrap(
+ "chroma.query",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"chromadb",
"Collection.update",
- general_wrap("chroma.update", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ general_wrap(
+ "chroma.update",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"chromadb",
"Collection.upsert",
- general_wrap("chroma.upsert", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ general_wrap(
+ "chroma.upsert",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"chromadb",
"Collection.delete",
- general_wrap("chroma.delete", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ general_wrap(
+ "chroma.delete",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/chroma/chroma.py b/sdk/python/src/openlit/instrumentation/chroma/chroma.py
index eebfabcf..b61c47a2 100644
--- a/sdk/python/src/openlit/instrumentation/chroma/chroma.py
+++ b/sdk/python/src/openlit/instrumentation/chroma/chroma.py
@@ -12,8 +12,18 @@ from openlit.instrumentation.chroma.utils import (
set_server_address_and_port,
)
-def general_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def general_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for ChromaDB function calls.
"""
@@ -43,9 +53,22 @@ def general_wrap(gen_ai_endpoint, version, environment, application_name,
try:
# Process response and generate telemetry
response = process_vectordb_response(
- response, db_operation, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version, instance, args, endpoint=gen_ai_endpoint, **kwargs
+ response,
+ db_operation,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ args,
+ endpoint=gen_ai_endpoint,
+ **kwargs,
)
except Exception as e:
@@ -53,5 +76,4 @@ def general_wrap(gen_ai_endpoint, version, environment, application_name,
return response
-
return wrapper
diff --git a/sdk/python/src/openlit/instrumentation/chroma/utils.py b/sdk/python/src/openlit/instrumentation/chroma/utils.py
index 1f3fcf14..68a4d764 100644
--- a/sdk/python/src/openlit/instrumentation/chroma/utils.py
+++ b/sdk/python/src/openlit/instrumentation/chroma/utils.py
@@ -1,6 +1,7 @@
"""
ChromaDB OpenTelemetry instrumentation utility functions
"""
+
import time
from urllib.parse import urlparse
from opentelemetry.trace import Status, StatusCode
@@ -23,12 +24,14 @@ DB_OPERATION_MAP = {
"chroma.delete": SemanticConvention.DB_OPERATION_DELETE,
}
+
def object_count(obj):
"""
Counts length of object if it exists, else returns 0.
"""
return len(obj) if obj else 0
+
def set_server_address_and_port(instance):
"""
Extracts server address and port from ChromaDB client instance.
@@ -74,8 +77,18 @@ def set_server_address_and_port(instance):
return server_address, server_port
-def common_vectordb_logic(scope, environment, application_name,
- metrics, capture_message_content, disable_metrics, version, instance=None, endpoint=None):
+
+def common_vectordb_logic(
+ scope,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance=None,
+ endpoint=None,
+):
"""
Process vector database request and generate telemetry.
"""
@@ -83,12 +96,22 @@ def common_vectordb_logic(scope, environment, application_name,
scope._end_time = time.time()
# Set common database span attributes using helper
- common_db_span_attributes(scope, SemanticConvention.DB_SYSTEM_CHROMA, scope._server_address, scope._server_port,
- environment, application_name, version)
+ common_db_span_attributes(
+ scope,
+ SemanticConvention.DB_SYSTEM_CHROMA,
+ scope._server_address,
+ scope._server_port,
+ environment,
+ application_name,
+ version,
+ )
# Set DB operation specific attributes
scope._span.set_attribute(SemanticConvention.DB_OPERATION_NAME, scope._db_operation)
- scope._span.set_attribute(SemanticConvention.DB_CLIENT_OPERATION_DURATION, scope._end_time - scope._start_time)
+ scope._span.set_attribute(
+ SemanticConvention.DB_CLIENT_OPERATION_DURATION,
+ scope._end_time - scope._start_time,
+ )
# Set collection name from instance
if hasattr(instance, "name"):
@@ -97,13 +120,19 @@ def common_vectordb_logic(scope, environment, application_name,
# Set Create Collection operation specific attributes
if scope._db_operation == SemanticConvention.DB_OPERATION_CREATE_COLLECTION:
# Standard database attributes
- collection_name = scope._kwargs.get("name") or (scope._args[0] if scope._args else "unknown")
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ collection_name = scope._kwargs.get("name") or (
+ scope._args[0] if scope._args else "unknown"
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
# Vector database specific attributes (extensions)
metadata = scope._kwargs.get("metadata", {})
if metadata:
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_METADATA, str(metadata))
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_METADATA, str(metadata)
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_INSERT:
collection_name = getattr(instance, "name", "unknown")
@@ -111,14 +140,20 @@ def common_vectordb_logic(scope, environment, application_name,
# Standard database attributes
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(query))
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, object_count(query))
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_COUNT, object_count(query)
+ )
# Vector database specific attributes (extensions)
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
f"ids={query} "
- f"documents={scope._kwargs.get('documents', [])}")
+ f"documents={scope._kwargs.get('documents', [])}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_GET:
collection_name = getattr(instance, "name", "unknown")
@@ -130,17 +165,25 @@ def common_vectordb_logic(scope, environment, application_name,
# Standard database attributes
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(query))
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, object_count(query))
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_COUNT, object_count(query)
+ )
# Vector database specific attributes (extensions)
- scope._span.set_attribute(SemanticConvention.DB_FILTER, str(scope._kwargs.get("where", "")))
+ scope._span.set_attribute(
+ SemanticConvention.DB_FILTER, str(scope._kwargs.get("where", ""))
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
f"ids={query} "
f"limit={scope._kwargs.get('limit', 'None')} "
- f"offset={scope._kwargs.get('offset', 'None')}")
+ f"offset={scope._kwargs.get('offset', 'None')}",
+ )
elif endpoint == "chroma.query":
query_texts = scope._kwargs.get("query_texts", [])
@@ -156,24 +199,39 @@ def common_vectordb_logic(scope, environment, application_name,
# Standard database attributes
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, query_content)
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
# Vector database specific attributes (extensions)
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_QUERY_TOP_K, scope._kwargs.get("n_results", 10))
- scope._span.set_attribute(SemanticConvention.DB_FILTER, str(scope._kwargs.get("where", "")))
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_QUERY_TOP_K,
+ scope._kwargs.get("n_results", 10),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_FILTER, str(scope._kwargs.get("where", ""))
+ )
# Extract response metrics if available
if scope._response:
# Get number of results returned
if hasattr(scope._response, "get") and scope._response.get("ids"):
- returned_rows = object_count(scope._response["ids"][0]) if scope._response["ids"] else 0
- scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS, returned_rows)
-
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ returned_rows = (
+ object_count(scope._response["ids"][0])
+ if scope._response["ids"]
+ else 0
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_RESPONSE_RETURNED_ROWS, returned_rows
+ )
+
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
f"n_results={scope._kwargs.get('n_results', 10)} "
f"{query_content} "
- f"filter={scope._kwargs.get('where', 'None')}")
+ f"filter={scope._kwargs.get('where', 'None')}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_UPDATE:
collection_name = getattr(instance, "name", "unknown")
@@ -181,16 +239,22 @@ def common_vectordb_logic(scope, environment, application_name,
# Standard database attributes
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(query))
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, object_count(query))
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_COUNT, object_count(query)
+ )
# Vector database specific attributes (extensions)
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
f"ids={query} "
f"embeddings={scope._kwargs.get('embeddings', 'None')} "
f"metadatas={scope._kwargs.get('metadatas', 'None')} "
- f"documents={scope._kwargs.get('documents', 'None')}")
+ f"documents={scope._kwargs.get('documents', 'None')}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_UPSERT:
collection_name = getattr(instance, "name", "unknown")
@@ -198,16 +262,22 @@ def common_vectordb_logic(scope, environment, application_name,
# Standard database attributes
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(query))
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, object_count(query))
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_COUNT, object_count(query)
+ )
# Vector database specific attributes (extensions)
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
f"ids={query} "
f"embeddings={scope._kwargs.get('embeddings', 'None')} "
f"metadatas={scope._kwargs.get('metadatas', 'None')} "
- f"documents={scope._kwargs.get('documents', 'None')}")
+ f"documents={scope._kwargs.get('documents', 'None')}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_DELETE:
collection_name = getattr(instance, "name", "unknown")
@@ -215,16 +285,24 @@ def common_vectordb_logic(scope, environment, application_name,
# Standard database attributes
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(query))
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, object_count(query))
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_COUNT, object_count(query)
+ )
# Vector database specific attributes (extensions)
- scope._span.set_attribute(SemanticConvention.DB_FILTER, str(scope._kwargs.get("where", "")))
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_FILTER, str(scope._kwargs.get("where", ""))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
f"ids={query} "
f"filter={scope._kwargs.get('where', 'None')} "
- f"delete_all={scope._kwargs.get('delete_all', False)}")
+ f"delete_all={scope._kwargs.get('delete_all', False)}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_PEEK:
collection_name = getattr(instance, "name", "unknown")
@@ -232,25 +310,54 @@ def common_vectordb_logic(scope, environment, application_name,
# Standard database attributes
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, query)
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, scope._kwargs.get("limit", ""))
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_COUNT, scope._kwargs.get("limit", "")
+ )
# Vector database specific attributes (extensions)
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
- f"limit={scope._kwargs.get('limit', 'None')}")
+ f"limit={scope._kwargs.get('limit', 'None')}",
+ )
scope._span.set_status(Status(StatusCode.OK))
# Record metrics using helper
if not disable_metrics:
- record_db_metrics(metrics, SemanticConvention.DB_SYSTEM_CHROMA, scope._server_address, scope._server_port,
- environment, application_name, scope._start_time, scope._end_time)
-
-def process_vectordb_response(response, db_operation, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content=False, disable_metrics=False,
- version="1.0.0", instance=None, args=None, endpoint=None, **kwargs):
+ record_db_metrics(
+ metrics,
+ SemanticConvention.DB_SYSTEM_CHROMA,
+ scope._server_address,
+ scope._server_port,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ )
+
+
+def process_vectordb_response(
+ response,
+ db_operation,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ instance=None,
+ args=None,
+ endpoint=None,
+ **kwargs,
+):
"""
Process vector database response and generate telemetry following OpenTelemetry conventions.
"""
@@ -266,7 +373,16 @@ def process_vectordb_response(response, db_operation, server_address, server_por
scope._server_address = server_address
scope._server_port = server_port
- common_vectordb_logic(scope, environment, application_name,
- metrics, capture_message_content, disable_metrics, version, instance, endpoint)
+ common_vectordb_logic(
+ scope,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ endpoint,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/cohere/__init__.py b/sdk/python/src/openlit/instrumentation/cohere/__init__.py
index 5eb79ae6..a700225a 100644
--- a/sdk/python/src/openlit/instrumentation/cohere/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/cohere/__init__.py
@@ -5,19 +5,16 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.cohere.cohere import (
- chat,
- chat_stream,
- embed
-)
+from openlit.instrumentation.cohere.cohere import chat, chat_stream, embed
from openlit.instrumentation.cohere.async_cohere import (
async_chat,
async_chat_stream,
- async_embed
+ async_embed,
)
_instruments = ("cohere >= 5.14.0",)
+
class CohereInstrumentor(BaseInstrumentor):
"""
An instrumentor for Cohere client library.
@@ -40,48 +37,96 @@ class CohereInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"cohere.client_v2",
"ClientV2.chat",
- chat(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# sync chat streaming
wrap_function_wrapper(
"cohere.client_v2",
"ClientV2.chat_stream",
- chat_stream(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ chat_stream(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# sync embeddings
wrap_function_wrapper(
"cohere.client_v2",
"ClientV2.embed",
- embed(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ embed(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# async chat completions
wrap_function_wrapper(
"cohere.client_v2",
"AsyncClientV2.chat",
- async_chat(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# async chat streaming
wrap_function_wrapper(
"cohere.client_v2",
"AsyncClientV2.chat_stream",
- async_chat_stream(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_chat_stream(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# async embeddings
wrap_function_wrapper(
"cohere.client_v2",
"AsyncClientV2.embed",
- async_embed(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_embed(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/cohere/async_cohere.py b/sdk/python/src/openlit/instrumentation/cohere/async_cohere.py
index aab3f5b2..d3ac1708 100644
--- a/sdk/python/src/openlit/instrumentation/cohere/async_cohere.py
+++ b/sdk/python/src/openlit/instrumentation/cohere/async_cohere.py
@@ -16,8 +16,17 @@ from openlit.instrumentation.cohere.utils import (
)
from openlit.semcov import SemanticConvention
-def async_chat(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI chat function call
"""
@@ -27,7 +36,9 @@ def async_chat(version, environment, application_name,
Wraps the GenAI chat function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.cohere.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.cohere.com", 443
+ )
request_model = kwargs.get("model", "command-r-plus-08-2024")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -49,15 +60,24 @@ def async_chat(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
return response
return wrapper
-def async_chat_stream(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_chat_stream(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI chat_stream function call
"""
@@ -68,15 +88,15 @@ def async_chat_stream(version, environment, application_name,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -120,7 +140,9 @@ def async_chat_stream(version, environment, application_name,
return chunk
except StopAsyncIteration:
try:
- with tracer.start_as_current_span(self._span_name, kind= SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -129,7 +151,7 @@ def async_chat_stream(version, environment, application_name,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -142,7 +164,9 @@ def async_chat_stream(version, environment, application_name,
Wraps the GenAI chat_stream function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.cohere.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.cohere.com", 443
+ )
request_model = kwargs.get("model", "command-r-plus-08-2024")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -151,12 +175,23 @@ def async_chat_stream(version, environment, application_name,
awaited_wrapped = await wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedAsyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedAsyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
return wrapper
-def async_embed(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_embed(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI embedding function call
"""
@@ -166,10 +201,14 @@ def async_embed(version, environment, application_name,
Wraps the GenAI embedding function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.cohere.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.cohere.com", 443
+ )
request_model = kwargs.get("model", "embed-english-v3.0")
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
@@ -190,7 +229,7 @@ def async_embed(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/cohere/cohere.py b/sdk/python/src/openlit/instrumentation/cohere/cohere.py
index d74d27fb..eeda7363 100644
--- a/sdk/python/src/openlit/instrumentation/cohere/cohere.py
+++ b/sdk/python/src/openlit/instrumentation/cohere/cohere.py
@@ -16,8 +16,17 @@ from openlit.instrumentation.cohere.utils import (
)
from openlit.semcov import SemanticConvention
-def chat(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI chat function call
"""
@@ -27,7 +36,9 @@ def chat(version, environment, application_name,
Wraps the GenAI chat function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.cohere.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.cohere.com", 443
+ )
request_model = kwargs.get("model", "command-r-plus-08-2024")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -49,15 +60,24 @@ def chat(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
return response
return wrapper
-def chat_stream(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def chat_stream(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI chat_stream function call
"""
@@ -68,15 +88,15 @@ def chat_stream(version, environment, application_name,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -120,7 +140,9 @@ def chat_stream(version, environment, application_name,
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(self._span_name, kind= SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -129,7 +151,7 @@ def chat_stream(version, environment, application_name,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -142,7 +164,9 @@ def chat_stream(version, environment, application_name,
Wraps the GenAI chat_stream function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.cohere.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.cohere.com", 443
+ )
request_model = kwargs.get("model", "command-r-plus-08-2024")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -151,12 +175,23 @@ def chat_stream(version, environment, application_name,
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedSyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
return wrapper
-def embed(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def embed(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI embedding function call
"""
@@ -166,10 +201,14 @@ def embed(version, environment, application_name,
Wraps the GenAI embedding function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.cohere.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.cohere.com", 443
+ )
request_model = kwargs.get("model", "embed-english-v3.0")
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
@@ -190,7 +229,7 @@ def embed(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/cohere/utils.py b/sdk/python/src/openlit/instrumentation/cohere/utils.py
index a368eafe..c4554025 100644
--- a/sdk/python/src/openlit/instrumentation/cohere/utils.py
+++ b/sdk/python/src/openlit/instrumentation/cohere/utils.py
@@ -1,6 +1,7 @@
"""
Cohere OpenTelemetry instrumentation utility functions
"""
+
import time
from opentelemetry.trace import Status, StatusCode
@@ -17,6 +18,7 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def format_content(messages):
"""
Process a list of messages to extract content.
@@ -35,8 +37,9 @@ def format_content(messages):
if isinstance(content, list):
content_str = ", ".join(
- f'{item["type"]}: {item["text"] if "text" in item else item.get("image_url", "")}'
- if "type" in item else f'text: {item.get("text", "")}'
+ f"{item['type']}: {item['text'] if 'text' in item else item.get('image_url', '')}"
+ if "type" in item
+ else f"text: {item.get('text', '')}"
for item in content
)
formatted_messages.append(f"{role}: {content_str}")
@@ -45,6 +48,7 @@ def format_content(messages):
return "\n".join(formatted_messages)
+
def process_chunk(scope, chunk):
"""
Process a chunk of response data and update state.
@@ -65,13 +69,17 @@ def process_chunk(scope, chunk):
scope._response_id = chunked.get("id")
if chunked.get("type") == "content-delta":
- content = chunked.get("delta", {}).get("message", {}).get("content", {}).get("text")
+ content = (
+ chunked.get("delta", {}).get("message", {}).get("content", {}).get("text")
+ )
if content:
scope._llmresponse += content
# Handle tool plan deltas
if chunked.get("type") == "tool-plan-delta":
- tool_plan_text = chunked.get("delta", {}).get("message", {}).get("tool_plan", "")
+ tool_plan_text = (
+ chunked.get("delta", {}).get("message", {}).get("tool_plan", "")
+ )
if tool_plan_text:
if not hasattr(scope, "_tool_plan"):
scope._tool_plan = ""
@@ -94,8 +102,8 @@ def process_chunk(scope, chunk):
"type": tool_call.get("type", "function"),
"function": {
"name": tool_call.get("function", {}).get("name", ""),
- "arguments": ""
- }
+ "arguments": "",
+ },
}
# Handle tool call deltas (arguments)
@@ -103,7 +111,13 @@ def process_chunk(scope, chunk):
if hasattr(scope, "_tools") and scope._tools:
index = chunked.get("index", 0)
if index < len(scope._tools):
- arguments = chunked.get("delta", {}).get("message", {}).get("tool_calls", {}).get("function", {}).get("arguments", "")
+ arguments = (
+ chunked.get("delta", {})
+ .get("message", {})
+ .get("tool_calls", {})
+ .get("function", {})
+ .get("arguments", "")
+ )
if arguments:
scope._tools[index]["function"]["arguments"] += arguments
@@ -115,8 +129,18 @@ def process_chunk(scope, chunk):
scope._output_tokens = usage.get("output_tokens", 0)
scope._end_time = time.time()
-def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream):
+
+def common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream,
+):
"""
Process chat request and generate Telemetry
"""
@@ -127,58 +151,123 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
prompt = format_content(scope._kwargs.get("messages", []))
request_model = scope._kwargs.get("model", "command-r-plus-08-2024")
- cost = get_chat_model_cost(request_model, pricing_info, scope._input_tokens, scope._output_tokens)
+ cost = get_chat_model_cost(
+ request_model, pricing_info, scope._input_tokens, scope._output_tokens
+ )
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_COHERE,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_COHERE,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Request parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED, scope._kwargs.get("seed", ""))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY, scope._kwargs.get("frequency_penalty", 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, scope._kwargs.get("max_tokens", -1))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, scope._kwargs.get("presence_penalty", 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get("stop_sequences", []))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, scope._kwargs.get("temperature", 0.3))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_K, scope._kwargs.get("k", 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("p", 1.0))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_SEED, scope._kwargs.get("seed", "")
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
+ scope._kwargs.get("frequency_penalty", 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
+ scope._kwargs.get("max_tokens", -1),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
+ scope._kwargs.get("presence_penalty", 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES,
+ scope._kwargs.get("stop_sequences", []),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
+ scope._kwargs.get("temperature", 0.3),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_K, scope._kwargs.get("k", 1.0)
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("p", 1.0)
+ )
# Span Attributes for Response parameters
scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ "text" if isinstance(scope._llmresponse, str) else "json",
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens + scope._output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ scope._input_tokens + scope._output_tokens,
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Tools - optimized
if scope._tools:
tools = scope._tools if isinstance(scope._tools, list) else [scope._tools]
- names, ids, args = zip(*[
- (t.get("function", {}).get("name", ""),
- str(t.get("id", "")),
- str(t.get("function", {}).get("arguments", "")))
- for t in tools if isinstance(t, dict) and t
- ]) if tools else ([], [], [])
+ names, ids, args = (
+ zip(
+ *[
+ (
+ t.get("function", {}).get("name", ""),
+ str(t.get("id", "")),
+ str(t.get("function", {}).get("arguments", "")),
+ )
+ for t in tools
+ if isinstance(t, dict) and t
+ ]
+ )
+ if tools
+ else ([], [], [])
+ )
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, ", ".join(filter(None, names)))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, ", ".join(filter(None, ids)))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, ", ".join(filter(None, args)))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_NAME, ", ".join(filter(None, names))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_CALL_ID, ", ".join(filter(None, ids))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_ARGS, ", ".join(filter(None, args))
+ )
# Span Attributes for Tool Plan (Cohere specific)
if hasattr(scope, "_tool_plan") and scope._tool_plan:
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_REASONING, scope._tool_plan)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_REASONING, scope._tool_plan
+ )
# Span Attributes for Content
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -198,23 +287,69 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
# Metrics
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_COHERE,
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, scope._output_tokens,
- cost, scope._tbt, scope._ttft)
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_COHERE,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ scope._output_tokens,
+ cost,
+ scope._tbt,
+ scope._ttft,
+ )
-def process_streaming_chat_response(scope, pricing_info, environment, application_name, metrics,
- capture_message_content=False, disable_metrics=False, version=""):
+
+def process_streaming_chat_response(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="",
+):
"""
Process streaming chat request and generate Telemetry
"""
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=True)
-
-def process_chat_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=True,
+ )
+
+
+def process_chat_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process chat request and generate Telemetry
"""
@@ -235,8 +370,12 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
scope._llmresponse = ""
scope._response_id = response_dict.get("id")
scope._response_model = request_model
- scope._input_tokens = response_dict.get("usage", {}).get("billed_units", {}).get("input_tokens", 0)
- scope._output_tokens = response_dict.get("usage", {}).get("billed_units", {}).get("output_tokens", 0)
+ scope._input_tokens = (
+ response_dict.get("usage", {}).get("billed_units", {}).get("input_tokens", 0)
+ )
+ scope._output_tokens = (
+ response_dict.get("usage", {}).get("billed_units", {}).get("output_tokens", 0)
+ )
scope._timestamps = []
scope._ttft, scope._tbt = scope._end_time - scope._start_time, 0
scope._server_address, scope._server_port = server_address, server_port
@@ -252,13 +391,31 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
scope._tools = None
scope._tool_plan = ""
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=False)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=False,
+ )
return response
-def common_embedding_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version):
+
+def common_embedding_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+):
"""
Process embedding request and generate Telemetry
"""
@@ -269,19 +426,39 @@ def common_embedding_logic(scope, pricing_info, environment, application_name, m
cost = get_embed_model_cost(request_model, pricing_info, scope._input_tokens)
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_COHERE,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, False, 0, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
+ SemanticConvention.GEN_AI_SYSTEM_COHERE,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ False,
+ 0,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Request parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS, scope._kwargs.get("embedding_types", ["float"]))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS,
+ scope._kwargs.get("embedding_types", ["float"]),
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, scope._response_type)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE, scope._response_type
+ )
# Span Attributes for Content
if capture_message_content:
@@ -299,13 +476,39 @@ def common_embedding_logic(scope, pricing_info, environment, application_name, m
# Metrics
if not disable_metrics:
- record_embedding_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_COHERE,
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, cost)
+ record_embedding_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
+ SemanticConvention.GEN_AI_SYSTEM_COHERE,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ cost,
+ )
+
-def process_embedding_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+def process_embedding_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process embedding request and generate Telemetry
"""
@@ -317,14 +520,24 @@ def process_embedding_response(response, request_model, pricing_info, server_por
scope._start_time = start_time
scope._end_time = time.time()
scope._span = span
- scope._input_tokens = response_dict.get("meta", {}).get("billed_units", {}).get("input_tokens", 0)
+ scope._input_tokens = (
+ response_dict.get("meta", {}).get("billed_units", {}).get("input_tokens", 0)
+ )
scope._response_model = request_model
scope._response_type = response_dict.get("response_type", "")
scope._ttft = scope._end_time - scope._start_time
scope._server_address, scope._server_port = server_address, server_port
scope._kwargs = kwargs
- common_embedding_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version)
+ common_embedding_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/controlflow/__init__.py b/sdk/python/src/openlit/instrumentation/controlflow/__init__.py
index a7b97f7c..01009753 100644
--- a/sdk/python/src/openlit/instrumentation/controlflow/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/controlflow/__init__.py
@@ -6,12 +6,11 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.controlflow.controlflow import (
- wrap_controlflow
-)
+from openlit.instrumentation.controlflow.controlflow import wrap_controlflow
_instruments = ("controlflow >= 0.3.2",)
+
class ControlFlowInstrumentor(BaseInstrumentor):
"""
An instrumentor for controlflow's client library.
@@ -33,22 +32,49 @@ class ControlFlowInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"controlflow.agents.agent",
"Agent.__init__",
- wrap_controlflow("controlflow.create_agent", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ wrap_controlflow(
+ "controlflow.create_agent",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"controlflow.tasks.task",
"Task.__init__",
- wrap_controlflow("controlflow.create_task", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ wrap_controlflow(
+ "controlflow.create_task",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"controlflow",
"run",
- wrap_controlflow("controlflow.run", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ wrap_controlflow(
+ "controlflow.run",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/controlflow/controlflow.py b/sdk/python/src/openlit/instrumentation/controlflow/controlflow.py
index 5e011f95..7b59d8e4 100644
--- a/sdk/python/src/openlit/instrumentation/controlflow/controlflow.py
+++ b/sdk/python/src/openlit/instrumentation/controlflow/controlflow.py
@@ -5,15 +5,29 @@ Module for monitoring controlflow.
import logging
from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from openlit.__helpers import handle_exception
from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def wrap_controlflow(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def wrap_controlflow(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Creates a wrapper around a function call to trace and log its execution metrics.
@@ -55,48 +69,67 @@ def wrap_controlflow(gen_ai_endpoint, version, environment, application_name,
errors are handled and logged appropriately.
"""
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(
+ gen_ai_endpoint, kind=SpanKind.CLIENT
+ ) as span:
response = wrapped(*args, **kwargs)
try:
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,
- gen_ai_endpoint)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_CONTROLFLOW)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT)
+ span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM,
+ SemanticConvention.GEN_AI_SYSTEM_CONTROLFLOW,
+ )
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
+ span.set_attribute(SERVICE_NAME, application_name)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
+ )
if gen_ai_endpoint == "controlflow.create_agent":
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ROLE,
- instance.name)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_INSTRUCTIONS,
- kwargs.get("instructions", ""))
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TOOLS,
- str(kwargs.get("tools", "")))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_ROLE, instance.name
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_INSTRUCTIONS,
+ kwargs.get("instructions", ""),
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_TOOLS,
+ str(kwargs.get("tools", "")),
+ )
try:
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- instance.model.model_name)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MODEL,
+ instance.model.model_name,
+ )
except:
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- kwargs.get("model", "openai/gpt-4o-mini"))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MODEL,
+ kwargs.get("model", "openai/gpt-4o-mini"),
+ )
elif gen_ai_endpoint == "controlflow.create_task":
- if kwargs.get("objective","") == "":
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_GOAL,
- str(args[0]))
+ if kwargs.get("objective", "") == "":
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_GOAL, str(args[0])
+ )
else:
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_GOAL,
- kwargs.get("objective",""))
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_INSTRUCTIONS,
- kwargs.get("instructions", ""))
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_CONTEXT,
- str(kwargs.get("context", "")))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_GOAL,
+ kwargs.get("objective", ""),
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_INSTRUCTIONS,
+ kwargs.get("instructions", ""),
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_CONTEXT,
+ str(kwargs.get("context", "")),
+ )
span.set_status(Status(StatusCode.OK))
diff --git a/sdk/python/src/openlit/instrumentation/crawl4ai/__init__.py b/sdk/python/src/openlit/instrumentation/crawl4ai/__init__.py
index 13ac1d52..4a613870 100644
--- a/sdk/python/src/openlit/instrumentation/crawl4ai/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/crawl4ai/__init__.py
@@ -6,15 +6,12 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.crawl4ai.crawl4ai import (
- wrap_crawl
-)
-from openlit.instrumentation.crawl4ai.async_crawl4ai import (
- async_wrap_crawl
-)
+from openlit.instrumentation.crawl4ai.crawl4ai import wrap_crawl
+from openlit.instrumentation.crawl4ai.async_crawl4ai import async_wrap_crawl
_instruments = ("crawl4ai >= 0.4.0",)
+
class Crawl4AIInstrumentor(BaseInstrumentor):
"""
An instrumentor for crawl4ai's client library.
@@ -36,15 +33,33 @@ class Crawl4AIInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"crawl4ai.web_crawler",
"WebCrawler.run",
- wrap_crawl("crawl4ai.web_crawl", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ wrap_crawl(
+ "crawl4ai.web_crawl",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"crawl4ai.async_webcrawler",
"AsyncWebCrawler.arun",
- async_wrap_crawl("crawl4ai.web_crawl", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_wrap_crawl(
+ "crawl4ai.web_crawl",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/crawl4ai/async_crawl4ai.py b/sdk/python/src/openlit/instrumentation/crawl4ai/async_crawl4ai.py
index bd067745..afb5403b 100644
--- a/sdk/python/src/openlit/instrumentation/crawl4ai/async_crawl4ai.py
+++ b/sdk/python/src/openlit/instrumentation/crawl4ai/async_crawl4ai.py
@@ -5,7 +5,11 @@ Module for monitoring Crawl4AI calls.
import logging
from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from openlit.__helpers import (
handle_exception,
)
@@ -14,8 +18,18 @@ from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def async_wrap_crawl(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_wrap_crawl(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for chat completions to collect metrics.
@@ -50,44 +64,77 @@ def async_wrap_crawl(gen_ai_endpoint, version, environment, application_name,
"""
# pylint: disable=line-too-long
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(
+ gen_ai_endpoint, kind=SpanKind.CLIENT
+ ) as span:
response = await wrapped(*args, **kwargs)
try:
# Set base span attribues
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_CRAWL4AI)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT)
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,
- gen_ai_endpoint)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TYPE,
- SemanticConvention.GEN_AI_AGENT_TYPE_BROWSER)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ENABLE_CACHE, not kwargs.get("disable_cache", False))
-
- url = kwargs.get("url") if "url" in kwargs else str(args[0]) if args else None
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM,
+ SemanticConvention.GEN_AI_SYSTEM_CRAWL4AI,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
+ )
+ span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)
+ span.set_attribute(SERVICE_NAME, application_name)
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_TYPE,
+ SemanticConvention.GEN_AI_AGENT_TYPE_BROWSER,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_ENABLE_CACHE,
+ not kwargs.get("disable_cache", False),
+ )
+
+ url = (
+ kwargs.get("url")
+ if "url" in kwargs
+ else str(args[0])
+ if args
+ else None
+ )
if url is not None:
span.set_attribute(SemanticConvention.GEN_AI_AGENT_BROWSE_URL, url)
- extraction_strategy = kwargs.get("extraction_strategy", "NoExtractionStrategy")
- extraction_name = extraction_strategy.name if hasattr(extraction_strategy, 'name') else extraction_strategy
-
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_STRATEGY, extraction_name)
-
- if extraction_name == "LLMExtractionStrategy" and hasattr(extraction_strategy, 'provider'):
- _, llm_model = extraction_strategy.provider.split('/')
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, llm_model)
+ extraction_strategy = kwargs.get(
+ "extraction_strategy", "NoExtractionStrategy"
+ )
+ extraction_name = (
+ extraction_strategy.name
+ if hasattr(extraction_strategy, "name")
+ else extraction_strategy
+ )
+
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_STRATEGY, extraction_name
+ )
+
+ if extraction_name == "LLMExtractionStrategy" and hasattr(
+ extraction_strategy, "provider"
+ ):
+ _, llm_model = extraction_strategy.provider.split("/")
+ span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MODEL, llm_model
+ )
elif extraction_name == "CosineStrategy":
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, "all-MiniLM-L6-v2")
-
- elif extraction_name == "JsonCssExtractionStrategy" and hasattr(extraction_strategy, 'schema'):
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_SCHEMA, str(extraction_strategy.schema))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MODEL, "all-MiniLM-L6-v2"
+ )
+
+ elif extraction_name == "JsonCssExtractionStrategy" and hasattr(
+ extraction_strategy, "schema"
+ ):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_SCHEMA,
+ str(extraction_strategy.schema),
+ )
span.set_status(Status(StatusCode.OK))
diff --git a/sdk/python/src/openlit/instrumentation/crawl4ai/crawl4ai.py b/sdk/python/src/openlit/instrumentation/crawl4ai/crawl4ai.py
index d9dd68e4..30302966 100644
--- a/sdk/python/src/openlit/instrumentation/crawl4ai/crawl4ai.py
+++ b/sdk/python/src/openlit/instrumentation/crawl4ai/crawl4ai.py
@@ -5,7 +5,11 @@ Module for monitoring Crawl4AI calls.
import logging
from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from openlit.__helpers import (
handle_exception,
)
@@ -14,8 +18,18 @@ from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def wrap_crawl(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def wrap_crawl(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for chat completions to collect metrics.
@@ -50,44 +64,77 @@ def wrap_crawl(gen_ai_endpoint, version, environment, application_name,
"""
# pylint: disable=line-too-long
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(
+ gen_ai_endpoint, kind=SpanKind.CLIENT
+ ) as span:
response = wrapped(*args, **kwargs)
try:
# Set base span attribues
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_CRAWL4AI)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT)
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,
- gen_ai_endpoint)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TYPE,
- SemanticConvention.GEN_AI_AGENT_TYPE_BROWSER)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ENABLE_CACHE, not kwargs.get("disable_cache", False))
-
- url = kwargs.get("url") if "url" in kwargs else str(args[0]) if args else None
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM,
+ SemanticConvention.GEN_AI_SYSTEM_CRAWL4AI,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
+ )
+ span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)
+ span.set_attribute(SERVICE_NAME, application_name)
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_TYPE,
+ SemanticConvention.GEN_AI_AGENT_TYPE_BROWSER,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_ENABLE_CACHE,
+ not kwargs.get("disable_cache", False),
+ )
+
+ url = (
+ kwargs.get("url")
+ if "url" in kwargs
+ else str(args[0])
+ if args
+ else None
+ )
if url is not None:
span.set_attribute(SemanticConvention.GEN_AI_AGENT_BROWSE_URL, url)
- extraction_strategy = kwargs.get("extraction_strategy", "NoExtractionStrategy")
- extraction_name = extraction_strategy.name if hasattr(extraction_strategy, 'name') else extraction_strategy
-
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_STRATEGY, extraction_name)
-
- if extraction_name == "LLMExtractionStrategy" and hasattr(extraction_strategy, 'provider'):
- _, llm_model = extraction_strategy.provider.split('/')
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, llm_model)
+ extraction_strategy = kwargs.get(
+ "extraction_strategy", "NoExtractionStrategy"
+ )
+ extraction_name = (
+ extraction_strategy.name
+ if hasattr(extraction_strategy, "name")
+ else extraction_strategy
+ )
+
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_STRATEGY, extraction_name
+ )
+
+ if extraction_name == "LLMExtractionStrategy" and hasattr(
+ extraction_strategy, "provider"
+ ):
+ _, llm_model = extraction_strategy.provider.split("/")
+ span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MODEL, llm_model
+ )
elif extraction_name == "CosineStrategy":
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, "all-MiniLM-L6-v2")
-
- elif extraction_name == "JsonCssExtractionStrategy" and hasattr(extraction_strategy, 'schema'):
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_SCHEMA, str(extraction_strategy.schema))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MODEL, "all-MiniLM-L6-v2"
+ )
+
+ elif extraction_name == "JsonCssExtractionStrategy" and hasattr(
+ extraction_strategy, "schema"
+ ):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_SCHEMA,
+ str(extraction_strategy.schema),
+ )
span.set_status(Status(StatusCode.OK))
diff --git a/sdk/python/src/openlit/instrumentation/crewai/__init__.py b/sdk/python/src/openlit/instrumentation/crewai/__init__.py
index 4ab7bcf9..d32a3890 100644
--- a/sdk/python/src/openlit/instrumentation/crewai/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/crewai/__init__.py
@@ -19,7 +19,6 @@ WORKFLOW_OPERATIONS = [
("crewai.crew", "Crew.kickoff_async", "crew_kickoff_async"),
("crewai.crew", "Crew.kickoff_for_each", "crew_kickoff_for_each"),
("crewai.crew", "Crew.kickoff_for_each_async", "crew_kickoff_for_each_async"),
-
# High-level Agent and Task Operations
("crewai.agent", "Agent.execute_task", "agent_execute_task"),
("crewai.task", "Task.execute", "task_execute"),
@@ -33,19 +32,18 @@ COMPONENT_OPERATIONS = [
("crewai.tools.base", "BaseTool._run", "tool_run_internal"),
("crewai.memory.base", "BaseMemory.save", "memory_save"),
("crewai.memory.base", "BaseMemory.search", "memory_search"),
-
# Process and Collaboration Operations
("crewai.process", "Process.kickoff", "process_kickoff"),
("crewai.agent", "Agent.delegate", "agent_delegate"),
("crewai.agent", "Agent.ask_question", "agent_ask_question"),
("crewai.task", "Task.callback", "task_callback"),
-
# Internal Task Management
# Instrument only the core task execution (remove the sync duplicate)
# Task Operations (keep only core execution)
("crewai.task", "Task._execute_core", "task_execute_core"),
]
+
class CrewAIInstrumentor(BaseInstrumentor):
"""
Modern instrumentor for CrewAI framework with comprehensive coverage.
@@ -70,10 +68,19 @@ class CrewAIInstrumentor(BaseInstrumentor):
for module, method, operation_type in WORKFLOW_OPERATIONS:
try:
wrap_function_wrapper(
- module, method,
- general_wrap(operation_type, version, environment, application_name,
- tracer, pricing_info, capture_message_content,
- metrics, disable_metrics)
+ module,
+ method,
+ general_wrap(
+ operation_type,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
except Exception:
# Graceful degradation for missing operations
@@ -84,10 +91,19 @@ class CrewAIInstrumentor(BaseInstrumentor):
if "async" in operation_type:
try:
wrap_function_wrapper(
- module, method,
- async_general_wrap(operation_type, version, environment,
- application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics)
+ module,
+ method,
+ async_general_wrap(
+ operation_type,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
except Exception:
pass
@@ -97,10 +113,19 @@ class CrewAIInstrumentor(BaseInstrumentor):
for module, method, operation_type in COMPONENT_OPERATIONS:
try:
wrap_function_wrapper(
- module, method,
- general_wrap(operation_type, version, environment,
- application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics)
+ module,
+ method,
+ general_wrap(
+ operation_type,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
except Exception:
pass
diff --git a/sdk/python/src/openlit/instrumentation/crewai/async_crewai.py b/sdk/python/src/openlit/instrumentation/crewai/async_crewai.py
index 97e5e49d..7fecede9 100644
--- a/sdk/python/src/openlit/instrumentation/crewai/async_crewai.py
+++ b/sdk/python/src/openlit/instrumentation/crewai/async_crewai.py
@@ -12,8 +12,18 @@ from openlit.instrumentation.crewai.utils import (
set_server_address_and_port,
)
-def async_general_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_general_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Modern async wrapper for CrewAI operations following Framework Instrumentation Guide patterns.
"""
@@ -34,7 +44,9 @@ def async_general_wrap(gen_ai_endpoint, version, environment, application_name,
operation_type = OPERATION_MAP.get(gen_ai_endpoint, "framework")
# Generate span name following {operation_type} {operation_name} pattern
- span_name = _generate_span_name(operation_type, gen_ai_endpoint, instance, args, kwargs)
+ span_name = _generate_span_name(
+ operation_type, gen_ai_endpoint, instance, args, kwargs
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
@@ -43,10 +55,22 @@ def async_general_wrap(gen_ai_endpoint, version, environment, application_name,
try:
# Process response and generate comprehensive telemetry
response = process_crewai_response(
- response, operation_type, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version,
- instance, args, endpoint=gen_ai_endpoint, **kwargs
+ response,
+ operation_type,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ args,
+ endpoint=gen_ai_endpoint,
+ **kwargs,
)
except Exception as e:
@@ -56,6 +80,7 @@ def async_general_wrap(gen_ai_endpoint, version, environment, application_name,
return wrapper
+
def _generate_span_name(operation_type, endpoint, instance, args, kwargs):
"""
Generate proper span names following {operation_type} {operation_name} convention.
diff --git a/sdk/python/src/openlit/instrumentation/crewai/crewai.py b/sdk/python/src/openlit/instrumentation/crewai/crewai.py
index c9161b28..cfbd02ec 100644
--- a/sdk/python/src/openlit/instrumentation/crewai/crewai.py
+++ b/sdk/python/src/openlit/instrumentation/crewai/crewai.py
@@ -12,8 +12,18 @@ from openlit.instrumentation.crewai.utils import (
set_server_address_and_port,
)
-def general_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def general_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Modern wrapper for CrewAI operations following Framework Instrumentation Guide patterns.
"""
@@ -34,7 +44,9 @@ def general_wrap(gen_ai_endpoint, version, environment, application_name,
operation_type = OPERATION_MAP.get(gen_ai_endpoint, "framework")
# Generate span name following {operation_type} {operation_name} pattern
- span_name = _generate_span_name(operation_type, gen_ai_endpoint, instance, args, kwargs)
+ span_name = _generate_span_name(
+ operation_type, gen_ai_endpoint, instance, args, kwargs
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
@@ -43,10 +55,22 @@ def general_wrap(gen_ai_endpoint, version, environment, application_name,
try:
# Process response and generate comprehensive telemetry
response = process_crewai_response(
- response, operation_type, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version,
- instance, args, endpoint=gen_ai_endpoint, **kwargs
+ response,
+ operation_type,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ args,
+ endpoint=gen_ai_endpoint,
+ **kwargs,
)
except Exception as e:
@@ -56,6 +80,7 @@ def general_wrap(gen_ai_endpoint, version, environment, application_name,
return wrapper
+
def _generate_span_name(operation_type, endpoint, instance, args, kwargs):
"""
Generate proper span names following {operation_type} {operation_name} convention.
diff --git a/sdk/python/src/openlit/instrumentation/crewai/utils.py b/sdk/python/src/openlit/instrumentation/crewai/utils.py
index a9313ac7..27dc24f2 100644
--- a/sdk/python/src/openlit/instrumentation/crewai/utils.py
+++ b/sdk/python/src/openlit/instrumentation/crewai/utils.py
@@ -20,28 +20,25 @@ OPERATION_MAP = {
"crew_train": "invoke_agent",
"crew_replay": "invoke_agent",
"crew_test": "invoke_agent",
-
# Agent Operations (core agent functions)
"agent___init__": "create_agent",
"agent_execute_task": "invoke_agent",
"agent_backstory_property": "invoke_agent",
-
# Task Operations (task execution)
"task_execute": "invoke_agent",
"task_execute_async": "invoke_agent",
"task_execute_core": "invoke_agent",
-
# Tool Operations (tool execution)
"tool_run": "execute_tool",
"tool___call__": "execute_tool",
"tool_execute": "execute_tool",
-
# Memory Operations (knowledge management)
"memory_save": "invoke_agent",
"memory_search": "invoke_agent",
- "memory_reset": "invoke_agent"
+ "memory_reset": "invoke_agent",
}
+
def set_server_address_and_port(instance):
"""
Extract server information from CrewAI instance.
@@ -57,13 +54,13 @@ def set_server_address_and_port(instance):
# Try to extract LLM endpoint information
try:
- if hasattr(instance, 'llm') and hasattr(instance.llm, 'api_base'):
+ if hasattr(instance, "llm") and hasattr(instance.llm, "api_base"):
parsed = urlparse(instance.llm.api_base)
server_address = parsed.hostname or "localhost"
server_port = parsed.port or 443
- elif hasattr(instance, 'agent') and hasattr(instance.agent, 'llm'):
+ elif hasattr(instance, "agent") and hasattr(instance.agent, "llm"):
# For tasks that have an agent with LLM
- if hasattr(instance.agent.llm, 'api_base'):
+ if hasattr(instance.agent.llm, "api_base"):
parsed = urlparse(instance.agent.llm.api_base)
server_address = parsed.hostname or "localhost"
server_port = parsed.port or 443
@@ -73,10 +70,25 @@ def set_server_address_and_port(instance):
return server_address, server_port
-def process_crewai_response(response, operation_type, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version,
- instance, args, endpoint=None, **kwargs):
+
+def process_crewai_response(
+ response,
+ operation_type,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ args,
+ endpoint=None,
+ **kwargs,
+):
"""
Process CrewAI response with comprehensive business intelligence.
OpenLIT's competitive advantage through superior observability.
@@ -100,16 +112,19 @@ def process_crewai_response(response, operation_type, server_address, server_por
llm = getattr(instance, "llm", None)
if llm:
# Try different model attribute names used by different LLM libraries
- request_model = (getattr(llm, "model_name", None) or
- getattr(llm, "model", None) or
- getattr(llm, "_model_name", None) or
- "unknown")
+ request_model = (
+ getattr(llm, "model_name", None)
+ or getattr(llm, "model", None)
+ or getattr(llm, "_model_name", None)
+ or "unknown"
+ )
if request_model != "unknown":
request_model = str(request_model)
# Create a wrapper instance that exposes model_name for common_framework_span_attributes
class ModelWrapper:
"""Wrapper class to expose model_name for framework span attributes."""
+
def __init__(self, original_instance, model_name):
self._original = original_instance
self.model_name = model_name
@@ -125,8 +140,15 @@ def process_crewai_response(response, operation_type, server_address, server_por
# Set common framework span attributes
common_framework_span_attributes(
- scope, SemanticConvention.GEN_AI_SYSTEM_CREWAI, server_address, server_port,
- environment, application_name, version, endpoint, model_instance
+ scope,
+ SemanticConvention.GEN_AI_SYSTEM_CREWAI,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ version,
+ endpoint,
+ model_instance,
)
# Set span name following OpenTelemetry format
@@ -156,11 +178,14 @@ def process_crewai_response(response, operation_type, server_address, server_por
# === RECORD METRICS ===
if not disable_metrics and metrics:
- _record_crewai_metrics(metrics, standard_operation, duration_ms, environment, application_name)
+ _record_crewai_metrics(
+ metrics, standard_operation, duration_ms, environment, application_name
+ )
span.set_status(Status(StatusCode.OK))
return response
+
def _set_span_name(span, operation_type, instance, endpoint, args, kwargs):
"""Set span name following OpenTelemetry format: '{operation_type} {name}'"""
try:
@@ -175,11 +200,15 @@ def _set_span_name(span, operation_type, instance, endpoint, args, kwargs):
elif endpoint.startswith("agent_"):
if "create" in endpoint or endpoint == "agent___init__":
# Agent creation: "create_agent {agent_name}"
- agent_name = getattr(instance, "name", None) or getattr(instance, "role", "agent")
+ agent_name = getattr(instance, "name", None) or getattr(
+ instance, "role", "agent"
+ )
span.update_name(f"create_agent {agent_name}")
else:
# Agent invocation: "invoke_agent {agent_name}"
- agent_name = getattr(instance, "name", None) or getattr(instance, "role", "agent")
+ agent_name = getattr(instance, "name", None) or getattr(
+ instance, "role", "agent"
+ )
span.update_name(f"invoke_agent {agent_name}")
elif endpoint.startswith("task_"):
@@ -188,7 +217,10 @@ def _set_span_name(span, operation_type, instance, endpoint, args, kwargs):
elif endpoint.startswith("tool_"):
# Tool operations: "execute_tool {tool_name}"
- tool_name = getattr(instance, "name", None) or getattr(instance, "__class__", type(instance)).__name__
+ tool_name = (
+ getattr(instance, "name", None)
+ or getattr(instance, "__class__", type(instance)).__name__
+ )
span.update_name(f"execute_tool {tool_name}")
elif endpoint.startswith("memory_"):
@@ -205,6 +237,7 @@ def _set_span_name(span, operation_type, instance, endpoint, args, kwargs):
# Fallback naming
span.update_name(f"invoke_agent {endpoint}")
+
def _set_agent_business_intelligence(span, instance, endpoint, args, kwargs):
"""Set agent business intelligence using standard OpenTelemetry semantic conventions"""
if not endpoint.startswith("agent_"):
@@ -232,32 +265,46 @@ def _set_agent_business_intelligence(span, instance, endpoint, args, kwargs):
# Enhanced Agent Configuration Tracking using SemanticConvention
max_retry_limit = getattr(instance, "max_retry_limit", None)
if max_retry_limit is not None:
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_MAX_RETRY_LIMIT, max_retry_limit)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_MAX_RETRY_LIMIT, max_retry_limit
+ )
allow_delegation = getattr(instance, "allow_delegation", None)
if allow_delegation is not None:
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ALLOW_DELEGATION, allow_delegation)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_ALLOW_DELEGATION, allow_delegation
+ )
allow_code_execution = getattr(instance, "allow_code_execution", None)
if allow_code_execution is not None:
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ALLOW_CODE_EXECUTION, allow_code_execution)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_ALLOW_CODE_EXECUTION,
+ allow_code_execution,
+ )
# Tools tracking using SemanticConvention
tools = getattr(instance, "tools", [])
if tools:
- tool_names = [getattr(tool, "name", str(tool)) for tool in tools[:5]] # Limit to first 5
+ tool_names = [
+ getattr(tool, "name", str(tool)) for tool in tools[:5]
+ ] # Limit to first 5
if tool_names:
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TOOLS, ", ".join(tool_names))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_TOOLS, ", ".join(tool_names)
+ )
# === OpenAI Agent-specific Attributes ===
_set_openai_agent_attributes(span, instance, endpoint, args, kwargs)
# === Conversation and Data Source Tracking ===
- _set_conversation_and_data_source_attributes(span, instance, endpoint, args, kwargs)
+ _set_conversation_and_data_source_attributes(
+ span, instance, endpoint, args, kwargs
+ )
except Exception as e:
handle_exception(span, e)
+
def _set_openai_agent_attributes(span, instance, endpoint, args, kwargs):
"""Set OpenAI-specific agent attributes when using OpenAI models"""
try:
@@ -276,16 +323,27 @@ def _set_openai_agent_attributes(span, instance, endpoint, args, kwargs):
# OpenAI service tier if available
service_tier = getattr(llm, "service_tier", None)
if service_tier:
- span.set_attribute(SemanticConvention.GEN_AI_OPENAI_REQUEST_SERVICE_TIER, service_tier)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_REQUEST_SERVICE_TIER,
+ service_tier,
+ )
# OpenAI Assistant API attributes if available
- assistant_id = getattr(instance, "assistant_id", None) or kwargs.get("assistant_id")
+ assistant_id = getattr(instance, "assistant_id", None) or kwargs.get(
+ "assistant_id"
+ )
if assistant_id:
- span.set_attribute(SemanticConvention.GEN_AI_OPENAI_ASSISTANT_ID, assistant_id)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_ASSISTANT_ID, assistant_id
+ )
- thread_id = getattr(instance, "thread_id", None) or kwargs.get("thread_id")
+ thread_id = getattr(instance, "thread_id", None) or kwargs.get(
+ "thread_id"
+ )
if thread_id:
- span.set_attribute(SemanticConvention.GEN_AI_OPENAI_THREAD_ID, thread_id)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPENAI_THREAD_ID, thread_id
+ )
run_id = getattr(instance, "run_id", None) or kwargs.get("run_id")
if run_id:
@@ -299,18 +357,23 @@ def _set_openai_agent_attributes(span, instance, endpoint, args, kwargs):
except Exception as e:
handle_exception(span, e)
-def _set_conversation_and_data_source_attributes(span, instance, endpoint, args, kwargs):
+
+def _set_conversation_and_data_source_attributes(
+ span, instance, endpoint, args, kwargs
+):
"""Set conversation tracking and data source attributes"""
try:
# Conversation ID for multi-turn interactions
conversation_id = (
- getattr(instance, "conversation_id", None) or
- getattr(instance, "session_id", None) or
- kwargs.get("conversation_id") or
- kwargs.get("session_id")
+ getattr(instance, "conversation_id", None)
+ or getattr(instance, "session_id", None)
+ or kwargs.get("conversation_id")
+ or kwargs.get("session_id")
)
if conversation_id:
- span.set_attribute(SemanticConvention.GEN_AI_CONVERSATION_ID, str(conversation_id))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_CONVERSATION_ID, str(conversation_id)
+ )
# Data source tracking for RAG operations
memory = getattr(instance, "memory", None)
@@ -319,25 +382,37 @@ def _set_conversation_and_data_source_attributes(span, instance, endpoint, args,
memory_provider = getattr(memory, "provider", None)
if memory_provider:
span.set_attribute(SemanticConvention.GEN_AI_DATA_SOURCE_TYPE, "memory")
- span.set_attribute(SemanticConvention.GEN_AI_DATA_SOURCE_ID, str(memory_provider))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_DATA_SOURCE_ID, str(memory_provider)
+ )
# Knowledge base or vector store detection
knowledge_source = getattr(instance, "knowledge_source", None)
if knowledge_source:
- span.set_attribute(SemanticConvention.GEN_AI_DATA_SOURCE_TYPE, "knowledge_base")
- span.set_attribute(SemanticConvention.GEN_AI_DATA_SOURCE_ID, str(knowledge_source))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_DATA_SOURCE_TYPE, "knowledge_base"
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_DATA_SOURCE_ID, str(knowledge_source)
+ )
# Tool-based data sources
tools = getattr(instance, "tools", [])
for tool in tools:
tool_name = getattr(tool, "name", "").lower()
- if any(keyword in tool_name for keyword in ["search", "retrieval", "database", "vector"]):
- span.set_attribute(SemanticConvention.GEN_AI_DATA_SOURCE_TYPE, "external_tool")
+ if any(
+ keyword in tool_name
+ for keyword in ["search", "retrieval", "database", "vector"]
+ ):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_DATA_SOURCE_TYPE, "external_tool"
+ )
break
except Exception as e:
handle_exception(span, e)
+
def _set_task_business_intelligence(span, instance, endpoint, args, kwargs):
"""Set task business intelligence using standard OpenTelemetry semantic conventions"""
if not endpoint.startswith("task_"):
@@ -352,16 +427,21 @@ def _set_task_business_intelligence(span, instance, endpoint, args, kwargs):
# Task description
task_description = getattr(instance, "description", "")
if task_description:
- span.set_attribute(SemanticConvention.GEN_AI_TASK_DESCRIPTION, task_description)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_TASK_DESCRIPTION, task_description
+ )
# Task expected output (keep only essential attributes that have semantic conventions or are critical)
expected_output = getattr(instance, "expected_output", "")
if expected_output:
- span.set_attribute(SemanticConvention.GEN_AI_TASK_EXPECTED_OUTPUT, expected_output)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_TASK_EXPECTED_OUTPUT, expected_output
+ )
except Exception as e:
handle_exception(span, e)
+
def _set_crew_business_intelligence(span, instance, endpoint, args, kwargs):
"""Set crew business intelligence using standard OpenTelemetry semantic conventions"""
if not endpoint.startswith("crew_"):
@@ -374,6 +454,7 @@ def _set_crew_business_intelligence(span, instance, endpoint, args, kwargs):
except Exception as e:
handle_exception(span, e)
+
def _set_tool_business_intelligence(span, instance, endpoint, args, kwargs):
"""Set tool business intelligence using standard OpenTelemetry semantic conventions"""
if not endpoint.startswith("tool_"):
@@ -381,14 +462,19 @@ def _set_tool_business_intelligence(span, instance, endpoint, args, kwargs):
try:
# Standard OpenTelemetry Gen AI Tool attributes
- tool_name = getattr(instance, "name", None) or getattr(instance, "__class__", type(instance)).__name__
+ tool_name = (
+ getattr(instance, "name", None)
+ or getattr(instance, "__class__", type(instance)).__name__
+ )
if tool_name:
span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, tool_name)
# Tool call ID if available (for tracking specific tool invocations)
tool_call_id = kwargs.get("call_id", None) or getattr(instance, "call_id", None)
if tool_call_id:
- span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, str(tool_call_id))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_CALL_ID, str(tool_call_id)
+ )
# === OpenAI Function Calling Attributes ===
_set_openai_tool_attributes(span, instance, endpoint, args, kwargs)
@@ -396,6 +482,7 @@ def _set_tool_business_intelligence(span, instance, endpoint, args, kwargs):
except Exception as e:
handle_exception(span, e)
+
def _set_openai_tool_attributes(span, instance, endpoint, args, kwargs):
"""Set OpenAI function calling specific attributes using standard conventions"""
try:
@@ -409,7 +496,9 @@ def _set_openai_tool_attributes(span, instance, endpoint, args, kwargs):
tool_type = "api_client"
elif any(keyword in tool_class for keyword in ["database", "sql", "query"]):
tool_type = "database"
- elif any(keyword in tool_class for keyword in ["vector", "embedding", "retrieval"]):
+ elif any(
+ keyword in tool_class for keyword in ["vector", "embedding", "retrieval"]
+ ):
tool_type = "vector_store"
else:
tool_type = "custom"
@@ -420,6 +509,7 @@ def _set_openai_tool_attributes(span, instance, endpoint, args, kwargs):
except Exception as e:
handle_exception(span, e)
+
def _capture_content(span, instance, response, endpoint):
"""Capture input/output content with MIME types"""
@@ -429,8 +519,10 @@ def _capture_content(span, instance, response, endpoint):
span.add_event(
name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
attributes={
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: str(response)[:1000], # Limit size
- }
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION: str(response)[
+ :1000
+ ], # Limit size
+ },
)
# Capture input content based on operation type
@@ -440,14 +532,17 @@ def _capture_content(span, instance, response, endpoint):
span.add_event(
name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: task_description[:1000],
- }
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: task_description[
+ :1000
+ ],
+ },
)
except Exception:
# Graceful degradation
pass
+
def _track_cost_and_tokens(span, instance, response, endpoint):
"""Track cost and token usage for business intelligence"""
@@ -462,7 +557,9 @@ def _track_cost_and_tokens(span, instance, response, endpoint):
response_length = len(str(response))
# Estimate token count (rough approximation: 4 chars per token)
estimated_tokens = response_length // 4
- span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, estimated_tokens)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, estimated_tokens
+ )
# Cost estimation would require pricing information
# This could be enhanced with actual cost tracking
@@ -471,7 +568,10 @@ def _track_cost_and_tokens(span, instance, response, endpoint):
# Graceful degradation
pass
-def _record_crewai_metrics(metrics, operation_type, duration_ms, environment, application_name):
+
+def _record_crewai_metrics(
+ metrics, operation_type, duration_ms, environment, application_name
+):
"""Record CrewAI-specific metrics"""
try:
@@ -484,7 +584,9 @@ def _record_crewai_metrics(metrics, operation_type, duration_ms, environment, ap
# Record operation duration
if "genai_client_operation_duration" in metrics:
- metrics["genai_client_operation_duration"].record(duration_ms / 1000, attributes)
+ metrics["genai_client_operation_duration"].record(
+ duration_ms / 1000, attributes
+ )
# Record operation count
if "genai_requests" in metrics:
@@ -494,6 +596,7 @@ def _record_crewai_metrics(metrics, operation_type, duration_ms, environment, ap
# Graceful degradation
pass
+
def _parse_tools(tools):
"""Parse tools list into JSON format"""
diff --git a/sdk/python/src/openlit/instrumentation/dynamiq/__init__.py b/sdk/python/src/openlit/instrumentation/dynamiq/__init__.py
index c493e94f..d627023b 100644
--- a/sdk/python/src/openlit/instrumentation/dynamiq/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/dynamiq/__init__.py
@@ -6,12 +6,11 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.dynamiq.dynamiq import (
- dynamiq_wrap
-)
+from openlit.instrumentation.dynamiq.dynamiq import dynamiq_wrap
_instruments = ("dynamiq >= 0.4.0",)
+
class DynamiqInstrumentor(BaseInstrumentor):
"""
An instrumentor for dynamiq's client library.
@@ -33,32 +32,67 @@ class DynamiqInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"dynamiq.nodes.agents.base",
"Agent.run",
- dynamiq_wrap("dynamiq.agent_run", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ dynamiq_wrap(
+ "dynamiq.agent_run",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"dynamiq",
"Workflow.run",
- dynamiq_wrap("dynamiq.workflow_run", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ dynamiq_wrap(
+ "dynamiq.workflow_run",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"dynamiq.memory",
"Memory.add",
- dynamiq_wrap("dynamiq.memory_add", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ dynamiq_wrap(
+ "dynamiq.memory_add",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"dynamiq.memory",
"Memory.search",
- dynamiq_wrap("dynamiq.memory_search", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ dynamiq_wrap(
+ "dynamiq.memory_search",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
-
def _uninstrument(self, **kwargs):
# Proper uninstrumentation logic to revert patched methods
pass
diff --git a/sdk/python/src/openlit/instrumentation/dynamiq/dynamiq.py b/sdk/python/src/openlit/instrumentation/dynamiq/dynamiq.py
index 4eb3f978..9c02dfd1 100644
--- a/sdk/python/src/openlit/instrumentation/dynamiq/dynamiq.py
+++ b/sdk/python/src/openlit/instrumentation/dynamiq/dynamiq.py
@@ -5,7 +5,11 @@ Module for monitoring Dynamiq calls.
import logging
from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from openlit.__helpers import (
handle_exception,
)
@@ -14,8 +18,18 @@ from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def dynamiq_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def dynamiq_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for chat completions to collect metrics.
@@ -50,49 +64,76 @@ def dynamiq_wrap(gen_ai_endpoint, version, environment, application_name,
"""
# pylint: disable=line-too-long
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(
+ gen_ai_endpoint, kind=SpanKind.CLIENT
+ ) as span:
response = wrapped(*args, **kwargs)
try:
# Set base span attribues
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_DYNAMIQ)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT)
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,
- gen_ai_endpoint)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM,
+ SemanticConvention.GEN_AI_SYSTEM_DYNAMIQ,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
+ )
+ span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)
+ span.set_attribute(SERVICE_NAME, application_name)
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
if gen_ai_endpoint == "dynamiq.agent_run":
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ID,
- getattr(instance, 'id', '') or '')
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ROLE,
- getattr(instance, 'name', '') or '')
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- getattr(getattr(instance, 'llm', None), 'model', '') or '')
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TYPE,
- str(getattr(instance, 'type', '')) or '')
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_ID,
+ getattr(instance, "id", "") or "",
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_ROLE,
+ getattr(instance, "name", "") or "",
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MODEL,
+ getattr(getattr(instance, "llm", None), "model", "") or "",
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_TYPE,
+ str(getattr(instance, "type", "")) or "",
+ )
elif gen_ai_endpoint == "dynamiq.workflow_run":
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ID,
- getattr(instance, 'id', '') or '')
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- getattr(getattr(instance.flow, 'nodes', [None])[0], 'model', 'default_model'))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_ID,
+ getattr(instance, "id", "") or "",
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MODEL,
+ getattr(
+ getattr(instance.flow, "nodes", [None])[0],
+ "model",
+ "default_model",
+ ),
+ )
elif gen_ai_endpoint == "dynamiq.memory_add":
- span.set_attribute(SemanticConvention.DB_OPERATION_NAME,
- SemanticConvention.DB_OPERATION_ADD)
- span.set_attribute(SemanticConvention.DB_METADATA, str(kwargs.get('metadata', '')))
+ span.set_attribute(
+ SemanticConvention.DB_OPERATION_NAME,
+ SemanticConvention.DB_OPERATION_ADD,
+ )
+ span.set_attribute(
+ SemanticConvention.DB_METADATA, str(kwargs.get("metadata", ""))
+ )
elif gen_ai_endpoint == "dynamiq.memory_search":
- query_value = kwargs.get('query', '') or (args[0] if args else '')
- span.set_attribute(SemanticConvention.DB_OPERATION_NAME,
- SemanticConvention.DB_OPERATION_GET)
- span.set_attribute(SemanticConvention.DB_FILTER, str(kwargs.get('filters', '')))
+ query_value = kwargs.get("query", "") or (args[0] if args else "")
+ span.set_attribute(
+ SemanticConvention.DB_OPERATION_NAME,
+ SemanticConvention.DB_OPERATION_GET,
+ )
+ span.set_attribute(
+ SemanticConvention.DB_FILTER, str(kwargs.get("filters", ""))
+ )
span.set_attribute(SemanticConvention.DB_STATEMENT, query_value)
span.set_status(Status(StatusCode.OK))
diff --git a/sdk/python/src/openlit/instrumentation/elevenlabs/__init__.py b/sdk/python/src/openlit/instrumentation/elevenlabs/__init__.py
index ebb898a2..09fbbb6a 100644
--- a/sdk/python/src/openlit/instrumentation/elevenlabs/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/elevenlabs/__init__.py
@@ -10,6 +10,7 @@ from openlit.instrumentation.elevenlabs.async_elevenlabs import async_generate
_instruments = ("elevenlabs >= 1.4.0",)
+
class ElevenLabsInstrumentor(BaseInstrumentor):
"""
An instrumentor for ElevenLabs client library.
@@ -32,16 +33,34 @@ class ElevenLabsInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"elevenlabs.text_to_speech.client",
"TextToSpeechClient.convert",
- generate("elevenlabs.text_to_speech", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ generate(
+ "elevenlabs.text_to_speech",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# async text_to_speech.convert
wrap_function_wrapper(
"elevenlabs.text_to_speech.client",
"AsyncTextToSpeechClient.convert",
- async_generate("elevenlabs.text_to_speech", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_generate(
+ "elevenlabs.text_to_speech",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/elevenlabs/async_elevenlabs.py b/sdk/python/src/openlit/instrumentation/elevenlabs/async_elevenlabs.py
index a8229bd4..f392dcb7 100644
--- a/sdk/python/src/openlit/instrumentation/elevenlabs/async_elevenlabs.py
+++ b/sdk/python/src/openlit/instrumentation/elevenlabs/async_elevenlabs.py
@@ -8,8 +8,18 @@ from openlit.__helpers import handle_exception
from openlit.instrumentation.elevenlabs.utils import process_audio_response
from openlit.semcov import SemanticConvention
-def async_generate(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_generate(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -20,7 +30,9 @@ def async_generate(gen_ai_endpoint, version, environment, application_name,
"""
server_address, server_port = "api.elevenlabs.io", 443
- request_model = kwargs.get("model", kwargs.get("model_id", "eleven_multilingual_v2"))
+ request_model = kwargs.get(
+ "model", kwargs.get("model_id", "eleven_multilingual_v2")
+ )
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO} {request_model}"
@@ -44,7 +56,7 @@ def async_generate(gen_ai_endpoint, version, environment, application_name,
kwargs=kwargs,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/elevenlabs/elevenlabs.py b/sdk/python/src/openlit/instrumentation/elevenlabs/elevenlabs.py
index 4963c89c..1a7de5b6 100644
--- a/sdk/python/src/openlit/instrumentation/elevenlabs/elevenlabs.py
+++ b/sdk/python/src/openlit/instrumentation/elevenlabs/elevenlabs.py
@@ -8,8 +8,18 @@ from openlit.__helpers import handle_exception
from openlit.instrumentation.elevenlabs.utils import process_audio_response
from openlit.semcov import SemanticConvention
-def generate(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def generate(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -20,7 +30,9 @@ def generate(gen_ai_endpoint, version, environment, application_name,
"""
server_address, server_port = "api.elevenlabs.io", 443
- request_model = kwargs.get("model", kwargs.get("model_id", "eleven_multilingual_v2"))
+ request_model = kwargs.get(
+ "model", kwargs.get("model_id", "eleven_multilingual_v2")
+ )
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO} {request_model}"
@@ -44,7 +56,7 @@ def generate(gen_ai_endpoint, version, environment, application_name,
kwargs=kwargs,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/elevenlabs/utils.py b/sdk/python/src/openlit/instrumentation/elevenlabs/utils.py
index 2a83d952..65274840 100644
--- a/sdk/python/src/openlit/instrumentation/elevenlabs/utils.py
+++ b/sdk/python/src/openlit/instrumentation/elevenlabs/utils.py
@@ -1,9 +1,14 @@
"""
ElevenLabs OpenTelemetry instrumentation utility functions
"""
+
import time
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from opentelemetry.trace import Status, StatusCode
from openlit.__helpers import (
@@ -12,14 +17,29 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def format_content(text):
"""
Process text input to extract content.
"""
return str(text) if text else ""
-def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_address, server_port,
- request_model, response_model, environment, application_name, is_stream, tbt, ttft, version):
+
+def common_span_attributes(
+ scope,
+ gen_ai_operation,
+ gen_ai_system,
+ server_address,
+ server_port,
+ request_model,
+ response_model,
+ environment,
+ application_name,
+ is_stream,
+ tbt,
+ ttft,
+ version,
+):
"""
Set common span attributes for both chat and RAG operations.
"""
@@ -30,7 +50,9 @@ def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_addres
scope._span.set_attribute(SemanticConvention.SERVER_ADDRESS, server_address)
scope._span.set_attribute(SemanticConvention.SERVER_PORT, server_port)
scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, request_model)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, scope._response_model)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_MODEL, scope._response_model
+ )
scope._span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
scope._span.set_attribute(SERVICE_NAME, application_name)
scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, is_stream)
@@ -38,8 +60,21 @@ def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_addres
scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, scope._ttft)
scope._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
-def record_audio_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
- request_model, response_model, environment, application_name, start_time, end_time, cost):
+
+def record_audio_metrics(
+ metrics,
+ gen_ai_operation,
+ gen_ai_system,
+ server_address,
+ server_port,
+ request_model,
+ response_model,
+ environment,
+ application_name,
+ start_time,
+ end_time,
+ cost,
+):
"""
Record audio generation metrics for the operation.
"""
@@ -58,33 +93,64 @@ def record_audio_metrics(metrics, gen_ai_operation, gen_ai_system, server_addres
metrics["genai_requests"].add(1, attributes)
metrics["genai_cost"].record(cost, attributes)
-def common_audio_logic(scope, gen_ai_endpoint, pricing_info, environment, application_name,
- metrics, capture_message_content, disable_metrics, version):
+
+def common_audio_logic(
+ scope,
+ gen_ai_endpoint,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+):
"""
Process audio generation request and generate Telemetry
"""
text = format_content(scope._kwargs.get("text", ""))
- request_model = scope._kwargs.get("model", scope._kwargs.get("model_id", "eleven_multilingual_v2"))
+ request_model = scope._kwargs.get(
+ "model", scope._kwargs.get("model_id", "eleven_multilingual_v2")
+ )
is_stream = False # ElevenLabs audio generation is not streaming
cost = get_audio_model_cost(request_model, pricing_info, text)
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO, SemanticConvention.GEN_AI_SYSTEM_ELEVENLABS,
- scope._server_address, scope._server_port, request_model, request_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO,
+ SemanticConvention.GEN_AI_SYSTEM_ELEVENLABS,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Cost and Tokens
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Response parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, scope._kwargs.get("output_format", "mp3_44100_128"))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ scope._kwargs.get("output_format", "mp3_44100_128"),
+ )
# Audio-specific span attributes
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_VOICE, scope._kwargs.get("voice_id", ""))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_SETTINGS, str(scope._kwargs.get("voice_settings", "")))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_AUDIO_VOICE, scope._kwargs.get("voice_id", "")
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_AUDIO_SETTINGS,
+ str(scope._kwargs.get("voice_settings", "")),
+ )
# Span Attributes for Content
if capture_message_content:
@@ -102,13 +168,39 @@ def common_audio_logic(scope, gen_ai_endpoint, pricing_info, environment, applic
# Metrics
if not disable_metrics:
- record_audio_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO, SemanticConvention.GEN_AI_SYSTEM_ELEVENLABS,
- scope._server_address, scope._server_port, request_model, request_model, environment,
- application_name, scope._start_time, scope._end_time, cost)
+ record_audio_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO,
+ SemanticConvention.GEN_AI_SYSTEM_ELEVENLABS,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ cost,
+ )
+
-def process_audio_response(response, gen_ai_endpoint, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, args, kwargs, capture_message_content=False,
- disable_metrics=False, version="1.0.0"):
+def process_audio_response(
+ response,
+ gen_ai_endpoint,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ args,
+ kwargs,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+):
"""
Process audio generation request and generate Telemetry
"""
@@ -123,11 +215,22 @@ def process_audio_response(response, gen_ai_endpoint, pricing_info, server_port,
scope._args = args
# Initialize streaming and timing values for ElevenLabs audio generation
- scope._response_model = kwargs.get("model", kwargs.get("model_id", "eleven_multilingual_v2"))
+ scope._response_model = kwargs.get(
+ "model", kwargs.get("model_id", "eleven_multilingual_v2")
+ )
scope._tbt = 0.0
scope._ttft = scope._end_time - scope._start_time
- common_audio_logic(scope, gen_ai_endpoint, pricing_info, environment, application_name,
- metrics, capture_message_content, disable_metrics, version)
+ common_audio_logic(
+ scope,
+ gen_ai_endpoint,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/embedchain/__init__.py b/sdk/python/src/openlit/instrumentation/embedchain/__init__.py
index da05f40e..3fecbaf6 100644
--- a/sdk/python/src/openlit/instrumentation/embedchain/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/embedchain/__init__.py
@@ -1,5 +1,6 @@
# pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code
"""Initializer of Auto Instrumentation of EmbedChain Functions"""
+
from typing import Collection
import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
@@ -24,6 +25,7 @@ WRAPPED_METHODS = [
},
]
+
class EmbedChainInstrumentor(BaseInstrumentor):
"""An instrumentor for EmbedChain's client library."""
@@ -46,8 +48,15 @@ class EmbedChainInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
wrap_package,
wrap_object,
- wrapper(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content),
+ wrapper(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ ),
)
@staticmethod
diff --git a/sdk/python/src/openlit/instrumentation/embedchain/embedchain.py b/sdk/python/src/openlit/instrumentation/embedchain/embedchain.py
index 2538ee71..9a4f2171 100644
--- a/sdk/python/src/openlit/instrumentation/embedchain/embedchain.py
+++ b/sdk/python/src/openlit/instrumentation/embedchain/embedchain.py
@@ -5,15 +5,27 @@ Module for monitoring EmbedChain applications.
import logging
from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from openlit.__helpers import handle_exception
from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def evaluate(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content):
+
+def evaluate(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+):
"""
Creates a wrapper around a function call to trace and log its execution metrics.
@@ -54,27 +66,36 @@ def evaluate(gen_ai_endpoint, version, environment, application_name,
on the span based on the function's execution and response, and ensures
errors are handled and logged appropriately.
"""
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(
+ gen_ai_endpoint, kind=SpanKind.CLIENT
+ ) as span:
response = wrapped(*args, **kwargs)
try:
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_EMBEDCHAIN)
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,
- gen_ai_endpoint)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_EVAL_CONTEXT_RELEVANCY,
- response["context_relevancy"])
- span.set_attribute(SemanticConvention.GEN_AI_EVAL_ANSWER_RELEVANCY,
- response["answer_relevancy"])
- span.set_attribute(SemanticConvention.GEN_AI_EVAL_GROUNDEDNESS,
- response["groundedness"])
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM,
+ SemanticConvention.GEN_AI_SYSTEM_EMBEDCHAIN,
+ )
+ span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
+ )
+ span.set_attribute(SERVICE_NAME, application_name)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_EVAL_CONTEXT_RELEVANCY,
+ response["context_relevancy"],
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_EVAL_ANSWER_RELEVANCY,
+ response["answer_relevancy"],
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_EVAL_GROUNDEDNESS,
+ response["groundedness"],
+ )
span.set_status(Status(StatusCode.OK))
@@ -90,8 +111,16 @@ def evaluate(gen_ai_endpoint, version, environment, application_name,
return wrapper
-def get_data_sources(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content):
+
+def get_data_sources(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+):
"""
Creates a wrapper around a function call to trace and log its execution metrics.
@@ -132,23 +161,27 @@ def get_data_sources(gen_ai_endpoint, version, environment, application_name,
on the span based on the function's execution and response, and ensures
errors are handled and logged appropriately.
"""
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(
+ gen_ai_endpoint, kind=SpanKind.CLIENT
+ ) as span:
response = wrapped(*args, **kwargs)
try:
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_EMBEDCHAIN)
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,
- gen_ai_endpoint)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_DATA_SOURCES,
- len(response))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM,
+ SemanticConvention.GEN_AI_SYSTEM_EMBEDCHAIN,
+ )
+ span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
+ )
+ span.set_attribute(SERVICE_NAME, application_name)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_DATA_SOURCES, len(response)
+ )
span.set_status(Status(StatusCode.OK))
diff --git a/sdk/python/src/openlit/instrumentation/firecrawl/__init__.py b/sdk/python/src/openlit/instrumentation/firecrawl/__init__.py
index cf22a727..119a5f92 100644
--- a/sdk/python/src/openlit/instrumentation/firecrawl/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/firecrawl/__init__.py
@@ -6,12 +6,11 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.firecrawl.firecrawl import (
- wrap_crawl
-)
+from openlit.instrumentation.firecrawl.firecrawl import wrap_crawl
_instruments = ("firecrawl-py >= 1.6.3",)
+
class FireCrawlInstrumentor(BaseInstrumentor):
"""
An instrumentor for firecrawl's client library.
@@ -33,15 +32,33 @@ class FireCrawlInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"firecrawl.firecrawl",
"FirecrawlApp.scrape_url",
- wrap_crawl("firecrawl.scrape_url", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ wrap_crawl(
+ "firecrawl.scrape_url",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"firecrawl.firecrawl",
"FirecrawlApp.crawl_url",
- wrap_crawl("firecrawl.crawl_url", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ wrap_crawl(
+ "firecrawl.crawl_url",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/firecrawl/firecrawl.py b/sdk/python/src/openlit/instrumentation/firecrawl/firecrawl.py
index 538c63ce..d48c4530 100644
--- a/sdk/python/src/openlit/instrumentation/firecrawl/firecrawl.py
+++ b/sdk/python/src/openlit/instrumentation/firecrawl/firecrawl.py
@@ -5,7 +5,11 @@ Module for monitoring FireCrawl calls.
import logging
from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from openlit.__helpers import (
handle_exception,
)
@@ -14,8 +18,18 @@ from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def wrap_crawl(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def wrap_crawl(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for chat completions to collect metrics.
@@ -50,28 +64,40 @@ def wrap_crawl(gen_ai_endpoint, version, environment, application_name,
"""
# pylint: disable=line-too-long
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(
+ gen_ai_endpoint, kind=SpanKind.CLIENT
+ ) as span:
response = wrapped(*args, **kwargs)
try:
# Set base span attribues
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_FIRECRAWL)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT)
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,
- gen_ai_endpoint)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TYPE,
- SemanticConvention.GEN_AI_AGENT_TYPE_BROWSER)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_PARAMS,
- str(kwargs.get("params")))
-
- url = kwargs.get("url") if "url" in kwargs else str(args[0]) if args else None
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM,
+ SemanticConvention.GEN_AI_SYSTEM_FIRECRAWL,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
+ )
+ span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)
+ span.set_attribute(SERVICE_NAME, application_name)
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_TYPE,
+ SemanticConvention.GEN_AI_AGENT_TYPE_BROWSER,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_PARAMS, str(kwargs.get("params"))
+ )
+
+ url = (
+ kwargs.get("url")
+ if "url" in kwargs
+ else str(args[0])
+ if args
+ else None
+ )
if url is not None:
span.set_attribute(SemanticConvention.GEN_AI_AGENT_BROWSE_URL, url)
diff --git a/sdk/python/src/openlit/instrumentation/google_ai_studio/__init__.py b/sdk/python/src/openlit/instrumentation/google_ai_studio/__init__.py
index 5f13bbca..82a27304 100644
--- a/sdk/python/src/openlit/instrumentation/google_ai_studio/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/google_ai_studio/__init__.py
@@ -6,15 +6,18 @@ from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
from openlit.instrumentation.google_ai_studio.google_ai_studio import (
- generate, generate_stream
+ generate,
+ generate_stream,
)
from openlit.instrumentation.google_ai_studio.async_google_ai_studio import (
- async_generate, async_generate_stream
+ async_generate,
+ async_generate_stream,
)
_instruments = ("google-genai >= 1.3.0",)
+
class GoogleAIStudioInstrumentor(BaseInstrumentor):
"""
An instrumentor for google-genai's client library.
@@ -37,32 +40,64 @@ class GoogleAIStudioInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"google.genai.models",
"Models.generate_content",
- generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ generate(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# sync stream generate
wrap_function_wrapper(
"google.genai.models",
"Models.generate_content_stream",
- generate_stream(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ generate_stream(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# async generate
wrap_function_wrapper(
"google.genai.models",
"AsyncModels.generate_content",
- async_generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_generate(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# async stream generate
wrap_function_wrapper(
"google.genai.models",
"AsyncModels.generate_content_stream",
- async_generate_stream(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_generate_stream(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/google_ai_studio/async_google_ai_studio.py b/sdk/python/src/openlit/instrumentation/google_ai_studio/async_google_ai_studio.py
index b9070c55..9916fee6 100644
--- a/sdk/python/src/openlit/instrumentation/google_ai_studio/async_google_ai_studio.py
+++ b/sdk/python/src/openlit/instrumentation/google_ai_studio/async_google_ai_studio.py
@@ -4,19 +4,25 @@ Module for monitoring Google AI Studio API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.google_ai_studio.utils import (
process_chat_response,
process_chunk,
- process_streaming_chat_response
+ process_streaming_chat_response,
)
from openlit.semcov import SemanticConvention
-def async_generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_generate(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -26,7 +32,9 @@ def async_generate(version, environment, application_name,
Wraps the GenAI function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "generativelanguage.googleapis.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "generativelanguage.googleapis.com", 443
+ )
request_model = kwargs.get("model", "gemini-2.0-flash")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -37,22 +45,22 @@ def async_generate(version, environment, application_name,
try:
response = process_chat_response(
- instance = instance,
- response=response,
- request_model=request_model,
- pricing_info=pricing_info,
- server_port=server_port,
- server_address=server_address,
- environment=environment,
- application_name=application_name,
- metrics=metrics,
- start_time=start_time,
- span=span,
- args=args,
- kwargs=kwargs,
- capture_message_content=capture_message_content,
- disable_metrics=disable_metrics,
- version=version,
+ instance=instance,
+ response=response,
+ request_model=request_model,
+ pricing_info=pricing_info,
+ server_port=server_port,
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ span=span,
+ args=args,
+ kwargs=kwargs,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
)
except Exception as e:
@@ -63,8 +71,17 @@ def async_generate(version, environment, application_name,
return wrapper
-def async_generate_stream(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_generate_stream(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -75,23 +92,23 @@ def async_generate_stream(version, environment, application_name,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
- self._llmresponse = ''
- self._finish_reason = ''
- self._output_tokens = ''
- self._input_tokens = ''
- self._response_model = ''
+ self._llmresponse = ""
+ self._finish_reason = ""
+ self._output_tokens = ""
+ self._input_tokens = ""
+ self._response_model = ""
self._tools = None
self._args = args
@@ -125,7 +142,9 @@ def async_generate_stream(version, environment, application_name,
return chunk
except StopAsyncIteration:
try:
- with tracer.start_as_current_span(self._span_name, kind= SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -134,7 +153,7 @@ def async_generate_stream(version, environment, application_name,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -146,7 +165,9 @@ def async_generate_stream(version, environment, application_name,
Wraps the GenAI function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "generativelanguage.googleapis.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "generativelanguage.googleapis.com", 443
+ )
request_model = kwargs.get("model", "gemini-2.0-flash")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -154,6 +175,8 @@ def async_generate_stream(version, environment, application_name,
awaited_wrapped = await wrapped(*args, **kwargs)
span = tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT)
- return TracedAsyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedAsyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
return wrapper
diff --git a/sdk/python/src/openlit/instrumentation/google_ai_studio/google_ai_studio.py b/sdk/python/src/openlit/instrumentation/google_ai_studio/google_ai_studio.py
index 13234572..c8692d33 100644
--- a/sdk/python/src/openlit/instrumentation/google_ai_studio/google_ai_studio.py
+++ b/sdk/python/src/openlit/instrumentation/google_ai_studio/google_ai_studio.py
@@ -4,19 +4,25 @@ Module for monitoring Google AI Studio API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.google_ai_studio.utils import (
process_chat_response,
process_chunk,
- process_streaming_chat_response
+ process_streaming_chat_response,
)
from openlit.semcov import SemanticConvention
-def generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def generate(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -26,7 +32,9 @@ def generate(version, environment, application_name,
Wraps the GenAI function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "generativelanguage.googleapis.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "generativelanguage.googleapis.com", 443
+ )
request_model = kwargs.get("model", "gemini-2.0-flash")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -37,22 +45,22 @@ def generate(version, environment, application_name,
try:
response = process_chat_response(
- instance = instance,
- response=response,
- request_model=request_model,
- pricing_info=pricing_info,
- server_port=server_port,
- server_address=server_address,
- environment=environment,
- application_name=application_name,
- metrics=metrics,
- start_time=start_time,
- span=span,
- args=args,
- kwargs=kwargs,
- capture_message_content=capture_message_content,
- disable_metrics=disable_metrics,
- version=version,
+ instance=instance,
+ response=response,
+ request_model=request_model,
+ pricing_info=pricing_info,
+ server_port=server_port,
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ start_time=start_time,
+ span=span,
+ args=args,
+ kwargs=kwargs,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
)
except Exception as e:
@@ -63,8 +71,17 @@ def generate(version, environment, application_name,
return wrapper
-def generate_stream(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def generate_stream(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -75,23 +92,23 @@ def generate_stream(version, environment, application_name,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
- self._llmresponse = ''
- self._finish_reason = ''
- self._output_tokens = ''
- self._input_tokens = ''
- self._response_model = ''
+ self._llmresponse = ""
+ self._finish_reason = ""
+ self._output_tokens = ""
+ self._input_tokens = ""
+ self._response_model = ""
self._tools = None
self._args = args
@@ -125,7 +142,9 @@ def generate_stream(version, environment, application_name,
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(self._span_name, kind= SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -134,7 +153,7 @@ def generate_stream(version, environment, application_name,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -146,7 +165,9 @@ def generate_stream(version, environment, application_name,
Wraps the GenAI function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "generativelanguage.googleapis.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "generativelanguage.googleapis.com", 443
+ )
request_model = kwargs.get("model", "gemini-2.0-flash")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -154,6 +175,8 @@ def generate_stream(version, environment, application_name,
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedSyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
return wrapper
diff --git a/sdk/python/src/openlit/instrumentation/google_ai_studio/utils.py b/sdk/python/src/openlit/instrumentation/google_ai_studio/utils.py
index efd2fb0a..a8f882b8 100644
--- a/sdk/python/src/openlit/instrumentation/google_ai_studio/utils.py
+++ b/sdk/python/src/openlit/instrumentation/google_ai_studio/utils.py
@@ -1,8 +1,13 @@
"""
Google AI Studio OpenTelemetry instrumentation utility functions
"""
+
import time
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from opentelemetry.trace import Status, StatusCode
from openlit.__helpers import (
calculate_ttft,
@@ -13,6 +18,7 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def format_content(messages):
"""
Process a list of messages to extract content.
@@ -37,7 +43,9 @@ def format_content(messages):
if part.thought:
content_str.append(f"thought: {part.thought}")
if part.code_execution_result:
- content_str.append(f"code_execution_result: {part.code_execution_result}")
+ content_str.append(
+ f"code_execution_result: {part.code_execution_result}"
+ )
if part.executable_code:
content_str.append(f"executable_code: {part.executable_code}")
if part.file_data:
@@ -45,7 +53,9 @@ def format_content(messages):
if part.function_call:
content_str.append(f"function_call: {part.function_call}")
if part.function_response:
- content_str.append(f"function_response: {part.function_response}")
+ content_str.append(
+ f"function_response: {part.function_response}"
+ )
if part.inline_data:
content_str.append(f"inline_data: {part.inline_data}")
@@ -61,6 +71,7 @@ def format_content(messages):
return prompt
+
def process_chunk(self, chunk):
"""
Process a chunk of response data and update state.
@@ -76,25 +87,41 @@ def process_chunk(self, chunk):
chunked = response_as_dict(chunk)
-
- self._response_id = str(chunked.get('response_id'))
- self._input_tokens = chunked.get('usage_metadata').get('prompt_token_count')
- self._response_model = chunked.get('model_version')
+ self._response_id = str(chunked.get("response_id"))
+ self._input_tokens = chunked.get("usage_metadata").get("prompt_token_count")
+ self._response_model = chunked.get("model_version")
if chunk.text:
self._llmresponse += str(chunk.text)
- self._output_tokens = chunked.get('usage_metadata').get('candidates_token_count')
- self._reasoning_tokens = chunked.get('usage_metadata').get('thoughts_token_count') or 0
- self._finish_reason = str(chunked.get('candidates')[0].get('finish_reason'))
+ self._output_tokens = chunked.get("usage_metadata").get("candidates_token_count")
+ self._reasoning_tokens = (
+ chunked.get("usage_metadata").get("thoughts_token_count") or 0
+ )
+ self._finish_reason = str(chunked.get("candidates")[0].get("finish_reason"))
try:
- self._tools = chunked.get('candidates', [])[0].get('content', {}).get('parts', [])[0].get('function_call', '')
+ self._tools = (
+ chunked.get("candidates", [])[0]
+ .get("content", {})
+ .get("parts", [])[0]
+ .get("function_call", "")
+ )
except:
self._tools = None
-def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream):
+
+def common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream,
+):
"""
Process chat request and generate Telemetry
"""
@@ -103,29 +130,36 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
if len(scope._timestamps) > 1:
scope._tbt = calculate_tbt(scope._timestamps)
- prompt = format_content(scope._kwargs.get('contents', ''))
+ prompt = format_content(scope._kwargs.get("contents", ""))
request_model = scope._kwargs.get("model", "gemini-2.0-flash")
- cost = get_chat_model_cost(request_model, pricing_info, scope._input_tokens, scope._output_tokens)
+ cost = get_chat_model_cost(
+ request_model, pricing_info, scope._input_tokens, scope._output_tokens
+ )
# Set Span attributes (OTel Semconv)
- scope._span.set_attribute(TELEMETRY_SDK_NAME, 'openlit')
- scope._span.set_attribute(SemanticConvention.GEN_AI_OPERATION, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT)
- scope._span.set_attribute(SemanticConvention.GEN_AI_SYSTEM, SemanticConvention.GEN_AI_SYSTEM_GEMINI)
+ scope._span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM, SemanticConvention.GEN_AI_SYSTEM_GEMINI
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, request_model)
scope._span.set_attribute(SemanticConvention.SERVER_PORT, scope._server_port)
scope._span.set_attribute(SemanticConvention.SERVER_ADDRESS, scope._server_address)
- inference_config = scope._kwargs.get('config', {})
+ inference_config = scope._kwargs.get("config", {})
attributes = [
- (SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY, 'frequency_penalty'),
- (SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, 'max_tokens'),
- (SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, 'presence_penalty'),
- (SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, 'stop_sequences'),
- (SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, 'temperature'),
- (SemanticConvention.GEN_AI_REQUEST_TOP_P, 'top_p'),
- (SemanticConvention.GEN_AI_REQUEST_TOP_K, 'top_k'),
+ (SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY, "frequency_penalty"),
+ (SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, "max_tokens"),
+ (SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, "presence_penalty"),
+ (SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, "stop_sequences"),
+ (SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, "temperature"),
+ (SemanticConvention.GEN_AI_REQUEST_TOP_P, "top_p"),
+ (SemanticConvention.GEN_AI_REQUEST_TOP_K, "top_k"),
]
# Set each attribute if the corresponding value exists and is not None
@@ -135,14 +169,26 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
if value is not None:
scope._span.set_attribute(attribute, value)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, scope._response_model)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_REASONING_TOKENS, scope._reasoning_tokens)
-
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
- 'text' if isinstance(scope._llmresponse, str) else 'json')
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_MODEL, scope._response_model
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_REASONING_TOKENS, scope._reasoning_tokens
+ )
+
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ "text" if isinstance(scope._llmresponse, str) else "json",
+ )
scope._span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
scope._span.set_attribute(SERVICE_NAME, application_name)
@@ -151,18 +197,28 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TBT, scope._tbt)
scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, scope._ttft)
scope._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
- scope._input_tokens + scope._output_tokens + scope._reasoning_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ scope._input_tokens + scope._output_tokens + scope._reasoning_tokens,
+ )
if scope._tools:
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, scope._tools.get('name',''))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get('id','')))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, str(scope._tools.get('args','')))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_NAME, scope._tools.get("name", "")
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get("id", ""))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_ARGS, str(scope._tools.get("args", ""))
+ )
# To be removed one the change to span_attributes (from span events) is complete
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
scope._span.add_event(
name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
attributes={
@@ -190,57 +246,114 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
response_model=scope._response_model,
)
- metrics['genai_client_operation_duration'].record(scope._end_time - scope._start_time, metrics_attributes)
- metrics['genai_server_tbt'].record(scope._tbt, metrics_attributes)
- metrics['genai_server_ttft'].record(scope._ttft, metrics_attributes)
- metrics['genai_requests'].add(1, metrics_attributes)
- metrics['genai_completion_tokens'].add(scope._output_tokens, metrics_attributes)
- metrics['genai_prompt_tokens'].add(scope._input_tokens, metrics_attributes)
- metrics['genai_reasoning_tokens'].add(scope._reasoning_tokens, metrics_attributes)
- metrics['genai_cost'].record(cost, metrics_attributes)
- metrics['genai_client_usage_tokens'].record(
- scope._input_tokens + scope._output_tokens + scope._reasoning_tokens, metrics_attributes)
+ metrics["genai_client_operation_duration"].record(
+ scope._end_time - scope._start_time, metrics_attributes
+ )
+ metrics["genai_server_tbt"].record(scope._tbt, metrics_attributes)
+ metrics["genai_server_ttft"].record(scope._ttft, metrics_attributes)
+ metrics["genai_requests"].add(1, metrics_attributes)
+ metrics["genai_completion_tokens"].add(scope._output_tokens, metrics_attributes)
+ metrics["genai_prompt_tokens"].add(scope._input_tokens, metrics_attributes)
+ metrics["genai_reasoning_tokens"].add(
+ scope._reasoning_tokens, metrics_attributes
+ )
+ metrics["genai_cost"].record(cost, metrics_attributes)
+ metrics["genai_client_usage_tokens"].record(
+ scope._input_tokens + scope._output_tokens + scope._reasoning_tokens,
+ metrics_attributes,
+ )
-def process_streaming_chat_response(self, pricing_info, environment, application_name, metrics,
- capture_message_content=False, disable_metrics=False, version=''):
+def process_streaming_chat_response(
+ self,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="",
+):
"""
Process chat request and generate Telemetry
"""
- common_chat_logic(self, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=True)
-
-def process_chat_response(instance, response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time,
- span, args, kwargs, capture_message_content=False, disable_metrics=False, version="1.0.0"):
+ common_chat_logic(
+ self,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=True,
+ )
+
+
+def process_chat_response(
+ instance,
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ args,
+ kwargs,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+):
"""
Process chat request and generate Telemetry
"""
- self = type('GenericScope', (), {})()
+ self = type("GenericScope", (), {})()
response_dict = response_as_dict(response)
self._start_time = start_time
self._end_time = time.time()
self._span = span
self._llmresponse = str(response.text)
- self._input_tokens = response_dict.get('usage_metadata').get('prompt_token_count')
- self._output_tokens = response_dict.get('usage_metadata').get('candidates_token_count')
- self._reasoning_tokens = response_dict.get('usage_metadata').get('thoughts_token_count') or 0
- self._response_model = response_dict.get('model_version')
+ self._input_tokens = response_dict.get("usage_metadata").get("prompt_token_count")
+ self._output_tokens = response_dict.get("usage_metadata").get(
+ "candidates_token_count"
+ )
+ self._reasoning_tokens = (
+ response_dict.get("usage_metadata").get("thoughts_token_count") or 0
+ )
+ self._response_model = response_dict.get("model_version")
self._timestamps = []
self._ttft, self._tbt = self._end_time - self._start_time, 0
self._server_address, self._server_port = server_address, server_port
self._kwargs = kwargs
- self._finish_reason = str(response_dict.get('candidates')[0].get('finish_reason'))
+ self._finish_reason = str(response_dict.get("candidates")[0].get("finish_reason"))
try:
- self._tools = response_dict.get('candidates', [])[0].get('content', {}).get('parts', [])[0].get('function_call', '')
+ self._tools = (
+ response_dict.get("candidates", [])[0]
+ .get("content", {})
+ .get("parts", [])[0]
+ .get("function_call", "")
+ )
except:
self._tools = None
- common_chat_logic(self, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=False)
+ common_chat_logic(
+ self,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=False,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/gpt4all/__init__.py b/sdk/python/src/openlit/instrumentation/gpt4all/__init__.py
index 0348dbd7..0a55334f 100644
--- a/sdk/python/src/openlit/instrumentation/gpt4all/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/gpt4all/__init__.py
@@ -5,12 +5,11 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.gpt4all.gpt4all import (
- embed, generate
-)
+from openlit.instrumentation.gpt4all.gpt4all import embed, generate
_instruments = ("gpt4all >= 2.6.0",)
+
class GPT4AllInstrumentor(BaseInstrumentor):
"""
An instrumentor for GPT4All client library.
@@ -33,16 +32,32 @@ class GPT4AllInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"gpt4all",
"GPT4All.generate",
- generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ generate(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# embed
wrap_function_wrapper(
"gpt4all",
"Embed4All.embed",
- embed(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ embed(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/gpt4all/gpt4all.py b/sdk/python/src/openlit/instrumentation/gpt4all/gpt4all.py
index b971bcc1..3ebef655 100644
--- a/sdk/python/src/openlit/instrumentation/gpt4all/gpt4all.py
+++ b/sdk/python/src/openlit/instrumentation/gpt4all/gpt4all.py
@@ -4,20 +4,26 @@ Module for monitoring GPT4All API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.gpt4all.utils import (
process_generate_response,
process_chunk,
process_streaming_generate_response,
- process_embedding_response
+ process_embedding_response,
)
from openlit.semcov import SemanticConvention
-def generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def generate(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -28,16 +34,16 @@ def generate(version, environment, application_name,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- args,
- kwargs,
- server_address,
- server_port,
- request_model,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ args,
+ kwargs,
+ server_address,
+ server_port,
+ request_model,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -75,7 +81,9 @@ def generate(version, environment, application_name,
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(self._span_name, kind=SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_generate_response(
self,
pricing_info=pricing_info,
@@ -84,7 +92,7 @@ def generate(version, environment, application_name,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -100,8 +108,13 @@ def generate(version, environment, application_name,
# Check if streaming is enabled for the API call
streaming = kwargs.get("streaming", False)
- server_address, server_port = set_server_address_and_port(instance, "127.0.0.1", 80)
- request_model = str(instance.model.model_path).rsplit("/", maxsplit=1)[-1] or "orca-mini-3b-gguf2-q4_0.gguf"
+ server_address, server_port = set_server_address_and_port(
+ instance, "127.0.0.1", 80
+ )
+ request_model = (
+ str(instance.model.model_path).rsplit("/", maxsplit=1)[-1]
+ or "orca-mini-3b-gguf2-q4_0.gguf"
+ )
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -109,7 +122,16 @@ def generate(version, environment, application_name,
# Special handling for streaming response to accommodate the nature of data flow
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(awaited_wrapped, span, span_name, args, kwargs, server_address, server_port, request_model)
+ return TracedSyncStream(
+ awaited_wrapped,
+ span,
+ span_name,
+ args,
+ kwargs,
+ server_address,
+ server_port,
+ request_model,
+ )
# Handling for non-streaming responses
else:
@@ -133,7 +155,7 @@ def generate(version, environment, application_name,
kwargs=kwargs,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -143,8 +165,17 @@ def generate(version, environment, application_name,
return wrapper
-def embed(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def embed(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -154,10 +185,17 @@ def embed(version, environment, application_name,
Wraps the GenAI function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "127.0.0.1", 80)
- request_model = str(instance.gpt4all.model.model_path).rsplit("/", maxsplit=1)[-1] or "all-MiniLM-L6-v2.gguf2.f16.gguf"
+ server_address, server_port = set_server_address_and_port(
+ instance, "127.0.0.1", 80
+ )
+ request_model = (
+ str(instance.gpt4all.model.model_path).rsplit("/", maxsplit=1)[-1]
+ or "all-MiniLM-L6-v2.gguf2.f16.gguf"
+ )
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
@@ -178,7 +216,7 @@ def embed(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/gpt4all/utils.py b/sdk/python/src/openlit/instrumentation/gpt4all/utils.py
index 56481687..0c413df6 100644
--- a/sdk/python/src/openlit/instrumentation/gpt4all/utils.py
+++ b/sdk/python/src/openlit/instrumentation/gpt4all/utils.py
@@ -1,9 +1,14 @@
"""
GPT4All OpenTelemetry instrumentation utility functions
"""
+
import time
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from opentelemetry.trace import Status, StatusCode
from openlit.__helpers import (
@@ -16,12 +21,14 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def format_content(prompt):
"""
Process a prompt to extract content.
"""
return str(prompt) if prompt else ""
+
def process_chunk(scope, chunk):
"""
Process a chunk of response data and update state.
@@ -38,8 +45,22 @@ def process_chunk(scope, chunk):
scope._llmresponse += chunk
scope._end_time = time.time()
-def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_address, server_port,
- request_model, response_model, environment, application_name, is_stream, tbt, ttft, version):
+
+def common_span_attributes(
+ scope,
+ gen_ai_operation,
+ gen_ai_system,
+ server_address,
+ server_port,
+ request_model,
+ response_model,
+ environment,
+ application_name,
+ is_stream,
+ tbt,
+ ttft,
+ version,
+):
"""
Set common span attributes for both generate and embed operations.
"""
@@ -58,9 +79,25 @@ def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_addres
scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft)
scope._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
-def record_completion_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
- request_model, response_model, environment, application_name, start_time, end_time,
- input_tokens, output_tokens, cost, tbt=None, ttft=None):
+
+def record_completion_metrics(
+ metrics,
+ gen_ai_operation,
+ gen_ai_system,
+ server_address,
+ server_port,
+ request_model,
+ response_model,
+ environment,
+ application_name,
+ start_time,
+ end_time,
+ input_tokens,
+ output_tokens,
+ cost,
+ tbt=None,
+ ttft=None,
+):
"""
Record completion-specific metrics for the operation.
"""
@@ -79,16 +116,31 @@ def record_completion_metrics(metrics, gen_ai_operation, gen_ai_system, server_a
metrics["genai_requests"].add(1, attributes)
metrics["genai_prompt_tokens"].add(input_tokens, attributes)
metrics["genai_completion_tokens"].add(output_tokens, attributes)
- metrics["genai_client_usage_tokens"].record(input_tokens + output_tokens, attributes)
+ metrics["genai_client_usage_tokens"].record(
+ input_tokens + output_tokens, attributes
+ )
metrics["genai_cost"].record(cost, attributes)
if tbt is not None:
metrics["genai_server_tbt"].record(tbt, attributes)
if ttft is not None:
metrics["genai_server_ttft"].record(ttft, attributes)
-def record_embedding_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
- request_model, response_model, environment, application_name, start_time, end_time,
- input_tokens, cost):
+
+def record_embedding_metrics(
+ metrics,
+ gen_ai_operation,
+ gen_ai_system,
+ server_address,
+ server_port,
+ request_model,
+ response_model,
+ environment,
+ application_name,
+ start_time,
+ end_time,
+ input_tokens,
+ cost,
+):
"""
Record embedding-specific metrics for the operation.
"""
@@ -109,8 +161,18 @@ def record_embedding_metrics(metrics, gen_ai_operation, gen_ai_system, server_ad
metrics["genai_prompt_tokens"].add(input_tokens, attributes)
metrics["genai_cost"].record(cost, attributes)
-def common_t2s_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream):
+
+def common_t2s_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream,
+):
"""
Process generate request and generate Telemetry
"""
@@ -118,7 +180,9 @@ def common_t2s_logic(scope, pricing_info, environment, application_name, metrics
if len(scope._timestamps) > 1:
scope._tbt = calculate_tbt(scope._timestamps)
- prompt = format_content(scope._kwargs.get("prompt") or (scope._args[0] if scope._args else "") or "")
+ prompt = format_content(
+ scope._kwargs.get("prompt") or (scope._args[0] if scope._args else "") or ""
+ )
request_model = scope._request_model
# Calculate tokens using input prompt and aggregated response
@@ -128,36 +192,80 @@ def common_t2s_logic(scope, pricing_info, environment, application_name, metrics
cost = get_chat_model_cost(request_model, pricing_info, input_tokens, output_tokens)
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_GPT4ALL,
- scope._server_address, scope._server_port, request_model, request_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_GPT4ALL,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Request parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY, scope._kwargs.get("repeat_penalty", 1.18))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, scope._kwargs.get("max_tokens", 200))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, scope._kwargs.get("presence_penalty", 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, scope._kwargs.get("temp", 0.7))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 0.4))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_K, scope._kwargs.get("top_k", 40))
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
+ scope._kwargs.get("repeat_penalty", 1.18),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
+ scope._kwargs.get("max_tokens", 200),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
+ scope._kwargs.get("presence_penalty", 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, scope._kwargs.get("temp", 0.7)
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 0.4)
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_K, scope._kwargs.get("top_k", 40)
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ "text" if isinstance(scope._llmresponse, str) else "json",
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Tools
if scope._tools:
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, scope._tools.get("function","")).get("name","")
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get("id","")))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, str(scope._tools.get("function","").get("arguments","")))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_NAME, scope._tools.get("function", "")
+ ).get("name", "")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get("id", ""))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_ARGS,
+ str(scope._tools.get("function", "").get("arguments", "")),
+ )
# Span Attributes for Content
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
# To be removed one the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -177,13 +285,36 @@ def common_t2s_logic(scope, pricing_info, environment, application_name, metrics
# Metrics
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_GPT4ALL,
- scope._server_address, scope._server_port, request_model, request_model, environment,
- application_name, scope._start_time, scope._end_time, input_tokens, output_tokens,
- cost, scope._tbt, scope._ttft)
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_GPT4ALL,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ input_tokens,
+ output_tokens,
+ cost,
+ scope._tbt,
+ scope._ttft,
+ )
-def common_embedding_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version):
+
+def common_embedding_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+):
"""
Process embedding request and generate Telemetry
"""
@@ -196,14 +327,29 @@ def common_embedding_logic(scope, pricing_info, environment, application_name, m
cost = get_embed_model_cost(request_model, pricing_info, input_tokens)
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_GPT4ALL,
- scope._server_address, scope._server_port, request_model, request_model,
- environment, application_name, False, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
+ SemanticConvention.GEN_AI_SYSTEM_GPT4ALL,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ False,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Embedding-specific span attributes
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Content
@@ -211,7 +357,9 @@ def common_embedding_logic(scope, pricing_info, environment, application_name, m
scope._span.add_event(
name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: str(scope._kwargs.get("input", "")),
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: str(
+ scope._kwargs.get("input", "")
+ ),
},
)
@@ -219,21 +367,66 @@ def common_embedding_logic(scope, pricing_info, environment, application_name, m
# Metrics
if not disable_metrics:
- record_embedding_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_GPT4ALL,
- scope._server_address, scope._server_port, request_model, request_model, environment,
- application_name, scope._start_time, scope._end_time, input_tokens, cost)
+ record_embedding_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
+ SemanticConvention.GEN_AI_SYSTEM_GPT4ALL,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ input_tokens,
+ cost,
+ )
-def process_streaming_generate_response(scope, pricing_info, environment, application_name, metrics,
- capture_message_content=False, disable_metrics=False, version=""):
+
+def process_streaming_generate_response(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="",
+):
"""
Process generate request and generate Telemetry
"""
- common_t2s_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=True)
+ common_t2s_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=True,
+ )
-def process_generate_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, args, kwargs, capture_message_content=False,
- disable_metrics=False, version="1.0.0"):
+
+def process_generate_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ args,
+ kwargs,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+):
"""
Process generate request and generate Telemetry
"""
@@ -252,14 +445,37 @@ def process_generate_response(response, request_model, pricing_info, server_port
scope._args = args
scope._tools = None
- common_t2s_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=False)
+ common_t2s_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=False,
+ )
return response
-def process_embedding_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+
+def process_embedding_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process embedding request and generate Telemetry
"""
@@ -275,7 +491,15 @@ def process_embedding_response(response, request_model, pricing_info, server_por
scope._server_address, scope._server_port = server_address, server_port
scope._kwargs = kwargs
- common_embedding_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version)
+ common_embedding_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/gpu/__init__.py b/sdk/python/src/openlit/instrumentation/gpu/__init__.py
index c511ecb4..e8e84530 100644
--- a/sdk/python/src/openlit/instrumentation/gpu/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/gpu/__init__.py
@@ -5,13 +5,18 @@ from typing import Collection, Iterable
import logging
from functools import partial
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from opentelemetry.metrics import get_meter, CallbackOptions, Observation
from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
+
class GPUInstrumentor(BaseInstrumentor):
"""
An instrumentor for collecting GPU metrics.
@@ -55,8 +60,14 @@ class GPUInstrumentor(BaseInstrumentor):
for semantic_name, internal_name in metric_names:
meter.create_observable_gauge(
name=getattr(SemanticConvention, semantic_name),
- callbacks=[partial(self._collect_metric,
- environment, application_name, internal_name)],
+ callbacks=[
+ partial(
+ self._collect_metric,
+ environment,
+ application_name,
+ internal_name,
+ )
+ ],
description=f"GPU {internal_name.replace('_', ' ').title()}",
)
@@ -67,32 +78,38 @@ class GPUInstrumentor(BaseInstrumentor):
def _get_gpu_type(self) -> str:
try:
import pynvml
+
pynvml.nvmlInit()
return "nvidia"
except Exception:
try:
import amdsmi
+
amdsmi.amdsmi_init()
return "amd"
except Exception:
return None
-
- def _collect_metric(self, environment, application_name,
- metric_name,
- options: CallbackOptions) -> Iterable[Observation]:
+ def _collect_metric(
+ self, environment, application_name, metric_name, options: CallbackOptions
+ ) -> Iterable[Observation]:
# pylint: disable=no-else-return
if self.gpu_type == "nvidia":
- return self._collect_nvidia_metrics(environment, application_name, metric_name, options)
+ return self._collect_nvidia_metrics(
+ environment, application_name, metric_name, options
+ )
elif self.gpu_type == "amd":
- return self._collect_amd_metrics(environment, application_name, metric_name, options)
+ return self._collect_amd_metrics(
+ environment, application_name, metric_name, options
+ )
return []
- def _collect_nvidia_metrics(self, environment, application_name,
- metric_name,
- options: CallbackOptions) -> Iterable[Observation]:
+ def _collect_nvidia_metrics(
+ self, environment, application_name, metric_name, options: CallbackOptions
+ ) -> Iterable[Observation]:
try:
import pynvml
+
gpu_count = pynvml.nvmlDeviceGetCount()
mega_bytes = 1024 * 1024
gpu_index = 0
@@ -103,8 +120,9 @@ class GPUInstrumentor(BaseInstrumentor):
try:
# pylint: disable=no-else-return
if metric_name == "temperature":
- return pynvml.nvmlDeviceGetTemperature(handle,
- pynvml.NVML_TEMPERATURE_GPU)
+ return pynvml.nvmlDeviceGetTemperature(
+ handle, pynvml.NVML_TEMPERATURE_GPU
+ )
elif metric_name == "utilization":
return pynvml.nvmlDeviceGetUtilizationRates(handle).gpu
elif metric_name == "utilization_enc":
@@ -115,26 +133,43 @@ class GPUInstrumentor(BaseInstrumentor):
return 0
elif metric_name == "memory_available":
memory_info = pynvml.nvmlDeviceGetMemoryInfo(handle)
- return (memory_info.free // mega_bytes) # Assuming reserved memory is 0
+ return (
+ memory_info.free // mega_bytes
+ ) # Assuming reserved memory is 0
elif metric_name == "memory_total":
- return (pynvml.nvmlDeviceGetMemoryInfo(handle).total // mega_bytes)
+ return (
+ pynvml.nvmlDeviceGetMemoryInfo(handle).total
+ // mega_bytes
+ )
elif metric_name == "memory_used":
- return (pynvml.nvmlDeviceGetMemoryInfo(handle).used // mega_bytes)
+ return (
+ pynvml.nvmlDeviceGetMemoryInfo(handle).used
+ // mega_bytes
+ )
elif metric_name == "memory_free":
- return (pynvml.nvmlDeviceGetMemoryInfo(handle).free // mega_bytes)
+ return (
+ pynvml.nvmlDeviceGetMemoryInfo(handle).free
+ // mega_bytes
+ )
elif metric_name == "power_draw":
- return (pynvml.nvmlDeviceGetPowerUsage(handle) // 1000.0)
+ return pynvml.nvmlDeviceGetPowerUsage(handle) // 1000.0
elif metric_name == "power_limit":
- return (pynvml.nvmlDeviceGetEnforcedPowerLimit(handle) // 1000.0)
+ return (
+ pynvml.nvmlDeviceGetEnforcedPowerLimit(handle) // 1000.0
+ )
except Exception as e:
# pylint: disable=cell-var-from-loop
- logger.error("Error collecting metric %s for GPU %d: %s", metric_name,
- gpu_index, e)
+ logger.error(
+ "Error collecting metric %s for GPU %d: %s",
+ metric_name,
+ gpu_index,
+ e,
+ )
return 0
def safe_decode(byte_string):
if isinstance(byte_string, bytes):
- return byte_string.decode('utf-8')
+ return byte_string.decode("utf-8")
return byte_string
attributes = {
@@ -142,19 +177,24 @@ class GPUInstrumentor(BaseInstrumentor):
SERVICE_NAME: application_name,
DEPLOYMENT_ENVIRONMENT: environment,
SemanticConvention.GPU_INDEX: str(gpu_index),
- SemanticConvention.GPU_UUID: safe_decode(pynvml.nvmlDeviceGetUUID(handle)),
- SemanticConvention.GPU_NAME: safe_decode(pynvml.nvmlDeviceGetName(handle))
+ SemanticConvention.GPU_UUID: safe_decode(
+ pynvml.nvmlDeviceGetUUID(handle)
+ ),
+ SemanticConvention.GPU_NAME: safe_decode(
+ pynvml.nvmlDeviceGetName(handle)
+ ),
}
yield Observation(get_metric_value(handle, metric_name), attributes)
except Exception as e:
logger.error("Error in GPU metrics collection: %s", e)
- def _collect_amd_metrics(self, environment, application_name,
- metric_name,
- options: CallbackOptions) -> Iterable[Observation]:
+ def _collect_amd_metrics(
+ self, environment, application_name, metric_name, options: CallbackOptions
+ ) -> Iterable[Observation]:
try:
import amdsmi
+
# Get the number of AMD GPUs
devices = amdsmi.amdsmi_get_processor_handles()
mega_bytes = 1024 * 1024
@@ -165,36 +205,69 @@ class GPUInstrumentor(BaseInstrumentor):
# pylint: disable=no-else-return
if metric_name == "temperature":
# pylint: disable=line-too-long
- return amdsmi.amdsmi_get_temp_metric(device_handle,
- amdsmi.AmdSmiTemperatureType.EDGE,
- amdsmi.AmdSmiTemperatureMetric.CURRENT)
+ return amdsmi.amdsmi_get_temp_metric(
+ device_handle,
+ amdsmi.AmdSmiTemperatureType.EDGE,
+ amdsmi.AmdSmiTemperatureMetric.CURRENT,
+ )
elif metric_name == "utilization":
# pylint: disable=line-too-long
- return amdsmi.amdsmi_get_utilization_count(device_handle,
- amdsmi.AmdSmiUtilizationCounterType.COARSE_GRAIN_GFX_ACTIVITY)
+ return amdsmi.amdsmi_get_utilization_count(
+ device_handle,
+ amdsmi.AmdSmiUtilizationCounterType.COARSE_GRAIN_GFX_ACTIVITY,
+ )
elif metric_name in ["utilization_enc", "utilization_dec"]:
return 0 # Placeholder if unsupported
elif metric_name == "fan_speed":
return amdsmi.amdsmi_get_gpu_fan_speed(device_handle, 0)
elif metric_name == "memory_available":
- return (amdsmi.amdsmi_get_gpu_memory_total(device_handle) // mega_bytes)
+ return (
+ amdsmi.amdsmi_get_gpu_memory_total(device_handle)
+ // mega_bytes
+ )
elif metric_name == "memory_total":
- return (amdsmi.amdsmi_get_gpu_memory_total(device_handle) // mega_bytes)
+ return (
+ amdsmi.amdsmi_get_gpu_memory_total(device_handle)
+ // mega_bytes
+ )
elif metric_name == "memory_used":
- return (amdsmi.amdsmi_get_gpu_memory_usage(device_handle) // mega_bytes)
+ return (
+ amdsmi.amdsmi_get_gpu_memory_usage(device_handle)
+ // mega_bytes
+ )
elif metric_name == "memory_free":
- total_mem = (amdsmi.amdsmi_get_gpu_memory_total(device_handle) // mega_bytes)
- used_mem = (amdsmi.amdsmi_get_gpu_memory_usage(device_handle) // mega_bytes)
- return (total_mem - used_mem)
+ total_mem = (
+ amdsmi.amdsmi_get_gpu_memory_total(device_handle)
+ // mega_bytes
+ )
+ used_mem = (
+ amdsmi.amdsmi_get_gpu_memory_usage(device_handle)
+ // mega_bytes
+ )
+ return total_mem - used_mem
elif metric_name == "power_draw":
# pylint: disable=line-too-long
- return (amdsmi.amdsmi_get_power_info(device_handle)['average_socket_power'] // 1000.0)
+ return (
+ amdsmi.amdsmi_get_power_info(device_handle)[
+ "average_socket_power"
+ ]
+ // 1000.0
+ )
elif metric_name == "power_limit":
# pylint: disable=line-too-long
- return (amdsmi.amdsmi_get_power_info(device_handle)['power_limit'] // 1000.0)
+ return (
+ amdsmi.amdsmi_get_power_info(device_handle)[
+ "power_limit"
+ ]
+ // 1000.0
+ )
except Exception as e:
- logger.error("Error collecting metric %s for AMD GPU %d: %s", metric_name,
- amdsmi.amdsmi_get_xgmi_info(device_handle)['index'], e)
+ logger.error(
+ "Error collecting metric %s for AMD GPU %d: %s",
+ metric_name,
+ amdsmi.amdsmi_get_xgmi_info(device_handle)["index"],
+ e,
+ )
return 0
attributes = {
@@ -202,12 +275,20 @@ class GPUInstrumentor(BaseInstrumentor):
SERVICE_NAME: application_name,
DEPLOYMENT_ENVIRONMENT: environment,
# pylint: disable=line-too-long
- SemanticConvention.GPU_INDEX: amdsmi.amdsmi_get_xgmi_info(device_handle)['index'],
+ SemanticConvention.GPU_INDEX: amdsmi.amdsmi_get_xgmi_info(
+ device_handle
+ )["index"],
# pylint: disable=line-too-long
- SemanticConvention.GPU_UUID: amdsmi.amdsmi_get_gpu_asic_info(device_handle)['market_name'],
- SemanticConvention.GPU_NAME: amdsmi.amdsmi_get_device_name(device_handle)
+ SemanticConvention.GPU_UUID: amdsmi.amdsmi_get_gpu_asic_info(
+ device_handle
+ )["market_name"],
+ SemanticConvention.GPU_NAME: amdsmi.amdsmi_get_device_name(
+ device_handle
+ ),
}
- yield Observation(get_metric_value(device_handle, metric_name), attributes)
+ yield Observation(
+ get_metric_value(device_handle, metric_name), attributes
+ )
except Exception as e:
logger.error("Error in AMD GPU metrics collection: %s", e)
diff --git a/sdk/python/src/openlit/instrumentation/groq/__init__.py b/sdk/python/src/openlit/instrumentation/groq/__init__.py
index 605af363..b82afc62 100644
--- a/sdk/python/src/openlit/instrumentation/groq/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/groq/__init__.py
@@ -10,6 +10,7 @@ from openlit.instrumentation.groq.async_groq import async_chat
_instruments = ("groq >= 0.5.0",)
+
class GroqInstrumentor(BaseInstrumentor):
"""
An instrumentor for Groq client library.
@@ -32,16 +33,32 @@ class GroqInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"groq.resources.chat.completions",
"Completions.create",
- chat(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# Chat completions
wrap_function_wrapper(
"groq.resources.chat.completions",
"AsyncCompletions.create",
- async_chat(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/groq/async_groq.py b/sdk/python/src/openlit/instrumentation/groq/async_groq.py
index b66b5fb6..adafb85d 100644
--- a/sdk/python/src/openlit/instrumentation/groq/async_groq.py
+++ b/sdk/python/src/openlit/instrumentation/groq/async_groq.py
@@ -4,19 +4,25 @@ Module for monitoring Groq API calls (async version).
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.groq.utils import (
process_chunk,
process_streaming_chat_response,
- process_chat_response
+ process_chat_response,
)
from openlit.semcov import SemanticConvention
-def async_chat(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def async_chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -27,15 +33,15 @@ def async_chat(version, environment, application_name, tracer, pricing_info,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -78,7 +84,9 @@ def async_chat(version, environment, application_name, tracer, pricing_info,
return chunk
except StopAsyncIteration:
try:
- with tracer.start_as_current_span(self._span_name, kind= SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -87,7 +95,7 @@ def async_chat(version, environment, application_name, tracer, pricing_info,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -101,7 +109,9 @@ def async_chat(version, environment, application_name, tracer, pricing_info,
"""
# Check if streaming is enabled for the API call
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "api.groq.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.groq.com", 443
+ )
request_model = kwargs.get("model", "mixtral-8x7b-32768")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -110,7 +120,9 @@ def async_chat(version, environment, application_name, tracer, pricing_info,
# Special handling for streaming response
awaited_wrapped = await wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedAsyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedAsyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
else:
# Handling for non-streaming responses
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -132,7 +144,7 @@ def async_chat(version, environment, application_name, tracer, pricing_info,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/groq/groq.py b/sdk/python/src/openlit/instrumentation/groq/groq.py
index 15da7708..724fa8e8 100644
--- a/sdk/python/src/openlit/instrumentation/groq/groq.py
+++ b/sdk/python/src/openlit/instrumentation/groq/groq.py
@@ -4,19 +4,25 @@ Module for monitoring Groq API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.groq.utils import (
process_chunk,
process_streaming_chat_response,
- process_chat_response
+ process_chat_response,
)
from openlit.semcov import SemanticConvention
-def chat(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -27,15 +33,15 @@ def chat(version, environment, application_name, tracer, pricing_info,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -78,7 +84,9 @@ def chat(version, environment, application_name, tracer, pricing_info,
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(self._span_name, kind= SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -87,7 +95,7 @@ def chat(version, environment, application_name, tracer, pricing_info,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -101,7 +109,9 @@ def chat(version, environment, application_name, tracer, pricing_info,
"""
# Check if streaming is enabled for the API call
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "api.groq.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.groq.com", 443
+ )
request_model = kwargs.get("model", "mixtral-8x7b-32768")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -110,7 +120,9 @@ def chat(version, environment, application_name, tracer, pricing_info,
# Special handling for streaming response
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedSyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
else:
# Handling for non-streaming responses
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -132,7 +144,7 @@ def chat(version, environment, application_name, tracer, pricing_info,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/groq/utils.py b/sdk/python/src/openlit/instrumentation/groq/utils.py
index 23c9980b..f07d06f1 100644
--- a/sdk/python/src/openlit/instrumentation/groq/utils.py
+++ b/sdk/python/src/openlit/instrumentation/groq/utils.py
@@ -1,6 +1,7 @@
"""
Groq OpenTelemetry instrumentation utility functions
"""
+
import time
from opentelemetry.trace import Status, StatusCode
@@ -15,6 +16,7 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def format_content(messages):
"""
Process a list of messages to extract content.
@@ -27,8 +29,9 @@ def format_content(messages):
if isinstance(content, list):
content_str = ", ".join(
- f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
- if "type" in item else f'text: {item["text"]}'
+ f"{item['type']}: {item['text'] if 'text' in item else item['image_url']}"
+ if "type" in item
+ else f"text: {item['text']}"
for item in content
)
formatted_messages.append(f"{role}: {content_str}")
@@ -37,6 +40,7 @@ def format_content(messages):
return "\n".join(formatted_messages)
+
def process_chunk(scope, chunk):
"""
Process a chunk of response data and update state.
@@ -53,27 +57,41 @@ def process_chunk(scope, chunk):
chunked = response_as_dict(chunk)
# Collect message IDs and aggregated response from events
- if (len(chunked.get("choices", [])) > 0 and
- "delta" in chunked.get("choices")[0] and
- "content" in chunked.get("choices")[0].get("delta", {})):
-
+ if (
+ len(chunked.get("choices", [])) > 0
+ and "delta" in chunked.get("choices")[0]
+ and "content" in chunked.get("choices")[0].get("delta", {})
+ ):
content = chunked.get("choices")[0].get("delta").get("content")
if content:
scope._llmresponse += content
- if chunked.get('x_groq') is not None:
- if chunked.get('x_groq').get('usage') is not None:
- scope._input_tokens = chunked.get('x_groq').get('usage').get('prompt_tokens')
- scope._output_tokens = chunked.get('x_groq').get('usage').get('completion_tokens')
- scope._response_id = chunked.get('x_groq').get('id')
- scope._response_model = chunked.get('x_groq').get('model')
- scope._finish_reason = chunked.get('choices', [{}])[0].get('finish_reason')
- scope._system_fingerprint = chunked.get('x_groq').get('system_fingerprint')
+ if chunked.get("x_groq") is not None:
+ if chunked.get("x_groq").get("usage") is not None:
+ scope._input_tokens = (
+ chunked.get("x_groq").get("usage").get("prompt_tokens")
+ )
+ scope._output_tokens = (
+ chunked.get("x_groq").get("usage").get("completion_tokens")
+ )
+ scope._response_id = chunked.get("x_groq").get("id")
+ scope._response_model = chunked.get("x_groq").get("model")
+ scope._finish_reason = chunked.get("choices", [{}])[0].get("finish_reason")
+ scope._system_fingerprint = chunked.get("x_groq").get("system_fingerprint")
scope._end_time = time.time()
-def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream):
+def common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream,
+):
"""
Process chat request and generate Telemetry
"""
@@ -84,47 +102,107 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
prompt = format_content(scope._kwargs.get("messages", []))
request_model = scope._kwargs.get("model", "mixtral-8x7b-32768")
- cost = get_chat_model_cost(request_model, pricing_info, scope._input_tokens, scope._output_tokens)
+ cost = get_chat_model_cost(
+ request_model, pricing_info, scope._input_tokens, scope._output_tokens
+ )
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_GROQ,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_GROQ,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Request parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED, scope._kwargs.get("seed", ""))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY, scope._kwargs.get("frequency_penalty", 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, scope._kwargs.get("max_completion_tokens", -1))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, scope._kwargs.get("presence_penalty", 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get("stop", []))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, scope._kwargs.get("temperature", 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, scope._kwargs.get("user", ""))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER, scope._kwargs.get("service_tier", "on_demand"))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_SEED, scope._kwargs.get("seed", "")
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
+ scope._kwargs.get("frequency_penalty", 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
+ scope._kwargs.get("max_completion_tokens", -1),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
+ scope._kwargs.get("presence_penalty", 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get("stop", [])
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
+ scope._kwargs.get("temperature", 1.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 1.0)
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_USER, scope._kwargs.get("user", "")
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER,
+ scope._kwargs.get("service_tier", "on_demand"),
+ )
# Span Attributes for Response parameters
scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT, scope._system_fingerprint)
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT, scope._system_fingerprint
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ "text" if isinstance(scope._llmresponse, str) else "json",
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens + scope._output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ scope._input_tokens + scope._output_tokens,
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Tools
if scope._tools:
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, scope._tools.get("function", {}).get("name", ""))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get("id", "")))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, str(scope._tools.get("function", {}).get("arguments", "")))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_NAME,
+ scope._tools.get("function", {}).get("name", ""),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get("id", ""))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_ARGS,
+ str(scope._tools.get("function", {}).get("arguments", "")),
+ )
# Span Attributes for Content
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
# To be removed one the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -144,23 +222,69 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
# Metrics
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_GROQ,
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, scope._output_tokens,
- cost, scope._tbt, scope._ttft)
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_GROQ,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ scope._output_tokens,
+ cost,
+ scope._tbt,
+ scope._ttft,
+ )
-def process_streaming_chat_response(scope, pricing_info, environment, application_name, metrics,
- capture_message_content=False, disable_metrics=False, version=""):
+
+def process_streaming_chat_response(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="",
+):
"""
Process chat request and generate Telemetry
"""
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=True)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=True,
+ )
+
-def process_chat_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+def process_chat_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process chat request and generate Telemetry
"""
@@ -185,15 +309,28 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
scope._server_address, scope._server_port = server_address, server_port
scope._kwargs = kwargs
scope._system_fingerprint = response_dict.get("system_fingerprint")
- scope._finish_reason = str(response_dict.get("choices", [])[0].get("finish_reason", ""))
+ scope._finish_reason = str(
+ response_dict.get("choices", [])[0].get("finish_reason", "")
+ )
# Handle tool calls
if scope._kwargs.get("tools"):
- scope._tools = response_dict.get("choices", [{}])[0].get("message", {}).get("tool_calls")
+ scope._tools = (
+ response_dict.get("choices", [{}])[0].get("message", {}).get("tool_calls")
+ )
else:
scope._tools = None
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=False)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=False,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/haystack/__init__.py b/sdk/python/src/openlit/instrumentation/haystack/__init__.py
index 17fc8c8b..943c13f9 100644
--- a/sdk/python/src/openlit/instrumentation/haystack/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/haystack/__init__.py
@@ -12,6 +12,7 @@ from openlit.instrumentation.haystack.async_haystack import async_general_wrap
_instruments = ("haystack-ai >= 2.0.0",)
+
class HaystackInstrumentor(BaseInstrumentor):
"""Optimized instrumentor for Haystack with minimal overhead"""
@@ -32,16 +33,34 @@ class HaystackInstrumentor(BaseInstrumentor):
# Pipeline operations (always enabled)
try:
wrap_function_wrapper(
- "haystack", "Pipeline.run",
- general_wrap("pipeline", version, environment, application_name,
- tracer, pricing_info, capture_message_content,
- metrics, disable_metrics)
+ "haystack",
+ "Pipeline.run",
+ general_wrap(
+ "pipeline",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
- "haystack", "AsyncPipeline.run_async",
- async_general_wrap("pipeline", version, environment,
- application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics)
+ "haystack",
+ "AsyncPipeline.run_async",
+ async_general_wrap(
+ "pipeline",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
except Exception:
pass
@@ -49,27 +68,54 @@ class HaystackInstrumentor(BaseInstrumentor):
# Component operations (only if detailed_tracing enabled)
if detailed_tracing:
components = [
- ("haystack.components.retrievers.in_memory",
- "InMemoryBM25Retriever.run", "bm25_retriever"),
- ("haystack.components.builders.prompt_builder",
- "PromptBuilder.run", "prompt_builder"),
- ("haystack.components.generators.openai",
- "OpenAIGenerator.run", "openai_generator"),
- ("haystack.components.generators.chat.openai",
- "OpenAIChatGenerator.run", "openai_chat_generator"),
- ("haystack.components.embedders.openai_text_embedder",
- "OpenAITextEmbedder.run", "text_embedder"),
- ("haystack.components.embedders.openai_document_embedder",
- "OpenAIDocumentEmbedder.run", "document_embedder"),
+ (
+ "haystack.components.retrievers.in_memory",
+ "InMemoryBM25Retriever.run",
+ "bm25_retriever",
+ ),
+ (
+ "haystack.components.builders.prompt_builder",
+ "PromptBuilder.run",
+ "prompt_builder",
+ ),
+ (
+ "haystack.components.generators.openai",
+ "OpenAIGenerator.run",
+ "openai_generator",
+ ),
+ (
+ "haystack.components.generators.chat.openai",
+ "OpenAIChatGenerator.run",
+ "openai_chat_generator",
+ ),
+ (
+ "haystack.components.embedders.openai_text_embedder",
+ "OpenAITextEmbedder.run",
+ "text_embedder",
+ ),
+ (
+ "haystack.components.embedders.openai_document_embedder",
+ "OpenAIDocumentEmbedder.run",
+ "document_embedder",
+ ),
]
for module, method, component_type in components:
try:
wrap_function_wrapper(
- module, method,
- general_wrap(component_type, version, environment,
- application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics)
+ module,
+ method,
+ general_wrap(
+ component_type,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
except Exception:
pass
diff --git a/sdk/python/src/openlit/instrumentation/haystack/async_haystack.py b/sdk/python/src/openlit/instrumentation/haystack/async_haystack.py
index 544b6f83..f6de4acc 100644
--- a/sdk/python/src/openlit/instrumentation/haystack/async_haystack.py
+++ b/sdk/python/src/openlit/instrumentation/haystack/async_haystack.py
@@ -12,8 +12,18 @@ from openlit.instrumentation.haystack.utils import (
set_server_address_and_port,
)
-def async_general_wrap(endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_general_wrap(
+ endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""Optimized async wrapper for Haystack operations"""
async def wrapper(wrapped, instance, args, kwargs):
@@ -41,10 +51,22 @@ def async_general_wrap(endpoint, version, environment, application_name,
try:
response = process_haystack_response(
- response, operation_type, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version,
- instance, args, endpoint=endpoint, **kwargs
+ response,
+ operation_type,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ args,
+ endpoint=endpoint,
+ **kwargs,
)
except Exception as e:
handle_exception(span, e)
diff --git a/sdk/python/src/openlit/instrumentation/haystack/haystack.py b/sdk/python/src/openlit/instrumentation/haystack/haystack.py
index b2c9e8fb..0e3b473c 100644
--- a/sdk/python/src/openlit/instrumentation/haystack/haystack.py
+++ b/sdk/python/src/openlit/instrumentation/haystack/haystack.py
@@ -12,8 +12,18 @@ from openlit.instrumentation.haystack.utils import (
set_server_address_and_port,
)
-def general_wrap(endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def general_wrap(
+ endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""Optimized wrapper for Haystack operations"""
def wrapper(wrapped, instance, args, kwargs):
@@ -41,10 +51,22 @@ def general_wrap(endpoint, version, environment, application_name,
try:
response = process_haystack_response(
- response, operation_type, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version,
- instance, args, endpoint=endpoint, **kwargs
+ response,
+ operation_type,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ args,
+ endpoint=endpoint,
+ **kwargs,
)
except Exception as e:
handle_exception(span, e)
diff --git a/sdk/python/src/openlit/instrumentation/haystack/utils.py b/sdk/python/src/openlit/instrumentation/haystack/utils.py
index 53793f34..0b2e7f1d 100644
--- a/sdk/python/src/openlit/instrumentation/haystack/utils.py
+++ b/sdk/python/src/openlit/instrumentation/haystack/utils.py
@@ -1,6 +1,7 @@
"""
Haystack utilities
"""
+
import time
import json
from typing import Dict, Any
@@ -19,10 +20,12 @@ OPERATION_MAP = {
"document_embedder": SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
}
+
def set_server_address_and_port(instance):
"""Fast server address extraction"""
return "localhost", 8080
+
def object_count(obj):
"""Fast object counting"""
try:
@@ -30,7 +33,10 @@ def object_count(obj):
except:
return 1
-def extract_component_technical_details(instance, args, kwargs, endpoint) -> Dict[str, Any]:
+
+def extract_component_technical_details(
+ instance, args, kwargs, endpoint
+) -> Dict[str, Any]:
"""Extract comprehensive component technical details with performance optimization"""
details = {}
@@ -41,7 +47,9 @@ def extract_component_technical_details(instance, args, kwargs, endpoint) -> Dic
details["module_name"] = instance.__class__.__module__
# Component input type extraction (optimized)
- if hasattr(instance, "_component_config") and hasattr(instance._component_config, "input_types"):
+ if hasattr(instance, "_component_config") and hasattr(
+ instance._component_config, "input_types"
+ ):
input_types = {}
for name, type_info in instance._component_config.input_types.items():
input_types[name] = str(type_info) if type_info else "Any"
@@ -53,7 +61,9 @@ def extract_component_technical_details(instance, args, kwargs, endpoint) -> Dic
details["input_types"] = input_types
# Component output type extraction (optimized)
- if hasattr(instance, "_component_config") and hasattr(instance._component_config, "output_types"):
+ if hasattr(instance, "_component_config") and hasattr(
+ instance._component_config, "output_types"
+ ):
output_types = {}
for name, type_info in instance._component_config.output_types.items():
output_types[name] = str(type_info) if type_info else "Any"
@@ -70,7 +80,7 @@ def extract_component_technical_details(instance, args, kwargs, endpoint) -> Dic
spec_info = {
"type": str(getattr(socket, "type", "Any")),
"default_value": str(getattr(socket, "default_value", None)),
- "is_optional": getattr(socket, "is_optional", False)
+ "is_optional": getattr(socket, "is_optional", False),
}
input_spec[socket_name] = spec_info
details["input_spec"] = input_spec
@@ -81,7 +91,7 @@ def extract_component_technical_details(instance, args, kwargs, endpoint) -> Dic
for socket_name, socket in config.output_sockets.items():
spec_info = {
"type": str(getattr(socket, "type", "Any")),
- "is_list": getattr(socket, "is_list", False)
+ "is_list": getattr(socket, "is_list", False),
}
output_spec[socket_name] = spec_info
details["output_spec"] = output_spec
@@ -103,6 +113,7 @@ def extract_component_technical_details(instance, args, kwargs, endpoint) -> Dic
return details
+
def extract_pipeline_metadata(instance, args, kwargs) -> Dict[str, Any]:
"""Extract pipeline-level metadata and configuration"""
metadata = {}
@@ -126,7 +137,7 @@ def extract_pipeline_metadata(instance, args, kwargs) -> Dict[str, Any]:
connection_info = {
"source": source,
"target": target,
- "data": str(data) if data else None
+ "data": str(data) if data else None,
}
connections.append(connection_info)
metadata["connections"] = connections
@@ -134,10 +145,14 @@ def extract_pipeline_metadata(instance, args, kwargs) -> Dict[str, Any]:
# Component list with types
components = []
for node in graph.nodes():
- node_data = graph.nodes[node] if hasattr(graph.nodes[node], "get") else {}
+ node_data = (
+ graph.nodes[node] if hasattr(graph.nodes[node], "get") else {}
+ )
component_info = {
"name": node,
- "type": str(type(node_data.get("instance", ""))) if node_data.get("instance") else "unknown"
+ "type": str(type(node_data.get("instance", "")))
+ if node_data.get("instance")
+ else "unknown",
}
components.append(component_info)
metadata["components"] = components
@@ -157,7 +172,9 @@ def extract_pipeline_metadata(instance, args, kwargs) -> Dict[str, Any]:
if isinstance(value, (str, int, float, bool)):
sanitized_input[key] = value
elif isinstance(value, dict):
- sanitized_input[key] = {k: str(v)[:100] for k, v in value.items()}
+ sanitized_input[key] = {
+ k: str(v)[:100] for k, v in value.items()
+ }
else:
sanitized_input[key] = str(type(value)).__name__
metadata["input_data"] = sanitized_input
@@ -168,33 +185,38 @@ def extract_pipeline_metadata(instance, args, kwargs) -> Dict[str, Any]:
return metadata
+
def extract_component_connections(instance) -> Dict[str, Any]:
"""Extract component connection and data flow information"""
connections = {}
try:
# Extract senders (components that send data to this component)
- if hasattr(instance, "_component_config") and hasattr(instance._component_config, "input_sockets"):
+ if hasattr(instance, "_component_config") and hasattr(
+ instance._component_config, "input_sockets"
+ ):
senders = []
for socket_name, socket in instance._component_config.input_sockets.items():
if hasattr(socket, "_senders") and socket._senders:
for sender in socket._senders:
- sender_info = {
- "component": str(sender),
- "socket": socket_name
- }
+ sender_info = {"component": str(sender), "socket": socket_name}
senders.append(sender_info)
connections["senders"] = senders
# Extract receivers (components that receive data from this component)
- if hasattr(instance, "_component_config") and hasattr(instance._component_config, "output_sockets"):
+ if hasattr(instance, "_component_config") and hasattr(
+ instance._component_config, "output_sockets"
+ ):
receivers = []
- for socket_name, socket in instance._component_config.output_sockets.items():
+ for (
+ socket_name,
+ socket,
+ ) in instance._component_config.output_sockets.items():
if hasattr(socket, "_receivers") and socket._receivers:
for receiver in socket._receivers:
receiver_info = {
"component": str(receiver),
- "socket": socket_name
+ "socket": socket_name,
}
receivers.append(receiver_info)
connections["receivers"] = receivers
@@ -205,26 +227,50 @@ def extract_component_connections(instance) -> Dict[str, Any]:
return connections
-def process_haystack_response(response, operation_type, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version, instance=None,
- args=None, endpoint=None, **kwargs):
+
+def process_haystack_response(
+ response,
+ operation_type,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance=None,
+ args=None,
+ endpoint=None,
+ **kwargs,
+):
"""Enhanced response processing with comprehensive technical details and optimized performance"""
end_time = time.time()
# Essential attributes
common_framework_span_attributes(
- type("Scope", (), {
- "_span": span,
- "_server_address": server_address,
- "_server_port": server_port,
- "_start_time": start_time,
- "_end_time": end_time
- })(),
+ type(
+ "Scope",
+ (),
+ {
+ "_span": span,
+ "_server_address": server_address,
+ "_server_port": server_port,
+ "_start_time": start_time,
+ "_end_time": end_time,
+ },
+ )(),
SemanticConvention.GEN_AI_SYSTEM_HAYSTACK,
- server_address, server_port, environment, application_name,
- version, endpoint, instance
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ version,
+ endpoint,
+ instance,
)
# Core operation attributes
@@ -233,41 +279,60 @@ def process_haystack_response(response, operation_type, server_address, server_p
# Enhanced technical details collection
if instance:
# Extract comprehensive component technical details
- tech_details = extract_component_technical_details(instance, args, kwargs, endpoint)
+ tech_details = extract_component_technical_details(
+ instance, args, kwargs, endpoint
+ )
# Apply component technical attributes using new semantic conventions
if tech_details.get("class_name"):
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_CLASS_NAME, tech_details["class_name"])
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_CLASS_NAME,
+ tech_details["class_name"],
+ )
if tech_details.get("input_types"):
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_INPUT_TYPES,
- json.dumps(tech_details["input_types"]))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_INPUT_TYPES,
+ json.dumps(tech_details["input_types"]),
+ )
if tech_details.get("output_types"):
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_OUTPUT_TYPES,
- json.dumps(tech_details["output_types"]))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_OUTPUT_TYPES,
+ json.dumps(tech_details["output_types"]),
+ )
if tech_details.get("input_spec"):
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_INPUT_SPEC,
- json.dumps(tech_details["input_spec"]))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_INPUT_SPEC,
+ json.dumps(tech_details["input_spec"]),
+ )
if tech_details.get("output_spec"):
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_OUTPUT_SPEC,
- json.dumps(tech_details["output_spec"]))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_OUTPUT_SPEC,
+ json.dumps(tech_details["output_spec"]),
+ )
# Component connections and data flow
connections = extract_component_connections(instance)
if connections.get("senders"):
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_SENDERS,
- json.dumps(connections["senders"]))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_SENDERS,
+ json.dumps(connections["senders"]),
+ )
if connections.get("receivers"):
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_RECEIVERS,
- json.dumps(connections["receivers"]))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_RECEIVERS,
+ json.dumps(connections["receivers"]),
+ )
# Enhanced telemetry - pipeline level
if endpoint == "pipeline" and isinstance(response, dict):
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_CONTEXT_COUNT, len(response))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_CONTEXT_COUNT, len(response)
+ )
# Enhanced pipeline metadata collection
if instance:
@@ -275,24 +340,34 @@ def process_haystack_response(response, operation_type, server_address, server_p
# Apply pipeline metadata using new semantic conventions
if pipeline_metadata.get("component_count"):
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_PIPELINE_COMPONENT_COUNT,
- pipeline_metadata["component_count"])
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_PIPELINE_COMPONENT_COUNT,
+ pipeline_metadata["component_count"],
+ )
if pipeline_metadata.get("max_runs_per_component"):
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_PIPELINE_MAX_RUNS,
- pipeline_metadata["max_runs_per_component"])
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_PIPELINE_MAX_RUNS,
+ pipeline_metadata["max_runs_per_component"],
+ )
if pipeline_metadata.get("connections"):
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_CONNECTIONS,
- json.dumps(pipeline_metadata["connections"]))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_CONNECTIONS,
+ json.dumps(pipeline_metadata["connections"]),
+ )
if pipeline_metadata.get("components"):
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_PIPELINE_METADATA,
- json.dumps(pipeline_metadata["components"]))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_PIPELINE_METADATA,
+ json.dumps(pipeline_metadata["components"]),
+ )
if pipeline_metadata.get("input_data"):
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_PIPELINE_INPUT_DATA,
- json.dumps(pipeline_metadata["input_data"]))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_PIPELINE_INPUT_DATA,
+ json.dumps(pipeline_metadata["input_data"]),
+ )
# Pipeline output data
if response:
@@ -305,53 +380,87 @@ def process_haystack_response(response, operation_type, server_address, server_p
sanitized_output[key] = f"{len(value['replies'])} replies"
else:
sanitized_output[key] = str(type(value)).__name__
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_PIPELINE_OUTPUT_DATA,
- json.dumps(sanitized_output))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_PIPELINE_OUTPUT_DATA,
+ json.dumps(sanitized_output),
+ )
# Fast LLM response extraction
for key, value in response.items():
- if key in ["llm", "generator"] and isinstance(value, dict) and "replies" in value:
+ if (
+ key in ["llm", "generator"]
+ and isinstance(value, dict)
+ and "replies" in value
+ ):
replies = value["replies"]
if replies and capture_message_content:
- span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, str(replies[0])[:500])
+ span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION,
+ str(replies[0])[:500],
+ )
break
# Enhanced telemetry - retriever level
- elif "retriever" in endpoint and isinstance(response, dict) and "documents" in response:
+ elif (
+ "retriever" in endpoint
+ and isinstance(response, dict)
+ and "documents" in response
+ ):
docs = response["documents"]
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_RETRIEVAL_COUNT, object_count(docs))
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_DOCUMENTS_COUNT, object_count(docs))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_RETRIEVAL_COUNT, object_count(docs)
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_DOCUMENTS_COUNT, object_count(docs)
+ )
# Component identification
if instance:
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_NAME, endpoint)
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_TYPE, "retriever")
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_NAME, endpoint
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_TYPE, "retriever"
+ )
# Enhanced telemetry - generator level
elif "generator" in endpoint:
# Component identification
if instance:
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_NAME, endpoint)
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_TYPE, "generator")
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_NAME, endpoint
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_TYPE, "generator"
+ )
if args and capture_message_content:
span.set_attribute(SemanticConvention.GEN_AI_PROMPT, str(args[0])[:500])
if isinstance(response, dict) and "replies" in response:
replies = response["replies"]
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_CONTEXT_COUNT, object_count(replies))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_CONTEXT_COUNT, object_count(replies)
+ )
# Enhanced telemetry - prompt builder level
elif endpoint == "prompt_builder":
# Component identification
if instance:
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_NAME, endpoint)
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_TYPE, "prompt_builder")
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_NAME, endpoint
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_COMPONENT_TYPE, "prompt_builder"
+ )
if kwargs and capture_message_content:
for key, value in kwargs.items():
if key in ["documents", "question"] and value:
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_CONTEXT_COUNT, object_count([value]))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_CONTEXT_COUNT,
+ object_count([value]),
+ )
break
# Component visit tracking (simulate component execution count)
@@ -361,17 +470,30 @@ def process_haystack_response(response, operation_type, server_address, server_p
# Duration and status
execution_time = end_time - start_time
- span.set_attribute(SemanticConvention.GEN_AI_CLIENT_OPERATION_DURATION, execution_time)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_OPERATION_DURATION, execution_time
+ )
# Pipeline execution time tracking
if endpoint == "pipeline":
- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_PIPELINE_EXECUTION_TIME, execution_time)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_PIPELINE_EXECUTION_TIME, execution_time
+ )
span.set_status(Status(StatusCode.OK))
# Metrics
if not disable_metrics:
- record_framework_metrics(metrics, operation_type, SemanticConvention.GEN_AI_SYSTEM_HAYSTACK,
- server_address, server_port, environment, application_name, start_time, end_time)
+ record_framework_metrics(
+ metrics,
+ operation_type,
+ SemanticConvention.GEN_AI_SYSTEM_HAYSTACK,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ start_time,
+ end_time,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/julep/__init__.py b/sdk/python/src/openlit/instrumentation/julep/__init__.py
index 40d68a80..59bdb594 100644
--- a/sdk/python/src/openlit/instrumentation/julep/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/julep/__init__.py
@@ -6,16 +6,13 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.julep.julep import (
- wrap_julep
-)
+from openlit.instrumentation.julep.julep import wrap_julep
-from openlit.instrumentation.julep.async_julep import (
- async_wrap_julep
-)
+from openlit.instrumentation.julep.async_julep import async_wrap_julep
_instruments = ("julep >= 1.42.0",)
+
class JulepInstrumentor(BaseInstrumentor):
"""
An instrumentor for Julep's client library.
@@ -38,43 +35,96 @@ class JulepInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"julep.resources.agents.agents",
"AgentsResource.create",
- wrap_julep("julep.agents_create", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ wrap_julep(
+ "julep.agents_create",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"julep.resources.tasks",
"TasksResource.create",
- wrap_julep("julep.task_create", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ wrap_julep(
+ "julep.task_create",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"julep.resources.executions.executions",
"ExecutionsResource.create",
- wrap_julep("julep.execution_create", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ wrap_julep(
+ "julep.execution_create",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# async
wrap_function_wrapper(
"julep.resources.agents.agents",
"AsyncAgentsResource.create",
- async_wrap_julep("julep.agents_create", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_wrap_julep(
+ "julep.agents_create",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"julep.resources.tasks",
"AsyncTasksResource.create",
- async_wrap_julep("julep.task_create", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_wrap_julep(
+ "julep.task_create",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"julep.resources.executions.executions",
"AsyncExecutionsResource.create",
- async_wrap_julep("julep.execution_create", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_wrap_julep(
+ "julep.execution_create",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
-
def _uninstrument(self, **kwargs):
# Proper uninstrumentation logic to revert patched methods
pass
diff --git a/sdk/python/src/openlit/instrumentation/julep/async_julep.py b/sdk/python/src/openlit/instrumentation/julep/async_julep.py
index 1da5e3f7..8d98738e 100644
--- a/sdk/python/src/openlit/instrumentation/julep/async_julep.py
+++ b/sdk/python/src/openlit/instrumentation/julep/async_julep.py
@@ -5,15 +5,29 @@ Module for monitoring Julep.
import logging
from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from openlit.__helpers import handle_exception
from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def async_wrap_julep(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_wrap_julep(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Creates a wrapper around a function call to trace and log its execution metrics.
@@ -55,44 +69,56 @@ def async_wrap_julep(gen_ai_endpoint, version, environment, application_name,
errors are handled and logged appropriately.
"""
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(
+ gen_ai_endpoint, kind=SpanKind.CLIENT
+ ) as span:
response = await wrapped(*args, **kwargs)
try:
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,
- gen_ai_endpoint)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_JULEP)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT)
+ span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM,
+ SemanticConvention.GEN_AI_SYSTEM_JULEP,
+ )
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
+ span.set_attribute(SERVICE_NAME, application_name)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
+ )
if gen_ai_endpoint == "julep.agents_create":
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ID,
- response.id)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ROLE,
- kwargs.get("name", ""))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- kwargs.get("model", "gpt-4-turbo"))
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_CONTEXT,
- kwargs.get("about", ""))
+ span.set_attribute(SemanticConvention.GEN_AI_AGENT_ID, response.id)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_ROLE, kwargs.get("name", "")
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MODEL,
+ kwargs.get("model", "gpt-4-turbo"),
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_CONTEXT, kwargs.get("about", "")
+ )
elif gen_ai_endpoint == "julep.task_create":
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TOOLS,
- str(kwargs.get("tools", "")))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_TOOLS,
+ str(kwargs.get("tools", "")),
+ )
elif gen_ai_endpoint == "julep.execution_create":
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TASK_ID,
- kwargs.get("task_id", ""))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_TASK_ID,
+ kwargs.get("task_id", ""),
+ )
if capture_message_content:
span.add_event(
name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT:str(kwargs.get("input", "")),
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: str(
+ kwargs.get("input", "")
+ ),
},
)
diff --git a/sdk/python/src/openlit/instrumentation/julep/julep.py b/sdk/python/src/openlit/instrumentation/julep/julep.py
index 1480e9b6..a1bfc8dd 100644
--- a/sdk/python/src/openlit/instrumentation/julep/julep.py
+++ b/sdk/python/src/openlit/instrumentation/julep/julep.py
@@ -5,15 +5,29 @@ Module for monitoring Julep.
import logging
from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from openlit.__helpers import handle_exception
from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def wrap_julep(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def wrap_julep(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Creates a wrapper around a function call to trace and log its execution metrics.
@@ -55,48 +69,59 @@ def wrap_julep(gen_ai_endpoint, version, environment, application_name,
errors are handled and logged appropriately.
"""
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(
+ gen_ai_endpoint, kind=SpanKind.CLIENT
+ ) as span:
response = wrapped(*args, **kwargs)
try:
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,
- gen_ai_endpoint)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_JULEP)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT)
+ span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM,
+ SemanticConvention.GEN_AI_SYSTEM_JULEP,
+ )
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
+ span.set_attribute(SERVICE_NAME, application_name)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
+ )
if gen_ai_endpoint == "julep.agents_create":
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ID,
- response.id)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ROLE,
- kwargs.get("name", ""))
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- kwargs.get("model", "gpt-4-turbo"))
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_CONTEXT,
- kwargs.get("about", ""))
+ span.set_attribute(SemanticConvention.GEN_AI_AGENT_ID, response.id)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_ROLE, kwargs.get("name", "")
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MODEL,
+ kwargs.get("model", "gpt-4-turbo"),
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_CONTEXT, kwargs.get("about", "")
+ )
elif gen_ai_endpoint == "julep.task_create":
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TOOLS,
- str(kwargs.get("tools", "")))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_TOOLS,
+ str(kwargs.get("tools", "")),
+ )
elif gen_ai_endpoint == "julep.execution_create":
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TASK_ID,
- kwargs.get("task_id", ""))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_TASK_ID,
+ kwargs.get("task_id", ""),
+ )
if capture_message_content:
span.add_event(
name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT:str(kwargs.get("input", "")),
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: str(
+ kwargs.get("input", "")
+ ),
},
)
-
span.set_status(Status(StatusCode.OK))
# Return original response
diff --git a/sdk/python/src/openlit/instrumentation/langchain/__init__.py b/sdk/python/src/openlit/instrumentation/langchain/__init__.py
index a136adbd..75b99ca4 100644
--- a/sdk/python/src/openlit/instrumentation/langchain/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/langchain/__init__.py
@@ -1,68 +1,47 @@
-"""Initializer of Auto Instrumentation of LangChain Functions"""
+"""
+OpenLIT LangChain Instrumentation - Callback-Based Hierarchical Spans
+"""
+
from typing import Collection
import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.langchain.langchain import (
- hub,
- chat
-)
-from openlit.instrumentation.langchain.async_langchain import (
- async_hub,
- async_chat
+from openlit.instrumentation.langchain.callback_handler import (
+ OpenLITLangChainCallbackHandler,
)
_instruments = ("langchain >= 0.1.20",)
-WRAPPED_METHODS = [
- {
- "package": "langchain.hub",
- "object": "pull",
- "endpoint": "langchain.retrieve.prompt",
- "wrapper": hub,
- },
- {
- "package": "langchain_core.language_models.llms",
- "object": "BaseLLM.invoke",
- "endpoint": "langchain.llm",
- "wrapper": chat,
- },
- {
- "package": "langchain_core.language_models.llms",
- "object": "BaseLLM.ainvoke",
- "endpoint": "langchain.llm",
- "wrapper": async_chat,
- },
- {
- "package": "langchain_core.language_models.chat_models",
- "object": "BaseChatModel.invoke",
- "endpoint": "langchain.chat_models",
- "wrapper": chat,
- },
- {
- "package": "langchain_core.language_models.chat_models",
- "object": "BaseChatModel.ainvoke",
- "endpoint": "langchain.chat_models",
- "wrapper": async_chat,
- },
- {
- "package": "langchain.chains.base",
- "object": "Chain.invoke",
- "endpoint": "langchain.chain.invoke",
- "wrapper": chat,
- },
- {
- "package": "langchain.chains.base",
- "object": "Chain.ainvoke",
- "endpoint": "langchain.chain.invoke",
- "wrapper": async_chat,
- }
-]
+
+class CallbackManagerWrapper: # pylint: disable=too-few-public-methods
+ """Wrapper to inject OpenLIT callback handler into LangChain's callback system"""
+
+ def __init__(self, callback_handler: OpenLITLangChainCallbackHandler):
+ self.callback_handler = callback_handler
+
+ def __call__(self, wrapped, instance, args, kwargs):
+ """Inject OpenLIT callback handler when BaseCallbackManager is initialized"""
+
+ # Call original initialization
+ wrapped(*args, **kwargs)
+
+ # Check if our callback handler is already registered
+ for handler in instance.inheritable_handlers:
+ if isinstance(handler, type(self.callback_handler)):
+ break
+ else:
+ # Add our callback handler to the manager
+ instance.add_handler(self.callback_handler, True)
+
class LangChainInstrumentor(BaseInstrumentor):
"""
- An instrumentor for LangChain client library.
+ OpenLIT LangChain instrumentor using callback-based hierarchical span creation.
+
+ This approach hooks into LangChain's built-in callback system to create
+ proper parent-child span relationships automatically, providing superior
+ observability with OpenLIT's comprehensive business intelligence.
"""
def instrumentation_dependencies(self) -> Collection[str]:
@@ -78,17 +57,49 @@ class LangChainInstrumentor(BaseInstrumentor):
metrics = kwargs.get("metrics_dict")
disable_metrics = kwargs.get("disable_metrics")
- for wrapped_method in WRAPPED_METHODS:
- wrap_package = wrapped_method.get("package")
- wrap_object = wrapped_method.get("object")
- gen_ai_endpoint = wrapped_method.get("endpoint")
- wrapper = wrapped_method.get("wrapper")
+ # Create OpenLIT callback handler with all configuration
+ openlit_callback_handler = OpenLITLangChainCallbackHandler(
+ tracer=tracer,
+ version=version,
+ environment=environment,
+ application_name=application_name,
+ pricing_info=pricing_info,
+ capture_message_content=capture_message_content,
+ metrics=metrics,
+ disable_metrics=disable_metrics,
+ )
+
+ # Hook into LangChain's callback system
+ # This automatically provides hierarchical spans for:
+ # - RunnableSequence (workflow spans)
+ # - PromptTemplate (task spans)
+ # - ChatOpenAI (chat spans)
+ # - Tools, Retrievers, etc.
+ try:
wrap_function_wrapper(
- wrap_package,
- wrap_object,
- wrapper(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ module="langchain_core.callbacks",
+ name="BaseCallbackManager.__init__",
+ wrapper=CallbackManagerWrapper(openlit_callback_handler),
)
+ except Exception:
+ # Graceful degradation if callback system unavailable
+ pass
+
+ # === COMPETITIVE ADVANTAGE SUMMARY ===
+ # Callback-based approach provides:
+ # 1. ✅ Proper span hierarchy (like OpenLLMetry/OpenInference)
+ # 2. ✅ Comprehensive business intelligence (cost, tokens, performance)
+ # 3. ✅ Automatic operation coverage (no manual function wrapping)
+ # 4. ✅ Native LangChain integration (uses built-in callback system)
+ # 5. ✅ Superior observability depth and quality
+
+ # Result: Best of both worlds - hierarchy + business intelligence
def _uninstrument(self, **kwargs):
- pass
+ """Remove instrumentation"""
+ try:
+ from opentelemetry.instrumentation.utils import unwrap
+
+ unwrap("langchain_core.callbacks", "BaseCallbackManager.__init__")
+ except Exception:
+ pass
diff --git a/sdk/python/src/openlit/instrumentation/langchain/async_langchain.py b/sdk/python/src/openlit/instrumentation/langchain/async_langchain.py
deleted file mode 100644
index 45ef71f3..00000000
--- a/sdk/python/src/openlit/instrumentation/langchain/async_langchain.py
+++ /dev/null
@@ -1,102 +0,0 @@
-"""
-Module for monitoring LangChain API calls.
-"""
-
-import time
-from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
-from openlit.instrumentation.langchain.utils import (
- get_model_from_instance,
- process_chat_response,
- process_hub_response,
-)
-from openlit.semcov import SemanticConvention
-
-def async_hub(gen_ai_endpoint, version, environment, application_name, tracer,
- pricing_info, capture_message_content, metrics, disable_metrics):
- """
- Generates a telemetry wrapper for LangChain async hub operations.
- """
-
- async def wrapper(wrapped, instance, args, kwargs):
- """
- Wraps the LangChain async hub operation call.
- """
-
- server_address, server_port = set_server_address_and_port(instance, "langchain.com", 443)
-
- with tracer.start_as_current_span(gen_ai_endpoint, kind=SpanKind.CLIENT) as span:
- response = await wrapped(*args, **kwargs)
-
- try:
- response = process_hub_response(
- response=response,
- gen_ai_endpoint=gen_ai_endpoint,
- server_port=server_port,
- server_address=server_address,
- environment=environment,
- application_name=application_name,
- span=span,
- version=version
- )
-
- except Exception as e:
- handle_exception(span, e)
-
- return response
-
- return wrapper
-
-def async_chat(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
- """
- Generates a telemetry wrapper for LangChain async chat operations.
- """
-
- async def wrapper(wrapped, instance, args, kwargs):
- """
- Wraps the LangChain async chat operation call.
- """
-
- server_address, server_port = set_server_address_and_port(instance, "langchain.com", 443)
- request_model = get_model_from_instance(instance)
-
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
-
- with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
- start_time = time.time()
- response = await wrapped(*args, **kwargs)
- end_time = time.time()
-
- try:
- # Add instance to kwargs for processing
- kwargs["instance"] = instance
-
- response = process_chat_response(
- response=response,
- request_model=request_model,
- pricing_info=pricing_info,
- server_port=server_port,
- server_address=server_address,
- environment=environment,
- application_name=application_name,
- metrics=metrics,
- start_time=start_time,
- end_time=end_time,
- span=span,
- capture_message_content=capture_message_content,
- disable_metrics=disable_metrics,
- version=version,
- args=args,
- **kwargs
- )
-
- except Exception as e:
- handle_exception(span, e)
-
- return response
-
- return wrapper
diff --git a/sdk/python/src/openlit/instrumentation/langchain/callback_handler.py b/sdk/python/src/openlit/instrumentation/langchain/callback_handler.py
new file mode 100644
index 00000000..b629390a
--- /dev/null
+++ b/sdk/python/src/openlit/instrumentation/langchain/callback_handler.py
@@ -0,0 +1,1100 @@
+"""
+OpenLIT LangChain Callback Handler for Hierarchical Span Creation
+"""
+
+import time
+import json
+from datetime import datetime, timezone
+from typing import Any, Dict, List, Optional
+from uuid import UUID
+
+from langchain_core.callbacks import BaseCallbackHandler
+from langchain_core.messages import BaseMessage
+from langchain_core.outputs import LLMResult, ChatGeneration, Generation
+
+from opentelemetry import context as context_api
+from opentelemetry.trace import SpanKind, set_span_in_context, Status, StatusCode
+from opentelemetry.trace.span import Span
+
+from openlit.__helpers import (
+ common_framework_span_attributes,
+ handle_exception,
+ get_chat_model_cost,
+ general_tokens,
+)
+from openlit.semcov import SemanticConvention
+
+# Enhanced Provider Mapping (inspired by OpenInference)
+LANGCHAIN_PROVIDER_MAP = {
+ "anthropic": "anthropic",
+ "azure": "azure",
+ "azure_ai": "azure",
+ "azure_openai": "azure",
+ "bedrock": "aws",
+ "bedrock_converse": "aws",
+ "cohere": "cohere",
+ "deepseek": "deepseek",
+ "fireworks": "fireworks",
+ "google": "google",
+ "google_anthropic_vertex": "google",
+ "google_genai": "google",
+ "google_vertexai": "google",
+ "groq": "groq",
+ "huggingface": "huggingface",
+ "ibm": "ibm",
+ "mistralai": "mistralai",
+ "nvidia": "nvidia",
+ "ollama": "ollama",
+ "openai": "openai",
+ "perplexity": "perplexity",
+ "together": "together",
+ "vertex": "google",
+ "vertexai": "google",
+ "xai": "xai",
+}
+
+
+class SpanHolder: # pylint: disable=too-few-public-methods
+ """Holds span information and tracks relationships"""
+
+ def __init__(self, span: Span, start_time: float, context_token=None):
+ self.span = span
+ self.start_time = start_time
+ self.children: List[UUID] = []
+ self.workflow_name = ""
+ self.entity_name = ""
+ self.context_token = context_token # For context restoration
+
+
+class OpenLITLangChainCallbackHandler(BaseCallbackHandler):
+ """
+ Enhanced OpenLIT callback handler
+ """
+
+ def __init__(
+ self,
+ tracer,
+ version,
+ environment,
+ application_name,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ):
+ super().__init__()
+ self.tracer = tracer
+ self.version = version
+ self.environment = environment
+ self.application_name = application_name
+ self.pricing_info = pricing_info
+ self.capture_message_content = capture_message_content
+ self.metrics = metrics
+ self.disable_metrics = disable_metrics
+
+ # Track active spans by run_id
+ self.spans: Dict[UUID, SpanHolder] = {}
+
+ self.session_name = environment # Map environment to session
+ self.tags_enabled = True # Enable tagging system
+ self.events_enabled = True # Enable events tracking
+ self.performance_baselines = {} # Store performance baselines
+ self.error_classification_enabled = True # Enable error classification
+
+ # Required BaseCallbackHandler properties
+ @property
+ def raise_error(self) -> bool:
+ """Should the handler raise errors instead of logging them."""
+ return False
+
+ @property
+ def run_inline(self) -> bool:
+ """Should the handler run inline with the main thread."""
+ return True
+
+ # Ignore flags - all set to False so we capture everything
+ @property
+ def ignore_llm(self) -> bool:
+ return False
+
+ @property
+ def ignore_chain(self) -> bool:
+ return False
+
+ @property
+ def ignore_agent(self) -> bool:
+ return False
+
+ @property
+ def ignore_retriever(self) -> bool:
+ return False
+
+ @property
+ def ignore_chat_model(self) -> bool:
+ return False
+
+ def _get_span_name(self, serialized: Dict[str, Any], operation_type: str) -> str:
+ """Generate OpenLIT-style span names following our naming convention"""
+
+ # Handle None serialized (common for RunnableSequence)
+ if not serialized:
+ return f"{operation_type} RunnableSequence"
+
+ # Extract class name for component identification
+ if "id" in serialized and serialized["id"]:
+ component_name = serialized["id"][-1] # Last part is usually the class name
+ elif "name" in serialized:
+ component_name = serialized["name"]
+ else:
+ component_name = "unknown"
+
+ # Follow OpenLIT naming: {operation_type} {component_name}
+ return f"{operation_type} {component_name}"
+
+ def _create_span(
+ self,
+ run_id: UUID,
+ parent_run_id: Optional[UUID],
+ span_name: str,
+ kind: SpanKind = SpanKind.CLIENT,
+ model_name: Optional[str] = None,
+ ) -> Span:
+ """Create a span with proper parent-child relationship and set as active context"""
+
+ # If we have a parent, create child span in parent context
+ if parent_run_id and parent_run_id in self.spans:
+ parent_span = self.spans[parent_run_id].span
+ span = self.tracer.start_span(
+ span_name, context=set_span_in_context(parent_span), kind=kind
+ )
+ # Track parent-child relationship
+ self.spans[parent_run_id].children.append(run_id)
+ else:
+ # Create root span
+ span = self.tracer.start_span(span_name, kind=kind)
+
+ # Set this span as the active context so downstream instrumentations (like OpenAI)
+ # will create child spans under it
+ span_context = set_span_in_context(span)
+ context_token = context_api.attach(span_context)
+
+ # Store span with start time and context token
+ start_time = time.time()
+ self.spans[run_id] = SpanHolder(span, start_time, context_token)
+
+ # Set common framework span attributes for consistency
+ scope = type("GenericScope", (), {})()
+ scope._span = span
+ scope._start_time = start_time
+ scope._end_time = None
+
+ # Create mock instance with model name for common_framework_span_attributes
+ mock_instance = None
+ if model_name:
+ mock_instance = type("MockInstance", (), {"model_name": model_name})()
+
+ common_framework_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,
+ "localhost", # Default server address for LangChain
+ 8080, # Default port
+ self.environment,
+ self.application_name,
+ self.version,
+ span_name,
+ mock_instance,
+ )
+
+ return span
+
+ def _end_span(self, run_id: UUID) -> None:
+ """End span and all its children, restore context"""
+ if run_id not in self.spans:
+ return
+
+ span_holder = self.spans[run_id]
+ span = span_holder.span
+
+ # End all child spans first
+ for child_id in span_holder.children:
+ if child_id in self.spans:
+ self._end_span(child_id)
+
+ # Restore the previous context before ending span
+ if span_holder.context_token:
+ context_api.detach(span_holder.context_token)
+
+ # Update end time for duration calculation
+ end_time = time.time()
+ duration = end_time - span_holder.start_time
+ span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_OPERATION_DURATION, duration
+ )
+
+ # End this span
+ span.set_status(Status(StatusCode.OK))
+ span.end()
+
+ # Clean up
+ del self.spans[run_id]
+
+ def _add_langsmith_events(
+ self, span: Span, event_type: str, data: Optional[Dict] = None
+ ):
+ """Add LangSmith-style events to spans"""
+ if not self.events_enabled:
+ return
+
+ try:
+ event_data = {
+ "timestamp": datetime.now(timezone.utc).isoformat(),
+ "event_type": event_type,
+ "session_name": self.session_name,
+ }
+
+ if data:
+ event_data.update(data)
+
+ span.add_event(f"langchain.{event_type}", event_data)
+ except Exception:
+ pass # Graceful degradation
+
+ def _classify_error(self, error: Exception) -> str:
+ """Classify errors like LangSmith does"""
+ error_type = type(error).__name__
+
+ # LangSmith-style error classification
+ if "rate" in str(error).lower() or "429" in str(error):
+ return "RATE_LIMIT_ERROR"
+ elif "timeout" in str(error).lower():
+ return "TIMEOUT_ERROR"
+ elif "auth" in str(error).lower() or "401" in str(error):
+ return "AUTH_ERROR"
+ elif "not found" in str(error).lower() or "404" in str(error):
+ return "NOT_FOUND_ERROR"
+ elif "connection" in str(error).lower():
+ return "CONNECTION_ERROR"
+ elif "validation" in str(error).lower():
+ return "VALIDATION_ERROR"
+ else:
+ return f"GENERAL_ERROR_{error_type}"
+
+ def _add_tags_from_context(self, span: Span, run_id: UUID, **kwargs):
+ """Add LangSmith-style tags to spans"""
+ if not self.tags_enabled:
+ return
+
+ try:
+ tags = []
+
+ # Auto-generate tags based on context
+ if "model" in kwargs:
+ tags.append(f"model:{kwargs['model']}")
+ if "temperature" in kwargs:
+ tags.append(f"temperature:{kwargs['temperature']}")
+ if hasattr(kwargs.get("invocation_params", {}), "stream"):
+ tags.append(
+ f"streaming:{kwargs['invocation_params'].get('stream', False)}"
+ )
+
+ # Add session tag
+ tags.append(f"session:{self.session_name}")
+
+ # Add environment tag
+ tags.append(f"env:{self.environment}")
+
+ if tags:
+ span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_TAGS, tags)
+
+ except Exception:
+ pass # Graceful degradation
+
+ def _track_performance_baseline(
+ self, span: Span, operation_name: str, duration_ms: float
+ ):
+ """Track performance against baselines like LangSmith"""
+ try:
+ # Store baseline if not exists
+ if operation_name not in self.performance_baselines:
+ self.performance_baselines[operation_name] = {
+ "avg_duration": duration_ms,
+ "min_duration": duration_ms,
+ "max_duration": duration_ms,
+ "count": 1,
+ }
+ else:
+ baseline = self.performance_baselines[operation_name]
+ baseline["count"] += 1
+ baseline["avg_duration"] = (
+ baseline["avg_duration"] * (baseline["count"] - 1) + duration_ms
+ ) / baseline["count"]
+ baseline["min_duration"] = min(baseline["min_duration"], duration_ms)
+ baseline["max_duration"] = max(baseline["max_duration"], duration_ms)
+
+ # Add performance comparison attributes
+ baseline = self.performance_baselines[operation_name]
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_PERFORMANCE_VS_BASELINE,
+ duration_ms / baseline["avg_duration"],
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_PERFORMANCE_BASELINE_AVG,
+ baseline["avg_duration"],
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_PERFORMANCE_BASELINE_PERCENTILE,
+ _calculate_percentile(duration_ms, baseline),
+ )
+
+ except Exception:
+ pass # Graceful degradation
+
+ # Enhanced callback methods with new features
+ def on_llm_start(
+ self,
+ serialized: Dict[str, Any],
+ prompts: List[str],
+ *,
+ run_id: UUID,
+ parent_run_id: Optional[UUID] = None,
+ **kwargs: Any,
+ ) -> None:
+ """Called when an LLM starts"""
+
+ try:
+ # Create workflow span for chain operations
+ span_name = self._get_span_name(serialized, "workflow")
+ span = self._create_span(
+ run_id, parent_run_id, span_name, SpanKind.INTERNAL
+ )
+
+ # Set OpenLIT attributes
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM,
+ SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
+ )
+
+ # Set workflow-specific attributes
+ if serialized and "id" in serialized and serialized["id"]:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_WORKFLOW_TYPE, serialized["id"][-1]
+ )
+ else:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_WORKFLOW_TYPE, "RunnableSequence"
+ )
+
+ # LangSmith-style serialized function capture
+ self._capture_serialized_info(span, serialized)
+
+ # Capture input if enabled (with safe JSON serialization)
+ if self.capture_message_content:
+ try:
+ input_str = json.dumps(prompts, default=str)[:1000]
+ span.set_attribute(
+ SemanticConvention.GEN_AI_WORKFLOW_INPUT, input_str
+ )
+ except Exception:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_WORKFLOW_INPUT, str(prompts)[:1000]
+ )
+
+ # Framework enhancements - use attributes only (not events for non-chat operations)
+
+ self._add_tags_from_context(span, run_id, **kwargs)
+
+ # Additional metadata is captured through semantic conventions above
+
+ # Extract provider information (OpenInference-inspired enhancement)
+ self._extract_provider_info(span, **kwargs)
+
+ except Exception:
+ # Graceful error handling to prevent callback system failure
+ pass
+
+ def on_llm_end(
+ self,
+ response: LLMResult,
+ *,
+ run_id: UUID,
+ parent_run_id: Optional[UUID] = None,
+ **kwargs: Any,
+ ) -> None:
+ """Called when an LLM call ends"""
+
+ try:
+ if run_id not in self.spans:
+ return
+
+ span_holder = self.spans[run_id]
+ span = span_holder.span
+
+ # Process LLM response with OpenLIT's business intelligence
+ self._process_llm_response(span, response, run_id)
+
+ # Duration is set in _end_span method
+
+ # NEW: Add performance baseline tracking
+ if span_holder:
+ duration_ms = (time.time() - span_holder.start_time) * 1000
+ self._track_performance_baseline(span, span.name, duration_ms)
+
+ # Framework completion - use attributes only (not events for non-chat operations)
+
+ self._end_span(run_id)
+
+ except Exception:
+ # Graceful error handling
+ pass
+
+ def on_llm_error(
+ self,
+ error: Exception,
+ *,
+ run_id: UUID,
+ parent_run_id: Optional[UUID] = None,
+ **kwargs: Any,
+ ) -> None:
+ """Called when an LLM call ends with an error"""
+
+ try:
+ if run_id not in self.spans:
+ return
+
+ span_holder = self.spans[run_id]
+ span = span_holder.span
+
+ # NEW: Enhanced error classification and tracking
+ # Framework error classification
+ error_class = self._classify_error(error)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_ERROR_CLASS, error_class
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_ERROR_TYPE, type(error).__name__
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_ERROR_MESSAGE, str(error)
+ )
+
+ # Framework error - use attributes only (not events for non-chat operations)
+
+ # Set error status
+ span.set_status(Status(StatusCode.ERROR, str(error)))
+ span.record_exception(error)
+
+ self._end_span(run_id)
+
+ except Exception:
+ # Graceful error handling
+ pass
+
+ def on_chain_start(
+ self,
+ serialized: Dict[str, Any],
+ inputs: Dict[str, Any],
+ *,
+ run_id: UUID,
+ parent_run_id: Optional[UUID] = None,
+ tags: Optional[List[str]] = None,
+ metadata: Optional[Dict[str, Any]] = None,
+ **kwargs: Any,
+ ) -> None:
+ """Called when a chain (RunnableSequence, etc.) starts"""
+
+ try:
+ # Create workflow span for chain operations
+ span_name = self._get_span_name(serialized, "workflow")
+ span = self._create_span(
+ run_id, parent_run_id, span_name, SpanKind.INTERNAL
+ )
+
+ # Set OpenLIT attributes
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM,
+ SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
+ )
+
+ # Set workflow-specific attributes
+ if serialized and "id" in serialized and serialized["id"]:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_WORKFLOW_TYPE, serialized["id"][-1]
+ )
+ else:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_WORKFLOW_TYPE, "RunnableSequence"
+ )
+
+ # LangSmith-style serialized function capture
+ self._capture_serialized_info(span, serialized)
+
+ # Capture input if enabled (with safe JSON serialization)
+ if self.capture_message_content:
+ try:
+ input_str = json.dumps(inputs, default=str)[:1000]
+ span.set_attribute(
+ SemanticConvention.GEN_AI_WORKFLOW_INPUT, input_str
+ )
+ except Exception:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_WORKFLOW_INPUT, str(inputs)[:1000]
+ )
+
+ # Enhanced chain tracking - use attributes only (not events for non-chat operations)
+
+ # Add chain-specific tags
+ chain_tags = [f"chain_type:{serialized.get('id', ['unknown'])[-1]}"]
+ if inputs:
+ chain_tags.append(f"input_count:{len(inputs)}")
+ span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_TAGS, chain_tags)
+
+ except Exception:
+ # Graceful error handling to prevent callback system failure
+ pass
+
+ def on_chain_end(
+ self,
+ outputs: Dict[str, Any],
+ *,
+ run_id: UUID,
+ parent_run_id: Optional[UUID] = None,
+ **kwargs: Any,
+ ) -> None:
+ """Called when a chain ends"""
+
+ try:
+ if run_id not in self.spans:
+ return
+
+ span = self.spans[run_id].span
+
+ # Capture output if enabled (with safe JSON serialization)
+ if self.capture_message_content:
+ try:
+ output_str = json.dumps(outputs, default=str)[:1000]
+ span.set_attribute(
+ SemanticConvention.GEN_AI_WORKFLOW_OUTPUT, output_str
+ )
+ except Exception:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_WORKFLOW_OUTPUT, str(outputs)[:1000]
+ )
+
+ # Duration is set in _end_span method
+
+ self._end_span(run_id)
+
+ except Exception:
+ # Graceful error handling
+ pass
+
+ def on_chat_model_start(
+ self,
+ serialized: Dict[str, Any],
+ messages: List[List[BaseMessage]],
+ *,
+ run_id: UUID,
+ parent_run_id: Optional[UUID] = None,
+ tags: Optional[List[str]] = None,
+ metadata: Optional[Dict[str, Any]] = None,
+ **kwargs: Any,
+ ) -> None:
+ """Called when a chat model (ChatOpenAI, etc.) starts"""
+
+ try:
+ # Extract model name from multiple sources with fallback chain
+ model_name = "unknown"
+
+ # DEBUG: Log serialized data to understand structure
+ # This helps us improve model extraction logic
+ # print(f"DEBUG: serialized={serialized}")
+ # print(f"DEBUG: kwargs={kwargs}")
+
+ # Try extracting from serialized kwargs first (most reliable)
+ if (
+ serialized
+ and "kwargs" in serialized
+ and "model" in serialized["kwargs"]
+ ):
+ model_name = serialized["kwargs"]["model"]
+ elif kwargs.get("model"):
+ model_name = kwargs["model"]
+ elif serialized:
+ # Try extracting from different parts of serialized data
+ # LangChain often stores model info in various locations
+ if "model" in serialized:
+ model_name = serialized["model"]
+ elif "model_name" in serialized:
+ model_name = serialized["model_name"]
+ elif (
+ "name" in serialized and "model" in str(serialized["name"]).lower()
+ ):
+ model_name = serialized["name"]
+ elif "id" in serialized and serialized["id"]:
+ # Extract from class identifier - this gives us the framework being used
+ class_info = serialized["id"]
+ if isinstance(class_info, list) and len(class_info) > 0:
+ class_name = class_info[
+ -1
+ ] # Last part is usually the class name
+ # Infer model based on LangChain provider class
+ if "chatopenai" in class_name.lower():
+ model_name = "gpt-3.5-turbo" # Default OpenAI model
+ elif "chatanthropic" in class_name.lower():
+ model_name = "claude-3"
+ elif (
+ "chatgooglevertexai" in class_name.lower()
+ or "chatgoogleai" in class_name.lower()
+ ):
+ model_name = "gemini-pro"
+ elif "llama" in class_name.lower():
+ model_name = "llama-2"
+ else:
+ # Use the class name itself as model identifier
+ model_name = class_name
+
+ # Create chat span with model name
+ span_name = f"chat {model_name}"
+ span = self._create_span(
+ run_id, parent_run_id, span_name, SpanKind.CLIENT, model_name
+ )
+
+ # Set OpenLIT chat operation attributes
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM, "openai"
+ ) # Most common
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ )
+ span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, model_name)
+
+ # Streaming detection
+ is_streaming = kwargs.get("stream", False) or kwargs.get("streaming", False)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_IS_STREAM, is_streaming
+ )
+
+ # Process messages for content capture and token counting
+ if messages and len(messages) > 0:
+ formatted_messages = self._format_messages(messages[0])
+
+ if self.capture_message_content:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_PROMPT, formatted_messages
+ )
+
+ # Calculate input tokens
+ input_tokens = general_tokens(formatted_messages)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens
+ )
+
+ except Exception:
+ # Graceful error handling
+ pass
+
+ def _format_messages(self, messages: List[BaseMessage]) -> str:
+ """Format LangChain messages for content capture"""
+ formatted = []
+ for message in messages:
+ role = self._get_message_role(message)
+ content = getattr(message, "content", str(message))
+ formatted.append(f"{role}: {content}")
+ return "\n".join(formatted)
+
+ def _get_message_role(self, message: BaseMessage) -> str:
+ """Extract role from LangChain message"""
+ message_type = message.__class__.__name__.lower()
+ if "human" in message_type:
+ return "user"
+ elif "ai" in message_type:
+ return "assistant"
+ elif "system" in message_type:
+ return "system"
+ elif "tool" in message_type:
+ return "tool"
+ else:
+ return "user"
+
+ def _process_llm_response(
+ self, span: Span, response: LLMResult, run_id: UUID
+ ) -> None:
+ """Process LLM response with OpenLIT's comprehensive business intelligence"""
+
+ try:
+ # Extract response content
+ if response.generations and len(response.generations) > 0:
+ generation = response.generations[0][0]
+
+ if isinstance(generation, ChatGeneration):
+ completion_content = generation.message.content
+ elif isinstance(generation, Generation):
+ completion_content = generation.text
+ else:
+ completion_content = str(generation)
+
+ # Set completion content
+ if self.capture_message_content:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, completion_content
+ )
+
+ # Calculate output tokens
+ output_tokens = general_tokens(completion_content)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens
+ )
+
+ # Use enhanced token extraction method (inspired by OpenInference)
+ self._extract_token_usage(span, response)
+
+ # Extract additional LLM output information
+ if hasattr(response, "llm_output") and response.llm_output:
+ llm_output = response.llm_output
+
+ # Model information
+ if "model_name" in llm_output:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_MODEL,
+ llm_output["model_name"],
+ )
+
+ # Calculate cost using OpenLIT's cost tracking
+ input_tokens = span.attributes.get(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, 0
+ )
+ output_tokens = span.attributes.get(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, 0
+ )
+ model_name = span.attributes.get(
+ SemanticConvention.GEN_AI_REQUEST_MODEL, ""
+ )
+
+ if input_tokens and output_tokens and model_name:
+ cost = get_chat_model_cost(
+ model_name, self.pricing_info, input_tokens, output_tokens
+ )
+ span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
+
+ except Exception as e:
+ handle_exception(span, e)
+
+ def _extract_token_usage(self, span: Span, result: LLMResult) -> None:
+ """Extract comprehensive token usage with OpenInference-style parsing"""
+ try:
+ # Try multiple token usage extraction patterns
+ token_usage = None
+
+ # Pattern 1: Standard non-streaming (OpenAI, most providers)
+ if hasattr(result, "llm_output") and result.llm_output:
+ token_usage = result.llm_output.get(
+ "token_usage"
+ ) or result.llm_output.get("usage")
+
+ # Pattern 2: Streaming outputs (when stream_usage=True)
+ if not token_usage and result.generations:
+ try:
+ first_gen = result.generations[0][0]
+ if hasattr(first_gen, "message") and hasattr(
+ first_gen.message, "kwargs"
+ ):
+ token_usage = first_gen.message.kwargs.get("usage_metadata")
+ except (IndexError, AttributeError):
+ pass
+
+ # Pattern 3: VertexAI-specific (generation_info.usage_metadata)
+ if not token_usage and result.generations:
+ try:
+ first_gen = result.generations[0][0]
+ if (
+ hasattr(first_gen, "generation_info")
+ and first_gen.generation_info
+ ):
+ token_usage = first_gen.generation_info.get("usage_metadata")
+ except (IndexError, AttributeError):
+ pass
+
+ if token_usage:
+ # Support multiple token field names from different providers
+ input_tokens = (
+ token_usage.get("prompt_tokens")
+ or token_usage.get("input_tokens") # Anthropic
+ or token_usage.get("prompt_token_count")
+ ) # Gemini
+
+ output_tokens = (
+ token_usage.get("completion_tokens")
+ or token_usage.get("output_tokens") # Anthropic
+ or token_usage.get("candidates_token_count")
+ ) # Gemini
+
+ total_tokens = token_usage.get("total_tokens") or token_usage.get(
+ "total_token_count"
+ ) # Gemini
+
+ if input_tokens:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens
+ )
+ if output_tokens:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens
+ )
+ if total_tokens:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS, total_tokens
+ )
+
+ # Enhanced token details (OpenAI-specific)
+ if details := token_usage.get("completion_tokens_details"):
+ if audio_tokens := details.get("audio_tokens"):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_COMPLETION_TOKENS_DETAILS_AUDIO,
+ audio_tokens,
+ )
+ if reasoning_tokens := details.get("reasoning_tokens"):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_COMPLETION_TOKENS_DETAILS_REASONING,
+ reasoning_tokens,
+ )
+
+ if details := token_usage.get("prompt_tokens_details"):
+ if cached_tokens := details.get("cached_tokens"):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_READ,
+ cached_tokens,
+ )
+ if audio_tokens := details.get("audio_tokens"):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_READ,
+ audio_tokens,
+ )
+
+ # Anthropic cache tokens
+ if cache_read := token_usage.get("cache_read_input_tokens"):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_READ,
+ cache_read,
+ )
+ if cache_write := token_usage.get("cache_creation_input_tokens"):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_WRITE,
+ cache_write,
+ )
+
+ except Exception as e:
+ handle_exception(span, e)
+
+ def _extract_provider_info(self, span: Span, **kwargs) -> None:
+ """Extract provider information with OpenInference-style mapping"""
+ try:
+ # Extract from metadata if available
+ if "metadata" in kwargs:
+ metadata = kwargs["metadata"]
+ if isinstance(metadata, dict) and "ls_provider" in metadata:
+ provider = metadata["ls_provider"].lower()
+ mapped_provider = LANGCHAIN_PROVIDER_MAP.get(provider, provider)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM, mapped_provider
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_PROVIDER, mapped_provider
+ )
+ return
+
+ # Extract from invocation parameters
+ if "invocation_params" in kwargs:
+ params = kwargs["invocation_params"]
+ if isinstance(params, dict):
+ # Look for provider indicators in model names
+ model = params.get("model") or params.get("model_name", "")
+ if model:
+ if "gpt-" in model or "o1-" in model:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM, "openai"
+ )
+ elif "claude-" in model:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM, "anthropic"
+ )
+ elif "gemini-" in model or "bison-" in model:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM, "google"
+ )
+ elif "mistral-" in model:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM, "mistralai"
+ )
+
+ except Exception as e:
+ handle_exception(span, e)
+
+ def _capture_serialized_info(self, span: Span, serialized: Dict[str, Any]) -> None:
+ """Capture LangSmith-style serialized function information"""
+ if not serialized:
+ return
+
+ try:
+ # Capture function name (LangSmith enhancement)
+ if "name" in serialized and serialized["name"]:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SERIALIZED_NAME, serialized["name"]
+ )
+
+ # Capture function signature if available
+ if "signature" in serialized and serialized["signature"]:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SERIALIZED_SIGNATURE,
+ str(serialized["signature"])[:500],
+ )
+
+ # Capture docstring if available
+ if "doc" in serialized and serialized["doc"]:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SERIALIZED_DOC,
+ str(serialized["doc"])[:200],
+ )
+
+ # Capture module information
+ if "id" in serialized and isinstance(serialized["id"], list):
+ module_path = (
+ ".".join(serialized["id"][:-1]) if len(serialized["id"]) > 1 else ""
+ )
+ if module_path:
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SERIALIZED_MODULE, module_path
+ )
+
+ except Exception:
+ # Graceful failure for serialized info capture
+ pass
+
+ def on_tool_start(
+ self,
+ serialized: Dict[str, Any],
+ input_str: str,
+ *,
+ run_id: UUID,
+ parent_run_id: Optional[UUID] = None,
+ **kwargs: Any,
+ ) -> None:
+ """Called when a tool starts"""
+
+ span_name = self._get_span_name(serialized, "tool")
+ span = self._create_span(run_id, parent_run_id, span_name, SpanKind.CLIENT)
+
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM, SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_TOOLS,
+ )
+
+ if self.capture_message_content:
+ span.set_attribute(SemanticConvention.GEN_AI_TOOL_INPUT, input_str[:1000])
+
+ def on_tool_end(
+ self,
+ output: str,
+ *,
+ run_id: UUID,
+ parent_run_id: Optional[UUID] = None,
+ **kwargs: Any,
+ ) -> None:
+ """Called when a tool ends"""
+
+ if run_id not in self.spans:
+ return
+
+ span = self.spans[run_id].span
+
+ if self.capture_message_content:
+ span.set_attribute(SemanticConvention.GEN_AI_TOOL_OUTPUT, output[:1000])
+
+ # Duration is set in _end_span method
+
+ self._end_span(run_id)
+
+ def on_retriever_start(
+ self,
+ serialized: Dict[str, Any],
+ query: str,
+ *,
+ run_id: UUID,
+ parent_run_id: Optional[UUID] = None,
+ **kwargs: Any,
+ ) -> None:
+ """Called when a retriever starts"""
+
+ span_name = self._get_span_name(serialized, "retrieval")
+ span = self._create_span(run_id, parent_run_id, span_name, SpanKind.CLIENT)
+
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM, SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_RETRIEVE,
+ )
+
+ if self.capture_message_content:
+ span.set_attribute(SemanticConvention.GEN_AI_RETRIEVAL_QUERY, query[:1000])
+
+ def on_retriever_end(
+ self,
+ documents,
+ *,
+ run_id: UUID,
+ parent_run_id: Optional[UUID] = None,
+ **kwargs: Any,
+ ) -> None:
+ """Called when a retriever ends"""
+
+ if run_id not in self.spans:
+ return
+
+ span = self.spans[run_id].span
+
+ # Document count
+ doc_count = len(documents) if documents else 0
+ span.set_attribute(
+ SemanticConvention.GEN_AI_RETRIEVAL_DOCUMENT_COUNT, doc_count
+ )
+
+ # Sample document content
+ if self.capture_message_content and documents:
+ sample_docs = []
+ for doc in documents[:3]: # First 3 docs
+ if hasattr(doc, "page_content"):
+ sample_docs.append(doc.page_content[:200])
+ else:
+ sample_docs.append(str(doc)[:200])
+ span.set_attribute(
+ SemanticConvention.GEN_AI_RETRIEVAL_DOCUMENTS, "; ".join(sample_docs)
+ )
+
+ # Duration is set in _end_span method
+
+ self._end_span(run_id)
+
+
+def _calculate_percentile(value: float, baseline: Dict[str, Any]) -> float:
+ """Calculate rough percentile based on min/max"""
+ try:
+ min_val = baseline["min_duration"]
+ max_val = baseline["max_duration"]
+ if max_val == min_val:
+ return 50.0
+ return ((value - min_val) / (max_val - min_val)) * 100
+ except:
+ return 50.0 # Default to median
diff --git a/sdk/python/src/openlit/instrumentation/langchain/langchain.py b/sdk/python/src/openlit/instrumentation/langchain/langchain.py
deleted file mode 100644
index 80b137ab..00000000
--- a/sdk/python/src/openlit/instrumentation/langchain/langchain.py
+++ /dev/null
@@ -1,102 +0,0 @@
-"""
-Module for monitoring LangChain API calls.
-"""
-
-import time
-from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
-from openlit.instrumentation.langchain.utils import (
- get_model_from_instance,
- process_chat_response,
- process_hub_response,
-)
-from openlit.semcov import SemanticConvention
-
-def hub(gen_ai_endpoint, version, environment, application_name, tracer,
- pricing_info, capture_message_content, metrics, disable_metrics):
- """
- Generates a telemetry wrapper for LangChain hub operations.
- """
-
- def wrapper(wrapped, instance, args, kwargs):
- """
- Wraps the LangChain hub operation call.
- """
-
- server_address, server_port = set_server_address_and_port(instance, "langchain.com", 443)
-
- with tracer.start_as_current_span(gen_ai_endpoint, kind=SpanKind.CLIENT) as span:
- response = wrapped(*args, **kwargs)
-
- try:
- response = process_hub_response(
- response=response,
- gen_ai_endpoint=gen_ai_endpoint,
- server_port=server_port,
- server_address=server_address,
- environment=environment,
- application_name=application_name,
- span=span,
- version=version
- )
-
- except Exception as e:
- handle_exception(span, e)
-
- return response
-
- return wrapper
-
-def chat(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
- """
- Generates a telemetry wrapper for GenAI operations.
- """
-
- def wrapper(wrapped, instance, args, kwargs):
- """
- Wraps the GenAI operation call.
- """
-
- server_address, server_port = set_server_address_and_port(instance, "langchain.com", 443)
- request_model = get_model_from_instance(instance)
-
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
-
- with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
- start_time = time.time()
- response = wrapped(*args, **kwargs)
- end_time = time.time()
-
- try:
- # Add instance to kwargs for processing
- kwargs["instance"] = instance
-
- response = process_chat_response(
- response=response,
- request_model=request_model,
- pricing_info=pricing_info,
- server_port=server_port,
- server_address=server_address,
- environment=environment,
- application_name=application_name,
- metrics=metrics,
- start_time=start_time,
- end_time=end_time,
- span=span,
- capture_message_content=capture_message_content,
- disable_metrics=disable_metrics,
- version=version,
- args=args,
- **kwargs
- )
-
- except Exception as e:
- handle_exception(span, e)
-
- return response
-
- return wrapper
diff --git a/sdk/python/src/openlit/instrumentation/langchain/utils.py b/sdk/python/src/openlit/instrumentation/langchain/utils.py
deleted file mode 100644
index d206caa2..00000000
--- a/sdk/python/src/openlit/instrumentation/langchain/utils.py
+++ /dev/null
@@ -1,252 +0,0 @@
-"""
-LangChain OpenTelemetry instrumentation utility functions
-"""
-import time
-
-from opentelemetry.trace import Status, StatusCode
-
-from openlit.__helpers import (
- get_chat_model_cost,
- general_tokens,
- record_completion_metrics,
- common_span_attributes,
-)
-from openlit.semcov import SemanticConvention
-
-def format_content(messages):
- """
- Format the messages into a string for span events.
- """
-
- if not messages:
- return ""
-
- # Handle string input (simple case)
- if isinstance(messages, str):
- return messages
-
- # Handle list of messages
- formatted_messages = []
- for message in messages:
- # Handle the case where message is a tuple
- if isinstance(message, tuple) and len(message) == 2:
- role, content = message
- # Handle the case where message is a dictionary
- elif isinstance(message, dict):
- role = message.get("role", "user")
- content = message.get("content", "")
- else:
- continue
-
- # Check if the content is a list
- if isinstance(content, list):
- content_str = ", ".join(
- f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
- if "type" in item else f'text: {item["text"]}'
- for item in content
- )
- formatted_messages.append(f"{role}: {content_str}")
- else:
- formatted_messages.append(f"{role}: {content}")
-
- return "\n".join(formatted_messages)
-
-def get_model_from_instance(instance):
- """
- Extract model name from LangChain instance.
- """
- if hasattr(instance, "model_id"):
- return instance.model_id
- elif hasattr(instance, "model"):
- return instance.model
- elif hasattr(instance, "model_name"):
- return instance.model_name
- else:
- return "langchain-model"
-
-def get_attribute_from_instance(instance, attribute_name, default=-1):
- """
- Get attribute from instance, checking model_kwargs first.
- """
- # Attempt to retrieve model_kwargs from the instance
- model_kwargs = getattr(instance, "model_kwargs", None)
-
- # Check for attribute in model_kwargs if it exists
- if model_kwargs and attribute_name in model_kwargs:
- value = model_kwargs[attribute_name]
- return value if value is not None else default
-
- # Attempt to get the attribute directly from the instance
- try:
- value = getattr(instance, attribute_name)
- # Return default if value is None
- return value if value is not None else default
- except AttributeError:
- # Special handling for "model" attribute to consider "model_id"
- if attribute_name == "model":
- return getattr(instance, "model_id", "langchain-model")
-
- # Default if the attribute isnt found in model_kwargs or the instance
- return default
-
-def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream):
- """
- Process chat request and generate Telemetry
- """
-
- scope._end_time = time.time()
- scope._tbt = 0 # LangChain doesnt support streaming yet
- scope._ttft = scope._end_time - scope._start_time
-
- # Extract prompt - check args[0] first (positional), then kwargs (keyword arguments)
- messages = None
- if scope._args and len(scope._args) > 0:
- messages = scope._args[0] # llm.invoke([("system", "..."), ("human", "...")])
- else:
- messages = scope._kwargs.get("messages", "") or scope._kwargs.get("input", "") # llm.invoke(messages=[...])
-
- formatted_messages = format_content(messages)
- request_model = scope._request_model
-
- # Use actual token counts from response if available, otherwise calculate them using general_tokens
- if (scope._input_tokens in [None, 0] or scope._output_tokens in [None, 0]):
- scope._input_tokens = general_tokens(str(formatted_messages))
- scope._output_tokens = general_tokens(str(scope._llmresponse))
-
- cost = get_chat_model_cost(request_model, pricing_info, scope._input_tokens, scope._output_tokens)
-
- # Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
-
- # Span Attributes for Request parameters
- instance = scope._kwargs.get("instance")
- if instance:
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
- get_attribute_from_instance(instance, "temperature", 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_K,
- get_attribute_from_instance(instance, "top_k", 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P,
- get_attribute_from_instance(instance, "top_p", 1.0))
-
- # Span Attributes for Response parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
-
- # Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens + scope._output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
-
- # Span Attributes for Content
- if capture_message_content:
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, formatted_messages)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
-
- # To be removed once the change to span_attributes (from span events) is complete
- scope._span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: formatted_messages,
- },
- )
- scope._span.add_event(
- name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
- attributes={
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: scope._llmresponse,
- },
- )
-
- scope._span.set_status(Status(StatusCode.OK))
-
- # Record metrics
- if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, scope._output_tokens,
- cost, scope._tbt, scope._ttft)
-
-def process_chat_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, end_time,
- span, capture_message_content=False, disable_metrics=False,
- version="1.0.0", args=None, **kwargs):
- """
- Process chat response and generate telemetry.
- """
-
- # Create scope object
- scope = type("GenericScope", (), {})()
-
- scope._start_time = start_time
- scope._end_time = end_time
- scope._span = span
- scope._server_address = server_address
- scope._server_port = server_port
- scope._request_model = request_model
- scope._kwargs = kwargs
- scope._args = args or ()
-
- # Extract response content and metadata - only extract what comes from the response
- try:
- scope._llmresponse = response.content
- except AttributeError:
- scope._llmresponse = str(response)
-
- # Extract token information from usage_metadata if available
- usage_metadata = getattr(response, "usage_metadata", None)
- if usage_metadata:
- scope._input_tokens = usage_metadata.get("input_tokens", 0)
- scope._output_tokens = usage_metadata.get("output_tokens", 0)
- scope._total_tokens = usage_metadata.get("total_tokens", 0)
- else:
- # Will be calculated in common_chat_logic if not available
- scope._input_tokens = None
- scope._output_tokens = None
- scope._total_tokens = None
-
- # Extract response metadata
- response_metadata = getattr(response, "response_metadata", {})
- scope._response_model = response_metadata.get("model_name", request_model)
- scope._finish_reason = response_metadata.get("finish_reason", "stop")
-
- # Extract response ID
- scope._response_id = getattr(response, "id", "")
-
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=False)
-
- return response
-
-
-
-def process_hub_response(response, gen_ai_endpoint, server_port, server_address,
- environment, application_name, span, version="1.0.0"):
- """
- Process LangChain hub operations and generate telemetry.
- """
-
- # Set span attributes for hub operations
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM, SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN)
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION, SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK)
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
- span.set_attribute(SemanticConvention.GEN_AI_ENVIRONMENT, environment)
- span.set_attribute(SemanticConvention.GEN_AI_APPLICATION_NAME, application_name)
-
- # Try to extract hub metadata
- try:
- span.set_attribute(SemanticConvention.GEN_AI_HUB_OWNER,
- response.metadata.get("lc_hub_owner", "unknown"))
- span.set_attribute(SemanticConvention.GEN_AI_HUB_REPO,
- response.metadata.get("lc_hub_repo", "unknown"))
- except (AttributeError, KeyError):
- pass
-
- span.set_status(Status(StatusCode.OK))
-
- return response
diff --git a/sdk/python/src/openlit/instrumentation/langchain_community/__init__.py b/sdk/python/src/openlit/instrumentation/langchain_community/__init__.py
index 39a87363..99b426e7 100644
--- a/sdk/python/src/openlit/instrumentation/langchain_community/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/langchain_community/__init__.py
@@ -1,4 +1,5 @@
"""Initializer of Auto Instrumentation of LangChain Community Functions"""
+
from typing import Collection
import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
@@ -40,6 +41,7 @@ WRAPPED_METHODS = [
},
]
+
class LangChainCommunityInstrumentor(BaseInstrumentor):
"""
An instrumentor for LangChain Community client library.
@@ -66,8 +68,17 @@ class LangChainCommunityInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
wrap_package,
wrap_object,
- wrapper(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ wrapper(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/langchain_community/async_langchain_community.py b/sdk/python/src/openlit/instrumentation/langchain_community/async_langchain_community.py
index 156ebb41..b45a26ce 100644
--- a/sdk/python/src/openlit/instrumentation/langchain_community/async_langchain_community.py
+++ b/sdk/python/src/openlit/instrumentation/langchain_community/async_langchain_community.py
@@ -12,8 +12,18 @@ from openlit.instrumentation.langchain_community.utils import process_general_re
# Initialize logger for LangChain Community instrumentation
logger = logging.getLogger(__name__)
-def async_general_wrap(gen_ai_endpoint, version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def async_general_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI operations.
"""
@@ -28,15 +38,23 @@ def async_general_wrap(gen_ai_endpoint, version, environment, application_name,
server_port = "80"
# Get the parent span from the tracer
- with tracer.start_as_current_span(gen_ai_endpoint, kind=trace.SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(
+ gen_ai_endpoint, kind=trace.SpanKind.CLIENT
+ ) as span:
try:
# Call the original async function
response = await wrapped(*args, **kwargs)
# Process the response using the utility function
response = process_general_response(
- response, gen_ai_endpoint, server_port, server_address,
- environment, application_name, span, version
+ response,
+ gen_ai_endpoint,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ span,
+ version,
)
span.set_status(Status(StatusCode.OK))
diff --git a/sdk/python/src/openlit/instrumentation/langchain_community/langchain_community.py b/sdk/python/src/openlit/instrumentation/langchain_community/langchain_community.py
index 77d2ad88..49d57e05 100644
--- a/sdk/python/src/openlit/instrumentation/langchain_community/langchain_community.py
+++ b/sdk/python/src/openlit/instrumentation/langchain_community/langchain_community.py
@@ -12,8 +12,18 @@ from openlit.instrumentation.langchain_community.utils import process_general_re
# Initialize logger for LangChain Community instrumentation
logger = logging.getLogger(__name__)
-def general_wrap(gen_ai_endpoint, version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def general_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI operations.
"""
@@ -28,15 +38,23 @@ def general_wrap(gen_ai_endpoint, version, environment, application_name, tracer
server_port = "80"
# Get the parent span from the tracer
- with tracer.start_as_current_span(gen_ai_endpoint, kind=trace.SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(
+ gen_ai_endpoint, kind=trace.SpanKind.CLIENT
+ ) as span:
try:
# Call the original function
response = wrapped(*args, **kwargs)
# Process the response using the utility function
response = process_general_response(
- response, gen_ai_endpoint, server_port, server_address,
- environment, application_name, span, version
+ response,
+ gen_ai_endpoint,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ span,
+ version,
)
span.set_status(Status(StatusCode.OK))
diff --git a/sdk/python/src/openlit/instrumentation/langchain_community/utils.py b/sdk/python/src/openlit/instrumentation/langchain_community/utils.py
index 4aaaea82..238ba840 100644
--- a/sdk/python/src/openlit/instrumentation/langchain_community/utils.py
+++ b/sdk/python/src/openlit/instrumentation/langchain_community/utils.py
@@ -5,8 +5,17 @@ Utility functions for LangChain Community instrumentation.
from opentelemetry.trace import Status, StatusCode
from openlit.semcov import SemanticConvention
-def process_general_response(response, gen_ai_endpoint, server_port, server_address,
- environment, application_name, span, version="1.0.0"):
+
+def process_general_response(
+ response,
+ gen_ai_endpoint,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ span,
+ version="1.0.0",
+):
"""
Process general LangChain Community operations (document loading, text splitting) and generate telemetry.
@@ -25,9 +34,14 @@ def process_general_response(response, gen_ai_endpoint, server_port, server_addr
"""
# Set span attributes for general operations
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM, SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM, SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN
+ )
span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION, SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
+ )
span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
span.set_attribute(SemanticConvention.GEN_AI_ENVIRONMENT, environment)
span.set_attribute(SemanticConvention.GEN_AI_APPLICATION_NAME, application_name)
@@ -38,9 +52,13 @@ def process_general_response(response, gen_ai_endpoint, server_port, server_addr
if hasattr(response, "__iter__") and len(response) > 0:
# For document loaders, try to get source from first document
first_doc = response[0]
- if hasattr(first_doc, "metadata") and isinstance(first_doc.metadata, dict):
+ if hasattr(first_doc, "metadata") and isinstance(
+ first_doc.metadata, dict
+ ):
source = first_doc.metadata.get("source", "unknown")
- span.set_attribute(SemanticConvention.GEN_AI_RETRIEVAL_SOURCE, source)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_RETRIEVAL_SOURCE, source
+ )
# Count number of documents loaded
span.set_attribute("gen_ai.retrieval.documents.count", len(response))
@@ -49,17 +67,25 @@ def process_general_response(response, gen_ai_endpoint, server_port, server_addr
pass
# For text splitting operations
- elif gen_ai_endpoint and ("split_documents" in gen_ai_endpoint or "create_documents" in gen_ai_endpoint):
+ elif gen_ai_endpoint and (
+ "split_documents" in gen_ai_endpoint or "create_documents" in gen_ai_endpoint
+ ):
try:
if hasattr(response, "__iter__") and len(response) > 0:
# Count number of text chunks created
span.set_attribute("gen_ai.text_splitter.chunks.count", len(response))
# Try to get average chunk size
- total_chars = sum(len(doc.page_content) for doc in response if hasattr(doc, "page_content"))
+ total_chars = sum(
+ len(doc.page_content)
+ for doc in response
+ if hasattr(doc, "page_content")
+ )
if total_chars > 0:
avg_chunk_size = total_chars // len(response)
- span.set_attribute("gen_ai.text_splitter.avg_chunk_size", avg_chunk_size)
+ span.set_attribute(
+ "gen_ai.text_splitter.avg_chunk_size", avg_chunk_size
+ )
except (AttributeError, TypeError):
# If we cant extract chunk information, just continue without it
pass
diff --git a/sdk/python/src/openlit/instrumentation/letta/__init__.py b/sdk/python/src/openlit/instrumentation/letta/__init__.py
index 47af4145..253cabb3 100644
--- a/sdk/python/src/openlit/instrumentation/letta/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/letta/__init__.py
@@ -6,12 +6,11 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.letta.letta import (
- create_agent, send_message
-)
+from openlit.instrumentation.letta.letta import create_agent, send_message
_instruments = ("letta >= 0.6.2",)
+
class LettaInstrumentor(BaseInstrumentor):
"""
An instrumentor for Letta's client library.
@@ -33,43 +32,97 @@ class LettaInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"letta.client.client",
"LocalClient.create_agent",
- create_agent("letta.create_agent", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ create_agent(
+ "letta.create_agent",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"letta.client.client",
"LocalClient.get_agent",
- create_agent("letta.get_agent", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ create_agent(
+ "letta.get_agent",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"letta.client.client",
"LocalClient.send_message",
- send_message("letta.send_message", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ send_message(
+ "letta.send_message",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"letta.client.client",
"RESTClient.create_agent",
- create_agent("letta.create_agent", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ create_agent(
+ "letta.create_agent",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"letta.client.client",
"RESTClient.get_agent",
- create_agent("letta.get_agent", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ create_agent(
+ "letta.get_agent",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"letta.client.client",
"RESTClient.send_message",
- send_message("letta.send_message", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ send_message(
+ "letta.send_message",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/letta/letta.py b/sdk/python/src/openlit/instrumentation/letta/letta.py
index 508c88db..8df8671a 100644
--- a/sdk/python/src/openlit/instrumentation/letta/letta.py
+++ b/sdk/python/src/openlit/instrumentation/letta/letta.py
@@ -5,17 +5,29 @@ Module for monitoring Letta calls.
import logging
from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
-from openlit.__helpers import (
- handle_exception, get_chat_model_cost
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
)
+from openlit.__helpers import handle_exception, get_chat_model_cost
from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def create_agent(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def create_agent(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for chat completions to collect metrics.
@@ -50,34 +62,39 @@ def create_agent(gen_ai_endpoint, version, environment, application_name,
"""
# pylint: disable=line-too-long
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(
+ gen_ai_endpoint, kind=SpanKind.CLIENT
+ ) as span:
response = wrapped(*args, **kwargs)
try:
# Set base span attribues
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_LETTA)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT)
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,
- gen_ai_endpoint)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ID,
- response.id)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ROLE,
- response.name)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_INSTRUCTIONS,
- response.system)
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- response.llm_config.model)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TYPE,
- response.agent_type)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TOOLS,
- response.tool_names)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM,
+ SemanticConvention.GEN_AI_SYSTEM_LETTA,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
+ )
+ span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)
+ span.set_attribute(SERVICE_NAME, application_name)
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
+ span.set_attribute(SemanticConvention.GEN_AI_AGENT_ID, response.id)
+ span.set_attribute(SemanticConvention.GEN_AI_AGENT_ROLE, response.name)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_INSTRUCTIONS, response.system
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MODEL, response.llm_config.model
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_TYPE, response.agent_type
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_TOOLS, response.tool_names
+ )
span.set_status(Status(StatusCode.OK))
@@ -93,8 +110,18 @@ def create_agent(gen_ai_endpoint, version, environment, application_name,
return wrapper
-def send_message(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def send_message(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for chat completions to collect metrics.
@@ -129,47 +156,65 @@ def send_message(gen_ai_endpoint, version, environment, application_name,
"""
# pylint: disable=line-too-long
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(
+ gen_ai_endpoint, kind=SpanKind.CLIENT
+ ) as span:
response = wrapped(*args, **kwargs)
try:
# Calculate cost of the operation
- cost = get_chat_model_cost(kwargs.get("model", "gpt-4o"),
- pricing_info, response.usage.prompt_tokens,
- response.usage.completion_tokens)
+ cost = get_chat_model_cost(
+ kwargs.get("model", "gpt-4o"),
+ pricing_info,
+ response.usage.prompt_tokens,
+ response.usage.completion_tokens,
+ )
# Set base span attribues
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_LETTA)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT)
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,
- gen_ai_endpoint)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_STEP_COUNT,
- response.usage.step_count)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
- response.usage.prompt_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS,
- response.usage.completion_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,
- response.usage.total_tokens)
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
- cost)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM,
+ SemanticConvention.GEN_AI_SYSTEM_LETTA,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
+ )
+ span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)
+ span.set_attribute(SERVICE_NAME, application_name)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_STEP_COUNT,
+ response.usage.step_count,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
+ response.usage.prompt_tokens,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS,
+ response.usage.completion_tokens,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,
+ response.usage.total_tokens,
+ )
+ span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
if capture_message_content:
span.add_event(
name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: kwargs.get("message", ""),
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: kwargs.get(
+ "message", ""
+ ),
},
)
span.add_event(
name=SemanticConvention.GEN_AI_CONTENT_COMPLETION_EVENT,
# pylint: disable=line-too-long
attributes={
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: str(response.messages),
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION: str(
+ response.messages
+ ),
},
)
diff --git a/sdk/python/src/openlit/instrumentation/litellm/__init__.py b/sdk/python/src/openlit/instrumentation/litellm/__init__.py
index a4600ebc..391d1546 100644
--- a/sdk/python/src/openlit/instrumentation/litellm/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/litellm/__init__.py
@@ -5,15 +5,12 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.litellm.litellm import (
- completion, embedding
-)
-from openlit.instrumentation.litellm.async_litellm import (
- acompletion, aembedding
-)
+from openlit.instrumentation.litellm.litellm import completion, embedding
+from openlit.instrumentation.litellm.async_litellm import acompletion, aembedding
_instruments = ("litellm >= 1.52.6",)
+
class LiteLLMInstrumentor(BaseInstrumentor):
"""
An instrumentor for LiteLLM client library.
@@ -36,32 +33,64 @@ class LiteLLMInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"litellm",
"completion",
- completion(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ completion(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# Async chat completions
wrap_function_wrapper(
"litellm",
"acompletion",
- acompletion(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ acompletion(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# Embeddings
wrap_function_wrapper(
"litellm",
"embedding",
- embedding(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ embedding(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# Async embeddings
wrap_function_wrapper(
"litellm",
"aembedding",
- aembedding(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ aembedding(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/litellm/async_litellm.py b/sdk/python/src/openlit/instrumentation/litellm/async_litellm.py
index 9cbfd63a..241f9cf8 100644
--- a/sdk/python/src/openlit/instrumentation/litellm/async_litellm.py
+++ b/sdk/python/src/openlit/instrumentation/litellm/async_litellm.py
@@ -4,20 +4,26 @@ Module for monitoring LiteLLM API calls (async version).
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.litellm.utils import (
process_chunk,
process_streaming_chat_response,
process_chat_response,
- process_embedding_response
+ process_embedding_response,
)
from openlit.semcov import SemanticConvention
-def acompletion(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def acompletion(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -28,15 +34,15 @@ def acompletion(version, environment, application_name, tracer, pricing_info,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -79,7 +85,9 @@ def acompletion(version, environment, application_name, tracer, pricing_info,
return chunk
except StopAsyncIteration:
try:
- with tracer.start_as_current_span(self._span_name, kind=SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -88,7 +96,7 @@ def acompletion(version, environment, application_name, tracer, pricing_info,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -102,7 +110,9 @@ def acompletion(version, environment, application_name, tracer, pricing_info,
"""
# Check if streaming is enabled for the API call
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "NOT_FOUND", "NOT_FOUND")
+ server_address, server_port = set_server_address_and_port(
+ instance, "NOT_FOUND", "NOT_FOUND"
+ )
request_model = kwargs.get("model", "openai/gpt-4o")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -111,7 +121,9 @@ def acompletion(version, environment, application_name, tracer, pricing_info,
# Special handling for streaming response
awaited_wrapped = await wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedAsyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedAsyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
else:
# Handling for non-streaming responses
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -133,7 +145,7 @@ def acompletion(version, environment, application_name, tracer, pricing_info,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -143,8 +155,17 @@ def acompletion(version, environment, application_name, tracer, pricing_info,
return wrapper
-def aembedding(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def aembedding(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI embedding function call
"""
@@ -153,10 +174,14 @@ def aembedding(version, environment, application_name, tracer, pricing_info,
"""
Wraps the GenAI embedding function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "NOT_FOUND", "NOT_FOUND")
+ server_address, server_port = set_server_address_and_port(
+ instance, "NOT_FOUND", "NOT_FOUND"
+ )
request_model = kwargs.get("model", "text-embedding-ada-002")
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
@@ -177,7 +202,7 @@ def aembedding(version, environment, application_name, tracer, pricing_info,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/litellm/litellm.py b/sdk/python/src/openlit/instrumentation/litellm/litellm.py
index 3fbc3acd..4afeb1ec 100644
--- a/sdk/python/src/openlit/instrumentation/litellm/litellm.py
+++ b/sdk/python/src/openlit/instrumentation/litellm/litellm.py
@@ -4,20 +4,26 @@ Module for monitoring LiteLLM API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.litellm.utils import (
process_chunk,
process_streaming_chat_response,
process_chat_response,
- process_embedding_response
+ process_embedding_response,
)
from openlit.semcov import SemanticConvention
-def completion(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def completion(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -28,15 +34,15 @@ def completion(version, environment, application_name, tracer, pricing_info,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -79,7 +85,9 @@ def completion(version, environment, application_name, tracer, pricing_info,
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(self._span_name, kind=SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -88,7 +96,7 @@ def completion(version, environment, application_name, tracer, pricing_info,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -102,7 +110,9 @@ def completion(version, environment, application_name, tracer, pricing_info,
"""
# Check if streaming is enabled for the API call
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "NOT_FOUND", "NOT_FOUND")
+ server_address, server_port = set_server_address_and_port(
+ instance, "NOT_FOUND", "NOT_FOUND"
+ )
request_model = kwargs.get("model", "openai/gpt-4o")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -111,7 +121,9 @@ def completion(version, environment, application_name, tracer, pricing_info,
# Special handling for streaming response
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedSyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
else:
# Handling for non-streaming responses
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -133,7 +145,7 @@ def completion(version, environment, application_name, tracer, pricing_info,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -143,8 +155,17 @@ def completion(version, environment, application_name, tracer, pricing_info,
return wrapper
-def embedding(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def embedding(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI embedding function call
"""
@@ -153,10 +174,14 @@ def embedding(version, environment, application_name, tracer, pricing_info,
"""
Wraps the GenAI embedding function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "NOT_FOUND", "NOT_FOUND")
+ server_address, server_port = set_server_address_and_port(
+ instance, "NOT_FOUND", "NOT_FOUND"
+ )
request_model = kwargs.get("model", "text-embedding-ada-002")
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
@@ -177,7 +202,7 @@ def embedding(version, environment, application_name, tracer, pricing_info,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/litellm/utils.py b/sdk/python/src/openlit/instrumentation/litellm/utils.py
index 19367ca9..68982554 100644
--- a/sdk/python/src/openlit/instrumentation/litellm/utils.py
+++ b/sdk/python/src/openlit/instrumentation/litellm/utils.py
@@ -1,6 +1,7 @@
"""
LiteLLM OpenTelemetry instrumentation utility functions
"""
+
import time
from opentelemetry.trace import Status, StatusCode
@@ -17,6 +18,7 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def format_content(messages):
"""
Process a list of messages to extract content.
@@ -24,20 +26,22 @@ def format_content(messages):
formatted_messages = []
for message in messages:
- role = message['role']
- content = message['content']
+ role = message["role"]
+ content = message["content"]
if isinstance(content, list):
content_str = ", ".join(
- f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
- if "type" in item else f'text: {item["text"]}'
+ f"{item['type']}: {item['text'] if 'text' in item else item['image_url']}"
+ if "type" in item
+ else f"text: {item['text']}"
for item in content
)
- formatted_messages.append(f'{role}: {content_str}')
+ formatted_messages.append(f"{role}: {content_str}")
else:
- formatted_messages.append(f'{role}: {content}')
+ formatted_messages.append(f"{role}: {content}")
+
+ return "\n".join(formatted_messages)
- return '\n'.join(formatted_messages)
def process_chunk(scope, chunk):
"""
@@ -55,45 +59,63 @@ def process_chunk(scope, chunk):
chunked = response_as_dict(chunk)
# Collect message IDs and aggregated response from events
- if (len(chunked.get('choices', [])) > 0 and ('delta' in chunked.get('choices')[0] and
- 'content' in chunked.get('choices')[0].get('delta', {}))):
-
- content = chunked.get('choices')[0].get('delta').get('content')
+ if len(chunked.get("choices", [])) > 0 and (
+ "delta" in chunked.get("choices")[0]
+ and "content" in chunked.get("choices")[0].get("delta", {})
+ ):
+ content = chunked.get("choices")[0].get("delta").get("content")
if content:
scope._llmresponse += content
# Handle tool calls in streaming - optimized
- delta_tools = chunked.get('choices', [{}])[0].get('delta', {}).get('tool_calls')
+ delta_tools = chunked.get("choices", [{}])[0].get("delta", {}).get("tool_calls")
if delta_tools:
scope._tools = scope._tools or []
for tool in delta_tools:
- idx = tool.get('index', 0)
+ idx = tool.get("index", 0)
# Extend list if needed
scope._tools.extend([{}] * (idx + 1 - len(scope._tools)))
- if tool.get('id'): # New tool (id exists)
- func = tool.get('function', {})
+ if tool.get("id"): # New tool (id exists)
+ func = tool.get("function", {})
scope._tools[idx] = {
- 'id': tool['id'],
- 'function': {'name': func.get('name', ''), 'arguments': func.get('arguments', '')},
- 'type': tool.get('type', 'function')
+ "id": tool["id"],
+ "function": {
+ "name": func.get("name", ""),
+ "arguments": func.get("arguments", ""),
+ },
+ "type": tool.get("type", "function"),
}
- elif scope._tools[idx] and 'function' in tool: # Append args (id is None)
- scope._tools[idx]['function']['arguments'] += tool['function'].get('arguments', '')
-
- if chunked.get('usage'):
- scope._input_tokens = chunked.get('usage').get('prompt_tokens', 0)
- scope._output_tokens = chunked.get('usage').get('completion_tokens', 0)
- scope._response_id = chunked.get('id')
- scope._response_model = chunked.get('model')
- scope._finish_reason = chunked.get('choices', [{}])[0].get('finish_reason')
- scope._response_service_tier = str(chunked.get('system_fingerprint', ''))
+ elif (
+ scope._tools[idx] and "function" in tool
+ ): # Append args (id is None)
+ scope._tools[idx]["function"]["arguments"] += tool["function"].get(
+ "arguments", ""
+ )
+
+ if chunked.get("usage"):
+ scope._input_tokens = chunked.get("usage").get("prompt_tokens", 0)
+ scope._output_tokens = chunked.get("usage").get("completion_tokens", 0)
+ scope._response_id = chunked.get("id")
+ scope._response_model = chunked.get("model")
+ scope._finish_reason = chunked.get("choices", [{}])[0].get("finish_reason")
+ scope._response_service_tier = str(chunked.get("system_fingerprint", ""))
scope._end_time = time.time()
-def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream):
+
+def common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream,
+):
"""
Process chat request and generate Telemetry
"""
@@ -101,65 +123,135 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
if len(scope._timestamps) > 1:
scope._tbt = calculate_tbt(scope._timestamps)
- prompt = format_content(scope._kwargs.get('messages', []))
- request_model = scope._kwargs.get('model', 'openai/gpt-4o')
+ prompt = format_content(scope._kwargs.get("messages", []))
+ request_model = scope._kwargs.get("model", "openai/gpt-4o")
- cost = get_chat_model_cost(request_model, pricing_info, scope._input_tokens, scope._output_tokens)
+ cost = get_chat_model_cost(
+ request_model, pricing_info, scope._input_tokens, scope._output_tokens
+ )
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_LITELLM,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_LITELLM,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Helper function to handle None values with proper defaults
def safe_get(value, default):
return default if value is None else value
# Span Attributes for Request parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED, safe_get(scope._kwargs.get('seed'), ''))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
- safe_get(scope._kwargs.get('frequency_penalty'), 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, safe_get(scope._kwargs.get('max_tokens'), -1))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, safe_get(scope._kwargs.get('presence_penalty'), 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get('stop', []))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, safe_get(scope._kwargs.get('temperature'), 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, safe_get(scope._kwargs.get('top_p'), 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, safe_get(scope._kwargs.get('user'), ''))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER, safe_get(scope._kwargs.get('service_tier'), 'auto'))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_SEED, safe_get(scope._kwargs.get("seed"), "")
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
+ safe_get(scope._kwargs.get("frequency_penalty"), 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
+ safe_get(scope._kwargs.get("max_tokens"), -1),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
+ safe_get(scope._kwargs.get("presence_penalty"), 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get("stop", [])
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
+ safe_get(scope._kwargs.get("temperature"), 1.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_P,
+ safe_get(scope._kwargs.get("top_p"), 1.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_USER, safe_get(scope._kwargs.get("user"), "")
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER,
+ safe_get(scope._kwargs.get("service_tier"), "auto"),
+ )
# Span Attributes for Response parameters
scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SERVICE_TIER, scope._response_service_tier)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT, scope._response_service_tier)
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_SERVICE_TIER, scope._response_service_tier
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT,
+ scope._response_service_tier,
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ "text" if isinstance(scope._llmresponse, str) else "json",
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens + scope._output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ scope._input_tokens + scope._output_tokens,
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Tools - optimized
if scope._tools:
tools = scope._tools if isinstance(scope._tools, list) else [scope._tools]
- names, ids, args = zip(*[
- (t.get("function", {}).get("name", ""),
- str(t.get("id", "")),
- str(t.get("function", {}).get("arguments", "")))
- for t in tools if isinstance(t, dict) and t
- ]) if tools else ([], [], [])
+ names, ids, args = (
+ zip(
+ *[
+ (
+ t.get("function", {}).get("name", ""),
+ str(t.get("id", "")),
+ str(t.get("function", {}).get("arguments", "")),
+ )
+ for t in tools
+ if isinstance(t, dict) and t
+ ]
+ )
+ if tools
+ else ([], [], [])
+ )
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, ", ".join(filter(None, names)))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, ", ".join(filter(None, ids)))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, ", ".join(filter(None, args)))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_NAME, ", ".join(filter(None, names))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_CALL_ID, ", ".join(filter(None, ids))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_ARGS, ", ".join(filter(None, args))
+ )
# Span Attributes for Content
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -179,23 +271,69 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
# Metrics
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_LITELLM,
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, scope._output_tokens,
- cost, scope._tbt, scope._ttft)
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_LITELLM,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ scope._output_tokens,
+ cost,
+ scope._tbt,
+ scope._ttft,
+ )
+
-def process_streaming_chat_response(scope, pricing_info, environment, application_name, metrics,
- capture_message_content=False, disable_metrics=False, version=""):
+def process_streaming_chat_response(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="",
+):
"""
Process streaming chat request and generate Telemetry
"""
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=True)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=True,
+ )
+
-def process_chat_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+def process_chat_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process chat request and generate Telemetry
"""
@@ -211,12 +349,14 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
(choice.get("message", {}).get("content") or "")
for choice in response_dict.get("choices", [])
)
- scope._input_tokens = response_dict.get('usage', {}).get('prompt_tokens', 0)
- scope._output_tokens = response_dict.get('usage', {}).get('completion_tokens', 0)
- scope._response_id = response_dict.get('id')
- scope._response_model = response_dict.get('model')
- scope._finish_reason = str(response_dict.get('choices', [])[0].get('finish_reason', ''))
- scope._response_service_tier = str(response_dict.get('system_fingerprint', ''))
+ scope._input_tokens = response_dict.get("usage", {}).get("prompt_tokens", 0)
+ scope._output_tokens = response_dict.get("usage", {}).get("completion_tokens", 0)
+ scope._response_id = response_dict.get("id")
+ scope._response_model = response_dict.get("model")
+ scope._finish_reason = str(
+ response_dict.get("choices", [])[0].get("finish_reason", "")
+ )
+ scope._response_service_tier = str(response_dict.get("system_fingerprint", ""))
scope._timestamps = []
scope._ttft, scope._tbt = scope._end_time - scope._start_time, 0
scope._server_address, scope._server_port = server_address, server_port
@@ -224,18 +364,43 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
# Handle tool calls
if scope._kwargs.get("tools"):
- scope._tools = response_dict.get("choices", [{}])[0].get("message", {}).get("tool_calls")
+ scope._tools = (
+ response_dict.get("choices", [{}])[0].get("message", {}).get("tool_calls")
+ )
else:
scope._tools = None
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=False)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=False,
+ )
return response
-def process_embedding_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+
+def process_embedding_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process embedding request and generate Telemetry
"""
@@ -247,8 +412,8 @@ def process_embedding_response(response, request_model, pricing_info, server_por
scope._start_time = start_time
scope._end_time = time.time()
scope._span = span
- scope._input_tokens = response_dict.get('usage', {}).get('prompt_tokens', 0)
- scope._response_model = response_dict.get('model')
+ scope._input_tokens = response_dict.get("usage", {}).get("prompt_tokens", 0)
+ scope._response_model = response_dict.get("model")
scope._server_address, scope._server_port = server_address, server_port
scope._kwargs = kwargs
@@ -256,33 +421,58 @@ def process_embedding_response(response, request_model, pricing_info, server_por
cost = get_embed_model_cost(request_model, pricing_info, scope._input_tokens)
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_LITELLM,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, False, 0, scope._end_time - scope._start_time, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
+ SemanticConvention.GEN_AI_SYSTEM_LITELLM,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ False,
+ 0,
+ scope._end_time - scope._start_time,
+ version,
+ )
# Helper function to handle None values with proper defaults
def safe_get(value, default):
return default if value is None else value
# Span Attributes for Request parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS, [scope._kwargs.get('encoding_format', 'float')])
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, safe_get(scope._kwargs.get('user'), ''))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS,
+ [scope._kwargs.get("encoding_format", "float")],
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_USER, safe_get(scope._kwargs.get("user"), "")
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Content
if capture_message_content:
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, str(scope._kwargs.get('input', '')))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_PROMPT,
+ str(scope._kwargs.get("input", "")),
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: str(scope._kwargs.get('input', '')),
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: str(
+ scope._kwargs.get("input", "")
+ ),
},
)
@@ -290,8 +480,20 @@ def process_embedding_response(response, request_model, pricing_info, server_por
# Metrics
if not disable_metrics:
- record_embedding_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_LITELLM,
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, cost)
+ record_embedding_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
+ SemanticConvention.GEN_AI_SYSTEM_LITELLM,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ cost,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/llamaindex/__init__.py b/sdk/python/src/openlit/instrumentation/llamaindex/__init__.py
index 375b1c2a..ebd462a9 100644
--- a/sdk/python/src/openlit/instrumentation/llamaindex/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/llamaindex/__init__.py
@@ -8,12 +8,11 @@ from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
from openlit.instrumentation.llamaindex.llamaindex import general_wrap
-from openlit.instrumentation.llamaindex.async_llamaindex import (
- async_general_wrap
-)
+from openlit.instrumentation.llamaindex.async_llamaindex import async_general_wrap
_instruments = ("llama-index >= 0.10.0",)
+
class LlamaIndexInstrumentor(BaseInstrumentor):
"""Framework guide compliant instrumentor with optimized performance"""
@@ -34,119 +33,279 @@ class LlamaIndexInstrumentor(BaseInstrumentor):
# === WORKFLOW OPERATIONS (Always enabled) - 17 operations ===
workflow_operations = [
# Document Loading & Processing Pipeline
- ("llama_index.core.readers", "SimpleDirectoryReader.load_data",
- "document_load"),
- ("llama_index.core.readers.base", "BaseReader.load_data",
- "document_load"),
- ("llama_index.core.document_transformer",
- "DocumentTransformer.transform", "document_transform"),
- ("llama_index.core.node_parser",
- "SentenceSplitter.get_nodes_from_documents", "document_split"),
+ (
+ "llama_index.core.readers",
+ "SimpleDirectoryReader.load_data",
+ "document_load",
+ ),
+ ("llama_index.core.readers.base", "BaseReader.load_data", "document_load"),
+ (
+ "llama_index.core.document_transformer",
+ "DocumentTransformer.transform",
+ "document_transform",
+ ),
+ (
+ "llama_index.core.node_parser",
+ "SentenceSplitter.get_nodes_from_documents",
+ "document_split",
+ ),
# Index Construction & Management
- ("llama_index.core.indices.vector_store.base",
- "VectorStoreIndex.from_documents", "index_construct"),
- ("llama_index.core.indices.vector_store.base",
- "VectorStoreIndex.from_vector_store", "index_construct"),
- ("llama_index.core.indices.list.base", "ListIndex.from_documents",
- "index_construct"),
- ("llama_index.core.indices.tree.base", "TreeIndex.from_documents",
- "index_construct"),
+ (
+ "llama_index.core.indices.vector_store.base",
+ "VectorStoreIndex.from_documents",
+ "index_construct",
+ ),
+ (
+ "llama_index.core.indices.vector_store.base",
+ "VectorStoreIndex.from_vector_store",
+ "index_construct",
+ ),
+ (
+ "llama_index.core.indices.list.base",
+ "ListIndex.from_documents",
+ "index_construct",
+ ),
+ (
+ "llama_index.core.indices.tree.base",
+ "TreeIndex.from_documents",
+ "index_construct",
+ ),
# Query Engine Operations
- ("llama_index.core.indices.base", "BaseIndex.as_query_engine",
- "query_engine_create"),
- ("llama_index.core.query_engine.retriever_query_engine",
- "RetrieverQueryEngine.query", "query_engine_query"),
- ("llama_index.core.query_engine.transform_query_engine",
- "TransformQueryEngine.query", "query_engine_query"),
+ (
+ "llama_index.core.indices.base",
+ "BaseIndex.as_query_engine",
+ "query_engine_create",
+ ),
+ (
+ "llama_index.core.query_engine.retriever_query_engine",
+ "RetrieverQueryEngine.query",
+ "query_engine_query",
+ ),
+ (
+ "llama_index.core.query_engine.transform_query_engine",
+ "TransformQueryEngine.query",
+ "query_engine_query",
+ ),
# Retrieval Operations
- ("llama_index.core.indices.base", "BaseIndex.as_retriever",
- "retriever_create"),
- ("llama_index.core.indices.vector_store.retrievers.retriever",
- "VectorIndexRetriever.retrieve", "retriever_retrieve"),
- ("llama_index.core.retrievers.base", "BaseRetriever.retrieve",
- "retriever_retrieve"),
+ (
+ "llama_index.core.indices.base",
+ "BaseIndex.as_retriever",
+ "retriever_create",
+ ),
+ (
+ "llama_index.core.indices.vector_store.retrievers.retriever",
+ "VectorIndexRetriever.retrieve",
+ "retriever_retrieve",
+ ),
+ (
+ "llama_index.core.retrievers.base",
+ "BaseRetriever.retrieve",
+ "retriever_retrieve",
+ ),
# LLM & Embedding Operations
("llama_index.core.llms.llm", "LLM.complete", "llm_complete"),
("llama_index.core.llms.llm", "LLM.chat", "llm_chat"),
- ("llama_index.core.embeddings.base",
- "BaseEmbedding.get_text_embedding_batch", "embedding_generate"),
+ (
+ "llama_index.core.embeddings.base",
+ "BaseEmbedding.get_text_embedding_batch",
+ "embedding_generate",
+ ),
]
# === ASYNC OPERATIONS - 13 operations ===
async_operations = [
- ("llama_index.core.readers", "SimpleDirectoryReader.aload_data", "document_load_async"),
- ("llama_index.core.readers.base", "BaseReader.aload_data", "document_load_async"),
- ("llama_index.core.query_engine.retriever_query_engine", "RetrieverQueryEngine.aquery", "query_engine_query_async"),
- ("llama_index.core.query_engine.transform_query_engine", "TransformQueryEngine.aquery", "query_engine_query_async"),
- ("llama_index.core.indices.vector_store.retrievers.retriever",
- "VectorIndexRetriever.aretrieve", "retriever_retrieve_async"),
- ("llama_index.core.retrievers.base", "BaseRetriever.aretrieve", "retriever_retrieve_async"),
- ("llama_index.core.embeddings.base", "BaseEmbedding.aget_text_embedding", "embedding_generate_async"),
- ("llama_index.core.embeddings.base", "BaseEmbedding.aget_text_embedding_batch", "embedding_generate_async"),
+ (
+ "llama_index.core.readers",
+ "SimpleDirectoryReader.aload_data",
+ "document_load_async",
+ ),
+ (
+ "llama_index.core.readers.base",
+ "BaseReader.aload_data",
+ "document_load_async",
+ ),
+ (
+ "llama_index.core.query_engine.retriever_query_engine",
+ "RetrieverQueryEngine.aquery",
+ "query_engine_query_async",
+ ),
+ (
+ "llama_index.core.query_engine.transform_query_engine",
+ "TransformQueryEngine.aquery",
+ "query_engine_query_async",
+ ),
+ (
+ "llama_index.core.indices.vector_store.retrievers.retriever",
+ "VectorIndexRetriever.aretrieve",
+ "retriever_retrieve_async",
+ ),
+ (
+ "llama_index.core.retrievers.base",
+ "BaseRetriever.aretrieve",
+ "retriever_retrieve_async",
+ ),
+ (
+ "llama_index.core.embeddings.base",
+ "BaseEmbedding.aget_text_embedding",
+ "embedding_generate_async",
+ ),
+ (
+ "llama_index.core.embeddings.base",
+ "BaseEmbedding.aget_text_embedding_batch",
+ "embedding_generate_async",
+ ),
("llama_index.core.llms.llm", "LLM.acomplete", "llm_complete_async"),
("llama_index.core.llms.llm", "LLM.achat", "llm_chat_async"),
("llama_index.core.llms.llm", "LLM.astream_complete", "llm_stream_async"),
("llama_index.core.llms.llm", "LLM.astream_chat", "llm_stream_async"),
- ("llama_index.core.response_synthesizers.base", "BaseSynthesizer.asynthesize", "response_generate_async"),
+ (
+ "llama_index.core.response_synthesizers.base",
+ "BaseSynthesizer.asynthesize",
+ "response_generate_async",
+ ),
]
# === COMPONENT OPERATIONS (Detailed tracing only) - 25 operations ===
component_operations = [
# Text Processing Components
- ("llama_index.core.node_parser.text.sentence", "SentenceSplitter.split_text", "text_splitter_split"),
- ("llama_index.core.node_parser.text.sentence", "SentenceSplitter._postprocess_nodes", "text_splitter_postprocess"),
- ("llama_index.core.node_parser.interface", "NodeParser.get_nodes_from_node", "node_parser_parse"),
-
+ (
+ "llama_index.core.node_parser.text.sentence",
+ "SentenceSplitter.split_text",
+ "text_splitter_split",
+ ),
+ (
+ "llama_index.core.node_parser.text.sentence",
+ "SentenceSplitter._postprocess_nodes",
+ "text_splitter_postprocess",
+ ),
+ (
+ "llama_index.core.node_parser.interface",
+ "NodeParser.get_nodes_from_node",
+ "node_parser_parse",
+ ),
# Embedding Processing Components
- ("llama_index.core.embeddings.base", "BaseEmbedding._get_text_embeddings", "embedding_encode"),
- ("llama_index.core.embeddings.utils", "embed_nodes", "embedding_embed_nodes"),
+ (
+ "llama_index.core.embeddings.base",
+ "BaseEmbedding._get_text_embeddings",
+ "embedding_encode",
+ ),
+ (
+ "llama_index.core.embeddings.utils",
+ "embed_nodes",
+ "embedding_embed_nodes",
+ ),
("llama_index.core.embeddings.utils", "similarity", "embedding_similarity"),
-
# Retrieval Processing Components
- ("llama_index.core.retrievers.base", "BaseRetriever._retrieve_nodes", "retrieval_retrieve_nodes"),
- ("llama_index.core.indices.vector_store.retrievers.retriever",
- "VectorIndexRetriever._get_nodes_with_embeddings", "retrieval_get_nodes"),
- ("llama_index.core.indices.vector_store.retrievers.retriever",
- "VectorIndexRetriever._build_node_list_from_query_result", "retrieval_build_nodes"),
- ("llama_index.core.postprocessor.node",
- "BaseNodePostprocessor.postprocess_nodes", "postprocessor_process"),
-
+ (
+ "llama_index.core.retrievers.base",
+ "BaseRetriever._retrieve_nodes",
+ "retrieval_retrieve_nodes",
+ ),
+ (
+ "llama_index.core.indices.vector_store.retrievers.retriever",
+ "VectorIndexRetriever._get_nodes_with_embeddings",
+ "retrieval_get_nodes",
+ ),
+ (
+ "llama_index.core.indices.vector_store.retrievers.retriever",
+ "VectorIndexRetriever._build_node_list_from_query_result",
+ "retrieval_build_nodes",
+ ),
+ (
+ "llama_index.core.postprocessor.node",
+ "BaseNodePostprocessor.postprocess_nodes",
+ "postprocessor_process",
+ ),
# Response Generation Components
- ("llama_index.core.response_synthesizers.base",
- "BaseSynthesizer.synthesize", "response_synthesize"),
- ("llama_index.core.response_synthesizers.compact_and_refine",
- "CompactAndRefine.get_response", "response_compact_refine"),
- ("llama_index.core.response_synthesizers.tree_summarize",
- "TreeSummarize.get_response", "response_tree_summarize"),
-
+ (
+ "llama_index.core.response_synthesizers.base",
+ "BaseSynthesizer.synthesize",
+ "response_synthesize",
+ ),
+ (
+ "llama_index.core.response_synthesizers.compact_and_refine",
+ "CompactAndRefine.get_response",
+ "response_compact_refine",
+ ),
+ (
+ "llama_index.core.response_synthesizers.tree_summarize",
+ "TreeSummarize.get_response",
+ "response_tree_summarize",
+ ),
# Vector Store Components
- ("llama_index.core.vector_stores.simple", "SimpleVectorStore.add", "vector_store_add"),
- ("llama_index.core.vector_stores.simple", "SimpleVectorStore.delete", "vector_store_delete"),
- ("llama_index.core.vector_stores.simple", "SimpleVectorStore.query", "vector_store_query"),
-
+ (
+ "llama_index.core.vector_stores.simple",
+ "SimpleVectorStore.add",
+ "vector_store_add",
+ ),
+ (
+ "llama_index.core.vector_stores.simple",
+ "SimpleVectorStore.delete",
+ "vector_store_delete",
+ ),
+ (
+ "llama_index.core.vector_stores.simple",
+ "SimpleVectorStore.query",
+ "vector_store_query",
+ ),
# Index Maintenance Components
- ("llama_index.core.indices.vector_store.base", "VectorStoreIndex._insert", "index_insert"),
- ("llama_index.core.indices.vector_store.base", "VectorStoreIndex._delete_node", "index_delete"),
- ("llama_index.core.indices.vector_store.base", "VectorStoreIndex._build_index_from_nodes", "index_build"),
-
+ (
+ "llama_index.core.indices.vector_store.base",
+ "VectorStoreIndex._insert",
+ "index_insert",
+ ),
+ (
+ "llama_index.core.indices.vector_store.base",
+ "VectorStoreIndex._delete_node",
+ "index_delete",
+ ),
+ (
+ "llama_index.core.indices.vector_store.base",
+ "VectorStoreIndex._build_index_from_nodes",
+ "index_build",
+ ),
# Additional Framework Components
("llama_index.core.schema", "Document.get_content", "document_get_content"),
("llama_index.core.schema", "TextNode.get_content", "node_get_content"),
- ("llama_index.core.schema", "TextNode.get_metadata_str", "node_get_metadata"),
- ("llama_index.core.readers.base", "BaseReader._extract_metadata", "document_extract_metadata"),
- ("llama_index.core.vector_stores.utils", "node_to_metadata_dict", "embedding_metadata"),
- ("llama_index.core.query_engine.retriever_query_engine",
- "RetrieverQueryEngine._prepare_response_builder", "query_prepare_response"),
+ (
+ "llama_index.core.schema",
+ "TextNode.get_metadata_str",
+ "node_get_metadata",
+ ),
+ (
+ "llama_index.core.readers.base",
+ "BaseReader._extract_metadata",
+ "document_extract_metadata",
+ ),
+ (
+ "llama_index.core.vector_stores.utils",
+ "node_to_metadata_dict",
+ "embedding_metadata",
+ ),
+ (
+ "llama_index.core.query_engine.retriever_query_engine",
+ "RetrieverQueryEngine._prepare_response_builder",
+ "query_prepare_response",
+ ),
]
# Wrap workflow operations (always enabled)
for module, method, operation_type in workflow_operations:
try:
wrap_function_wrapper(
- module, method,
- general_wrap(operation_type, version, environment, application_name,
- tracer, pricing_info, capture_message_content,
- metrics, disable_metrics)
+ module,
+ method,
+ general_wrap(
+ operation_type,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
except Exception:
pass
@@ -155,10 +314,19 @@ class LlamaIndexInstrumentor(BaseInstrumentor):
for module, method, operation_type in async_operations:
try:
wrap_function_wrapper(
- module, method,
- async_general_wrap(operation_type, version, environment,
- application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics)
+ module,
+ method,
+ async_general_wrap(
+ operation_type,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
except Exception:
pass
@@ -168,10 +336,19 @@ class LlamaIndexInstrumentor(BaseInstrumentor):
for module, method, operation_type in component_operations:
try:
wrap_function_wrapper(
- module, method,
- general_wrap(operation_type, version, environment,
- application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics)
+ module,
+ method,
+ general_wrap(
+ operation_type,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
except Exception:
pass
diff --git a/sdk/python/src/openlit/instrumentation/llamaindex/async_llamaindex.py b/sdk/python/src/openlit/instrumentation/llamaindex/async_llamaindex.py
index ce3200e4..39381e70 100644
--- a/sdk/python/src/openlit/instrumentation/llamaindex/async_llamaindex.py
+++ b/sdk/python/src/openlit/instrumentation/llamaindex/async_llamaindex.py
@@ -12,8 +12,18 @@ from openlit.instrumentation.llamaindex.utils import (
set_server_address_and_port,
)
-def async_general_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_general_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for LlamaIndex async function calls.
"""
@@ -39,10 +49,22 @@ def async_general_wrap(gen_ai_endpoint, version, environment, application_name,
try:
# Process response and generate telemetry
response = process_llamaindex_response(
- response, operation_type, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version,
- instance, args, endpoint=gen_ai_endpoint, **kwargs
+ response,
+ operation_type,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ args,
+ endpoint=gen_ai_endpoint,
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/llamaindex/llamaindex.py b/sdk/python/src/openlit/instrumentation/llamaindex/llamaindex.py
index 45ae3775..5760208f 100644
--- a/sdk/python/src/openlit/instrumentation/llamaindex/llamaindex.py
+++ b/sdk/python/src/openlit/instrumentation/llamaindex/llamaindex.py
@@ -13,8 +13,18 @@ from openlit.instrumentation.llamaindex.utils import (
set_server_address_and_port,
)
-def general_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def general_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for LlamaIndex function calls.
"""
@@ -40,10 +50,22 @@ def general_wrap(gen_ai_endpoint, version, environment, application_name,
try:
# Process response and generate telemetry
response = process_llamaindex_response(
- response, operation_type, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version,
- instance, args, endpoint=gen_ai_endpoint, **kwargs
+ response,
+ operation_type,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ args,
+ endpoint=gen_ai_endpoint,
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/llamaindex/utils.py b/sdk/python/src/openlit/instrumentation/llamaindex/utils.py
index 1b1d4e86..2f170e4e 100644
--- a/sdk/python/src/openlit/instrumentation/llamaindex/utils.py
+++ b/sdk/python/src/openlit/instrumentation/llamaindex/utils.py
@@ -1,6 +1,7 @@
"""
LlamaIndex OpenTelemetry Instrumentation
"""
+
import time
import hashlib
from opentelemetry.trace import Status, StatusCode
@@ -15,29 +16,24 @@ from openlit.semcov import SemanticConvention
# Simplified semantic conventions for efficient processing
OPERATION_MAP = {
# === WORKFLOW OPERATIONS (Business-level spans) ===
-
# Document Loading & Processing Pipeline
"document_load": SemanticConvention.GEN_AI_OPERATION_TYPE_RETRIEVE,
"document_load_async": SemanticConvention.GEN_AI_OPERATION_TYPE_RETRIEVE,
"document_transform": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
"document_split": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
-
# Index Construction & Management
"index_construct": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
"index_insert": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
"index_delete": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
"index_build": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
-
# Query Engine Operations
"query_engine_create": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
"query_engine_query": SemanticConvention.GEN_AI_OPERATION_TYPE_RETRIEVE,
"query_engine_query_async": SemanticConvention.GEN_AI_OPERATION_TYPE_RETRIEVE,
-
# Retriever Operations
"retriever_create": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
"retriever_retrieve": SemanticConvention.GEN_AI_OPERATION_TYPE_RETRIEVE,
"retriever_retrieve_async": SemanticConvention.GEN_AI_OPERATION_TYPE_RETRIEVE,
-
# LLM & Embedding Operations
"llm_complete": SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
"llm_complete_async": SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
@@ -46,39 +42,31 @@ OPERATION_MAP = {
"llm_stream_async": SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
"embedding_generate": SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
"embedding_generate_async": SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
-
# Response Generation Operations
"response_generate_async": SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
-
# === COMPONENT OPERATIONS (Technical-level spans) ===
-
# Text Processing Components
"text_splitter_split": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
"text_splitter_postprocess": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
"node_parser_parse": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
-
# Embedding Processing Components
"embedding_encode": SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
"embedding_embed_nodes": SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
"embedding_similarity": SemanticConvention.GEN_AI_OPERATION_TYPE_RETRIEVE,
"embedding_metadata": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
-
# Retrieval Processing Components
"retrieval_retrieve_nodes": SemanticConvention.GEN_AI_OPERATION_TYPE_RETRIEVE,
"retrieval_get_nodes": SemanticConvention.GEN_AI_OPERATION_TYPE_RETRIEVE,
"retrieval_build_nodes": SemanticConvention.GEN_AI_OPERATION_TYPE_RETRIEVE,
"postprocessor_process": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
-
# Response Generation Components
"response_synthesize": SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
"response_compact_refine": SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
"response_tree_summarize": SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
-
# Vector Store Components
"vector_store_add": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
"vector_store_delete": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
"vector_store_query": SemanticConvention.GEN_AI_OPERATION_TYPE_RETRIEVE,
-
# Document & Node Components
"document_get_content": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
"node_get_content": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
@@ -87,16 +75,17 @@ OPERATION_MAP = {
"query_prepare_response": SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
}
+
def set_server_address_and_port(instance, default_host="localhost", default_port=8000):
"""Extract server address and port with enhanced detection"""
- if hasattr(instance, '_client'):
+ if hasattr(instance, "_client"):
client = instance._client
- if hasattr(client, 'base_url'):
+ if hasattr(client, "base_url"):
base_url = str(client.base_url)
- if '://' in base_url:
- parts = base_url.split('://', 1)[1].split('/', 1)[0]
- if ':' in parts:
- host, port = parts.rsplit(':', 1)
+ if "://" in base_url:
+ parts = base_url.split("://", 1)[1].split("/", 1)[0]
+ if ":" in parts:
+ host, port = parts.rsplit(":", 1)
try:
return host, int(port)
except ValueError:
@@ -104,20 +93,22 @@ def set_server_address_and_port(instance, default_host="localhost", default_port
return parts, default_port
return default_host, default_port
+
def object_count(obj):
"""Enhanced object counting with type detection"""
if obj is None:
return 0
try:
- if hasattr(obj, '__len__'):
+ if hasattr(obj, "__len__"):
return len(obj)
- elif hasattr(obj, '__iter__'):
+ elif hasattr(obj, "__iter__"):
return sum(1 for _ in obj)
else:
return 1 if obj else 0
except Exception:
return 0
+
def extract_performance_metrics(scope):
"""Extract comprehensive performance metrics"""
duration = scope._end_time - scope._start_time
@@ -137,9 +128,10 @@ def extract_performance_metrics(scope):
"performance_tier": performance_tier,
"latency_ms": duration * 1000,
"is_fast": duration < 0.5,
- "needs_optimization": duration > 2.0
+ "needs_optimization": duration > 2.0,
}
+
def extract_content_metrics(content):
"""Extract advanced content analysis metrics"""
if not content:
@@ -161,9 +153,10 @@ def extract_content_metrics(content):
"complexity_score": complexity_score,
"content_hash": hashlib.md5(content_str.encode()).hexdigest()[:8],
"is_lengthy": char_count > 1000,
- "is_complex": complexity_score > 50
+ "is_complex": complexity_score > 50,
}
+
def extract_business_intelligence(scope, endpoint):
"""Extract superior business intelligence attributes"""
bi_attrs = {}
@@ -193,9 +186,19 @@ def extract_business_intelligence(scope, endpoint):
return bi_attrs
-def common_llamaindex_logic(scope, environment, application_name,
- metrics, capture_message_content, disable_metrics, version,
- instance=None, endpoint=None, **kwargs):
+
+def common_llamaindex_logic(
+ scope,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance=None,
+ endpoint=None,
+ **kwargs,
+):
"""
DOMINANCE EDITION: Process LlamaIndex with superior attribute richness
Enhanced to beat OpenInference with 5+ attributes per span vs their 2.3
@@ -203,18 +206,30 @@ def common_llamaindex_logic(scope, environment, application_name,
scope._end_time = time.time()
# Set common framework span attributes using centralized helper
- common_framework_span_attributes(scope, SemanticConvention.GEN_AI_SYSTEM_LLAMAINDEX,
- scope._server_address, scope._server_port, environment, application_name,
- version, endpoint, instance)
+ common_framework_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_SYSTEM_LLAMAINDEX,
+ scope._server_address,
+ scope._server_port,
+ environment,
+ application_name,
+ version,
+ endpoint,
+ instance,
+ )
# === CORE SEMANTIC ATTRIBUTES ===
- operation_type = OPERATION_MAP.get(endpoint, SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK)
+ operation_type = OPERATION_MAP.get(
+ endpoint, SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_OPERATION, operation_type)
# === PERFORMANCE INTELLIGENCE ===
performance = extract_performance_metrics(scope)
scope._span.set_attribute("gen_ai.operation.duration_ms", performance["latency_ms"])
- scope._span.set_attribute("gen_ai.operation.performance_tier", performance["performance_tier"])
+ scope._span.set_attribute(
+ "gen_ai.operation.performance_tier", performance["performance_tier"]
+ )
scope._span.set_attribute("gen_ai.operation.is_fast", performance["is_fast"])
# === BUSINESS INTELLIGENCE ===
@@ -229,115 +244,179 @@ def common_llamaindex_logic(scope, environment, application_name,
documents_count = scope._kwargs.get("documents", [])
if documents_count:
doc_count = object_count(documents_count)
- scope._span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_DOCUMENTS_COUNT, doc_count)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_DOCUMENTS_COUNT, doc_count
+ )
# Document source analysis
document_sources = []
total_content_length = 0
unique_authors = set()
- for doc in (documents_count[:10] if hasattr(documents_count, '__iter__') else []):
- if hasattr(doc, 'metadata'):
- source = doc.metadata.get('file_path', 'unknown')
- author = doc.metadata.get('author', 'unknown')
+ for doc in (
+ documents_count[:10] if hasattr(documents_count, "__iter__") else []
+ ):
+ if hasattr(doc, "metadata"):
+ source = doc.metadata.get("file_path", "unknown")
+ author = doc.metadata.get("author", "unknown")
document_sources.append(source)
unique_authors.add(author)
- if hasattr(doc, 'text'):
+ if hasattr(doc, "text"):
total_content_length += len(doc.text)
- scope._span.set_attribute("gen_ai.index.document_sources", str(document_sources[:5]))
- scope._span.set_attribute("gen_ai.index.total_content_length", total_content_length)
- scope._span.set_attribute("gen_ai.index.unique_authors", len(unique_authors))
- scope._span.set_attribute("gen_ai.index.avg_document_size", total_content_length // max(doc_count, 1))
-
- elif endpoint in ("framework.query_engine.query", "framework.query_engine.query_async"):
+ scope._span.set_attribute(
+ "gen_ai.index.document_sources", str(document_sources[:5])
+ )
+ scope._span.set_attribute(
+ "gen_ai.index.total_content_length", total_content_length
+ )
+ scope._span.set_attribute(
+ "gen_ai.index.unique_authors", len(unique_authors)
+ )
+ scope._span.set_attribute(
+ "gen_ai.index.avg_document_size",
+ total_content_length // max(doc_count, 1),
+ )
+
+ elif endpoint in (
+ "framework.query_engine.query",
+ "framework.query_engine.query_async",
+ ):
# Enhanced query processing telemetry
- query_text = scope._args[0] if scope._args else scope._kwargs.get("query", "unknown")
+ query_text = (
+ scope._args[0] if scope._args else scope._kwargs.get("query", "unknown")
+ )
if capture_message_content:
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, str(query_text))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_PROMPT, str(query_text)
+ )
# Query analysis
query_length = len(str(query_text))
query_words = len(str(query_text).split())
scope._span.set_attribute("gen_ai.query.length", query_length)
scope._span.set_attribute("gen_ai.query.word_count", query_words)
- scope._span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_QUERY_TYPE, "query_engine")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_QUERY_TYPE, "query_engine"
+ )
# Process index operations using helper function
_process_index_operations(scope, endpoint, capture_message_content)
- elif endpoint in ("framework.retriever.retrieve", "framework.retriever.retrieve_async"):
+ elif endpoint in (
+ "framework.retriever.retrieve",
+ "framework.retriever.retrieve_async",
+ ):
# Enhanced retrieval telemetry
- query_text = scope._args[0] if scope._args else scope._kwargs.get("query", "unknown")
+ query_text = (
+ scope._args[0] if scope._args else scope._kwargs.get("query", "unknown")
+ )
if capture_message_content:
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, str(query_text))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_PROMPT, str(query_text)
+ )
# Retrieval configuration analysis
similarity_top_k = scope._kwargs.get("similarity_top_k", 2)
scope._span.set_attribute("gen_ai.retrieval.top_k", similarity_top_k)
scope._span.set_attribute("gen_ai.retrieval.strategy", "vector_similarity")
- scope._span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_QUERY_TYPE, "retriever")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_QUERY_TYPE, "retriever"
+ )
elif endpoint == "framework.document.split":
# Enhanced document splitting telemetry
show_progress = scope._kwargs.get("show_progress", False)
- scope._span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_SHOW_PROGRESS, show_progress)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_SHOW_PROGRESS, show_progress
+ )
# Extract enhanced node creation info
- if scope._response and hasattr(scope._response, '__len__'):
+ if scope._response and hasattr(scope._response, "__len__"):
nodes_created = len(scope._response)
- scope._span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_NODES_CREATED, nodes_created)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_NODES_CREATED, nodes_created
+ )
# Extract comprehensive chunk configuration
- chunk_size = getattr(instance, 'chunk_size', 1024) if instance else 1024
- chunk_overlap = getattr(instance, 'chunk_overlap', 200) if instance else 200
- separator = getattr(instance, 'separator', '\\n\\n') if instance else '\\n\\n'
-
- scope._span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_CHUNK_SIZE, chunk_size)
- scope._span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_CHUNK_OVERLAP, chunk_overlap)
+ chunk_size = getattr(instance, "chunk_size", 1024) if instance else 1024
+ chunk_overlap = getattr(instance, "chunk_overlap", 200) if instance else 200
+ separator = getattr(instance, "separator", "\\n\\n") if instance else "\\n\\n"
+
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_CHUNK_SIZE, chunk_size
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_FRAMEWORK_CHUNK_OVERLAP, chunk_overlap
+ )
scope._span.set_attribute("gen_ai.splitter.separator", separator)
- scope._span.set_attribute("gen_ai.splitter.efficiency", nodes_created / max(1, chunk_size // 100))
+ scope._span.set_attribute(
+ "gen_ai.splitter.efficiency", nodes_created / max(1, chunk_size // 100)
+ )
# === COMPONENT-LEVEL ENHANCED PROCESSING ===
elif endpoint.startswith("component.text_splitter"):
if endpoint == "component.text_splitter.split":
- text_input = scope._args[0] if scope._args else scope._kwargs.get("text", "")
+ text_input = (
+ scope._args[0] if scope._args else scope._kwargs.get("text", "")
+ )
text_metrics = extract_content_metrics(text_input)
- scope._span.set_attribute("gen_ai.component.input_length", text_metrics.get("char_count", 0))
- scope._span.set_attribute("gen_ai.component.input_complexity", text_metrics.get("complexity_score", 0))
-
- if scope._response and hasattr(scope._response, '__len__'):
+ scope._span.set_attribute(
+ "gen_ai.component.input_length", text_metrics.get("char_count", 0)
+ )
+ scope._span.set_attribute(
+ "gen_ai.component.input_complexity",
+ text_metrics.get("complexity_score", 0),
+ )
+
+ if scope._response and hasattr(scope._response, "__len__"):
chunks_created = len(scope._response)
- scope._span.set_attribute("gen_ai.component.chunks_created", chunks_created)
- scope._span.set_attribute("gen_ai.component.compression_ratio",
- chunks_created / max(1, text_metrics.get("word_count", 1) // 100))
+ scope._span.set_attribute(
+ "gen_ai.component.chunks_created", chunks_created
+ )
+ scope._span.set_attribute(
+ "gen_ai.component.compression_ratio",
+ chunks_created / max(1, text_metrics.get("word_count", 1) // 100),
+ )
elif endpoint.startswith("component.embedding"):
if endpoint == "component.embedding.encode":
texts = scope._args[0] if scope._args else scope._kwargs.get("texts", [])
embedding_count = object_count(texts)
- scope._span.set_attribute("gen_ai.component.embedding_count", embedding_count)
+ scope._span.set_attribute(
+ "gen_ai.component.embedding_count", embedding_count
+ )
- if embedding_count > 0 and hasattr(texts, '__iter__'):
+ if embedding_count > 0 and hasattr(texts, "__iter__"):
total_chars = sum(len(str(text)) for text in texts)
scope._span.set_attribute("gen_ai.component.total_chars", total_chars)
- scope._span.set_attribute("gen_ai.component.avg_text_length", total_chars // embedding_count)
+ scope._span.set_attribute(
+ "gen_ai.component.avg_text_length", total_chars // embedding_count
+ )
elif endpoint.startswith("component.retrieval"):
if endpoint == "component.retrieval.retrieve_nodes":
# Enhanced retrieval component analysis
- query_embedding = scope._args[0] if scope._args else scope._kwargs.get("query_embedding")
- if query_embedding and hasattr(query_embedding, '__len__'):
- scope._span.set_attribute("gen_ai.component.embedding_dimension", len(query_embedding))
+ query_embedding = (
+ scope._args[0] if scope._args else scope._kwargs.get("query_embedding")
+ )
+ if query_embedding and hasattr(query_embedding, "__len__"):
+ scope._span.set_attribute(
+ "gen_ai.component.embedding_dimension", len(query_embedding)
+ )
similarity_threshold = scope._kwargs.get("similarity_threshold", 0.0)
- scope._span.set_attribute("gen_ai.component.similarity_threshold", similarity_threshold)
+ scope._span.set_attribute(
+ "gen_ai.component.similarity_threshold", similarity_threshold
+ )
# === UNIVERSAL ATTRIBUTES ===
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_OPERATION_DURATION,
- scope._end_time - scope._start_time)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_OPERATION_DURATION,
+ scope._end_time - scope._start_time,
+ )
scope._span.set_attribute("gen_ai.operation.endpoint", endpoint)
scope._span.set_attribute("gen_ai.framework.version", version)
scope._span.set_attribute("gen_ai.operation.success", True)
@@ -346,14 +425,37 @@ def common_llamaindex_logic(scope, environment, application_name,
# Record enhanced metrics
if not disable_metrics:
- record_framework_metrics(metrics, scope._operation_type, SemanticConvention.GEN_AI_SYSTEM_LLAMAINDEX,
- scope._server_address, scope._server_port, environment, application_name,
- scope._start_time, scope._end_time)
-
-def process_llamaindex_response(response, operation_type, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version, instance=None,
- args=None, endpoint=None, **kwargs):
+ record_framework_metrics(
+ metrics,
+ scope._operation_type,
+ SemanticConvention.GEN_AI_SYSTEM_LLAMAINDEX,
+ scope._server_address,
+ scope._server_port,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ )
+
+
+def process_llamaindex_response(
+ response,
+ operation_type,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance=None,
+ args=None,
+ endpoint=None,
+ **kwargs,
+):
"""
DOMINANCE EDITION: Process LlamaIndex response with superior observability
"""
@@ -370,16 +472,23 @@ def process_llamaindex_response(response, operation_type, server_address, server
# Process with enhanced telemetry
common_llamaindex_logic(
- scope, environment, application_name, metrics,
- capture_message_content, disable_metrics, version,
- instance, endpoint
+ scope,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ endpoint,
)
return response
+
def _process_index_operations(scope, endpoint, capture_message_content):
"""Helper function to process index operations and reduce nesting"""
- if not hasattr(scope, '_result') or not scope._result:
+ if not hasattr(scope, "_result") or not scope._result:
return
try:
@@ -405,8 +514,12 @@ def _process_index_operations(scope, endpoint, capture_message_content):
if hasattr(node, "text"):
total_content_length += len(str(node.text))
- scope._span.set_attribute("gen_ai.index.total_content_length", total_content_length)
+ scope._span.set_attribute(
+ "gen_ai.index.total_content_length", total_content_length
+ )
scope._span.set_attribute("gen_ai.index.unique_authors", len(unique_authors))
- scope._span.set_attribute("gen_ai.index.avg_document_size", total_content_length // max(doc_count, 1))
+ scope._span.set_attribute(
+ "gen_ai.index.avg_document_size", total_content_length // max(doc_count, 1)
+ )
except Exception:
pass # Don't fail on metadata extraction
diff --git a/sdk/python/src/openlit/instrumentation/mem0/__init__.py b/sdk/python/src/openlit/instrumentation/mem0/__init__.py
index 1cb7dfb2..32e94a2e 100644
--- a/sdk/python/src/openlit/instrumentation/mem0/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/mem0/__init__.py
@@ -1,5 +1,6 @@
# pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code
"""Initializer of Auto Instrumentation of mem0 Functions"""
+
from typing import Collection
import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
@@ -48,6 +49,7 @@ WRAPPED_METHODS = [
},
]
+
class Mem0Instrumentor(BaseInstrumentor):
"""An instrumentor for mem0's client library."""
@@ -70,8 +72,15 @@ class Mem0Instrumentor(BaseInstrumentor):
wrap_function_wrapper(
wrap_package,
wrap_object,
- wrapper(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content),
+ wrapper(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ ),
)
@staticmethod
diff --git a/sdk/python/src/openlit/instrumentation/mem0/mem0.py b/sdk/python/src/openlit/instrumentation/mem0/mem0.py
index 748da3fb..1641d247 100644
--- a/sdk/python/src/openlit/instrumentation/mem0/mem0.py
+++ b/sdk/python/src/openlit/instrumentation/mem0/mem0.py
@@ -5,15 +5,27 @@ Module for monitoring mem0 applications.
import logging
from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from openlit.__helpers import handle_exception
from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def mem0_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content):
+
+def mem0_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+):
"""
Creates a wrapper around a function call to trace and log its execution metrics.
@@ -54,51 +66,60 @@ def mem0_wrap(gen_ai_endpoint, version, environment, application_name,
on the span based on the function's execution and response, and ensures
errors are handled and logged appropriately.
"""
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(
+ gen_ai_endpoint, kind=SpanKind.CLIENT
+ ) as span:
response = wrapped(*args, **kwargs)
try:
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_MEM0)
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,
- gen_ai_endpoint)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK)
- span.set_attribute(SERVICE_NAME,
- application_name)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM,
+ SemanticConvention.GEN_AI_SYSTEM_MEM0,
+ )
+ span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,
+ )
+ span.set_attribute(SERVICE_NAME, application_name)
if gen_ai_endpoint == "mem0.memory_add":
- span.set_attribute(SemanticConvention.DB_METADATA,
- str(kwargs.get('metadata', '')))
+ span.set_attribute(
+ SemanticConvention.DB_METADATA, str(kwargs.get("metadata", ""))
+ )
if response:
- span.set_attribute(SemanticConvention.GEN_AI_DATA_SOURCES,
- len(response))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_DATA_SOURCES, len(response)
+ )
elif gen_ai_endpoint == "mem0.memory_get_all":
if response:
- span.set_attribute(SemanticConvention.GEN_AI_DATA_SOURCES,
- len(response))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_DATA_SOURCES, len(response)
+ )
elif gen_ai_endpoint == "mem0.memory_get":
if response:
- span.set_attribute(SemanticConvention.GEN_AI_DATA_SOURCES,
- len(response))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_DATA_SOURCES, len(response)
+ )
elif gen_ai_endpoint == "mem0.memory_search":
- span.set_attribute(SemanticConvention.DB_STATEMENT,
- kwargs.get("query", ""))
+ span.set_attribute(
+ SemanticConvention.DB_STATEMENT, kwargs.get("query", "")
+ )
elif gen_ai_endpoint == "mem0.memory_update":
- span.set_attribute(SemanticConvention.DB_UPDATE_ID,
- kwargs.get("memory_id", ""))
+ span.set_attribute(
+ SemanticConvention.DB_UPDATE_ID, kwargs.get("memory_id", "")
+ )
elif gen_ai_endpoint == "mem0.memory_delete":
- span.set_attribute(SemanticConvention.DB_DELETE_ID,
- kwargs.get("memory_id", ""))
-
+ span.set_attribute(
+ SemanticConvention.DB_DELETE_ID, kwargs.get("memory_id", "")
+ )
span.set_status(Status(StatusCode.OK))
diff --git a/sdk/python/src/openlit/instrumentation/milvus/__init__.py b/sdk/python/src/openlit/instrumentation/milvus/__init__.py
index 8624b10d..4a71d49a 100644
--- a/sdk/python/src/openlit/instrumentation/milvus/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/milvus/__init__.py
@@ -23,6 +23,7 @@ MILVUS_OPERATIONS = [
("delete", "milvus.delete"),
]
+
class MilvusInstrumentor(BaseInstrumentor):
"""
An instrumentor for Milvus's client library.
@@ -47,8 +48,15 @@ class MilvusInstrumentor(BaseInstrumentor):
"pymilvus",
f"MilvusClient.{method_name}",
general_wrap(
- endpoint, version, environment, application_name, tracer,
- pricing_info, capture_message_content, metrics, disable_metrics
+ endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
),
)
diff --git a/sdk/python/src/openlit/instrumentation/milvus/milvus.py b/sdk/python/src/openlit/instrumentation/milvus/milvus.py
index 23140e39..a53c2554 100644
--- a/sdk/python/src/openlit/instrumentation/milvus/milvus.py
+++ b/sdk/python/src/openlit/instrumentation/milvus/milvus.py
@@ -12,11 +12,22 @@ from openlit.instrumentation.milvus.utils import (
set_server_address_and_port,
)
-def general_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def general_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for Milvus function calls.
"""
+
def wrapper(wrapped, instance, args, kwargs):
# CRITICAL: Suppression check
if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
@@ -27,7 +38,9 @@ def general_wrap(gen_ai_endpoint, version, environment, application_name,
db_operation = DB_OPERATION_MAP.get(gen_ai_endpoint, "unknown")
if db_operation == "create_collection":
- collection_name = kwargs.get("collection_name") or (args[0] if args else "unknown")
+ collection_name = kwargs.get("collection_name") or (
+ args[0] if args else "unknown"
+ )
else:
collection_name = kwargs.get("collection_name", "unknown")
span_name = f"{db_operation} {collection_name}"
@@ -39,9 +52,21 @@ def general_wrap(gen_ai_endpoint, version, environment, application_name,
try:
# Process response and generate telemetry
response = process_milvus_response(
- response, db_operation, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version, instance, args, **kwargs
+ response,
+ db_operation,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ args,
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/milvus/utils.py b/sdk/python/src/openlit/instrumentation/milvus/utils.py
index b702c456..28f8fde6 100644
--- a/sdk/python/src/openlit/instrumentation/milvus/utils.py
+++ b/sdk/python/src/openlit/instrumentation/milvus/utils.py
@@ -23,12 +23,14 @@ DB_OPERATION_MAP = {
"milvus.delete": SemanticConvention.DB_OPERATION_DELETE,
}
+
def object_count(obj):
"""
Counts length of object if it exists, else returns 0.
"""
return len(obj) if obj else 0
+
def set_server_address_and_port(instance):
"""
Extracts server address and port from Milvus client instance.
@@ -68,9 +70,18 @@ def set_server_address_and_port(instance):
return server_address, server_port
-def common_milvus_logic(scope, environment, application_name,
- metrics, capture_message_content, disable_metrics, version,
- instance=None, endpoint=None):
+
+def common_milvus_logic(
+ scope,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance=None,
+ endpoint=None,
+):
"""
Process Milvus database request and generate telemetry.
@@ -88,153 +99,233 @@ def common_milvus_logic(scope, environment, application_name,
scope._end_time = time.time()
# Set common database span attributes using helper
- common_db_span_attributes(scope, SemanticConvention.DB_SYSTEM_MILVUS,
- scope._server_address, scope._server_port, environment, application_name, version)
+ common_db_span_attributes(
+ scope,
+ SemanticConvention.DB_SYSTEM_MILVUS,
+ scope._server_address,
+ scope._server_port,
+ environment,
+ application_name,
+ version,
+ )
# Set DB operation specific attributes
scope._span.set_attribute(SemanticConvention.DB_OPERATION_NAME, scope._db_operation)
- scope._span.set_attribute(SemanticConvention.DB_CLIENT_OPERATION_DURATION,
- scope._end_time - scope._start_time)
+ scope._span.set_attribute(
+ SemanticConvention.DB_CLIENT_OPERATION_DURATION,
+ scope._end_time - scope._start_time,
+ )
if scope._db_operation == SemanticConvention.DB_OPERATION_CREATE_COLLECTION:
collection_name = scope._kwargs.get("collection_name", "unknown")
# Standard database attributes
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_DIMENSION,
- scope._kwargs.get("dimension", -1))
-
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_DIMENSION,
+ scope._kwargs.get("dimension", -1),
+ )
+
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
- f"dimension={scope._kwargs.get('dimension', 'None')}")
+ f"dimension={scope._kwargs.get('dimension', 'None')}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_DELETE_COLLECTION:
collection_name = scope._kwargs.get("collection_name", "unknown")
# Standard database attributes
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
- f"{scope._db_operation} {collection_name}")
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_INSERT:
collection_name = scope._kwargs.get("collection_name", "unknown")
data = scope._kwargs.get("data", [])
# Standard database attributes
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, object_count(data))
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_COUNT, object_count(data)
+ )
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(data))
# Response metrics
if scope._response and scope._response.get("insert_count"):
- scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
- scope._response["insert_count"])
+ scope._span.set_attribute(
+ SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
+ scope._response["insert_count"],
+ )
if scope._response and scope._response.get("cost"):
- scope._span.set_attribute(SemanticConvention.DB_OPERATION_COST,
- scope._response["cost"])
+ scope._span.set_attribute(
+ SemanticConvention.DB_OPERATION_COST, scope._response["cost"]
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
- f"{scope._db_operation} {collection_name} "
- f"data_count={object_count(data)}")
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} data_count={object_count(data)}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_UPSERT:
collection_name = scope._kwargs.get("collection_name", "unknown")
data = scope._kwargs.get("data", [])
# Standard database attributes
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, object_count(data))
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_COUNT, object_count(data)
+ )
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(data))
# Response metrics
if scope._response and scope._response.get("upsert_count"):
- scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
- scope._response["upsert_count"])
+ scope._span.set_attribute(
+ SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
+ scope._response["upsert_count"],
+ )
if scope._response and scope._response.get("cost"):
- scope._span.set_attribute(SemanticConvention.DB_OPERATION_COST,
- scope._response["cost"])
+ scope._span.set_attribute(
+ SemanticConvention.DB_OPERATION_COST, scope._response["cost"]
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
- f"{scope._db_operation} {collection_name} "
- f"data_count={object_count(data)}")
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} data_count={object_count(data)}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_SEARCH:
collection_name = scope._kwargs.get("collection_name", "unknown")
data = scope._kwargs.get("data", [])
# Standard database attributes
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(data))
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_QUERY_TOP_K,
- scope._kwargs.get("limit", -1))
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_QUERY_TOP_K, scope._kwargs.get("limit", -1)
+ )
# Search specific attributes
- scope._span.set_attribute(SemanticConvention.DB_FILTER,
- str(scope._kwargs.get("filter", "")))
+ scope._span.set_attribute(
+ SemanticConvention.DB_FILTER, str(scope._kwargs.get("filter", ""))
+ )
# Response metrics
if scope._response and isinstance(scope._response, list):
- scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
- len(scope._response))
+ scope._span.set_attribute(
+ SemanticConvention.DB_RESPONSE_RETURNED_ROWS, len(scope._response)
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
f"data={str(data)[:100]}... "
- f"limit={scope._kwargs.get('limit', 'None')}")
+ f"limit={scope._kwargs.get('limit', 'None')}",
+ )
- elif scope._db_operation in [SemanticConvention.DB_OPERATION_QUERY, SemanticConvention.DB_OPERATION_GET]:
+ elif scope._db_operation in [
+ SemanticConvention.DB_OPERATION_QUERY,
+ SemanticConvention.DB_OPERATION_GET,
+ ]:
collection_name = scope._kwargs.get("collection_name", "unknown")
output_fields = scope._kwargs.get("output_fields", [])
# Standard database attributes
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(output_fields))
# Query specific attributes
- scope._span.set_attribute(SemanticConvention.DB_FILTER,
- str(scope._kwargs.get("filter", "")))
+ scope._span.set_attribute(
+ SemanticConvention.DB_FILTER, str(scope._kwargs.get("filter", ""))
+ )
# Response metrics
if scope._response and isinstance(scope._response, list):
- scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
- len(scope._response))
+ scope._span.set_attribute(
+ SemanticConvention.DB_RESPONSE_RETURNED_ROWS, len(scope._response)
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
f"output_fields={output_fields} "
- f"filter={scope._kwargs.get('filter', 'None')}")
+ f"filter={scope._kwargs.get('filter', 'None')}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_DELETE:
collection_name = scope._kwargs.get("collection_name", "unknown")
# Standard database attributes
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
- scope._span.set_attribute(SemanticConvention.DB_FILTER,
- str(scope._kwargs.get("filter", "")))
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_FILTER, str(scope._kwargs.get("filter", ""))
+ )
# Response metrics
if scope._response and scope._response.get("delete_count"):
- scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
- scope._response["delete_count"])
+ scope._span.set_attribute(
+ SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
+ scope._response["delete_count"],
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
- f"filter={scope._kwargs.get('filter', 'None')}")
+ f"filter={scope._kwargs.get('filter', 'None')}",
+ )
scope._span.set_status(Status(StatusCode.OK))
# Record metrics using helper
if not disable_metrics:
- record_db_metrics(metrics, SemanticConvention.DB_SYSTEM_MILVUS,
- scope._server_address, scope._server_port, environment, application_name,
- scope._start_time, scope._end_time)
-
-def process_milvus_response(response, db_operation, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version, instance, args, **kwargs):
+ record_db_metrics(
+ metrics,
+ SemanticConvention.DB_SYSTEM_MILVUS,
+ scope._server_address,
+ scope._server_port,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ )
+
+
+def process_milvus_response(
+ response,
+ db_operation,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ args,
+ **kwargs,
+):
"""
Process Milvus response and generate telemetry.
@@ -270,7 +361,15 @@ def process_milvus_response(response, db_operation, server_address, server_port,
scope._kwargs = kwargs
# Process the response using common logic
- common_milvus_logic(scope, environment, application_name,
- metrics, capture_message_content, disable_metrics, version, instance)
+ common_milvus_logic(
+ scope,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/mistral/__init__.py b/sdk/python/src/openlit/instrumentation/mistral/__init__.py
index a9f20822..8c23ff33 100644
--- a/sdk/python/src/openlit/instrumentation/mistral/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/mistral/__init__.py
@@ -5,19 +5,16 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.mistral.mistral import (
- complete,
- stream,
- embed
-)
+from openlit.instrumentation.mistral.mistral import complete, stream, embed
from openlit.instrumentation.mistral.async_mistral import (
async_complete,
async_stream,
- async_embed
+ async_embed,
)
_instruments = ("mistralai >= 1.0.0",)
+
class MistralInstrumentor(BaseInstrumentor):
"""
An instrumentor for Mistral client library.
@@ -40,48 +37,96 @@ class MistralInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"mistralai.chat",
"Chat.complete",
- complete(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ complete(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# sync chat streaming
wrap_function_wrapper(
"mistralai.chat",
"Chat.stream",
- stream(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ stream(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# sync embeddings
wrap_function_wrapper(
"mistralai.embeddings",
"Embeddings.create",
- embed(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ embed(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# async chat completions
wrap_function_wrapper(
"mistralai.chat",
"Chat.complete_async",
- async_complete(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_complete(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# async chat streaming
wrap_function_wrapper(
"mistralai.chat",
"Chat.stream_async",
- async_stream(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_stream(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# async embeddings
wrap_function_wrapper(
"mistralai.embeddings",
"Embeddings.create_async",
- async_embed(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_embed(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/mistral/async_mistral.py b/sdk/python/src/openlit/instrumentation/mistral/async_mistral.py
index a441c0a4..586ac422 100644
--- a/sdk/python/src/openlit/instrumentation/mistral/async_mistral.py
+++ b/sdk/python/src/openlit/instrumentation/mistral/async_mistral.py
@@ -16,8 +16,17 @@ from openlit.instrumentation.mistral.utils import (
)
from openlit.semcov import SemanticConvention
-def async_complete(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_complete(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI complete function call
"""
@@ -27,7 +36,9 @@ def async_complete(version, environment, application_name,
Wraps the GenAI complete function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.mistral.ai", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.mistral.ai", 443
+ )
request_model = kwargs.get("model", "mistral-small-latest")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -49,15 +60,24 @@ def async_complete(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
return response
return wrapper
-def async_stream(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_stream(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI stream function call
"""
@@ -68,15 +88,15 @@ def async_stream(version, environment, application_name,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -119,7 +139,9 @@ def async_stream(version, environment, application_name,
return chunk
except StopAsyncIteration:
try:
- with tracer.start_as_current_span(self._span_name, kind= SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -128,7 +150,7 @@ def async_stream(version, environment, application_name,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -141,7 +163,9 @@ def async_stream(version, environment, application_name,
Wraps the GenAI stream function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.mistral.ai", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.mistral.ai", 443
+ )
request_model = kwargs.get("model", "mistral-small-latest")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -150,12 +174,23 @@ def async_stream(version, environment, application_name,
awaited_wrapped = await wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedAsyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedAsyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
return wrapper
-def async_embed(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_embed(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI embedding function call
"""
@@ -165,10 +200,14 @@ def async_embed(version, environment, application_name,
Wraps the GenAI embedding function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.mistral.ai", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.mistral.ai", 443
+ )
request_model = kwargs.get("model", "mistral-embed")
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
@@ -189,7 +228,7 @@ def async_embed(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/mistral/mistral.py b/sdk/python/src/openlit/instrumentation/mistral/mistral.py
index 9acfd206..da8320dc 100644
--- a/sdk/python/src/openlit/instrumentation/mistral/mistral.py
+++ b/sdk/python/src/openlit/instrumentation/mistral/mistral.py
@@ -16,8 +16,17 @@ from openlit.instrumentation.mistral.utils import (
)
from openlit.semcov import SemanticConvention
-def complete(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def complete(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI complete function call
"""
@@ -27,7 +36,9 @@ def complete(version, environment, application_name,
Wraps the GenAI complete function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.mistral.ai", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.mistral.ai", 443
+ )
request_model = kwargs.get("model", "mistral-small-latest")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -49,15 +60,24 @@ def complete(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
return response
return wrapper
-def stream(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def stream(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI stream function call
"""
@@ -68,15 +88,15 @@ def stream(version, environment, application_name,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -119,7 +139,9 @@ def stream(version, environment, application_name,
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(self._span_name, kind= SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -128,7 +150,7 @@ def stream(version, environment, application_name,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -141,7 +163,9 @@ def stream(version, environment, application_name,
Wraps the GenAI stream function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.mistral.ai", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.mistral.ai", 443
+ )
request_model = kwargs.get("model", "mistral-small-latest")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -150,12 +174,23 @@ def stream(version, environment, application_name,
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedSyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
return wrapper
-def embed(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def embed(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI embedding function call
"""
@@ -165,10 +200,14 @@ def embed(version, environment, application_name,
Wraps the GenAI embedding function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.mistral.ai", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.mistral.ai", 443
+ )
request_model = kwargs.get("model", "mistral-embed")
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
@@ -189,7 +228,7 @@ def embed(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/mistral/utils.py b/sdk/python/src/openlit/instrumentation/mistral/utils.py
index 54863d7d..35aa8bc2 100644
--- a/sdk/python/src/openlit/instrumentation/mistral/utils.py
+++ b/sdk/python/src/openlit/instrumentation/mistral/utils.py
@@ -1,6 +1,7 @@
"""
Mistral OpenTelemetry instrumentation utility functions
"""
+
import time
from opentelemetry.trace import Status, StatusCode
@@ -17,6 +18,7 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def format_content(messages):
"""
Process a list of messages to extract content.
@@ -29,8 +31,9 @@ def format_content(messages):
if isinstance(content, list):
content_str = ", ".join(
- f'{item["type"]}: {item["text"] if "text" in item else item.get("image_url", "")}'
- if "type" in item else f'text: {item.get("text", "")}'
+ f"{item['type']}: {item['text'] if 'text' in item else item.get('image_url', '')}"
+ if "type" in item
+ else f"text: {item.get('text', '')}"
for item in content
)
formatted_messages.append(f"{role}: {content_str}")
@@ -39,6 +42,7 @@ def format_content(messages):
return "\n".join(formatted_messages)
+
def process_chunk(scope, chunk):
"""
Process a chunk of response data and update state.
@@ -80,11 +84,18 @@ def process_chunk(scope, chunk):
func = tool.get("function", {})
scope._tools[idx] = {
"id": tool["id"],
- "function": {"name": func.get("name", ""), "arguments": func.get("arguments", "")},
- "type": tool.get("type", "function")
+ "function": {
+ "name": func.get("name", ""),
+ "arguments": func.get("arguments", ""),
+ },
+ "type": tool.get("type", "function"),
}
- elif scope._tools[idx] and "function" in tool: # Append args (id is None)
- scope._tools[idx]["function"]["arguments"] += tool["function"].get("arguments", "")
+ elif (
+ scope._tools[idx] and "function" in tool
+ ): # Append args (id is None)
+ scope._tools[idx]["function"]["arguments"] += tool[
+ "function"
+ ].get("arguments", "")
# Handle usage information (typically only in final chunk)
if data.get("usage"):
@@ -92,11 +103,23 @@ def process_chunk(scope, chunk):
scope._output_tokens = data.get("usage").get("completion_tokens", 0)
scope._response_id = data.get("id")
scope._response_model = data.get("model")
- scope._finish_reason = choices[0].get("finish_reason", "") if choices else ""
+ scope._finish_reason = (
+ choices[0].get("finish_reason", "") if choices else ""
+ )
scope._end_time = time.time()
-def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream):
+
+def common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream,
+):
"""
Process chat request and generate Telemetry
"""
@@ -107,54 +130,117 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
prompt = format_content(scope._kwargs.get("messages", []))
request_model = scope._kwargs.get("model", "mistral-small-latest")
- cost = get_chat_model_cost(request_model, pricing_info, scope._input_tokens, scope._output_tokens)
+ cost = get_chat_model_cost(
+ request_model, pricing_info, scope._input_tokens, scope._output_tokens
+ )
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_MISTRAL,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_MISTRAL,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Request parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED, scope._kwargs.get("seed", ""))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY, scope._kwargs.get("frequency_penalty", 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, scope._kwargs.get("max_tokens", -1))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, scope._kwargs.get("presence_penalty", 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get("stop_sequences", []))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, scope._kwargs.get("temperature", 0.3))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_K, scope._kwargs.get("k", 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("p", 1.0))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_SEED, scope._kwargs.get("seed", "")
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
+ scope._kwargs.get("frequency_penalty", 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
+ scope._kwargs.get("max_tokens", -1),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
+ scope._kwargs.get("presence_penalty", 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES,
+ scope._kwargs.get("stop_sequences", []),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
+ scope._kwargs.get("temperature", 0.3),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_K, scope._kwargs.get("k", 1.0)
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("p", 1.0)
+ )
# Span Attributes for Response parameters
scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ "text" if isinstance(scope._llmresponse, str) else "json",
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens + scope._output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ scope._input_tokens + scope._output_tokens,
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Tools - optimized
if scope._tools:
tools = scope._tools if isinstance(scope._tools, list) else [scope._tools]
- names, ids, args = zip(*[
- (t.get("function", {}).get("name", ""),
- str(t.get("id", "")),
- str(t.get("function", {}).get("arguments", "")))
- for t in tools if isinstance(t, dict) and t
- ]) if tools else ([], [], [])
+ names, ids, args = (
+ zip(
+ *[
+ (
+ t.get("function", {}).get("name", ""),
+ str(t.get("id", "")),
+ str(t.get("function", {}).get("arguments", "")),
+ )
+ for t in tools
+ if isinstance(t, dict) and t
+ ]
+ )
+ if tools
+ else ([], [], [])
+ )
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, ", ".join(filter(None, names)))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, ", ".join(filter(None, ids)))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, ", ".join(filter(None, args)))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_NAME, ", ".join(filter(None, names))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_CALL_ID, ", ".join(filter(None, ids))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_ARGS, ", ".join(filter(None, args))
+ )
# Span Attributes for Content
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -174,23 +260,69 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
# Metrics
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_MISTRAL,
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, scope._output_tokens,
- cost, scope._tbt, scope._ttft)
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_MISTRAL,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ scope._output_tokens,
+ cost,
+ scope._tbt,
+ scope._ttft,
+ )
+
-def process_streaming_chat_response(scope, pricing_info, environment, application_name, metrics,
- capture_message_content=False, disable_metrics=False, version=""):
+def process_streaming_chat_response(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="",
+):
"""
Process streaming chat request and generate Telemetry
"""
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=True)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=True,
+ )
-def process_chat_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+
+def process_chat_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process chat request and generate Telemetry
"""
@@ -214,21 +346,45 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
scope._ttft, scope._tbt = scope._end_time - scope._start_time, 0
scope._server_address, scope._server_port = server_address, server_port
scope._kwargs = kwargs
- scope._finish_reason = str(response_dict.get("choices", [])[0].get("finish_reason", "")) if response_dict.get("choices") else ""
+ scope._finish_reason = (
+ str(response_dict.get("choices", [])[0].get("finish_reason", ""))
+ if response_dict.get("choices")
+ else ""
+ )
# Handle tool calls
if kwargs.get("tools"):
- scope._tools = response_dict.get("choices", [{}])[0].get("message", {}).get("tool_calls")
+ scope._tools = (
+ response_dict.get("choices", [{}])[0].get("message", {}).get("tool_calls")
+ )
else:
scope._tools = None
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=False)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=False,
+ )
return response
-def common_embedding_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version):
+
+def common_embedding_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+):
"""
Process embedding request and generate Telemetry
"""
@@ -239,17 +395,35 @@ def common_embedding_logic(scope, pricing_info, environment, application_name, m
cost = get_embed_model_cost(request_model, pricing_info, scope._input_tokens)
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_MISTRAL,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, False, 0, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
+ SemanticConvention.GEN_AI_SYSTEM_MISTRAL,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ False,
+ 0,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Request parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS, [scope._kwargs.get("encoding_format", "float")])
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS,
+ [scope._kwargs.get("encoding_format", "float")],
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Content
@@ -268,13 +442,39 @@ def common_embedding_logic(scope, pricing_info, environment, application_name, m
# Metrics
if not disable_metrics:
- record_embedding_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_MISTRAL,
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, cost)
+ record_embedding_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
+ SemanticConvention.GEN_AI_SYSTEM_MISTRAL,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ cost,
+ )
-def process_embedding_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+
+def process_embedding_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process embedding request and generate Telemetry
"""
@@ -292,7 +492,15 @@ def process_embedding_response(response, request_model, pricing_info, server_por
scope._server_address, scope._server_port = server_address, server_port
scope._kwargs = kwargs
- common_embedding_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version)
+ common_embedding_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/multion/__init__.py b/sdk/python/src/openlit/instrumentation/multion/__init__.py
index 2487a5f0..9e1c19de 100644
--- a/sdk/python/src/openlit/instrumentation/multion/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/multion/__init__.py
@@ -6,16 +6,13 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.multion.multion import (
- multion_wrap
-)
+from openlit.instrumentation.multion.multion import multion_wrap
-from openlit.instrumentation.multion.async_multion import (
- async_multion_wrap
-)
+from openlit.instrumentation.multion.async_multion import async_multion_wrap
_instruments = ("multion >= 1.3.8",)
+
class MultiOnInstrumentor(BaseInstrumentor):
"""
An instrumentor for multion's client library.
@@ -38,43 +35,96 @@ class MultiOnInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"multion.client",
"MultiOn.browse",
- multion_wrap("multion.browse", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ multion_wrap(
+ "multion.browse",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"multion.client",
"MultiOn.retrieve",
- multion_wrap("multion.retrieve", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ multion_wrap(
+ "multion.retrieve",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"multion.sessions.client",
"SessionsClient.create",
- multion_wrap("multion.sessions.create", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ multion_wrap(
+ "multion.sessions.create",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# Asynchronus
wrap_function_wrapper(
"multion.client",
"AsyncMultiOn.browse",
- async_multion_wrap("multion.browse", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_multion_wrap(
+ "multion.browse",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"multion.client",
"AsyncMultiOn.retrieve",
- async_multion_wrap("multion.retrieve", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_multion_wrap(
+ "multion.retrieve",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"multion.sessions.client",
"AsyncSessionsClient.create",
- async_multion_wrap("multion.sessions.create", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_multion_wrap(
+ "multion.sessions.create",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
-
def _uninstrument(self, **kwargs):
# Proper uninstrumentation logic to revert patched methods
pass
diff --git a/sdk/python/src/openlit/instrumentation/multion/async_multion.py b/sdk/python/src/openlit/instrumentation/multion/async_multion.py
index bdca033b..792c0c80 100644
--- a/sdk/python/src/openlit/instrumentation/multion/async_multion.py
+++ b/sdk/python/src/openlit/instrumentation/multion/async_multion.py
@@ -5,7 +5,11 @@ Module for monitoring multion calls.
import logging
from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from openlit.__helpers import (
handle_exception,
)
@@ -14,8 +18,18 @@ from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def async_multion_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_multion_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for chat completions to collect metrics.
@@ -50,36 +64,47 @@ def async_multion_wrap(gen_ai_endpoint, version, environment, application_name,
"""
# pylint: disable=line-too-long
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(
+ gen_ai_endpoint, kind=SpanKind.CLIENT
+ ) as span:
response = await wrapped(*args, **kwargs)
try:
# Set base span attribues
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_MULTION)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT)
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,
- gen_ai_endpoint)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM,
+ SemanticConvention.GEN_AI_SYSTEM_MULTION,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
+ )
+ span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)
+ span.set_attribute(SERVICE_NAME, application_name)
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
if gen_ai_endpoint == "multion.browse":
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_BROWSE_URL,
- kwargs.get("url", ""))
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_STEP_COUNT,
- response.metadata.step_count)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_RESPONSE_TIME,
- response.metadata.processing_time)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_BROWSE_URL,
+ kwargs.get("url", ""),
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_STEP_COUNT,
+ response.metadata.step_count,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_RESPONSE_TIME,
+ response.metadata.processing_time,
+ )
if capture_message_content:
span.add_event(
name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: kwargs.get("cmd", ""),
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: kwargs.get(
+ "cmd", ""
+ ),
},
)
span.add_event(
@@ -89,14 +114,18 @@ def async_multion_wrap(gen_ai_endpoint, version, environment, application_name,
},
)
elif gen_ai_endpoint == "multion.retrieve":
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_BROWSE_URL,
- kwargs.get("url", ""))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_BROWSE_URL,
+ kwargs.get("url", ""),
+ )
if capture_message_content:
span.add_event(
name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: kwargs.get("cmd", ""),
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: kwargs.get(
+ "cmd", ""
+ ),
},
)
span.add_event(
@@ -107,8 +136,10 @@ def async_multion_wrap(gen_ai_endpoint, version, environment, application_name,
)
elif gen_ai_endpoint == "multion.sessions.create":
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_BROWSE_URL,
- kwargs.get("url", ""))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_BROWSE_URL,
+ kwargs.get("url", ""),
+ )
if capture_message_content:
span.add_event(
diff --git a/sdk/python/src/openlit/instrumentation/multion/multion.py b/sdk/python/src/openlit/instrumentation/multion/multion.py
index 05c9fd27..e5dd9876 100644
--- a/sdk/python/src/openlit/instrumentation/multion/multion.py
+++ b/sdk/python/src/openlit/instrumentation/multion/multion.py
@@ -5,7 +5,11 @@ Module for monitoring multion calls.
import logging
from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from openlit.__helpers import (
handle_exception,
)
@@ -14,8 +18,18 @@ from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def multion_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def multion_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for chat completions to collect metrics.
@@ -50,36 +64,47 @@ def multion_wrap(gen_ai_endpoint, version, environment, application_name,
"""
# pylint: disable=line-too-long
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(
+ gen_ai_endpoint, kind=SpanKind.CLIENT
+ ) as span:
response = wrapped(*args, **kwargs)
try:
# Set base span attribues
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_MULTION)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT)
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,
- gen_ai_endpoint)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM,
+ SemanticConvention.GEN_AI_SYSTEM_MULTION,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
+ )
+ span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)
+ span.set_attribute(SERVICE_NAME, application_name)
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
if gen_ai_endpoint == "multion.browse":
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_BROWSE_URL,
- kwargs.get("url", ""))
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_STEP_COUNT,
- response.metadata.step_count)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_RESPONSE_TIME,
- response.metadata.processing_time)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_BROWSE_URL,
+ kwargs.get("url", ""),
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_STEP_COUNT,
+ response.metadata.step_count,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_RESPONSE_TIME,
+ response.metadata.processing_time,
+ )
if capture_message_content:
span.add_event(
name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: kwargs.get("cmd", ""),
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: kwargs.get(
+ "cmd", ""
+ ),
},
)
span.add_event(
@@ -89,14 +114,18 @@ def multion_wrap(gen_ai_endpoint, version, environment, application_name,
},
)
elif gen_ai_endpoint == "multion.retrieve":
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_BROWSE_URL,
- kwargs.get("url", ""))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_BROWSE_URL,
+ kwargs.get("url", ""),
+ )
if capture_message_content:
span.add_event(
name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: kwargs.get("cmd", ""),
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: kwargs.get(
+ "cmd", ""
+ ),
},
)
span.add_event(
@@ -107,8 +136,10 @@ def multion_wrap(gen_ai_endpoint, version, environment, application_name,
)
elif gen_ai_endpoint == "multion.sessions.create":
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_BROWSE_URL,
- kwargs.get("url", ""))
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_BROWSE_URL,
+ kwargs.get("url", ""),
+ )
if capture_message_content:
span.add_event(
diff --git a/sdk/python/src/openlit/instrumentation/ollama/__init__.py b/sdk/python/src/openlit/instrumentation/ollama/__init__.py
index f9784f0f..876d597b 100644
--- a/sdk/python/src/openlit/instrumentation/ollama/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/ollama/__init__.py
@@ -7,15 +7,12 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.ollama.ollama import (
- chat, embeddings
-)
-from openlit.instrumentation.ollama.async_ollama import (
- async_chat, async_embeddings
-)
+from openlit.instrumentation.ollama.ollama import chat, embeddings
+from openlit.instrumentation.ollama.async_ollama import async_chat, async_embeddings
_instruments = ("ollama >= 0.2.0",)
+
# Dispatch wrapper to route instrumentation to chat or embeddings based on path
def _dispatch(sync_chat_wrap, sync_emb_wrap):
def wrapper(wrapped, instance, args, kwargs):
@@ -26,8 +23,10 @@ def _dispatch(sync_chat_wrap, sync_emb_wrap):
if op == "embeddings":
return sync_emb_wrap(wrapped, instance, args, kwargs)
return wrapped(*args, **kwargs)
+
return wrapper
+
def _dispatch_async(async_chat_wrap, async_emb_wrap):
async def wrapper(wrapped, instance, args, kwargs):
if len(args) > 2 and isinstance(args[2], str):
@@ -37,8 +36,10 @@ def _dispatch_async(async_chat_wrap, async_emb_wrap):
if op == "embeddings":
return await async_emb_wrap(wrapped, instance, args, kwargs)
return await wrapped(*args, **kwargs)
+
return wrapper
+
class OllamaInstrumentor(BaseInstrumentor):
"""
An instrumentor for Ollama client library.
@@ -59,24 +60,44 @@ class OllamaInstrumentor(BaseInstrumentor):
# Build wrapper factories for chat and embeddings
sync_chat_wrap = chat(
- version, environment, application_name,
- tracer, pricing_info,
- capture_message_content, metrics, disable_metrics
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
)
sync_emb_wrap = embeddings(
- version, environment, application_name,
- tracer, pricing_info,
- capture_message_content, metrics, disable_metrics
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
)
async_chat_wrap = async_chat(
- version, environment, application_name,
- tracer, pricing_info,
- capture_message_content, metrics, disable_metrics
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
)
async_emb_wrap = async_embeddings(
- version, environment, application_name,
- tracer, pricing_info,
- capture_message_content, metrics, disable_metrics
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
)
# Patch underlying request methods to ensure instrumentation regardless of import order
diff --git a/sdk/python/src/openlit/instrumentation/ollama/async_ollama.py b/sdk/python/src/openlit/instrumentation/ollama/async_ollama.py
index 5ce7de9b..c017ff91 100644
--- a/sdk/python/src/openlit/instrumentation/ollama/async_ollama.py
+++ b/sdk/python/src/openlit/instrumentation/ollama/async_ollama.py
@@ -4,20 +4,26 @@ Module for monitoring Ollama API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.ollama.utils import (
process_chunk,
process_chat_response,
process_streaming_chat_response,
- process_embedding_response
+ process_embedding_response,
)
from openlit.semcov import SemanticConvention
-def async_chat(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for Ollama async chat function call
"""
@@ -28,15 +34,15 @@ def async_chat(version, environment, application_name,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._llmresponse = ""
@@ -78,7 +84,9 @@ def async_chat(version, environment, application_name,
return chunk
except StopAsyncIteration:
try:
- with tracer.start_as_current_span(self._span_name, kind=SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -87,7 +95,7 @@ def async_chat(version, environment, application_name,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
handle_exception(self._span, e)
@@ -101,7 +109,9 @@ def async_chat(version, environment, application_name,
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "127.0.0.1", 11434)
+ server_address, server_port = set_server_address_and_port(
+ instance, "127.0.0.1", 11434
+ )
json_body = kwargs.get("json", {}) or {}
request_model = json_body.get("model") or kwargs.get("model")
@@ -110,7 +120,15 @@ def async_chat(version, environment, application_name,
if streaming:
awaited_wrapped = await wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedAsyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port, args)
+ return TracedAsyncStream(
+ awaited_wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ args,
+ )
else:
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -133,7 +151,7 @@ def async_chat(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -143,8 +161,17 @@ def async_chat(version, environment, application_name,
return wrapper
-def async_embeddings(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_embeddings(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for Ollama async embeddings function call
"""
@@ -154,10 +181,14 @@ def async_embeddings(version, environment, application_name,
Wraps the Ollama async embeddings function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "127.0.0.1", 11434)
+ server_address, server_port = set_server_address_and_port(
+ instance, "127.0.0.1", 11434
+ )
request_model = kwargs.get("model")
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.monotonic()
@@ -179,7 +210,7 @@ def async_embeddings(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/ollama/ollama.py b/sdk/python/src/openlit/instrumentation/ollama/ollama.py
index 0b1a9ebe..ce4016ab 100644
--- a/sdk/python/src/openlit/instrumentation/ollama/ollama.py
+++ b/sdk/python/src/openlit/instrumentation/ollama/ollama.py
@@ -4,20 +4,26 @@ Module for monitoring Ollama API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.ollama.utils import (
process_chunk,
process_chat_response,
process_streaming_chat_response,
- process_embedding_response
+ process_embedding_response,
)
from openlit.semcov import SemanticConvention
-def chat(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for Ollama chat function call
"""
@@ -28,15 +34,15 @@ def chat(version, environment, application_name,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._llmresponse = ""
@@ -78,7 +84,9 @@ def chat(version, environment, application_name,
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(self._span_name, kind=SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -87,7 +95,7 @@ def chat(version, environment, application_name,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
handle_exception(self._span, e)
@@ -101,7 +109,9 @@ def chat(version, environment, application_name,
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "127.0.0.1", 11434)
+ server_address, server_port = set_server_address_and_port(
+ instance, "127.0.0.1", 11434
+ )
json_body = kwargs.get("json", {}) or {}
request_model = json_body.get("model") or kwargs.get("model")
@@ -110,7 +120,15 @@ def chat(version, environment, application_name,
if streaming:
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port, args)
+ return TracedSyncStream(
+ awaited_wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ args,
+ )
else:
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -133,7 +151,7 @@ def chat(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -143,8 +161,17 @@ def chat(version, environment, application_name,
return wrapper
-def embeddings(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def embeddings(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for Ollama embeddings function call
"""
@@ -154,10 +181,14 @@ def embeddings(version, environment, application_name,
Wraps the Ollama embeddings function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "127.0.0.1", 11434)
+ server_address, server_port = set_server_address_and_port(
+ instance, "127.0.0.1", 11434
+ )
request_model = kwargs.get("model")
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.monotonic()
@@ -179,7 +210,7 @@ def embeddings(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/ollama/utils.py b/sdk/python/src/openlit/instrumentation/ollama/utils.py
index 7bf2dd16..8f3c0e77 100644
--- a/sdk/python/src/openlit/instrumentation/ollama/utils.py
+++ b/sdk/python/src/openlit/instrumentation/ollama/utils.py
@@ -1,6 +1,7 @@
"""
Ollama OpenTelemetry instrumentation utility functions
"""
+
import time
from opentelemetry.trace import Status, StatusCode
@@ -18,6 +19,7 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def format_content(messages):
"""
Process a list of messages to extract content.
@@ -30,8 +32,9 @@ def format_content(messages):
if isinstance(content, list):
content_str = ", ".join(
- f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
- if "type" in item else f'text: {item["text"]}'
+ f"{item['type']}: {item['text'] if 'text' in item else item['image_url']}"
+ if "type" in item
+ else f"text: {item['text']}"
for item in content
)
formatted_messages.append(f"{role}: {content_str}")
@@ -40,6 +43,7 @@ def format_content(messages):
return "\n".join(formatted_messages)
+
def process_chunk(self, chunk):
"""
Process a chunk of response data and update state.
@@ -66,8 +70,22 @@ def process_chunk(self, chunk):
self._response_model = chunked.get("model", "")
self._finish_reason = chunked.get("done_reason", "")
-def record_embedding_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
- request_model, response_model, environment, application_name, start_time, end_time, cost, input_tokens):
+
+def record_embedding_metrics(
+ metrics,
+ gen_ai_operation,
+ gen_ai_system,
+ server_address,
+ server_port,
+ request_model,
+ response_model,
+ environment,
+ application_name,
+ start_time,
+ end_time,
+ cost,
+ input_tokens,
+):
"""
Record embedding metrics for the operation.
"""
@@ -88,8 +106,18 @@ def record_embedding_metrics(metrics, gen_ai_operation, gen_ai_system, server_ad
metrics["genai_prompt_tokens"].add(input_tokens, attributes)
metrics["genai_cost"].record(cost, attributes)
-def common_chat_logic(scope, gen_ai_endpoint, pricing_info, environment, application_name,
- metrics, capture_message_content, disable_metrics, version):
+
+def common_chat_logic(
+ scope,
+ gen_ai_endpoint,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+):
"""
Process chat request and generate Telemetry
"""
@@ -103,13 +131,26 @@ def common_chat_logic(scope, gen_ai_endpoint, pricing_info, environment, applica
request_model = json_body.get("model") or scope._kwargs.get("model", "llama3.2")
is_stream = scope._kwargs.get("stream", False)
- cost = get_chat_model_cost(request_model, pricing_info, scope._input_tokens, scope._output_tokens)
+ cost = get_chat_model_cost(
+ request_model, pricing_info, scope._input_tokens, scope._output_tokens
+ )
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_OLLAMA,
- scope._server_address, scope._server_port, request_model, request_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_OLLAMA,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Request parameters
options = json_body.get("options", scope._kwargs.get("options", {}))
@@ -128,25 +169,46 @@ def common_chat_logic(scope, gen_ai_endpoint, pricing_info, environment, applica
scope._span.set_attribute(attribute, value)
# Span Attributes for Response parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,"text" if isinstance(scope._llmresponse, str) else "json")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ "text" if isinstance(scope._llmresponse, str) else "json",
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens + scope._output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ scope._input_tokens + scope._output_tokens,
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Tools
if scope._tools is not None:
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, scope._tools.get("function","")).get("name","")
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get("id","")))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, str(scope._tools.get("function","").get("arguments","")))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_NAME, scope._tools.get("function", "")
+ ).get("name", "")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get("id", ""))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_ARGS,
+ str(scope._tools.get("function", "").get("arguments", "")),
+ )
# Span Attributes for Content
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -166,13 +228,37 @@ def common_chat_logic(scope, gen_ai_endpoint, pricing_info, environment, applica
# Metrics
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
- SemanticConvention.GEN_AI_SYSTEM_OLLAMA, scope._server_address, scope._server_port,
- request_model, scope._response_model, environment, application_name, scope._start_time,
- scope._end_time, cost, scope._input_tokens, scope._output_tokens, scope._tbt, scope._ttft)
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_OLLAMA,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ cost,
+ scope._input_tokens,
+ scope._output_tokens,
+ scope._tbt,
+ scope._ttft,
+ )
-def common_embedding_logic(scope, gen_ai_endpoint, pricing_info, environment, application_name,
- metrics, capture_message_content, disable_metrics, version):
+
+def common_embedding_logic(
+ scope,
+ gen_ai_endpoint,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+):
"""
Process embedding request and generate Telemetry
"""
@@ -186,14 +272,29 @@ def common_embedding_logic(scope, gen_ai_endpoint, pricing_info, environment, ap
cost = get_embed_model_cost(request_model, pricing_info, input_tokens)
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_OLLAMA,
- scope._server_address, scope._server_port, request_model, request_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
+ SemanticConvention.GEN_AI_SYSTEM_OLLAMA,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Embedding-specific parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens
+ )
# Span Attributes for Cost
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
@@ -206,23 +307,66 @@ def common_embedding_logic(scope, gen_ai_endpoint, pricing_info, environment, ap
# Metrics
if not disable_metrics:
- record_embedding_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
- SemanticConvention.GEN_AI_SYSTEM_OLLAMA, scope._server_address, scope._server_port,
- request_model, request_model, environment, application_name, scope._start_time,
- scope._end_time, cost, input_tokens)
+ record_embedding_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
+ SemanticConvention.GEN_AI_SYSTEM_OLLAMA,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ cost,
+ input_tokens,
+ )
-def process_streaming_chat_response(self, pricing_info, environment, application_name, metrics,
- capture_message_content=False, disable_metrics=False, version=""):
+
+def process_streaming_chat_response(
+ self,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="",
+):
"""
Process streaming chat request and generate Telemetry
"""
- common_chat_logic(self, "ollama.chat", pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version)
+ common_chat_logic(
+ self,
+ "ollama.chat",
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
+
-def process_chat_response(response, gen_ai_endpoint, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+def process_chat_response(
+ response,
+ gen_ai_endpoint,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process chat request and generate Telemetry
"""
@@ -250,14 +394,37 @@ def process_chat_response(response, gen_ai_endpoint, pricing_info, server_port,
else:
scope._tools = None
- common_chat_logic(scope, gen_ai_endpoint, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version)
+ common_chat_logic(
+ scope,
+ gen_ai_endpoint,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
return response
-def process_embedding_response(response, gen_ai_endpoint, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+
+def process_embedding_response(
+ response,
+ gen_ai_endpoint,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process embedding request and generate Telemetry
"""
@@ -275,7 +442,16 @@ def process_embedding_response(response, gen_ai_endpoint, pricing_info, server_p
scope._tbt = 0.0
scope._ttft = scope._end_time - scope._start_time
- common_embedding_logic(scope, gen_ai_endpoint, pricing_info, environment, application_name,
- metrics, capture_message_content, disable_metrics, version)
+ common_embedding_logic(
+ scope,
+ gen_ai_endpoint,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/openai/__init__.py b/sdk/python/src/openlit/instrumentation/openai/__init__.py
index 4370df24..0fa40600 100644
--- a/sdk/python/src/openlit/instrumentation/openai/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/openai/__init__.py
@@ -1,20 +1,32 @@
"""Initializer of Auto Instrumentation of OpenAI Functions"""
+
from typing import Collection
import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
from openlit.instrumentation.openai.openai import (
- chat_completions, embedding, responses, chat_completions_parse,
- image_generate, image_variatons, audio_create
+ chat_completions,
+ embedding,
+ responses,
+ chat_completions_parse,
+ image_generate,
+ image_variatons,
+ audio_create,
)
from openlit.instrumentation.openai.async_openai import (
- async_chat_completions, async_embedding, async_chat_completions_parse,
- async_image_generate, async_image_variations, async_audio_create, async_responses
+ async_chat_completions,
+ async_embedding,
+ async_chat_completions_parse,
+ async_image_generate,
+ async_image_variations,
+ async_audio_create,
+ async_responses,
)
_instruments = ("openai >= 1.92.0",)
+
class OpenAIInstrumentor(BaseInstrumentor):
"""
An instrumentor for OpenAI client library.
@@ -37,105 +49,217 @@ class OpenAIInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"openai.resources.chat.completions",
"Completions.create",
- chat_completions(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ chat_completions(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"openai.resources.chat.completions",
"AsyncCompletions.create",
- async_chat_completions(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_chat_completions(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# chat completions parse
wrap_function_wrapper(
"openai.resources.chat.completions",
"Completions.parse",
- chat_completions_parse(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ chat_completions_parse(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"openai.resources.chat.completions",
"AsyncCompletions.parse",
- async_chat_completions_parse(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_chat_completions_parse(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# responses
wrap_function_wrapper(
"openai.resources.responses.responses",
"Responses.create",
- responses(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ responses(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"openai.resources.responses.responses",
"AsyncResponses.create",
- async_responses(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_responses(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# embeddings
wrap_function_wrapper(
"openai.resources.embeddings",
"Embeddings.create",
- embedding(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ embedding(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"openai.resources.embeddings",
"AsyncEmbeddings.create",
- async_embedding(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_embedding(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# image generation
wrap_function_wrapper(
"openai.resources.images",
"Images.generate",
- image_generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ image_generate(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"openai.resources.images",
"AsyncImages.generate",
- async_image_generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_image_generate(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# image variations
wrap_function_wrapper(
"openai.resources.images",
"Images.create_variation",
- image_variatons(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ image_variatons(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"openai.resources.images",
"AsyncImages.create_variation",
- async_image_variations(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_image_variations(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# audio generation
wrap_function_wrapper(
"openai.resources.audio.speech",
"Speech.create",
- audio_create(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ audio_create(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"openai.resources.audio.speech",
"AsyncSpeech.create",
- async_audio_create(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_audio_create(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/openai/async_openai.py b/sdk/python/src/openlit/instrumentation/openai/async_openai.py
index 7e07fdfe..677458ca 100644
--- a/sdk/python/src/openlit/instrumentation/openai/async_openai.py
+++ b/sdk/python/src/openlit/instrumentation/openai/async_openai.py
@@ -4,10 +4,7 @@ Module for monitoring OpenAI API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.openai.utils import (
process_chat_chunk,
process_response_chunk,
@@ -21,8 +18,17 @@ from openlit.instrumentation.openai.utils import (
)
from openlit.semcov import SemanticConvention
-def async_chat_completions(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def async_chat_completions(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for OpenAI async chat completions.
"""
@@ -33,14 +39,14 @@ def async_chat_completions(version, environment, application_name, tracer, prici
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -90,7 +96,7 @@ def async_chat_completions(version, environment, application_name, tracer, prici
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
handle_exception(self._span, e)
@@ -102,7 +108,9 @@ def async_chat_completions(version, environment, application_name, tracer, prici
"""
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
request_model = kwargs.get("model", "gpt-4o")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -111,7 +119,9 @@ def async_chat_completions(version, environment, application_name, tracer, prici
awaited_wrapped = await wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedAsyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedAsyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
else:
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -121,9 +131,9 @@ def async_chat_completions(version, environment, application_name, tracer, prici
try:
response = process_chat_response(
response=response,
- request_model=request_model,
+ request_model=request_model,
pricing_info=pricing_info,
- server_port=server_port,
+ server_port=server_port,
server_address=server_address,
environment=environment,
application_name=application_name,
@@ -133,7 +143,7 @@ def async_chat_completions(version, environment, application_name, tracer, prici
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -143,8 +153,17 @@ def async_chat_completions(version, environment, application_name, tracer, prici
return wrapper
-def async_responses(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def async_responses(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for OpenAI async responses API.
"""
@@ -155,14 +174,14 @@ def async_responses(version, environment, application_name, tracer, pricing_info
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -216,7 +235,7 @@ def async_responses(version, environment, application_name, tracer, pricing_info
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
handle_exception(self._span, e)
@@ -228,7 +247,9 @@ def async_responses(version, environment, application_name, tracer, pricing_info
"""
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
request_model = kwargs.get("model", "gpt-4o")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -237,7 +258,9 @@ def async_responses(version, environment, application_name, tracer, pricing_info
awaited_wrapped = await wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedAsyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedAsyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
else:
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -247,9 +270,9 @@ def async_responses(version, environment, application_name, tracer, pricing_info
try:
response = process_response_response(
response=response,
- request_model=request_model,
+ request_model=request_model,
pricing_info=pricing_info,
- server_port=server_port,
+ server_port=server_port,
server_address=server_address,
environment=environment,
application_name=application_name,
@@ -259,7 +282,7 @@ def async_responses(version, environment, application_name, tracer, pricing_info
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -269,8 +292,17 @@ def async_responses(version, environment, application_name, tracer, pricing_info
return wrapper
-def async_chat_completions_parse(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def async_chat_completions_parse(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for OpenAI async chat completions parse.
"""
@@ -280,7 +312,9 @@ def async_chat_completions_parse(version, environment, application_name, tracer,
Wraps the OpenAI async chat completions parse call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
request_model = kwargs.get("model", "gpt-4o")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -292,9 +326,9 @@ def async_chat_completions_parse(version, environment, application_name, tracer,
try:
response = process_chat_response(
response=response,
- request_model=request_model,
+ request_model=request_model,
pricing_info=pricing_info,
- server_port=server_port,
+ server_port=server_port,
server_address=server_address,
environment=environment,
application_name=application_name,
@@ -304,7 +338,7 @@ def async_chat_completions_parse(version, environment, application_name, tracer,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -314,8 +348,17 @@ def async_chat_completions_parse(version, environment, application_name, tracer,
return wrapper
-def async_embedding(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def async_embedding(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for OpenAI async embeddings.
"""
@@ -325,10 +368,14 @@ def async_embedding(version, environment, application_name, tracer, pricing_info
Wraps the OpenAI async embeddings call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
request_model = kwargs.get("model", "text-embedding-ada-002")
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
@@ -337,9 +384,9 @@ def async_embedding(version, environment, application_name, tracer, pricing_info
try:
response = process_embedding_response(
response=response,
- request_model=request_model,
+ request_model=request_model,
pricing_info=pricing_info,
- server_port=server_port,
+ server_port=server_port,
server_address=server_address,
environment=environment,
application_name=application_name,
@@ -349,7 +396,7 @@ def async_embedding(version, environment, application_name, tracer, pricing_info
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -359,8 +406,17 @@ def async_embedding(version, environment, application_name, tracer, pricing_info
return wrapper
-def async_image_generate(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def async_image_generate(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for OpenAI async image generation.
"""
@@ -370,7 +426,9 @@ def async_image_generate(version, environment, application_name, tracer, pricing
Wraps the OpenAI async image generation call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
request_model = kwargs.get("model", "dall-e-2")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE} {request_model}"
@@ -383,9 +441,9 @@ def async_image_generate(version, environment, application_name, tracer, pricing
try:
response = process_image_response(
response=response,
- request_model=request_model,
+ request_model=request_model,
pricing_info=pricing_info,
- server_port=server_port,
+ server_port=server_port,
server_address=server_address,
environment=environment,
application_name=application_name,
@@ -396,7 +454,7 @@ def async_image_generate(version, environment, application_name, tracer, pricing
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -406,8 +464,17 @@ def async_image_generate(version, environment, application_name, tracer, pricing
return wrapper
-def async_image_variations(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def async_image_variations(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for OpenAI async image variations.
"""
@@ -417,7 +484,9 @@ def async_image_variations(version, environment, application_name, tracer, prici
Wraps the OpenAI async image variations call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
request_model = kwargs.get("model", "dall-e-2")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE} {request_model}"
@@ -430,9 +499,9 @@ def async_image_variations(version, environment, application_name, tracer, prici
try:
response = process_image_response(
response=response,
- request_model=request_model,
+ request_model=request_model,
pricing_info=pricing_info,
- server_port=server_port,
+ server_port=server_port,
server_address=server_address,
environment=environment,
application_name=application_name,
@@ -443,7 +512,7 @@ def async_image_variations(version, environment, application_name, tracer, prici
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -453,8 +522,17 @@ def async_image_variations(version, environment, application_name, tracer, prici
return wrapper
-def async_audio_create(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def async_audio_create(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for OpenAI async audio creation.
"""
@@ -464,7 +542,9 @@ def async_audio_create(version, environment, application_name, tracer, pricing_i
Wraps the OpenAI async audio creation call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
request_model = kwargs.get("model", "tts-1")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO} {request_model}"
@@ -477,9 +557,9 @@ def async_audio_create(version, environment, application_name, tracer, pricing_i
try:
response = process_audio_response(
response=response,
- request_model=request_model,
+ request_model=request_model,
pricing_info=pricing_info,
- server_port=server_port,
+ server_port=server_port,
server_address=server_address,
environment=environment,
application_name=application_name,
@@ -490,7 +570,7 @@ def async_audio_create(version, environment, application_name, tracer, pricing_i
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/openai/openai.py b/sdk/python/src/openlit/instrumentation/openai/openai.py
index 7bfa9c63..25fa4634 100644
--- a/sdk/python/src/openlit/instrumentation/openai/openai.py
+++ b/sdk/python/src/openlit/instrumentation/openai/openai.py
@@ -4,10 +4,7 @@ Module for monitoring OpenAI API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.openai.utils import (
process_chat_chunk,
process_response_chunk,
@@ -21,8 +18,17 @@ from openlit.instrumentation.openai.utils import (
)
from openlit.semcov import SemanticConvention
-def chat_completions(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def chat_completions(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for OpenAI chat completions.
"""
@@ -33,14 +39,14 @@ def chat_completions(version, environment, application_name, tracer, pricing_inf
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -90,7 +96,7 @@ def chat_completions(version, environment, application_name, tracer, pricing_inf
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
handle_exception(self._span, e)
@@ -102,7 +108,9 @@ def chat_completions(version, environment, application_name, tracer, pricing_inf
"""
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
request_model = kwargs.get("model", "gpt-4o")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -111,7 +119,9 @@ def chat_completions(version, environment, application_name, tracer, pricing_inf
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedSyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
else:
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -121,9 +131,9 @@ def chat_completions(version, environment, application_name, tracer, pricing_inf
try:
response = process_chat_response(
response=response,
- request_model=request_model,
+ request_model=request_model,
pricing_info=pricing_info,
- server_port=server_port,
+ server_port=server_port,
server_address=server_address,
environment=environment,
application_name=application_name,
@@ -133,7 +143,7 @@ def chat_completions(version, environment, application_name, tracer, pricing_inf
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -143,8 +153,18 @@ def chat_completions(version, environment, application_name, tracer, pricing_inf
return wrapper
-def responses(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics, **kwargs):
+
+def responses(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ **kwargs,
+):
"""
Generates a telemetry wrapper for OpenAI responses API.
"""
@@ -155,14 +175,14 @@ def responses(version, environment, application_name, tracer, pricing_info,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -216,7 +236,7 @@ def responses(version, environment, application_name, tracer, pricing_info,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
handle_exception(self._span, e)
@@ -228,7 +248,9 @@ def responses(version, environment, application_name, tracer, pricing_info,
"""
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
request_model = kwargs.get("model", "gpt-4o")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -237,7 +259,9 @@ def responses(version, environment, application_name, tracer, pricing_info,
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedSyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
else:
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -247,9 +271,9 @@ def responses(version, environment, application_name, tracer, pricing_info,
try:
response = process_response_response(
response=response,
- request_model=request_model,
+ request_model=request_model,
pricing_info=pricing_info,
- server_port=server_port,
+ server_port=server_port,
server_address=server_address,
environment=environment,
application_name=application_name,
@@ -259,7 +283,7 @@ def responses(version, environment, application_name, tracer, pricing_info,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -269,8 +293,17 @@ def responses(version, environment, application_name, tracer, pricing_info,
return wrapper
-def chat_completions_parse(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def chat_completions_parse(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for OpenAI chat completions parse.
"""
@@ -280,7 +313,9 @@ def chat_completions_parse(version, environment, application_name, tracer, prici
Wraps the OpenAI chat completions parse call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
request_model = kwargs.get("model", "gpt-4o")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -292,9 +327,9 @@ def chat_completions_parse(version, environment, application_name, tracer, prici
try:
response = process_chat_response(
response=response,
- request_model=request_model,
+ request_model=request_model,
pricing_info=pricing_info,
- server_port=server_port,
+ server_port=server_port,
server_address=server_address,
environment=environment,
application_name=application_name,
@@ -304,7 +339,7 @@ def chat_completions_parse(version, environment, application_name, tracer, prici
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -314,8 +349,18 @@ def chat_completions_parse(version, environment, application_name, tracer, prici
return wrapper
-def embedding(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics, **kwargs):
+
+def embedding(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ **kwargs,
+):
"""
Generates a telemetry wrapper for OpenAI embeddings.
"""
@@ -325,10 +370,14 @@ def embedding(version, environment, application_name, tracer, pricing_info,
Wraps the OpenAI embeddings call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
request_model = kwargs.get("model", "text-embedding-ada-002")
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
@@ -337,9 +386,9 @@ def embedding(version, environment, application_name, tracer, pricing_info,
try:
response = process_embedding_response(
response=response,
- request_model=request_model,
+ request_model=request_model,
pricing_info=pricing_info,
- server_port=server_port,
+ server_port=server_port,
server_address=server_address,
environment=environment,
application_name=application_name,
@@ -349,7 +398,7 @@ def embedding(version, environment, application_name, tracer, pricing_info,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -359,8 +408,18 @@ def embedding(version, environment, application_name, tracer, pricing_info,
return wrapper
-def image_generate(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics, **kwargs):
+
+def image_generate(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ **kwargs,
+):
"""
Generates a telemetry wrapper for OpenAI image generation.
"""
@@ -370,7 +429,9 @@ def image_generate(version, environment, application_name, tracer, pricing_info,
Wraps the OpenAI image generation call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
request_model = kwargs.get("model", "dall-e-2")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE} {request_model}"
@@ -383,9 +444,9 @@ def image_generate(version, environment, application_name, tracer, pricing_info,
try:
response = process_image_response(
response=response,
- request_model=request_model,
+ request_model=request_model,
pricing_info=pricing_info,
- server_port=server_port,
+ server_port=server_port,
server_address=server_address,
environment=environment,
application_name=application_name,
@@ -396,7 +457,7 @@ def image_generate(version, environment, application_name, tracer, pricing_info,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -406,8 +467,17 @@ def image_generate(version, environment, application_name, tracer, pricing_info,
return wrapper
-def image_variatons(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def image_variatons(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for OpenAI image variations.
"""
@@ -417,7 +487,9 @@ def image_variatons(version, environment, application_name, tracer, pricing_info
Wraps the OpenAI image variations call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
request_model = kwargs.get("model", "dall-e-2")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE} {request_model}"
@@ -430,9 +502,9 @@ def image_variatons(version, environment, application_name, tracer, pricing_info
try:
response = process_image_response(
response=response,
- request_model=request_model,
+ request_model=request_model,
pricing_info=pricing_info,
- server_port=server_port,
+ server_port=server_port,
server_address=server_address,
environment=environment,
application_name=application_name,
@@ -443,7 +515,7 @@ def image_variatons(version, environment, application_name, tracer, pricing_info
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -453,8 +525,17 @@ def image_variatons(version, environment, application_name, tracer, pricing_info
return wrapper
-def audio_create(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def audio_create(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for OpenAI audio creation.
"""
@@ -464,7 +545,9 @@ def audio_create(version, environment, application_name, tracer, pricing_info,
Wraps the OpenAI audio creation call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.openai.com", 443
+ )
request_model = kwargs.get("model", "tts-1")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO} {request_model}"
@@ -477,9 +560,9 @@ def audio_create(version, environment, application_name, tracer, pricing_info,
try:
response = process_audio_response(
response=response,
- request_model=request_model,
+ request_model=request_model,
pricing_info=pricing_info,
- server_port=server_port,
+ server_port=server_port,
server_address=server_address,
environment=environment,
application_name=application_name,
@@ -490,7 +573,7 @@ def audio_create(version, environment, application_name, tracer, pricing_info,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/openai/utils.py b/sdk/python/src/openlit/instrumentation/openai/utils.py
index ca0dd960..940de6ec 100644
--- a/sdk/python/src/openlit/instrumentation/openai/utils.py
+++ b/sdk/python/src/openlit/instrumentation/openai/utils.py
@@ -1,6 +1,7 @@
"""
OpenAI OpenTelemetry instrumentation utility functions
"""
+
import time
from opentelemetry.trace import Status, StatusCode
@@ -22,16 +23,18 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def handle_not_given(value, default=None):
"""
Handle OpenAI's NotGiven values and None values by converting them to appropriate defaults.
"""
- if hasattr(value, '__class__') and value.__class__.__name__ == 'NotGiven':
+ if hasattr(value, "__class__") and value.__class__.__name__ == "NotGiven":
return default
if value is None:
return default
return value
+
def format_content(messages):
"""
Format the messages into a string for span events.
@@ -62,18 +65,19 @@ def format_content(messages):
for item in content:
# Chat completions format
if item.get("type") == "text":
- content_str_list.append(f'text: {item.get("text", "")}')
- elif (item.get("type") == "image_url" and
- not item.get("image_url", {}).get("url", "").startswith("data:")):
- content_str_list.append(f'image_url: {item["image_url"]["url"]}')
+ content_str_list.append(f"text: {item.get('text', '')}")
+ elif item.get("type") == "image_url" and not item.get(
+ "image_url", {}
+ ).get("url", "").startswith("data:"):
+ content_str_list.append(f"image_url: {item['image_url']['url']}")
# Responses API format
elif item.get("type") == "input_text":
- content_str_list.append(f'text: {item.get("text", "")}')
+ content_str_list.append(f"text: {item.get('text', '')}")
elif item.get("type") == "input_image":
image_url = item.get("image_url", "")
if image_url and not image_url.startswith("data:"):
- content_str_list.append(f'image_url: {image_url}')
+ content_str_list.append(f"image_url: {image_url}")
content_str = ", ".join(content_str_list)
formatted_messages.append(f"{role}: {content_str}")
@@ -82,6 +86,7 @@ def format_content(messages):
return "\n".join(formatted_messages)
+
def process_chat_chunk(scope, chunk):
"""
Process a chunk of chat response data and update state.
@@ -96,9 +101,7 @@ def process_chat_chunk(scope, chunk):
chunked = response_as_dict(chunk)
# Extract content from chat completions
- if (len(chunked.get("choices", [])) > 0 and
- "delta" in chunked.get("choices")[0]):
-
+ if len(chunked.get("choices", [])) > 0 and "delta" in chunked.get("choices")[0]:
delta = chunked.get("choices")[0]["delta"]
content = delta.get("content")
if content:
@@ -119,24 +122,36 @@ def process_chat_chunk(scope, chunk):
func = tool.get("function", {})
scope._tools[idx] = {
"id": tool["id"],
- "function": {"name": func.get("name", ""), "arguments": func.get("arguments", "")},
- "type": tool.get("type", "function")
+ "function": {
+ "name": func.get("name", ""),
+ "arguments": func.get("arguments", ""),
+ },
+ "type": tool.get("type", "function"),
}
- elif scope._tools[idx] and "function" in tool: # Append args (id is None)
- scope._tools[idx]["function"]["arguments"] += tool["function"].get("arguments", "")
+ elif (
+ scope._tools[idx] and "function" in tool
+ ): # Append args (id is None)
+ scope._tools[idx]["function"]["arguments"] += tool["function"].get(
+ "arguments", ""
+ )
# Extract metadata
scope._response_id = chunked.get("id") or scope._response_id
scope._response_model = chunked.get("model") or scope._response_model
try:
- scope._finish_reason = chunked.get("choices", [])[0].get("finish_reason") or scope._finish_reason
+ scope._finish_reason = (
+ chunked.get("choices", [])[0].get("finish_reason") or scope._finish_reason
+ )
except (IndexError, AttributeError, TypeError):
scope._finish_reason = "stop"
- scope._system_fingerprint = chunked.get("system_fingerprint") or scope._system_fingerprint
+ scope._system_fingerprint = (
+ chunked.get("system_fingerprint") or scope._system_fingerprint
+ )
scope._service_tier = chunked.get("service_tier") or scope._service_tier
+
def process_response_chunk(scope, chunk):
"""
Process a chunk of response API data and update state.
@@ -162,14 +177,16 @@ def process_response_chunk(scope, chunk):
item = chunked.get("item", {})
if item.get("type") == "function_call":
- scope._response_tools.append({
- "id": item.get("id", ""),
- "call_id": item.get("call_id", ""),
- "name": item.get("name", ""),
- "type": item.get("type", "function_call"),
- "arguments": item.get("arguments", ""),
- "status": item.get("status", "in_progress")
- })
+ scope._response_tools.append(
+ {
+ "id": item.get("id", ""),
+ "call_id": item.get("call_id", ""),
+ "name": item.get("name", ""),
+ "type": item.get("type", "function_call"),
+ "arguments": item.get("arguments", ""),
+ "status": item.get("status", "in_progress"),
+ }
+ )
elif chunked.get("type") == "response.function_call_arguments.delta":
# Tool arguments being streamed
@@ -204,12 +221,16 @@ def process_response_chunk(scope, chunk):
# Update the tool with final status and data
for tool in scope._response_tools:
if tool.get("id") == item_id:
- tool.update({
- "call_id": item.get("call_id", tool.get("call_id", "")),
- "name": item.get("name", tool.get("name", "")),
- "arguments": item.get("arguments", tool.get("arguments", "")),
- "status": item.get("status", "completed")
- })
+ tool.update(
+ {
+ "call_id": item.get("call_id", tool.get("call_id", "")),
+ "name": item.get("name", tool.get("name", "")),
+ "arguments": item.get(
+ "arguments", tool.get("arguments", "")
+ ),
+ "status": item.get("status", "completed"),
+ }
+ )
break
elif chunked.get("type") == "response.completed":
@@ -226,8 +247,18 @@ def process_response_chunk(scope, chunk):
output_tokens_details = usage.get("output_tokens_details", {})
scope._reasoning_tokens = output_tokens_details.get("reasoning_tokens", 0)
-def common_response_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream):
+
+def common_response_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream,
+):
"""
Process responses API request and generate Telemetry
"""
@@ -252,66 +283,115 @@ def common_response_logic(scope, pricing_info, environment, application_name, me
cost = get_chat_model_cost(request_model, pricing_info, input_tokens, output_tokens)
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_OPENAI,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Request parameters specific to responses API
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, handle_not_given(scope._kwargs.get("temperature"), 1.0))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
+ handle_not_given(scope._kwargs.get("temperature"), 1.0),
+ )
scope._span.set_attribute(
SemanticConvention.GEN_AI_REQUEST_TOP_P,
- handle_not_given(scope._kwargs.get("top_p"), 1.0)
+ handle_not_given(scope._kwargs.get("top_p"), 1.0),
)
scope._span.set_attribute(
SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
- handle_not_given(scope._kwargs.get("max_output_tokens"), -1)
+ handle_not_given(scope._kwargs.get("max_output_tokens"), -1),
)
# Reasoning parameters
reasoning = scope._kwargs.get("reasoning", {})
if reasoning:
if reasoning.get("effort"):
- scope._span.set_attribute("gen_ai.request.reasoning_effort", reasoning.get("effort"))
+ scope._span.set_attribute(
+ "gen_ai.request.reasoning_effort", reasoning.get("effort")
+ )
# Responses API specific attributes
if hasattr(scope, "_service_tier"):
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER, scope._service_tier)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER, scope._service_tier
+ )
# Span Attributes for Response parameters
scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text")
# Span Attributes for Tools (responses API structure) - optimized
if hasattr(scope, "_response_tools") and scope._response_tools:
- tools = scope._response_tools if isinstance(scope._response_tools, list) else [scope._response_tools]
+ tools = (
+ scope._response_tools
+ if isinstance(scope._response_tools, list)
+ else [scope._response_tools]
+ )
- names, ids, args = zip(*[
- (t.get("name", ""),
- str(t.get("call_id", "")), # Use call_id for responses API
- str(t.get("arguments", "")))
- for t in tools if isinstance(t, dict) and t
- ]) if tools else ([], [], [])
+ names, ids, args = (
+ zip(
+ *[
+ (
+ t.get("name", ""),
+ str(t.get("call_id", "")), # Use call_id for responses API
+ str(t.get("arguments", "")),
+ )
+ for t in tools
+ if isinstance(t, dict) and t
+ ]
+ )
+ if tools
+ else ([], [], [])
+ )
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, ", ".join(filter(None, names)))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, ", ".join(filter(None, ids)))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, ", ".join(filter(None, args)))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_NAME, ", ".join(filter(None, names))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_CALL_ID, ", ".join(filter(None, ids))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_ARGS, ", ".join(filter(None, args))
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Reasoning tokens
if hasattr(scope, "_reasoning_tokens") and scope._reasoning_tokens > 0:
- scope._span.set_attribute("gen_ai.usage.reasoning_tokens", scope._reasoning_tokens)
+ scope._span.set_attribute(
+ "gen_ai.usage.reasoning_tokens", scope._reasoning_tokens
+ )
# Span Attributes for Content
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -331,23 +411,69 @@ def common_response_logic(scope, pricing_info, environment, application_name, me
# Record metrics
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_OPENAI,
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, input_tokens, output_tokens,
- cost, scope._tbt, scope._ttft)
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ input_tokens,
+ output_tokens,
+ cost,
+ scope._tbt,
+ scope._ttft,
+ )
+
-def process_streaming_response_response(scope, pricing_info, environment, application_name, metrics,
- capture_message_content=False, disable_metrics=False, version=""):
+def process_streaming_response_response(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="",
+):
"""
Process streaming responses API response and generate telemetry.
"""
- common_response_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=True)
+ common_response_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=True,
+ )
+
-def process_response_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+def process_response_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process non-streaming responses API response and generate telemetry.
"""
@@ -373,14 +499,16 @@ def process_response_response(response, request_model, pricing_info, server_port
break
if item.get("type") == "function_call":
# Handle tool call
- scope._response_tools = [{
- "id": item.get("id", ""),
- "call_id": item.get("call_id", ""),
- "name": item.get("name", ""),
- "type": item.get("type", "function_call"),
- "arguments": item.get("arguments", ""),
- "status": item.get("status", "")
- }]
+ scope._response_tools = [
+ {
+ "id": item.get("id", ""),
+ "call_id": item.get("call_id", ""),
+ "name": item.get("name", ""),
+ "type": item.get("type", "function_call"),
+ "arguments": item.get("arguments", ""),
+ "status": item.get("status", ""),
+ }
+ ]
# Extract content from message item if found
if message_item:
@@ -406,13 +534,32 @@ def process_response_response(response, request_model, pricing_info, server_port
scope._service_tier = response_dict.get("service_tier", "default")
scope._finish_reason = response_dict.get("status", "completed")
- common_response_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=False)
+ common_response_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=False,
+ )
return response
-def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream):
+
+def common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream,
+):
"""
Process chat request and generate Telemetry
"""
@@ -447,63 +594,121 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
scope,
SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
SemanticConvention.GEN_AI_SYSTEM_OPENAI,
- scope._server_address, scope._server_port, request_model,
- scope._response_model, environment, application_name,
- is_stream, scope._tbt, scope._ttft, version
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
)
# Span Attributes for Request parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED, str(handle_not_given(scope._kwargs.get("seed"), "")))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_SEED,
+ str(handle_not_given(scope._kwargs.get("seed"), "")),
+ )
scope._span.set_attribute(
SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
- handle_not_given(scope._kwargs.get("frequency_penalty"), 0.0)
+ handle_not_given(scope._kwargs.get("frequency_penalty"), 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
+ handle_not_given(scope._kwargs.get("max_tokens"), -1),
)
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, handle_not_given(scope._kwargs.get("max_tokens"), -1))
scope._span.set_attribute(
SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
- handle_not_given(scope._kwargs.get("presence_penalty"), 0.0)
+ handle_not_given(scope._kwargs.get("presence_penalty"), 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES,
+ handle_not_given(scope._kwargs.get("stop"), []),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
+ handle_not_given(scope._kwargs.get("temperature"), 1.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_P,
+ handle_not_given(scope._kwargs.get("top_p"), 1.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_USER,
+ handle_not_given(scope._kwargs.get("user"), ""),
)
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, handle_not_given(scope._kwargs.get("stop"), []))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, handle_not_given(scope._kwargs.get("temperature"), 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, handle_not_given(scope._kwargs.get("top_p"), 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, handle_not_given(scope._kwargs.get("user"), ""))
# Span Attributes for Response parameters
scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ "text" if isinstance(scope._llmresponse, str) else "json",
+ )
# OpenAI-specific attributes
if hasattr(scope, "_system_fingerprint") and scope._system_fingerprint:
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT, scope._system_fingerprint)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT,
+ scope._system_fingerprint,
+ )
if hasattr(scope, "_service_tier") and scope._service_tier:
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER, scope._service_tier)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_SERVICE_TIER, scope._service_tier
+ )
# Span Attributes for Tools - optimized
if hasattr(scope, "_tools") and scope._tools:
tools = scope._tools if isinstance(scope._tools, list) else [scope._tools]
- names, ids, args = zip(*[
- (t.get("function", {}).get("name", ""),
- str(t.get("id", "")),
- str(t.get("function", {}).get("arguments", "")))
- for t in tools if isinstance(t, dict) and t
- ]) if tools else ([], [], [])
+ names, ids, args = (
+ zip(
+ *[
+ (
+ t.get("function", {}).get("name", ""),
+ str(t.get("id", "")),
+ str(t.get("function", {}).get("arguments", "")),
+ )
+ for t in tools
+ if isinstance(t, dict) and t
+ ]
+ )
+ if tools
+ else ([], [], [])
+ )
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, ", ".join(filter(None, names)))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, ", ".join(filter(None, ids)))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, ", ".join(filter(None, args)))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_NAME, ", ".join(filter(None, names))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_CALL_ID, ", ".join(filter(None, ids))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_ARGS, ", ".join(filter(None, args))
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Content
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -523,23 +728,69 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
# Record metrics
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_OPENAI,
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, input_tokens, output_tokens,
- cost, scope._tbt, scope._ttft)
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ input_tokens,
+ output_tokens,
+ cost,
+ scope._tbt,
+ scope._ttft,
+ )
+
-def process_streaming_chat_response(scope, pricing_info, environment, application_name, metrics,
- capture_message_content=False, disable_metrics=False, version=""):
+def process_streaming_chat_response(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="",
+):
"""
Process streaming chat response and generate telemetry.
"""
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=True)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=True,
+ )
+
-def process_chat_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+def process_chat_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process non-streaming chat response and generate telemetry.
"""
@@ -564,7 +815,11 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
scope._kwargs = kwargs
scope._system_fingerprint = response_dict.get("system_fingerprint", "")
scope._service_tier = response_dict.get("service_tier", "auto")
- scope._finish_reason = str(response_dict.get("choices", [])[0].get("finish_reason", "")) if response_dict.get("choices") else ""
+ scope._finish_reason = (
+ str(response_dict.get("choices", [])[0].get("finish_reason", ""))
+ if response_dict.get("choices")
+ else ""
+ )
# Handle operation type for responses API
if kwargs.get("_operation_type") == "responses":
@@ -572,17 +827,38 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
# Handle tool calls
if kwargs.get("tools"):
- scope._tools = response_dict.get("choices", [{}])[0].get("message", {}).get("tool_calls")
+ scope._tools = (
+ response_dict.get("choices", [{}])[0].get("message", {}).get("tool_calls")
+ )
else:
scope._tools = None
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=False)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=False,
+ )
return response
-def common_embedding_logic(scope, request_model, pricing_info, environment, application_name,
- metrics, capture_message_content, disable_metrics, version):
+
+def common_embedding_logic(
+ scope,
+ request_model,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+):
"""
Common logic for processing embedding operations.
"""
@@ -591,61 +867,127 @@ def common_embedding_logic(scope, request_model, pricing_info, environment, appl
cost = get_embed_model_cost(request_model, pricing_info, scope._input_tokens)
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_OPENAI,
- scope._server_address, scope._server_port, request_model, request_model,
- environment, application_name, False, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
+ SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ False,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Request parameters
scope._span.set_attribute(
SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS,
- [handle_not_given(scope._kwargs.get("encoding_format"), "float")]
+ [handle_not_given(scope._kwargs.get("encoding_format"), "float")],
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_USER,
+ handle_not_given(scope._kwargs.get("user"), ""),
)
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, handle_not_given(scope._kwargs.get("user"), ""))
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Content
if capture_message_content:
input_data = scope._kwargs.get("input", "")
- formatted_content = format_content(input_data) if isinstance(input_data, (list, dict)) else str(input_data)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, formatted_content)
+ formatted_content = (
+ format_content(input_data)
+ if isinstance(input_data, (list, dict))
+ else str(input_data)
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_PROMPT, formatted_content
+ )
scope._span.set_status(Status(StatusCode.OK))
# Record metrics
if not disable_metrics:
- record_embedding_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_OPENAI,
- scope._server_address, scope._server_port, request_model, request_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, cost)
+ record_embedding_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
+ SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ cost,
+ )
-def common_image_logic(scope, request_model, pricing_info, environment, application_name,
- metrics, capture_message_content, disable_metrics, version):
+
+def common_image_logic(
+ scope,
+ request_model,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+):
"""
Common logic for processing image operations.
"""
# Calculate cost
- cost = get_image_model_cost(request_model, pricing_info,
- scope._kwargs.get("size", "1024x1024"),
- scope._kwargs.get("quality", "standard"))
+ cost = get_image_model_cost(
+ request_model,
+ pricing_info,
+ scope._kwargs.get("size", "1024x1024"),
+ scope._kwargs.get("quality", "standard"),
+ )
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE, SemanticConvention.GEN_AI_SYSTEM_OPENAI,
- scope._server_address, scope._server_port, request_model, request_model,
- environment, application_name, False, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE,
+ SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ False,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Request parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IMAGE_SIZE, handle_not_given(scope._kwargs.get("size"), "1024x1024"))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_IMAGE_SIZE,
+ handle_not_given(scope._kwargs.get("size"), "1024x1024"),
+ )
scope._span.set_attribute(
SemanticConvention.GEN_AI_REQUEST_IMAGE_QUALITY,
- handle_not_given(scope._kwargs.get("quality"), "standard")
+ handle_not_given(scope._kwargs.get("quality"), "standard"),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_USER,
+ handle_not_given(scope._kwargs.get("user"), ""),
)
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, handle_not_given(scope._kwargs.get("user"), ""))
# Extract response data
response_dict = scope._response_dict
@@ -657,7 +999,9 @@ def common_image_logic(scope, request_model, pricing_info, environment, applicat
# Span Attributes for Response
if response_created:
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, str(response_created))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_ID, str(response_created)
+ )
# Process image data and collect URLs/base64 content
if images_data:
@@ -675,7 +1019,9 @@ def common_image_logic(scope, request_model, pricing_info, environment, applicat
# Set image response data using semantic conventions
if image_contents:
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_IMAGE, image_contents)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_IMAGE, image_contents
+ )
# Response-level attributes if different from request
if response_size:
@@ -683,7 +1029,9 @@ def common_image_logic(scope, request_model, pricing_info, environment, applicat
if response_quality:
scope._span.set_attribute("gen_ai.response.image_quality", response_quality)
if response_output_format:
- scope._span.set_attribute("gen_ai.response.output_format", response_output_format)
+ scope._span.set_attribute(
+ "gen_ai.response.output_format", response_output_format
+ )
# Span Attributes for Cost
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
@@ -703,7 +1051,9 @@ def common_image_logic(scope, request_model, pricing_info, environment, applicat
# Set revised prompts as span attribute if any were found
if revised_prompts:
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_REVISED_PROMPT, revised_prompts)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_REVISED_PROMPT, revised_prompts
+ )
# Add revised prompt events for detailed tracking
for i, image in enumerate(images_data):
@@ -711,7 +1061,9 @@ def common_image_logic(scope, request_model, pricing_info, environment, applicat
scope._span.add_event(
name=SemanticConvention.GEN_AI_CONTENT_REVISED_PROMPT,
attributes={
- SemanticConvention.GEN_AI_CONTENT_REVISED_PROMPT: image["revised_prompt"],
+ SemanticConvention.GEN_AI_CONTENT_REVISED_PROMPT: image[
+ "revised_prompt"
+ ],
"image_index": i,
},
)
@@ -720,12 +1072,33 @@ def common_image_logic(scope, request_model, pricing_info, environment, applicat
# Record metrics
if not disable_metrics:
- record_image_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE, SemanticConvention.GEN_AI_SYSTEM_OPENAI,
- scope._server_address, scope._server_port, request_model, request_model, environment,
- application_name, scope._start_time, scope._end_time, cost)
+ record_image_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE,
+ SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ cost,
+ )
-def common_audio_logic(scope, request_model, pricing_info, environment, application_name,
- metrics, capture_message_content, disable_metrics, version):
+
+def common_audio_logic(
+ scope,
+ request_model,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+):
"""
Common logic for processing audio operations.
"""
@@ -735,18 +1108,35 @@ def common_audio_logic(scope, request_model, pricing_info, environment, applicat
cost = get_audio_model_cost(request_model, pricing_info, input_text)
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO, SemanticConvention.GEN_AI_SYSTEM_OPENAI,
- scope._server_address, scope._server_port, request_model, request_model,
- environment, application_name, False, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO,
+ SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ False,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Request parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_VOICE, handle_not_given(scope._kwargs.get("voice"), "alloy"))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_AUDIO_VOICE,
+ handle_not_given(scope._kwargs.get("voice"), "alloy"),
+ )
scope._span.set_attribute(
SemanticConvention.GEN_AI_REQUEST_AUDIO_RESPONSE_FORMAT,
- handle_not_given(scope._kwargs.get("response_format"), "mp3")
+ handle_not_given(scope._kwargs.get("response_format"), "mp3"),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_AUDIO_SPEED,
+ handle_not_given(scope._kwargs.get("speed"), 1.0),
)
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_SPEED, handle_not_given(scope._kwargs.get("speed"), 1.0))
# Span Attributes for Cost
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
@@ -760,13 +1150,39 @@ def common_audio_logic(scope, request_model, pricing_info, environment, applicat
# Record metrics
if not disable_metrics:
- record_audio_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO, SemanticConvention.GEN_AI_SYSTEM_OPENAI,
- scope._server_address, scope._server_port, request_model, request_model, environment,
- application_name, scope._start_time, scope._end_time, cost)
+ record_audio_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO,
+ SemanticConvention.GEN_AI_SYSTEM_OPENAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ cost,
+ )
+
-def process_audio_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, end_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+def process_audio_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ end_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process audio generation response and generate telemetry.
"""
@@ -781,14 +1197,37 @@ def process_audio_response(response, request_model, pricing_info, server_port, s
scope._server_address, scope._server_port = server_address, server_port
scope._kwargs = kwargs
- common_audio_logic(scope, request_model, pricing_info, environment, application_name,
- metrics, capture_message_content, disable_metrics, version)
+ common_audio_logic(
+ scope,
+ request_model,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
return response
-def process_embedding_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+
+def process_embedding_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process embedding response and generate telemetry.
"""
@@ -805,14 +1244,38 @@ def process_embedding_response(response, request_model, pricing_info, server_por
scope._server_address, scope._server_port = server_address, server_port
scope._kwargs = kwargs
- common_embedding_logic(scope, request_model, pricing_info, environment, application_name,
- metrics, capture_message_content, disable_metrics, version)
+ common_embedding_logic(
+ scope,
+ request_model,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
return response
-def process_image_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, end_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+
+def process_image_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ end_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process image generation response and generate telemetry.
"""
@@ -829,7 +1292,16 @@ def process_image_response(response, request_model, pricing_info, server_port, s
scope._kwargs = kwargs
scope._response_dict = response_dict
- common_image_logic(scope, request_model, pricing_info, environment, application_name,
- metrics, capture_message_content, disable_metrics, version)
+ common_image_logic(
+ scope,
+ request_model,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/openai_agents/__init__.py b/sdk/python/src/openlit/instrumentation/openai_agents/__init__.py
index 8f99a692..1d44f874 100644
--- a/sdk/python/src/openlit/instrumentation/openai_agents/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/openai_agents/__init__.py
@@ -10,6 +10,7 @@ from openlit.instrumentation.openai_agents.processor import OpenLITTracingProces
_instruments = ("openai-agents >= 0.0.3",)
+
class OpenAIAgentsInstrumentor(BaseInstrumentor):
"""OpenLIT instrumentor for OpenAI Agents using native tracing system"""
@@ -37,18 +38,20 @@ class OpenAIAgentsInstrumentor(BaseInstrumentor):
capture_message_content=capture_message_content,
metrics=metrics,
disable_metrics=disable_metrics,
- detailed_tracing=detailed_tracing
+ detailed_tracing=detailed_tracing,
)
# Integrate with OpenAI Agents' native tracing system
try:
from agents import set_trace_processors
+
# Replace existing processors with our enhanced processor
set_trace_processors([processor])
except ImportError:
# Fallback: Add our processor to existing ones
try:
from agents import add_trace_processor
+
add_trace_processor(processor)
except ImportError:
pass # Agents package may not have tracing
@@ -57,6 +60,7 @@ class OpenAIAgentsInstrumentor(BaseInstrumentor):
# Clear our processors
try:
from agents import set_trace_processors
+
set_trace_processors([])
except ImportError:
pass
diff --git a/sdk/python/src/openlit/instrumentation/openai_agents/processor.py b/sdk/python/src/openlit/instrumentation/openai_agents/processor.py
index d97c602a..10302dd6 100644
--- a/sdk/python/src/openlit/instrumentation/openai_agents/processor.py
+++ b/sdk/python/src/openlit/instrumentation/openai_agents/processor.py
@@ -10,13 +10,14 @@ from opentelemetry.trace import SpanKind, Status, StatusCode, set_span_in_contex
from openlit.__helpers import (
common_framework_span_attributes,
handle_exception,
- get_chat_model_cost
+ get_chat_model_cost,
)
from openlit.semcov import SemanticConvention
# Try to import agents framework components with fallback
try:
from agents import TracingProcessor
+
if TYPE_CHECKING:
from agents import Trace, Span
TRACING_AVAILABLE = True
@@ -50,9 +51,19 @@ class OpenLITTracingProcessor(TracingProcessor):
cost tracking, and performance metrics.
"""
- def __init__(self, tracer, version, environment, application_name,
- pricing_info, capture_message_content, metrics,
- disable_metrics, detailed_tracing, **kwargs):
+ def __init__(
+ self,
+ tracer,
+ version,
+ environment,
+ application_name,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ detailed_tracing,
+ **kwargs,
+ ):
"""Initialize the OpenLIT tracing processor."""
super().__init__()
@@ -90,11 +101,10 @@ class OpenLITTracingProcessor(TracingProcessor):
kind=SpanKind.CLIENT,
attributes={
SemanticConvention.GEN_AI_SYSTEM: "openai_agents",
- SemanticConvention.GEN_AI_OPERATION:
- SemanticConvention.GEN_AI_OPERATION_TYPE_WORKFLOW,
+ SemanticConvention.GEN_AI_OPERATION: SemanticConvention.GEN_AI_OPERATION_TYPE_WORKFLOW,
"trace.id": trace_id,
"trace.name": name,
- }
+ },
)
# Create scope for common attributes
@@ -112,7 +122,7 @@ class OpenLITTracingProcessor(TracingProcessor):
self.environment,
self.application_name,
self.version,
- name
+ name,
)
# Track active span
@@ -164,28 +174,28 @@ class OpenLITTracingProcessor(TracingProcessor):
Formatted span name following semantic conventions
"""
# Extract context for naming
- agent_name = metadata.get('agent_name', '')
- model_name = metadata.get('model_name', '')
- tool_name = metadata.get('tool_name', '')
- workflow_name = metadata.get('workflow_name', '')
+ agent_name = metadata.get("agent_name", "")
+ model_name = metadata.get("model_name", "")
+ tool_name = metadata.get("tool_name", "")
+ workflow_name = metadata.get("workflow_name", "")
# Apply OpenTelemetry semantic conventions for GenAI agents
- if 'agent' in operation_name.lower():
+ if "agent" in operation_name.lower():
if agent_name:
return f"invoke_agent {agent_name}"
return "invoke_agent"
- if 'chat' in operation_name.lower():
+ if "chat" in operation_name.lower():
if model_name:
return f"chat {model_name}"
return "chat response"
- if 'tool' in operation_name.lower():
+ if "tool" in operation_name.lower():
if tool_name:
return f"execute_tool {tool_name}"
return "execute_tool"
- if 'handoff' in operation_name.lower():
- target_agent = metadata.get('target_agent', 'unknown')
+ if "handoff" in operation_name.lower():
+ target_agent = metadata.get("target_agent", "unknown")
return f"invoke_agent {target_agent}"
- if 'workflow' in operation_name.lower():
+ if "workflow" in operation_name.lower():
if workflow_name:
return f"workflow {workflow_name}"
return "workflow"
@@ -203,15 +213,15 @@ class OpenLITTracingProcessor(TracingProcessor):
"""
try:
# Extract span information
- span_name = getattr(span_data, 'name', 'unknown_operation')
- span_type = getattr(span_data, 'type', 'unknown')
+ span_name = getattr(span_data, "name", "unknown_operation")
+ span_type = getattr(span_data, "type", "unknown")
# Generate enhanced span name
enhanced_name = self._get_span_name(
span_name,
- agent_name=getattr(span_data, 'agent_name', None),
- model_name=getattr(span_data, 'model_name', None),
- tool_name=getattr(span_data, 'tool_name', None)
+ agent_name=getattr(span_data, "agent_name", None),
+ model_name=getattr(span_data, "model_name", None),
+ tool_name=getattr(span_data, "tool_name", None),
)
# Determine span operation type
@@ -229,15 +239,15 @@ class OpenLITTracingProcessor(TracingProcessor):
SemanticConvention.GEN_AI_SYSTEM: "openai_agents",
SemanticConvention.GEN_AI_OPERATION: operation_type,
"span.type": span_type,
- "span.id": getattr(span_data, 'span_id', ''),
- }
+ "span.id": getattr(span_data, "span_id", ""),
+ },
)
# Process specific span types
self._process_span_attributes(span, span_data, span_type)
# Track span
- span_id = getattr(span_data, 'span_id', len(self.span_stack))
+ span_id = getattr(span_data, "span_id", len(self.span_stack))
self.active_spans[f"{trace_id}:{span_id}"] = span
self.span_stack.append(span)
@@ -247,11 +257,11 @@ class OpenLITTracingProcessor(TracingProcessor):
def _get_operation_type(self, span_type: str, span_name: str) -> str:
"""Get operation type based on span characteristics."""
type_mapping = {
- 'agent': SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
- 'generation': SemanticConvention.GEN_AI_OPERATION_CHAT,
- 'function': SemanticConvention.GEN_AI_OPERATION_CHAT,
- 'tool': SemanticConvention.GEN_AI_OPERATION_CHAT,
- 'handoff': SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
+ "agent": SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
+ "generation": SemanticConvention.GEN_AI_OPERATION_CHAT,
+ "function": SemanticConvention.GEN_AI_OPERATION_CHAT,
+ "tool": SemanticConvention.GEN_AI_OPERATION_CHAT,
+ "handoff": SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
}
# Check span type first
@@ -270,28 +280,30 @@ class OpenLITTracingProcessor(TracingProcessor):
"""Process and set span attributes based on span type."""
try:
# Common attributes
- if hasattr(span_data, 'agent_name'):
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_NAME,
- span_data.agent_name)
+ if hasattr(span_data, "agent_name"):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_NAME, span_data.agent_name
+ )
- if hasattr(span_data, 'model_name'):
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- span_data.model_name)
+ if hasattr(span_data, "model_name"):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MODEL, span_data.model_name
+ )
# Agent-specific attributes
- if span_type == 'agent':
+ if span_type == "agent":
self._process_agent_span(span, span_data)
# Generation-specific attributes
- elif span_type == 'generation':
+ elif span_type == "generation":
self._process_generation_span(span, span_data)
# Function/Tool-specific attributes
- elif span_type in ['function', 'tool']:
+ elif span_type in ["function", "tool"]:
self._process_function_span(span, span_data)
# Handoff-specific attributes
- elif span_type == 'handoff':
+ elif span_type == "handoff":
self._process_handoff_span(span, span_data)
except Exception as e: # pylint: disable=broad-exception-caught
@@ -300,72 +312,77 @@ class OpenLITTracingProcessor(TracingProcessor):
def _process_agent_span(self, span, agent_span):
"""Process agent span data (unused parameter)."""
# Agent-specific processing
- if hasattr(agent_span, 'instructions'):
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_DESCRIPTION,
- str(agent_span.instructions)[:500])
+ if hasattr(agent_span, "instructions"):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_DESCRIPTION,
+ str(agent_span.instructions)[:500],
+ )
- if hasattr(agent_span, 'model'):
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- agent_span.model)
+ if hasattr(agent_span, "model"):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MODEL, agent_span.model
+ )
def _process_generation_span(self, span, generation_span):
"""Process generation span data."""
# Set generation-specific attributes
- if hasattr(generation_span, 'prompt'):
- span.set_attribute(SemanticConvention.GEN_AI_PROMPT,
- str(generation_span.prompt)[:1000])
+ if hasattr(generation_span, "prompt"):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_PROMPT, str(generation_span.prompt)[:1000]
+ )
- if hasattr(generation_span, 'completion'):
- span.set_attribute(SemanticConvention.GEN_AI_COMPLETION,
- str(generation_span.completion)[:1000])
+ if hasattr(generation_span, "completion"):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_COMPLETION,
+ str(generation_span.completion)[:1000],
+ )
- if hasattr(generation_span, 'usage'):
+ if hasattr(generation_span, "usage"):
usage = generation_span.usage
- if hasattr(usage, 'prompt_tokens'):
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS,
- usage.prompt_tokens)
- if hasattr(usage, 'completion_tokens'):
+ if hasattr(usage, "prompt_tokens"):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS, usage.prompt_tokens
+ )
+ if hasattr(usage, "completion_tokens"):
span.set_attribute(
SemanticConvention.GEN_AI_USAGE_COMPLETION_TOKENS,
- usage.completion_tokens
+ usage.completion_tokens,
)
def _process_function_span(self, span, function_span):
"""Process function/tool span data."""
- if hasattr(function_span, 'function_name'):
- span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME,
- function_span.function_name)
+ if hasattr(function_span, "function_name"):
+ span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_NAME, function_span.function_name
+ )
- if hasattr(function_span, 'arguments'):
- span.set_attribute("gen_ai.tool.arguments",
- str(function_span.arguments)[:500])
+ if hasattr(function_span, "arguments"):
+ span.set_attribute(
+ "gen_ai.tool.arguments", str(function_span.arguments)[:500]
+ )
- if hasattr(function_span, 'result'):
- span.set_attribute("gen_ai.tool.result",
- str(function_span.result)[:500])
+ if hasattr(function_span, "result"):
+ span.set_attribute("gen_ai.tool.result", str(function_span.result)[:500])
def _process_handoff_span(self, span, handoff_span):
"""Process handoff span data."""
- if hasattr(handoff_span, 'target_agent'):
- span.set_attribute("gen_ai.handoff.target_agent",
- handoff_span.target_agent)
+ if hasattr(handoff_span, "target_agent"):
+ span.set_attribute("gen_ai.handoff.target_agent", handoff_span.target_agent)
- if hasattr(handoff_span, 'reason'):
- span.set_attribute("gen_ai.handoff.reason",
- str(handoff_span.reason)[:200])
+ if hasattr(handoff_span, "reason"):
+ span.set_attribute("gen_ai.handoff.reason", str(handoff_span.reason)[:200])
def span_end(self, span_data, trace_id: str):
"""Handle span end events."""
try:
- span_id = getattr(span_data, 'span_id', '')
+ span_id = getattr(span_data, "span_id", "")
span_key = f"{trace_id}:{span_id}"
span = self.active_spans.get(span_key)
if span:
# Set final status
- if hasattr(span_data, 'error') and span_data.error:
- span.set_status(Status(StatusCode.ERROR,
- str(span_data.error)))
+ if hasattr(span_data, "error") and span_data.error:
+ span.set_status(Status(StatusCode.ERROR, str(span_data.error)))
else:
span.set_status(Status(StatusCode.OK))
@@ -379,7 +396,7 @@ class OpenLITTracingProcessor(TracingProcessor):
self.span_stack.remove(span)
except Exception as e: # pylint: disable=broad-exception-caught
- handle_exception(span if 'span' in locals() else None, e)
+ handle_exception(span if "span" in locals() else None, e)
def force_flush(self):
"""Force flush all pending spans."""
@@ -402,15 +419,16 @@ class OpenLITTracingProcessor(TracingProcessor):
"""Extract model information from span data."""
model_info = {}
- if hasattr(span_data, 'model'):
- model_info['model'] = span_data.model
- if hasattr(span_data, 'model_name'):
- model_info['model'] = span_data.model_name
+ if hasattr(span_data, "model"):
+ model_info["model"] = span_data.model
+ if hasattr(span_data, "model_name"):
+ model_info["model"] = span_data.model_name
return model_info
- def _calculate_cost(self, model: str, prompt_tokens: int,
- completion_tokens: int) -> float:
+ def _calculate_cost(
+ self, model: str, prompt_tokens: int, completion_tokens: int
+ ) -> float:
"""Calculate cost based on token usage."""
try:
return get_chat_model_cost(
@@ -423,22 +441,24 @@ class OpenLITTracingProcessor(TracingProcessor):
def on_trace_start(self, trace):
"""Called when a trace starts - required by OpenAI Agents framework"""
try:
- self.start_trace(getattr(trace, 'trace_id', 'unknown'),
- getattr(trace, 'name', 'workflow'))
+ self.start_trace(
+ getattr(trace, "trace_id", "unknown"),
+ getattr(trace, "name", "workflow"),
+ )
except Exception: # pylint: disable=broad-exception-caught
pass
def on_trace_end(self, trace):
"""Called when a trace ends - required by OpenAI Agents framework"""
try:
- self.end_trace(getattr(trace, 'trace_id', 'unknown'))
+ self.end_trace(getattr(trace, "trace_id", "unknown"))
except Exception: # pylint: disable=broad-exception-caught
pass
def on_span_start(self, span):
"""Called when a span starts - required by OpenAI Agents framework"""
try:
- trace_id = getattr(span, 'trace_id', 'unknown')
+ trace_id = getattr(span, "trace_id", "unknown")
self.span_start(span, trace_id)
except Exception: # pylint: disable=broad-exception-caught
pass
@@ -446,7 +466,7 @@ class OpenLITTracingProcessor(TracingProcessor):
def on_span_end(self, span):
"""Called when a span ends - required by OpenAI Agents framework"""
try:
- trace_id = getattr(span, 'trace_id', 'unknown')
+ trace_id = getattr(span, "trace_id", "unknown")
self.span_end(span, trace_id)
except Exception: # pylint: disable=broad-exception-caught
pass
diff --git a/sdk/python/src/openlit/instrumentation/phidata/__init__.py b/sdk/python/src/openlit/instrumentation/phidata/__init__.py
index ea6c433f..657d4ae0 100644
--- a/sdk/python/src/openlit/instrumentation/phidata/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/phidata/__init__.py
@@ -6,12 +6,11 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.phidata.phidata import (
- phidata_wrap
-)
+from openlit.instrumentation.phidata.phidata import phidata_wrap
_instruments = ("phidata >= 2.5.32",)
+
class PhidataInstrumentor(BaseInstrumentor):
"""
An instrumentor for Phidata's client library.
@@ -33,8 +32,17 @@ class PhidataInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"phi.agent",
"Agent.print_response",
- phidata_wrap("phidata.print_response", version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ phidata_wrap(
+ "phidata.print_response",
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/phidata/phidata.py b/sdk/python/src/openlit/instrumentation/phidata/phidata.py
index 8bafaf94..5f843dc7 100644
--- a/sdk/python/src/openlit/instrumentation/phidata/phidata.py
+++ b/sdk/python/src/openlit/instrumentation/phidata/phidata.py
@@ -5,7 +5,11 @@ Module for monitoring Phidata calls.
import logging
from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from openlit.__helpers import (
handle_exception,
)
@@ -14,8 +18,18 @@ from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def phidata_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def phidata_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for chat completions to collect metrics.
@@ -50,40 +64,61 @@ def phidata_wrap(gen_ai_endpoint, version, environment, application_name,
"""
# pylint: disable=line-too-long
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(
+ gen_ai_endpoint, kind=SpanKind.CLIENT
+ ) as span:
response = wrapped(*args, **kwargs)
try:
# Set base span attribues
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
- SemanticConvention.GEN_AI_SYSTEM_PHIDATA)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT)
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,
- gen_ai_endpoint)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ID,
- getattr(instance, 'agent_id', '') or '')
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ROLE,
- getattr(instance, 'name', '') or '')
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
- getattr(getattr(instance, 'model', None), 'id', '') or '')
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TOOLS,
- str(getattr(instance, 'tools', '')) or '')
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_CONTEXT,
- str(getattr(instance, 'knowledge', '')) or '')
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_TASK,
- str(getattr(instance, 'task', '')) or '')
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_INSTRUCTIONS,
- str(getattr(instance, 'instructions', '')) or '')
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_STORAGE,
- str(getattr(instance, 'storage', '')) or '')
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_ENABLE_HISTORY,
- str(getattr(instance, 'add_history_to_messages', '')) or '')
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM,
+ SemanticConvention.GEN_AI_SYSTEM_PHIDATA,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_OPERATION,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,
+ )
+ span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)
+ span.set_attribute(SERVICE_NAME, application_name)
+ span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_ID,
+ getattr(instance, "agent_id", "") or "",
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_ROLE,
+ getattr(instance, "name", "") or "",
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MODEL,
+ getattr(getattr(instance, "model", None), "id", "") or "",
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_TOOLS,
+ str(getattr(instance, "tools", "")) or "",
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_CONTEXT,
+ str(getattr(instance, "knowledge", "")) or "",
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_TASK,
+ str(getattr(instance, "task", "")) or "",
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_INSTRUCTIONS,
+ str(getattr(instance, "instructions", "")) or "",
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_STORAGE,
+ str(getattr(instance, "storage", "")) or "",
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_ENABLE_HISTORY,
+ str(getattr(instance, "add_history_to_messages", "")) or "",
+ )
span.set_status(Status(StatusCode.OK))
diff --git a/sdk/python/src/openlit/instrumentation/pinecone/__init__.py b/sdk/python/src/openlit/instrumentation/pinecone/__init__.py
index 0ab512e4..42d62ad1 100644
--- a/sdk/python/src/openlit/instrumentation/pinecone/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/pinecone/__init__.py
@@ -13,7 +13,11 @@ _instruments = ("pinecone >= 7.3.0",)
# Pinecone sync operations
PINECONE_SYNC_OPERATIONS = [
("pinecone.pinecone", "Pinecone.create_index", "pinecone.create_collection"),
- ("pinecone.pinecone", "Pinecone.create_index_for_model", "pinecone.create_collection"),
+ (
+ "pinecone.pinecone",
+ "Pinecone.create_index_for_model",
+ "pinecone.create_collection",
+ ),
("pinecone.db_data.index", "Index.upsert", "pinecone.upsert"),
("pinecone.db_data.index", "Index.upsert_records", "pinecone.upsert_records"),
("pinecone.db_data.index", "Index.query", "pinecone.query"),
@@ -26,18 +30,35 @@ PINECONE_SYNC_OPERATIONS = [
# Pinecone async operations
PINECONE_ASYNC_OPERATIONS = [
- ("pinecone.pinecone_asyncio", "PineconeAsyncio.create_index", "pinecone.create_index"),
- ("pinecone.pinecone_asyncio", "PineconeAsyncio.create_index_for_model", "pinecone.create_index"),
+ (
+ "pinecone.pinecone_asyncio",
+ "PineconeAsyncio.create_index",
+ "pinecone.create_index",
+ ),
+ (
+ "pinecone.pinecone_asyncio",
+ "PineconeAsyncio.create_index_for_model",
+ "pinecone.create_index",
+ ),
("pinecone.db_data.index_asyncio", "_IndexAsyncio.upsert", "pinecone.upsert"),
- ("pinecone.db_data.index_asyncio", "_IndexAsyncio.upsert_records", "pinecone.upsert_records"),
+ (
+ "pinecone.db_data.index_asyncio",
+ "_IndexAsyncio.upsert_records",
+ "pinecone.upsert_records",
+ ),
("pinecone.db_data.index_asyncio", "_IndexAsyncio.query", "pinecone.query"),
("pinecone.db_data.index_asyncio", "_IndexAsyncio.search", "pinecone.search"),
("pinecone.db_data.index_asyncio", "_IndexAsyncio.fetch", "pinecone.fetch"),
- ("pinecone.db_data.index_asyncio", "_IndexAsyncio.search_records", "pinecone.search_records"),
+ (
+ "pinecone.db_data.index_asyncio",
+ "_IndexAsyncio.search_records",
+ "pinecone.search_records",
+ ),
("pinecone.db_data.index_asyncio", "_IndexAsyncio.update", "pinecone.update"),
("pinecone.db_data.index_asyncio", "_IndexAsyncio.delete", "pinecone.delete"),
]
+
class PineconeInstrumentor(BaseInstrumentor):
"""
An instrumentor for Pinecone client library.
@@ -61,8 +82,17 @@ class PineconeInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
module,
class_method,
- general_wrap(endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ general_wrap(
+ endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# Wrap async operations
@@ -70,8 +100,17 @@ class PineconeInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
module,
class_method,
- async_general_wrap(endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_general_wrap(
+ endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/pinecone/async_pinecone.py b/sdk/python/src/openlit/instrumentation/pinecone/async_pinecone.py
index 31c1a859..27a4bca5 100644
--- a/sdk/python/src/openlit/instrumentation/pinecone/async_pinecone.py
+++ b/sdk/python/src/openlit/instrumentation/pinecone/async_pinecone.py
@@ -12,8 +12,18 @@ from openlit.instrumentation.pinecone.utils import (
set_server_address_and_port,
)
-def async_general_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_general_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for Pinecone async function calls.
"""
@@ -43,9 +53,21 @@ def async_general_wrap(gen_ai_endpoint, version, environment, application_name,
try:
# Process response and generate telemetry
response = process_vectordb_response(
- response, db_operation, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version, instance, args, **kwargs
+ response,
+ db_operation,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ args,
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/pinecone/pinecone.py b/sdk/python/src/openlit/instrumentation/pinecone/pinecone.py
index 7404837e..649a2359 100644
--- a/sdk/python/src/openlit/instrumentation/pinecone/pinecone.py
+++ b/sdk/python/src/openlit/instrumentation/pinecone/pinecone.py
@@ -12,8 +12,18 @@ from openlit.instrumentation.pinecone.utils import (
set_server_address_and_port,
)
-def general_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def general_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for Pinecone function calls.
"""
@@ -43,9 +53,21 @@ def general_wrap(gen_ai_endpoint, version, environment, application_name,
try:
# Process response and generate telemetry
response = process_vectordb_response(
- response, db_operation, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version, instance, args, **kwargs
+ response,
+ db_operation,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ args,
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/pinecone/utils.py b/sdk/python/src/openlit/instrumentation/pinecone/utils.py
index bf715085..183c815f 100644
--- a/sdk/python/src/openlit/instrumentation/pinecone/utils.py
+++ b/sdk/python/src/openlit/instrumentation/pinecone/utils.py
@@ -1,6 +1,7 @@
"""
Pinecone OpenTelemetry instrumentation utility functions
"""
+
import time
from urllib.parse import urlparse
from opentelemetry.trace import Status, StatusCode
@@ -24,12 +25,14 @@ DB_OPERATION_MAP = {
"pinecone.search_records": SemanticConvention.DB_OPERATION_QUERY,
}
+
def object_count(obj):
"""
Counts length of object if it exists, else returns 0.
"""
return len(obj) if obj else 0
+
def set_server_address_and_port(instance):
"""
Extracts server address and port from Pinecone client instance.
@@ -70,8 +73,17 @@ def set_server_address_and_port(instance):
return server_address, server_port
-def common_vectordb_logic(scope, environment, application_name,
- metrics, capture_message_content, disable_metrics, version, instance=None):
+
+def common_vectordb_logic(
+ scope,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance=None,
+):
"""
Process vector database request and generate telemetry.
"""
@@ -79,25 +91,47 @@ def common_vectordb_logic(scope, environment, application_name,
scope._end_time = time.time()
# Set common database span attributes using helper
- common_db_span_attributes(scope, SemanticConvention.DB_SYSTEM_PINECONE, scope._server_address, scope._server_port,
- environment, application_name, version)
+ common_db_span_attributes(
+ scope,
+ SemanticConvention.DB_SYSTEM_PINECONE,
+ scope._server_address,
+ scope._server_port,
+ environment,
+ application_name,
+ version,
+ )
# Set DB operation specific attributes
scope._span.set_attribute(SemanticConvention.DB_OPERATION_NAME, scope._db_operation)
- scope._span.set_attribute(SemanticConvention.DB_CLIENT_OPERATION_DURATION, scope._end_time - scope._start_time)
+ scope._span.set_attribute(
+ SemanticConvention.DB_CLIENT_OPERATION_DURATION,
+ scope._end_time - scope._start_time,
+ )
# Set Create Index operation specific attributes
if scope._db_operation == SemanticConvention.DB_OPERATION_CREATE_COLLECTION:
# Standard database attributes
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, scope._kwargs.get("name", ""))
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, scope._kwargs.get("name", "")
+ )
# Vector database specific attributes (extensions)
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_DIMENSION, scope._kwargs.get("dimension", -1))
- scope._span.set_attribute(SemanticConvention.DB_SEARCH_SIMILARITY_METRIC, scope._kwargs.get("metric", "cosine"))
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_SPEC, str(scope._kwargs.get("spec", "")))
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_DIMENSION,
+ scope._kwargs.get("dimension", -1),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_SEARCH_SIMILARITY_METRIC,
+ scope._kwargs.get("metric", "cosine"),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_SPEC, str(scope._kwargs.get("spec", ""))
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_SEARCH:
- namespace = scope._kwargs.get("namespace", "default") or (scope._args[0] if scope._args else "unknown")
+ namespace = scope._kwargs.get("namespace", "default") or (
+ scope._args[0] if scope._args else "unknown"
+ )
query = scope._kwargs.get("query", {})
# Extract query text or vector from different possible locations
@@ -110,15 +144,21 @@ def common_vectordb_logic(scope, environment, application_name,
scope._span.set_attribute(SemanticConvention.DB_NAMESPACE, namespace)
# Vector database specific attributes (extensions)
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_QUERY_TOP_K, query.get("top_k", -1))
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_QUERY_TOP_K, query.get("top_k", -1)
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {namespace} "
f"top_k={query.get('top_k', -1)} "
f"text={query_text} "
- f"vector={query_vector}")
+ f"vector={query_vector}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_QUERY:
- namespace = scope._kwargs.get("namespace", "default") or (scope._args[0] if scope._args else "unknown")
+ namespace = scope._kwargs.get("namespace", "default") or (
+ scope._args[0] if scope._args else "unknown"
+ )
query = scope._kwargs.get("vector", [])
# Standard database attributes
@@ -126,16 +166,24 @@ def common_vectordb_logic(scope, environment, application_name,
scope._span.set_attribute(SemanticConvention.DB_NAMESPACE, namespace)
# Vector database specific attributes (extensions)
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_QUERY_TOP_K, scope._kwargs.get("top_k", ""))
- scope._span.set_attribute(SemanticConvention.DB_FILTER, str(scope._kwargs.get("filter", "")))
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_QUERY_TOP_K, scope._kwargs.get("top_k", "")
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_FILTER, str(scope._kwargs.get("filter", ""))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {namespace} "
f"top_k={scope._kwargs.get('top_k', -1)} "
f"filtered={scope._kwargs.get('filter', '')} "
- f"vector={scope._kwargs.get('vector', '')}")
+ f"vector={scope._kwargs.get('vector', '')}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_FETCH:
- namespace = scope._kwargs.get("namespace", "default") or (scope._args[0] if scope._args else "unknown")
+ namespace = scope._kwargs.get("namespace", "default") or (
+ scope._args[0] if scope._args else "unknown"
+ )
query = scope._kwargs.get("ids", [])
# Standard database attributes
@@ -143,13 +191,19 @@ def common_vectordb_logic(scope, environment, application_name,
scope._span.set_attribute(SemanticConvention.DB_NAMESPACE, namespace)
# Vector database specific attributes (extensions)
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
- f"{scope._db_operation} {namespace} "
- f"ids={query}")
- scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS, object_count(scope._response.vectors))
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {namespace} ids={query}",
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
+ object_count(scope._response.vectors),
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_UPDATE:
- namespace = scope._kwargs.get("namespace") or (scope._args[0] if scope._args else "unknown")
+ namespace = scope._kwargs.get("namespace") or (
+ scope._args[0] if scope._args else "unknown"
+ )
query = scope._kwargs.get("id", "")
# Standard database attributes
@@ -157,54 +211,98 @@ def common_vectordb_logic(scope, environment, application_name,
scope._span.set_attribute(SemanticConvention.DB_NAMESPACE, namespace)
# Vector database specific attributes (extensions)
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {namespace} "
f"id={query} "
f"values={scope._kwargs.get('values', [])} "
- f"set_metadata={scope._kwargs.get('set_metadata', '')}")
+ f"set_metadata={scope._kwargs.get('set_metadata', '')}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_UPSERT:
- namespace = scope._kwargs.get("namespace") or (scope._args[0] if scope._args else "unknown")
- query = scope._kwargs.get("vectors") or (scope._args[1] if len(scope._args) > 1 else None)
+ namespace = scope._kwargs.get("namespace") or (
+ scope._args[0] if scope._args else "unknown"
+ )
+ query = scope._kwargs.get("vectors") or (
+ scope._args[1] if len(scope._args) > 1 else None
+ )
# Standard database attributes
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(query))
scope._span.set_attribute(SemanticConvention.DB_NAMESPACE, namespace)
# Vector database specific attributes (extensions)
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, object_count(query))
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
- f"{scope._db_operation} {namespace} vectors_count={object_count(query)}")
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_COUNT, object_count(query)
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {namespace} vectors_count={object_count(query)}",
+ )
elif scope._db_operation == SemanticConvention.DB_OPERATION_DELETE:
- namespace = scope._kwargs.get("namespace") or (scope._args[0] if scope._args else "unknown")
- query = scope._kwargs.get("ids") or (scope._args[1] if len(scope._args) > 1 else None)
+ namespace = scope._kwargs.get("namespace") or (
+ scope._args[0] if scope._args else "unknown"
+ )
+ query = scope._kwargs.get("ids") or (
+ scope._args[1] if len(scope._args) > 1 else None
+ )
# Standard database attributes
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(query))
scope._span.set_attribute(SemanticConvention.DB_NAMESPACE, namespace)
# Vector database specific attributes (extensions)
- scope._span.set_attribute(SemanticConvention.DB_ID_COUNT, object_count(scope._kwargs.get("ids")))
- scope._span.set_attribute(SemanticConvention.DB_FILTER, str(scope._kwargs.get("filter", "")))
- scope._span.set_attribute(SemanticConvention.DB_DELETE_ALL, scope._kwargs.get("delete_all", False))
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_ID_COUNT, object_count(scope._kwargs.get("ids"))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_FILTER, str(scope._kwargs.get("filter", ""))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_DELETE_ALL, scope._kwargs.get("delete_all", False)
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {namespace} "
f"ids={query} "
f"filter={scope._kwargs.get('filter', '')} "
- f"delete_all={scope._kwargs.get('delete_all', False)}")
+ f"delete_all={scope._kwargs.get('delete_all', False)}",
+ )
scope._span.set_status(Status(StatusCode.OK))
# Record metrics using helper
if not disable_metrics:
- record_db_metrics(metrics, SemanticConvention.DB_SYSTEM_PINECONE, scope._server_address, scope._server_port,
- environment, application_name, scope._start_time, scope._end_time)
-
-def process_vectordb_response(response, db_operation, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content=False, disable_metrics=False,
- version="1.0.0", instance=None, args=None, **kwargs):
+ record_db_metrics(
+ metrics,
+ SemanticConvention.DB_SYSTEM_PINECONE,
+ scope._server_address,
+ scope._server_port,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ )
+
+
+def process_vectordb_response(
+ response,
+ db_operation,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ instance=None,
+ args=None,
+ **kwargs,
+):
"""
Process vector database response and generate telemetry following OpenTelemetry conventions.
"""
@@ -220,7 +318,15 @@ def process_vectordb_response(response, db_operation, server_address, server_por
scope._server_address = server_address
scope._server_port = server_port
- common_vectordb_logic(scope, environment, application_name,
- metrics, capture_message_content, disable_metrics, version, instance)
+ common_vectordb_logic(
+ scope,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/premai/__init__.py b/sdk/python/src/openlit/instrumentation/premai/__init__.py
index d3e4fb81..21d20df3 100644
--- a/sdk/python/src/openlit/instrumentation/premai/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/premai/__init__.py
@@ -5,12 +5,11 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.premai.premai import (
- chat, embedding
-)
+from openlit.instrumentation.premai.premai import chat, embedding
_instruments = ("premai >= 0.3.79",)
+
class PremAIInstrumentor(BaseInstrumentor):
"""
An instrumentor for Prem AI's client library.
@@ -33,16 +32,32 @@ class PremAIInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"premai.api",
"ChatCompletionsModule.create",
- chat(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# sync embedding
wrap_function_wrapper(
"premai.api",
"EmbeddingsModule.create",
- embedding(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ embedding(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/premai/premai.py b/sdk/python/src/openlit/instrumentation/premai/premai.py
index cbe05521..8390ce74 100644
--- a/sdk/python/src/openlit/instrumentation/premai/premai.py
+++ b/sdk/python/src/openlit/instrumentation/premai/premai.py
@@ -4,20 +4,26 @@ Module for monitoring PremAI API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.premai.utils import (
process_chat_response,
process_chunk,
process_streaming_chat_response,
- process_embedding_response
+ process_embedding_response,
)
from openlit.semcov import SemanticConvention
-def chat(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -28,15 +34,15 @@ def chat(version, environment, application_name,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -75,7 +81,9 @@ def chat(version, environment, application_name,
finally:
try:
- with tracer.start_as_current_span(self._span_name, kind=SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -84,7 +92,7 @@ def chat(version, environment, application_name,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -98,7 +106,9 @@ def chat(version, environment, application_name,
# Check if streaming is enabled for the API call
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "app.premai.io", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "app.premai.io", 443
+ )
request_model = kwargs.get("model", "gpt-4o-mini")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -107,7 +117,9 @@ def chat(version, environment, application_name,
# Special handling for streaming response to accommodate the nature of data flow
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedSyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
# Handling for non-streaming responses
else:
@@ -130,7 +142,7 @@ def chat(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -140,8 +152,17 @@ def chat(version, environment, application_name,
return wrapper
-def embedding(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def embedding(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -151,10 +172,14 @@ def embedding(version, environment, application_name,
Wraps the GenAI function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "app.premai.io", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "app.premai.io", 443
+ )
request_model = kwargs.get("model", "text-embedding-ada-002")
- span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING} {request_model}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
@@ -175,7 +200,7 @@ def embedding(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/premai/utils.py b/sdk/python/src/openlit/instrumentation/premai/utils.py
index 17e7b7ac..427c9d91 100644
--- a/sdk/python/src/openlit/instrumentation/premai/utils.py
+++ b/sdk/python/src/openlit/instrumentation/premai/utils.py
@@ -1,6 +1,7 @@
"""
PremAI OpenTelemetry instrumentation utility functions
"""
+
import time
from opentelemetry.trace import Status, StatusCode
@@ -18,6 +19,7 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def format_content(messages):
"""
Process a list of messages to extract content.
@@ -30,8 +32,9 @@ def format_content(messages):
if isinstance(content, list):
content_str = ", ".join(
- f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
- if "type" in item else f'text: {item["text"]}'
+ f"{item['type']}: {item['text'] if 'text' in item else item['image_url']}"
+ if "type" in item
+ else f"text: {item['text']}"
for item in content
)
formatted_messages.append(f"{role}: {content_str}")
@@ -40,6 +43,7 @@ def format_content(messages):
return "\n".join(formatted_messages)
+
def process_chunk(scope, chunk):
"""
Process a chunk of response data and update state.
@@ -67,8 +71,18 @@ def process_chunk(scope, chunk):
scope._response_model = chunked.get("model")
scope._end_time = time.time()
-def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream):
+
+def common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream,
+):
"""
Process chat request and generate Telemetry
"""
@@ -90,40 +104,91 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
cost = get_chat_model_cost(request_model, pricing_info, input_tokens, output_tokens)
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,SemanticConvention.GEN_AI_SYSTEM_PREMAI,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_PREMAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Response parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED, scope._kwargs.get("seed", ""))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY, scope._kwargs.get("frequency_penalty", 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, scope._kwargs.get("max_tokens", -1))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, scope._kwargs.get("presence_penalty", 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get("stop", []))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, scope._kwargs.get("temperature", 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, scope._kwargs.get("user", ""))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_SEED, scope._kwargs.get("seed", "")
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
+ scope._kwargs.get("frequency_penalty", 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
+ scope._kwargs.get("max_tokens", -1),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
+ scope._kwargs.get("presence_penalty", 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get("stop", [])
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
+ scope._kwargs.get("temperature", 1.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 1.0)
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_USER, scope._kwargs.get("user", "")
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ "text" if isinstance(scope._llmresponse, str) else "json",
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Tools
if scope._tools:
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, scope._tools.get("function","")).get("name","")
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get("id","")))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, str(scope._tools.get("function","").get("arguments","")))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_NAME, scope._tools.get("function", "")
+ ).get("name", "")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get("id", ""))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_ARGS,
+ str(scope._tools.get("function", "").get("arguments", "")),
+ )
# Span Attributes for Content
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
# To be removed one the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -143,13 +208,36 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
# Metrics
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_PREMAI,
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, input_tokens, output_tokens,
- cost, scope._tbt, scope._ttft)
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_PREMAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ input_tokens,
+ output_tokens,
+ cost,
+ scope._tbt,
+ scope._ttft,
+ )
+
-def common_embedding_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version):
+def common_embedding_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+):
"""
Process embedding request and generate Telemetry
"""
@@ -159,16 +247,36 @@ def common_embedding_logic(scope, pricing_info, environment, application_name, m
cost = get_embed_model_cost(request_model, pricing_info, scope._input_tokens)
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_PREMAI,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, False, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
+ SemanticConvention.GEN_AI_SYSTEM_PREMAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ False,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Embedding-specific span attributes
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS, [scope._kwargs.get("encoding_format", "float")])
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_USER, scope._kwargs.get("user", ""))
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS,
+ [scope._kwargs.get("encoding_format", "float")],
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_USER, scope._kwargs.get("user", "")
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Content
@@ -176,7 +284,9 @@ def common_embedding_logic(scope, pricing_info, environment, application_name, m
scope._span.add_event(
name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: str(scope._kwargs.get("input", "")),
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: str(
+ scope._kwargs.get("input", "")
+ ),
},
)
@@ -184,21 +294,65 @@ def common_embedding_logic(scope, pricing_info, environment, application_name, m
# Metrics
if not disable_metrics:
- record_embedding_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING, SemanticConvention.GEN_AI_SYSTEM_PREMAI,
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, cost)
+ record_embedding_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EMBEDDING,
+ SemanticConvention.GEN_AI_SYSTEM_PREMAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ cost,
+ )
-def process_streaming_chat_response(scope, pricing_info, environment, application_name, metrics,
- capture_message_content=False, disable_metrics=False, version=""):
+
+def process_streaming_chat_response(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="",
+):
"""
Process chat request and generate Telemetry
"""
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=True)
-
-def process_chat_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=True,
+ )
+
+
+def process_chat_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process chat request and generate Telemetry
"""
@@ -209,7 +363,9 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
scope._start_time = start_time
scope._end_time = time.time()
scope._span = span
- scope._llmresponse = str(response_dict.get("choices")[0].get("message").get("content"))
+ scope._llmresponse = str(
+ response_dict.get("choices")[0].get("message").get("content")
+ )
scope._response_id = response_dict.get("additional_properties", {}).get("id")
scope._response_model = response_dict.get("model")
scope._input_tokens = response_dict.get("usage").get("prompt_tokens")
@@ -225,14 +381,37 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
else:
scope._tools = None
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=False)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=False,
+ )
return response
-def process_embedding_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+
+def process_embedding_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process embedding request and generate Telemetry
"""
@@ -250,7 +429,15 @@ def process_embedding_response(response, request_model, pricing_info, server_por
scope._server_address, scope._server_port = server_address, server_port
scope._kwargs = kwargs
- common_embedding_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version)
+ common_embedding_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/pydantic_ai/__init__.py b/sdk/python/src/openlit/instrumentation/pydantic_ai/__init__.py
index 9197c538..f586ed4e 100644
--- a/sdk/python/src/openlit/instrumentation/pydantic_ai/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/pydantic_ai/__init__.py
@@ -6,10 +6,13 @@ from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
from openlit.instrumentation.pydantic_ai.pydantic_ai import (
- agent_create, agent_run, async_agent_run
+ agent_create,
+ agent_run,
+ async_agent_run,
)
-_instruments = ('pydantic-ai >= 0.2.17',)
+_instruments = ("pydantic-ai >= 0.2.17",)
+
class PydanticAIInstrumentor(BaseInstrumentor):
"""
@@ -20,34 +23,58 @@ class PydanticAIInstrumentor(BaseInstrumentor):
return _instruments
def _instrument(self, **kwargs):
- application_name = kwargs.get('application_name', 'default_application')
- environment = kwargs.get('environment', 'default_environment')
- tracer = kwargs.get('tracer')
- metrics = kwargs.get('metrics_dict')
- pricing_info = kwargs.get('pricing_info', {})
- capture_message_content = kwargs.get('capture_message_content', False)
- disable_metrics = kwargs.get('disable_metrics')
- version = importlib.metadata.version('pydantic-ai')
+ application_name = kwargs.get("application_name", "default_application")
+ environment = kwargs.get("environment", "default_environment")
+ tracer = kwargs.get("tracer")
+ metrics = kwargs.get("metrics_dict")
+ pricing_info = kwargs.get("pricing_info", {})
+ capture_message_content = kwargs.get("capture_message_content", False)
+ disable_metrics = kwargs.get("disable_metrics")
+ version = importlib.metadata.version("pydantic-ai")
wrap_function_wrapper(
- 'pydantic_ai.agent',
- 'Agent.__init__',
- agent_create(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ "pydantic_ai.agent",
+ "Agent.__init__",
+ agent_create(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
- 'pydantic_ai.agent',
- 'Agent.run_sync',
- agent_run(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ "pydantic_ai.agent",
+ "Agent.run_sync",
+ agent_run(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
- 'pydantic_ai.agent',
- 'Agent.run',
- async_agent_run(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ "pydantic_ai.agent",
+ "Agent.run",
+ async_agent_run(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/pydantic_ai/pydantic_ai.py b/sdk/python/src/openlit/instrumentation/pydantic_ai/pydantic_ai.py
index 455998ff..8a660dce 100644
--- a/sdk/python/src/openlit/instrumentation/pydantic_ai/pydantic_ai.py
+++ b/sdk/python/src/openlit/instrumentation/pydantic_ai/pydantic_ai.py
@@ -4,48 +4,101 @@ Module for monitoring Pydantic AI API calls.
from openlit.instrumentation.pydantic_ai.utils import (
common_agent_run,
- common_agent_create
+ common_agent_create,
)
-def agent_create(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+def agent_create(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
def wrapper(wrapped, instance, args, kwargs):
response = wrapped(*args, **kwargs)
- return common_agent_create(wrapped, instance, args, kwargs, tracer,
- version, environment, application_name,
- capture_message_content, response=response)
+ return common_agent_create(
+ wrapped,
+ instance,
+ args,
+ kwargs,
+ tracer,
+ version,
+ environment,
+ application_name,
+ capture_message_content,
+ response=response,
+ )
return wrapper
-def agent_run(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def agent_run(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
def wrapper(wrapped, instance, args, kwargs):
response = wrapped(*args, **kwargs)
- return common_agent_run(wrapped, instance, args, kwargs, tracer,
- version, environment, application_name,
- capture_message_content, response=response)
+ return common_agent_run(
+ wrapped,
+ instance,
+ args,
+ kwargs,
+ tracer,
+ version,
+ environment,
+ application_name,
+ capture_message_content,
+ response=response,
+ )
return wrapper
-def async_agent_run(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_agent_run(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
async def wrapper(wrapped, instance, args, kwargs):
response = await wrapped(*args, **kwargs)
- return common_agent_run(wrapped, instance, args, kwargs, tracer,
- version, environment, application_name,
- capture_message_content, response=response)
+ return common_agent_run(
+ wrapped,
+ instance,
+ args,
+ kwargs,
+ tracer,
+ version,
+ environment,
+ application_name,
+ capture_message_content,
+ response=response,
+ )
return wrapper
diff --git a/sdk/python/src/openlit/instrumentation/pydantic_ai/utils.py b/sdk/python/src/openlit/instrumentation/pydantic_ai/utils.py
index a6333cd8..980eac41 100644
--- a/sdk/python/src/openlit/instrumentation/pydantic_ai/utils.py
+++ b/sdk/python/src/openlit/instrumentation/pydantic_ai/utils.py
@@ -1,27 +1,42 @@
"""
Pydantic AI OpenTelemetry instrumentation utility functions
"""
+
import logging
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
-from opentelemetry.trace import Status, StatusCode, SpanKind
-from openlit.__helpers import (
- handle_exception
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
)
+from opentelemetry.trace import Status, StatusCode, SpanKind
+from openlit.__helpers import handle_exception
from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def set_span_attributes(span, version, operation_name, environment,
- application_name, server_address, server_port, request_model, agent_name):
+
+def set_span_attributes(
+ span,
+ version,
+ operation_name,
+ environment,
+ application_name,
+ server_address,
+ server_port,
+ request_model,
+ agent_name,
+):
"""
Set common OpenTelemetry span attributes for Pydantic AI operations.
"""
# Set Span attributes (OTel Semconv)
- span.set_attribute(TELEMETRY_SDK_NAME, 'openlit')
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
span.set_attribute(SemanticConvention.GEN_AI_OPERATION, operation_name)
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM, SemanticConvention.GEN_AI_SYSTEM_PYDANTIC_AI)
+ span.set_attribute(
+ SemanticConvention.GEN_AI_SYSTEM, SemanticConvention.GEN_AI_SYSTEM_PYDANTIC_AI
+ )
span.set_attribute(SemanticConvention.GEN_AI_AGENT_NAME, agent_name)
span.set_attribute(SemanticConvention.SERVER_ADDRESS, server_address)
span.set_attribute(SemanticConvention.SERVER_PORT, server_port)
@@ -32,8 +47,19 @@ def set_span_attributes(span, version, operation_name, environment,
span.set_attribute(SERVICE_NAME, application_name)
span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
-def common_agent_run(wrapped, instance, args, kwargs, tracer, version, environment, application_name,
- capture_message_content, response):
+
+def common_agent_run(
+ wrapped,
+ instance,
+ args,
+ kwargs,
+ tracer,
+ version,
+ environment,
+ application_name,
+ capture_message_content,
+ response,
+):
"""
Handle telemetry for Pydantic AI agent run operations.
"""
@@ -41,13 +67,27 @@ def common_agent_run(wrapped, instance, args, kwargs, tracer, version, environme
server_address, server_port = instance.model.base_url, 443
agent_name = instance.name or "pydantic_agent"
request_model = str(instance.model.model_name)
- span_name = f'{SemanticConvention.GEN_AI_OPERATION_TYPE_EXECUTE_AGENT_TASK} {agent_name}'
+ span_name = (
+ f"{SemanticConvention.GEN_AI_OPERATION_TYPE_EXECUTE_AGENT_TASK} {agent_name}"
+ )
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
try:
- set_span_attributes(span, version, SemanticConvention.GEN_AI_OPERATION_TYPE_EXECUTE_AGENT_TASK,
- environment, application_name, server_address, server_port, request_model, agent_name)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_DESCRIPTION, str(instance._system_prompts))
+ set_span_attributes(
+ span,
+ version,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_EXECUTE_AGENT_TASK,
+ environment,
+ application_name,
+ server_address,
+ server_port,
+ request_model,
+ agent_name,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_DESCRIPTION,
+ str(instance._system_prompts),
+ )
span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, request_model)
if capture_message_content:
@@ -64,25 +104,50 @@ def common_agent_run(wrapped, instance, args, kwargs, tracer, version, environme
except Exception as e:
handle_exception(span, e)
- logger.error('Error in trace creation: %s', e)
+ logger.error("Error in trace creation: %s", e)
return response
-def common_agent_create(wrapped, instance, args, kwargs, tracer, version, environment, application_name,
- capture_message_content, response):
+
+def common_agent_create(
+ wrapped,
+ instance,
+ args,
+ kwargs,
+ tracer,
+ version,
+ environment,
+ application_name,
+ capture_message_content,
+ response,
+):
"""
Handle telemetry for Pydantic AI agent creation operations.
"""
- server_address, server_port = '127.0.0.1', 80
+ server_address, server_port = "127.0.0.1", 80
agent_name = kwargs.get("name", "pydantic_agent")
- span_name = f'{SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT} {agent_name}'
+ span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT} {agent_name}"
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
try:
- request_model = args[0] or kwargs.get("model", "google-gla:gemini-1.5-flash")
- set_span_attributes(span, version, SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT,
- environment, application_name, server_address, server_port, request_model, agent_name)
- span.set_attribute(SemanticConvention.GEN_AI_AGENT_DESCRIPTION, str(kwargs.get("system_prompt", "")))
+ request_model = args[0] or kwargs.get(
+ "model", "google-gla:gemini-1.5-flash"
+ )
+ set_span_attributes(
+ span,
+ version,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CREATE_AGENT,
+ environment,
+ application_name,
+ server_address,
+ server_port,
+ request_model,
+ agent_name,
+ )
+ span.set_attribute(
+ SemanticConvention.GEN_AI_AGENT_DESCRIPTION,
+ str(kwargs.get("system_prompt", "")),
+ )
span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, request_model)
span.set_status(Status(StatusCode.OK))
@@ -91,5 +156,5 @@ def common_agent_create(wrapped, instance, args, kwargs, tracer, version, enviro
except Exception as e:
handle_exception(span, e)
- logger.error('Error in trace creation: %s', e)
+ logger.error("Error in trace creation: %s", e)
return response
diff --git a/sdk/python/src/openlit/instrumentation/qdrant/__init__.py b/sdk/python/src/openlit/instrumentation/qdrant/__init__.py
index d6dfa6f9..2f479b23 100644
--- a/sdk/python/src/openlit/instrumentation/qdrant/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/qdrant/__init__.py
@@ -36,6 +36,7 @@ QDRANT_OPERATIONS = [
("query_points", "qdrant.query_points"),
]
+
class QdrantInstrumentor(BaseInstrumentor):
"""
An instrumentor for Qdrant client library.
@@ -60,8 +61,15 @@ class QdrantInstrumentor(BaseInstrumentor):
"qdrant_client",
f"QdrantClient.{method_name}",
general_wrap(
- endpoint, version, environment, application_name, tracer,
- pricing_info, capture_message_content, metrics, disable_metrics
+ endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
),
)
@@ -71,8 +79,15 @@ class QdrantInstrumentor(BaseInstrumentor):
"qdrant_client",
f"AsyncQdrantClient.{method_name}",
async_general_wrap(
- endpoint, version, environment, application_name, tracer,
- pricing_info, capture_message_content, metrics, disable_metrics
+ endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
),
)
diff --git a/sdk/python/src/openlit/instrumentation/qdrant/async_qdrant.py b/sdk/python/src/openlit/instrumentation/qdrant/async_qdrant.py
index 9d2d1a05..2915fdaf 100644
--- a/sdk/python/src/openlit/instrumentation/qdrant/async_qdrant.py
+++ b/sdk/python/src/openlit/instrumentation/qdrant/async_qdrant.py
@@ -7,10 +7,24 @@ import time
from opentelemetry.trace import SpanKind
from opentelemetry import context as context_api
from openlit.__helpers import handle_exception
-from openlit.instrumentation.qdrant.utils import DB_OPERATION_MAP, process_qdrant_response, set_server_address_and_port
+from openlit.instrumentation.qdrant.utils import (
+ DB_OPERATION_MAP,
+ process_qdrant_response,
+ set_server_address_and_port,
+)
-def async_general_wrap(gen_ai_endpoint, version, environment, application_name, tracer,
- pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_general_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for Pinecone function calls.
"""
@@ -41,10 +55,22 @@ def async_general_wrap(gen_ai_endpoint, version, environment, application_name,
try:
# Process response with endpoint information
response = process_qdrant_response(
- response, db_operation, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version,
- instance=instance, args=args, endpoint=gen_ai_endpoint, **kwargs
+ response,
+ db_operation,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance=instance,
+ args=args,
+ endpoint=gen_ai_endpoint,
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/qdrant/qdrant.py b/sdk/python/src/openlit/instrumentation/qdrant/qdrant.py
index 2eb1c21f..94c08868 100644
--- a/sdk/python/src/openlit/instrumentation/qdrant/qdrant.py
+++ b/sdk/python/src/openlit/instrumentation/qdrant/qdrant.py
@@ -7,10 +7,24 @@ import time
from opentelemetry.trace import SpanKind
from opentelemetry import context as context_api
from openlit.__helpers import handle_exception
-from openlit.instrumentation.qdrant.utils import DB_OPERATION_MAP, process_qdrant_response, set_server_address_and_port
+from openlit.instrumentation.qdrant.utils import (
+ DB_OPERATION_MAP,
+ process_qdrant_response,
+ set_server_address_and_port,
+)
-def general_wrap(gen_ai_endpoint, version, environment, application_name, tracer,
- pricing_info, capture_message_content, metrics, disable_metrics):
+
+def general_wrap(
+ gen_ai_endpoint,
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for Pinecone function calls.
"""
@@ -41,10 +55,22 @@ def general_wrap(gen_ai_endpoint, version, environment, application_name, tracer
try:
# Process response with endpoint information
response = process_qdrant_response(
- response, db_operation, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version,
- instance=instance, args=args, endpoint=gen_ai_endpoint, **kwargs
+ response,
+ db_operation,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance=instance,
+ args=args,
+ endpoint=gen_ai_endpoint,
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/qdrant/utils.py b/sdk/python/src/openlit/instrumentation/qdrant/utils.py
index f3b35724..35f32223 100644
--- a/sdk/python/src/openlit/instrumentation/qdrant/utils.py
+++ b/sdk/python/src/openlit/instrumentation/qdrant/utils.py
@@ -35,6 +35,7 @@ DB_OPERATION_MAP = {
"qdrant.create_payload_index": SemanticConvention.DB_OPERATION_CREATE_INDEX,
}
+
def object_count(obj):
"""
Counts Length of object if it exists, Else returns 0.
@@ -44,6 +45,7 @@ def object_count(obj):
except (TypeError, AttributeError):
return 0
+
def set_server_address_and_port(instance):
"""
Extracts server address and port from Qdrant client instance.
@@ -79,75 +81,117 @@ def set_server_address_and_port(instance):
return server_address, server_port
-def common_qdrant_logic(scope, environment, application_name,
- metrics, capture_message_content, disable_metrics, version,
- instance=None, endpoint=None):
+
+def common_qdrant_logic(
+ scope,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance=None,
+ endpoint=None,
+):
"""
Process Qdrant request and generate telemetry.
"""
scope._end_time = time.time()
# Set common database span attributes using helper
- common_db_span_attributes(scope, SemanticConvention.DB_SYSTEM_QDRANT,
- scope._server_address, scope._server_port,
- environment, application_name, version)
+ common_db_span_attributes(
+ scope,
+ SemanticConvention.DB_SYSTEM_QDRANT,
+ scope._server_address,
+ scope._server_port,
+ environment,
+ application_name,
+ version,
+ )
# Set DB operation specific attributes
scope._span.set_attribute(SemanticConvention.DB_OPERATION_NAME, scope._db_operation)
- scope._span.set_attribute(SemanticConvention.DB_CLIENT_OPERATION_DURATION,
- scope._end_time - scope._start_time)
+ scope._span.set_attribute(
+ SemanticConvention.DB_CLIENT_OPERATION_DURATION,
+ scope._end_time - scope._start_time,
+ )
# Handle collection management operations
- if scope._db_operation in [SemanticConvention.DB_OPERATION_CREATE_COLLECTION,
- SemanticConvention.DB_OPERATION_DELETE_COLLECTION,
- SemanticConvention.DB_OPERATION_UPDATE_COLLECTION]:
+ if scope._db_operation in [
+ SemanticConvention.DB_OPERATION_CREATE_COLLECTION,
+ SemanticConvention.DB_OPERATION_DELETE_COLLECTION,
+ SemanticConvention.DB_OPERATION_UPDATE_COLLECTION,
+ ]:
collection_name = scope._kwargs.get("collection_name", "unknown")
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
- scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT,
- f"Collection: {collection_name}")
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
- f"{scope._db_operation} {collection_name}")
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_TEXT, f"Collection: {collection_name}"
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name}",
+ )
# Handle data insertion operations
- elif scope._db_operation in [SemanticConvention.DB_OPERATION_INSERT,
- SemanticConvention.DB_OPERATION_UPSERT]:
+ elif scope._db_operation in [
+ SemanticConvention.DB_OPERATION_INSERT,
+ SemanticConvention.DB_OPERATION_UPSERT,
+ ]:
collection_name = scope._kwargs.get("collection_name", "unknown")
if endpoint == "qdrant.set_payload":
points = scope._kwargs.get("points", [])
payload = scope._kwargs.get("payload", {})
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(points))
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, object_count(points))
- scope._span.set_attribute(SemanticConvention.DB_PAYLOAD_COUNT, object_count(payload))
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_COUNT, object_count(points)
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_PAYLOAD_COUNT, object_count(payload)
+ )
# Set operation status if response available
if scope._response and hasattr(scope._response, "status"):
- scope._span.set_attribute(SemanticConvention.DB_OPERATION_STATUS,
- scope._response.status)
+ scope._span.set_attribute(
+ SemanticConvention.DB_OPERATION_STATUS, scope._response.status
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
f"points={object_count(points)} "
- f"payload={object_count(payload)}")
+ f"payload={object_count(payload)}",
+ )
elif endpoint in ["qdrant.upsert", "qdrant.upload_points"]:
points = scope._kwargs.get("points", [])
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(points))
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, object_count(points))
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_COUNT, object_count(points)
+ )
# Set operation status if response available
if scope._response and hasattr(scope._response, "status"):
- scope._span.set_attribute(SemanticConvention.DB_OPERATION_STATUS,
- scope._response.status)
+ scope._span.set_attribute(
+ SemanticConvention.DB_OPERATION_STATUS, scope._response.status
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
- f"points={object_count(points)}")
+ f"points={object_count(points)}",
+ )
# Handle data update operations
elif scope._db_operation == SemanticConvention.DB_OPERATION_UPDATE:
@@ -157,36 +201,52 @@ def common_qdrant_logic(scope, environment, application_name,
points = scope._kwargs.get("points", [])
payload = scope._kwargs.get("payload", {})
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(points))
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, object_count(points))
- scope._span.set_attribute(SemanticConvention.DB_PAYLOAD_COUNT, object_count(payload))
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_COUNT, object_count(points)
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_PAYLOAD_COUNT, object_count(payload)
+ )
# Set operation status if response available
if scope._response and hasattr(scope._response, "status"):
- scope._span.set_attribute(SemanticConvention.DB_OPERATION_STATUS,
- scope._response.status)
+ scope._span.set_attribute(
+ SemanticConvention.DB_OPERATION_STATUS, scope._response.status
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
f"points={object_count(points)} "
- f"payload={object_count(payload)}")
+ f"payload={object_count(payload)}",
+ )
elif endpoint == "qdrant.update_vectors":
points = scope._kwargs.get("points", [])
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(points))
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, object_count(points))
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_COUNT, object_count(points)
+ )
# Set operation status if response available
if scope._response and hasattr(scope._response, "status"):
- scope._span.set_attribute(SemanticConvention.DB_OPERATION_STATUS,
- scope._response.status)
+ scope._span.set_attribute(
+ SemanticConvention.DB_OPERATION_STATUS, scope._response.status
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
- f"points={object_count(points)}")
+ f"points={object_count(points)}",
+ )
# Handle data deletion operations
elif scope._db_operation == SemanticConvention.DB_OPERATION_DELETE:
@@ -195,34 +255,50 @@ def common_qdrant_logic(scope, environment, application_name,
if endpoint in ["qdrant.delete_payload", "qdrant.delete_vectors"]:
points = scope._kwargs.get("points", [])
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(points))
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, object_count(points))
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_COUNT, object_count(points)
+ )
# Set operation status if response available
if scope._response and hasattr(scope._response, "status"):
- scope._span.set_attribute(SemanticConvention.DB_OPERATION_STATUS,
- scope._response.status)
+ scope._span.set_attribute(
+ SemanticConvention.DB_OPERATION_STATUS, scope._response.status
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
- f"points={object_count(points)}")
+ f"points={object_count(points)}",
+ )
elif endpoint in ["qdrant.clear_payload", "qdrant.delete"]:
points_selector = scope._kwargs.get("points_selector", [])
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
- scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(points_selector))
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, object_count(points_selector))
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_TEXT, str(points_selector)
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_COUNT, object_count(points_selector)
+ )
# Set operation status if response available
if scope._response and hasattr(scope._response, "status"):
- scope._span.set_attribute(SemanticConvention.DB_OPERATION_STATUS,
- scope._response.status)
+ scope._span.set_attribute(
+ SemanticConvention.DB_OPERATION_STATUS, scope._response.status
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
- f"selector={object_count(points_selector)}")
+ f"selector={object_count(points_selector)}",
+ )
# Handle query operations
elif scope._db_operation == SemanticConvention.DB_OPERATION_GET:
@@ -231,82 +307,133 @@ def common_qdrant_logic(scope, environment, application_name,
if endpoint == "qdrant.retrieve":
ids = scope._kwargs.get("ids", [])
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(ids))
- scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, object_count(ids))
+ scope._span.set_attribute(
+ SemanticConvention.DB_VECTOR_COUNT, object_count(ids)
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
- f"{scope._db_operation} {collection_name} ids={ids}")
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} ids={ids}",
+ )
elif endpoint == "qdrant.scroll":
scroll_filter = scope._kwargs.get("scroll_filter", {})
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
- scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(scroll_filter))
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_TEXT, str(scroll_filter)
+ )
scope._span.set_attribute(SemanticConvention.DB_FILTER, str(scroll_filter))
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
- f"{scope._db_operation} {collection_name} "
- f"filter={scroll_filter}")
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} filter={scroll_filter}",
+ )
elif endpoint in ["qdrant.search", "qdrant.search_groups"]:
query_vector = scope._kwargs.get("query_vector", [])
limit = scope._kwargs.get("limit", 10)
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
- scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(query_vector))
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_TEXT, str(query_vector)
+ )
scope._span.set_attribute(SemanticConvention.DB_VECTOR_QUERY_TOP_K, limit)
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
- f"{scope._db_operation} {collection_name} "
- f"limit={limit}")
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} limit={limit}",
+ )
elif endpoint == "qdrant.recommend":
positive = scope._kwargs.get("positive", [])
negative = scope._kwargs.get("negative", [])
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
query_content = f"positive:{positive} negative:{negative}"
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, query_content)
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
f"positive={object_count(positive)} "
- f"negative={object_count(negative)}")
+ f"negative={object_count(negative)}",
+ )
elif endpoint == "qdrant.query_points":
query = scope._kwargs.get("query", {})
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(query))
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
- f"{scope._db_operation} {collection_name} query={query}")
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} query={query}",
+ )
# Handle index operations
elif scope._db_operation == SemanticConvention.DB_OPERATION_CREATE_INDEX:
collection_name = scope._kwargs.get("collection_name", "unknown")
field_name = scope._kwargs.get("field_name", "unknown")
- scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
- scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, f"Field: {field_name}")
+ scope._span.set_attribute(
+ SemanticConvention.DB_COLLECTION_NAME, collection_name
+ )
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_TEXT, f"Field: {field_name}"
+ )
- scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
- f"{scope._db_operation} {collection_name} field={field_name}")
+ scope._span.set_attribute(
+ SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} field={field_name}",
+ )
scope._span.set_status(Status(StatusCode.OK))
# Record metrics using helper
if not disable_metrics:
- record_db_metrics(metrics, SemanticConvention.DB_SYSTEM_QDRANT,
- scope._server_address, scope._server_port,
- environment, application_name,
- scope._start_time, scope._end_time)
-
-def process_qdrant_response(response, db_operation, server_address, server_port,
- environment, application_name, metrics, start_time, span,
- capture_message_content, disable_metrics, version,
- instance=None, args=None, endpoint=None, **kwargs):
+ record_db_metrics(
+ metrics,
+ SemanticConvention.DB_SYSTEM_QDRANT,
+ scope._server_address,
+ scope._server_port,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ )
+
+
+def process_qdrant_response(
+ response,
+ db_operation,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance=None,
+ args=None,
+ endpoint=None,
+ **kwargs,
+):
"""
Process Qdrant response and generate telemetry.
"""
@@ -321,8 +448,16 @@ def process_qdrant_response(response, db_operation, server_address, server_port,
scope._kwargs = kwargs
# Process the response
- common_qdrant_logic(scope, environment, application_name,
- metrics, capture_message_content, disable_metrics, version,
- instance=instance, endpoint=endpoint)
+ common_qdrant_logic(
+ scope,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ instance=instance,
+ endpoint=endpoint,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/reka/__init__.py b/sdk/python/src/openlit/instrumentation/reka/__init__.py
index ef71ecf5..97ad070a 100644
--- a/sdk/python/src/openlit/instrumentation/reka/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/reka/__init__.py
@@ -5,15 +5,12 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.reka.reka import (
- chat
-)
-from openlit.instrumentation.reka.async_reka import (
- async_chat
-)
+from openlit.instrumentation.reka.reka import chat
+from openlit.instrumentation.reka.async_reka import async_chat
_instruments = ("reka-api >= 3.2.0",)
+
class RekaInstrumentor(BaseInstrumentor):
"""
An instrumentor for Reka client library.
@@ -36,16 +33,32 @@ class RekaInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"reka.chat.client",
"ChatClient.create",
- chat(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# Chat completions
wrap_function_wrapper(
"reka.chat.client",
"AsyncChatClient.create",
- async_chat(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/reka/async_reka.py b/sdk/python/src/openlit/instrumentation/reka/async_reka.py
index f392f649..b37c06ff 100644
--- a/sdk/python/src/openlit/instrumentation/reka/async_reka.py
+++ b/sdk/python/src/openlit/instrumentation/reka/async_reka.py
@@ -4,17 +4,21 @@ Module for monitoring Reka API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
-from openlit.instrumentation.reka.utils import (
- process_chat_response
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
+from openlit.instrumentation.reka.utils import process_chat_response
from openlit.semcov import SemanticConvention
-def async_chat(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -24,7 +28,9 @@ def async_chat(version, environment, application_name,
Wraps the GenAI function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.reka.ai", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.reka.ai", 443
+ )
request_model = kwargs.get("model", "reka-core-20240501")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -48,7 +54,7 @@ def async_chat(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/reka/reka.py b/sdk/python/src/openlit/instrumentation/reka/reka.py
index af59b1dc..7853b7d4 100644
--- a/sdk/python/src/openlit/instrumentation/reka/reka.py
+++ b/sdk/python/src/openlit/instrumentation/reka/reka.py
@@ -4,17 +4,21 @@ Module for monitoring Reka API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
-from openlit.instrumentation.reka.utils import (
- process_chat_response
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
+from openlit.instrumentation.reka.utils import process_chat_response
from openlit.semcov import SemanticConvention
-def chat(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def chat(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -24,7 +28,9 @@ def chat(version, environment, application_name,
Wraps the GenAI function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.reka.ai", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.reka.ai", 443
+ )
request_model = kwargs.get("model", "reka-core-20240501")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -48,7 +54,7 @@ def chat(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/reka/utils.py b/sdk/python/src/openlit/instrumentation/reka/utils.py
index e6a69986..a6eee345 100644
--- a/sdk/python/src/openlit/instrumentation/reka/utils.py
+++ b/sdk/python/src/openlit/instrumentation/reka/utils.py
@@ -1,6 +1,7 @@
"""
Reka OpenTelemetry instrumentation utility functions
"""
+
import time
from opentelemetry.trace import Status, StatusCode
@@ -14,6 +15,7 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def format_content(messages):
"""
Process a list of messages to extract content.
@@ -26,8 +28,9 @@ def format_content(messages):
if isinstance(content, list):
content_str = ", ".join(
- f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
- if "type" in item else f'text: {item["text"]}'
+ f"{item['type']}: {item['text'] if 'text' in item else item['image_url']}"
+ if "type" in item
+ else f"text: {item['text']}"
for item in content
)
formatted_messages.append(f"{role}: {content_str}")
@@ -36,8 +39,18 @@ def format_content(messages):
return "\n".join(formatted_messages)
-def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream):
+
+def common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream,
+):
"""
Process chat request and generate Telemetry
"""
@@ -48,42 +61,92 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
prompt = format_content(scope._kwargs.get("messages", ""))
request_model = scope._kwargs.get("model", "reka-core-20240501")
- cost = get_chat_model_cost(request_model, pricing_info, scope._input_tokens, scope._output_tokens)
+ cost = get_chat_model_cost(
+ request_model, pricing_info, scope._input_tokens, scope._output_tokens
+ )
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,SemanticConvention.GEN_AI_SYSTEM_REKAAI,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_REKAAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Response parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED, scope._kwargs.get("seed", ""))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, scope._kwargs.get("max_tokens", -1))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, scope._kwargs.get("presence_penalty", 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get("stop", []))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, scope._kwargs.get("temperature", 0.4))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_K, scope._kwargs.get("top_k", 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 1.0))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_SEED, scope._kwargs.get("seed", "")
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
+ scope._kwargs.get("max_tokens", -1),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
+ scope._kwargs.get("presence_penalty", 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get("stop", [])
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
+ scope._kwargs.get("temperature", 0.4),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_K, scope._kwargs.get("top_k", 1.0)
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 1.0)
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ "text" if isinstance(scope._llmresponse, str) else "json",
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens + scope._output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ scope._input_tokens + scope._output_tokens,
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Tools
if scope._tools:
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, scope._tools.get("name",""))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get("id","")))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, str(scope._tools.get("parameters","")))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_NAME, scope._tools.get("name", "")
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get("id", ""))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_ARGS, str(scope._tools.get("parameters", ""))
+ )
# Span Attributes for Content
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
# To be removed one the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -103,14 +166,42 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
# Metrics
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_REKAAI,
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, scope._output_tokens,
- cost, scope._tbt, scope._ttft)
-
-def process_chat_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_REKAAI,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ scope._output_tokens,
+ cost,
+ scope._tbt,
+ scope._ttft,
+ )
+
+
+def process_chat_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process chat request and generate Telemetry
"""
@@ -135,11 +226,22 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
scope._kwargs = kwargs
scope._finish_reason = str(response_dict.get("responses")[0].get("finish_reason"))
if scope._kwargs.get("tools"):
- scope._tools = response_dict.get("responses")[0].get("message").get("tool_calls")
+ scope._tools = (
+ response_dict.get("responses")[0].get("message").get("tool_calls")
+ )
else:
scope._tools = None
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=False)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=False,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/together/__init__.py b/sdk/python/src/openlit/instrumentation/together/__init__.py
index 4210a350..dc25e4f6 100644
--- a/sdk/python/src/openlit/instrumentation/together/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/together/__init__.py
@@ -5,15 +5,15 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.together.together import (
- completion, image_generate
-)
+from openlit.instrumentation.together.together import completion, image_generate
from openlit.instrumentation.together.async_together import (
- async_completion, async_image_generate
+ async_completion,
+ async_image_generate,
)
_instruments = ("together >= 1.3.5",)
+
class TogetherInstrumentor(BaseInstrumentor):
"""
An instrumentor for Together client library.
@@ -36,32 +36,64 @@ class TogetherInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"together.resources.chat.completions",
"ChatCompletions.create",
- completion(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ completion(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# Image generate
wrap_function_wrapper(
"together.resources.images",
"Images.generate",
- image_generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ image_generate(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# Chat completions
wrap_function_wrapper(
"together.resources.chat.completions",
"AsyncChatCompletions.create",
- async_completion(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_completion(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# Image generate
wrap_function_wrapper(
"together.resources.images",
"AsyncImages.generate",
- async_image_generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_image_generate(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/together/async_together.py b/sdk/python/src/openlit/instrumentation/together/async_together.py
index e35f1d55..d901c346 100644
--- a/sdk/python/src/openlit/instrumentation/together/async_together.py
+++ b/sdk/python/src/openlit/instrumentation/together/async_together.py
@@ -4,20 +4,26 @@ Module for monitoring Together API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.together.utils import (
process_chat_response,
process_chunk,
process_streaming_chat_response,
- process_image_response
+ process_image_response,
)
from openlit.semcov import SemanticConvention
-def async_completion(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_completion(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -28,15 +34,15 @@ def async_completion(version, environment, application_name,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -78,7 +84,9 @@ def async_completion(version, environment, application_name,
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(self._span_name, kind= SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -87,7 +95,7 @@ def async_completion(version, environment, application_name,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -103,7 +111,9 @@ def async_completion(version, environment, application_name,
# Check if streaming is enabled for the API call
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "api.together.xyz", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.together.xyz", 443
+ )
request_model = kwargs.get("model", "gpt-4o")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -112,11 +122,13 @@ def async_completion(version, environment, application_name,
# Special handling for streaming response to accommodate the nature of data flow
awaited_wrapped = await wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedAsyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedAsyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
# Handling for non-streaming responses
else:
- with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
response = await wrapped(*args, **kwargs)
@@ -135,7 +147,7 @@ def async_completion(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -145,8 +157,17 @@ def async_completion(version, environment, application_name,
return wrapper
-def async_image_generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_image_generate(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -156,12 +177,14 @@ def async_image_generate(version, environment, application_name,
Wraps the GenAI function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.together.xyz", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.together.xyz", 443
+ )
request_model = kwargs.get("model", "dall-e-2")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE} {request_model}"
- with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
response = await wrapped(*args, **kwargs)
end_time = time.time()
@@ -182,7 +205,7 @@ def async_image_generate(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
return response
diff --git a/sdk/python/src/openlit/instrumentation/together/together.py b/sdk/python/src/openlit/instrumentation/together/together.py
index 390f578e..bc241327 100644
--- a/sdk/python/src/openlit/instrumentation/together/together.py
+++ b/sdk/python/src/openlit/instrumentation/together/together.py
@@ -4,20 +4,26 @@ Module for monitoring Together API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
from openlit.instrumentation.together.utils import (
process_chat_response,
process_chunk,
process_streaming_chat_response,
- process_image_response
+ process_image_response,
)
from openlit.semcov import SemanticConvention
-def completion(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def completion(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -28,15 +34,15 @@ def completion(version, environment, application_name,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- **args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ **args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -78,7 +84,9 @@ def completion(version, environment, application_name,
return chunk
except StopIteration:
try:
- with tracer.start_as_current_span(self._span_name, kind= SpanKind.CLIENT) as self._span:
+ with tracer.start_as_current_span(
+ self._span_name, kind=SpanKind.CLIENT
+ ) as self._span:
process_streaming_chat_response(
self,
pricing_info=pricing_info,
@@ -87,7 +95,7 @@ def completion(version, environment, application_name,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
@@ -103,7 +111,9 @@ def completion(version, environment, application_name,
# Check if streaming is enabled for the API call
streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "api.together.xyz", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.together.xyz", 443
+ )
request_model = kwargs.get("model", "gpt-4o")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
@@ -112,11 +122,13 @@ def completion(version, environment, application_name,
# Special handling for streaming response to accommodate the nature of data flow
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port)
+ return TracedSyncStream(
+ awaited_wrapped, span, span_name, kwargs, server_address, server_port
+ )
# Handling for non-streaming responses
else:
- with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
response = wrapped(*args, **kwargs)
@@ -135,7 +147,7 @@ def completion(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
@@ -145,8 +157,17 @@ def completion(version, environment, application_name,
return wrapper
-def image_generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+
+def image_generate(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -156,12 +177,14 @@ def image_generate(version, environment, application_name,
Wraps the GenAI function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "api.together.xyz", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "api.together.xyz", 443
+ )
request_model = kwargs.get("model", "dall-e-2")
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE} {request_model}"
- with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
response = wrapped(*args, **kwargs)
end_time = time.time()
@@ -182,7 +205,7 @@ def image_generate(version, environment, application_name,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
return response
diff --git a/sdk/python/src/openlit/instrumentation/together/utils.py b/sdk/python/src/openlit/instrumentation/together/utils.py
index 9c0021a7..0c5c594b 100644
--- a/sdk/python/src/openlit/instrumentation/together/utils.py
+++ b/sdk/python/src/openlit/instrumentation/together/utils.py
@@ -1,9 +1,14 @@
"""
Together AI OpenTelemetry instrumentation utility functions
"""
+
import time
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from opentelemetry.trace import Status, StatusCode
from openlit.__helpers import (
@@ -16,6 +21,7 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def format_content(messages):
"""
Process a list of messages to extract content.
@@ -28,8 +34,9 @@ def format_content(messages):
if isinstance(content, list):
content_str = ", ".join(
- f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
- if "type" in item else f'text: {item["text"]}'
+ f"{item['type']}: {item['text'] if 'text' in item else item['image_url']}"
+ if "type" in item
+ else f"text: {item['text']}"
for item in content
)
formatted_messages.append(f"{role}: {content_str}")
@@ -38,6 +45,7 @@ def format_content(messages):
return "\n".join(formatted_messages)
+
def process_chunk(scope, chunk):
"""
Process a chunk of response data and update state.
@@ -53,9 +61,10 @@ def process_chunk(scope, chunk):
chunked = response_as_dict(chunk)
# Collect message IDs and aggregated response from events
- if (len(chunked.get("choices")) > 0 and ("delta" in chunked.get("choices")[0] and
- "content" in chunked.get("choices")[0].get("delta"))):
-
+ if len(chunked.get("choices")) > 0 and (
+ "delta" in chunked.get("choices")[0]
+ and "content" in chunked.get("choices")[0].get("delta")
+ ):
content = chunked.get("choices")[0].get("delta").get("content")
if content:
scope._llmresponse += content
@@ -68,8 +77,22 @@ def process_chunk(scope, chunk):
scope._finish_reason = str(chunked.get("finish_reason"))
scope._end_time = time.time()
-def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_address, server_port,
- request_model, response_model, environment, application_name, is_stream, tbt, ttft, version):
+
+def common_span_attributes(
+ scope,
+ gen_ai_operation,
+ gen_ai_system,
+ server_address,
+ server_port,
+ request_model,
+ response_model,
+ environment,
+ application_name,
+ is_stream,
+ tbt,
+ ttft,
+ version,
+):
"""
Set common span attributes for both chat and RAG operations.
"""
@@ -88,9 +111,25 @@ def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_addres
scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft)
scope._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
-def record_common_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
- request_model, response_model, environment, application_name, start_time, end_time,
- input_tokens, output_tokens, cost, tbt=None, ttft=None):
+
+def record_common_metrics(
+ metrics,
+ gen_ai_operation,
+ gen_ai_system,
+ server_address,
+ server_port,
+ request_model,
+ response_model,
+ environment,
+ application_name,
+ start_time,
+ end_time,
+ input_tokens,
+ output_tokens,
+ cost,
+ tbt=None,
+ ttft=None,
+):
"""
Record common metrics for the operation.
"""
@@ -109,15 +148,27 @@ def record_common_metrics(metrics, gen_ai_operation, gen_ai_system, server_addre
metrics["genai_requests"].add(1, attributes)
metrics["genai_prompt_tokens"].add(input_tokens, attributes)
metrics["genai_completion_tokens"].add(output_tokens, attributes)
- metrics["genai_client_usage_tokens"].record(input_tokens + output_tokens, attributes)
+ metrics["genai_client_usage_tokens"].record(
+ input_tokens + output_tokens, attributes
+ )
metrics["genai_cost"].record(cost, attributes)
if tbt is not None:
metrics["genai_server_tbt"].record(tbt, attributes)
if ttft is not None:
metrics["genai_server_ttft"].record(ttft, attributes)
-def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream):
+
+def common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream,
+):
"""
Process chat request and generate Telemetry
"""
@@ -128,42 +179,94 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
prompt = format_content(scope._kwargs.get("messages", ""))
request_model = scope._kwargs.get("model", "jamba-1.5-mini")
- cost = get_chat_model_cost(request_model, pricing_info, scope._input_tokens, scope._output_tokens)
+ cost = get_chat_model_cost(
+ request_model, pricing_info, scope._input_tokens, scope._output_tokens
+ )
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,SemanticConvention.GEN_AI_SYSTEM_TOGETHER,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_TOGETHER,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Response parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_SEED, scope._kwargs.get("seed", ""))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY, scope._kwargs.get("frequency_penalty", 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, scope._kwargs.get("max_tokens", -1))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY, scope._kwargs.get("presence_penalty", 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get("stop", []))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, scope._kwargs.get("temperature", 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 1.0))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason]
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_SEED, scope._kwargs.get("seed", "")
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
+ scope._kwargs.get("frequency_penalty", 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
+ scope._kwargs.get("max_tokens", -1),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
+ scope._kwargs.get("presence_penalty", 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, scope._kwargs.get("stop", [])
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
+ scope._kwargs.get("temperature", 1.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_P, scope._kwargs.get("top_p", 1.0)
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ "text" if isinstance(scope._llmresponse, str) else "json",
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens + scope._output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ scope._input_tokens + scope._output_tokens,
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Tools
if scope._tools:
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, scope._tools.get("function","")).get("name","")
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get("id","")))
- scope._span.set_attribute(SemanticConvention.GEN_AI_TOOL_ARGS, str(scope._tools.get("function","").get("arguments","")))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_NAME, scope._tools.get("function", "")
+ ).get("name", "")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_CALL_ID, str(scope._tools.get("id", ""))
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_TOOL_ARGS,
+ str(scope._tools.get("function", "").get("arguments", "")),
+ )
# Span Attributes for Content
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
# To be removed one the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -183,22 +286,68 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
# Metrics
if not disable_metrics:
- record_common_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_TOGETHER,
- scope._server_address, scope._server_port, request_model, scope._response_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, scope._output_tokens,
- cost, scope._tbt, scope._ttft)
+ record_common_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_TOGETHER,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ scope._output_tokens,
+ cost,
+ scope._tbt,
+ scope._ttft,
+ )
-def process_streaming_chat_response(scope, pricing_info, environment, application_name, metrics,
- capture_message_content=False, disable_metrics=False, version=""):
+
+def process_streaming_chat_response(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="",
+):
"""
Process chat request and generate Telemetry
"""
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=True)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=True,
+ )
-def process_chat_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, capture_message_content=False,
- disable_metrics=False, version="1.0.0", **kwargs):
+
+def process_chat_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process chat request and generate Telemetry
"""
@@ -228,57 +377,105 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
else:
scope._tools = None
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=False)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=False,
+ )
return response
-def common_image_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version):
+
+def common_image_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+):
"""
Process image generation request and generate Telemetry
"""
# Find Image format
- if "response_format" in scope._kwargs and scope._kwargs["response_format"] == "b64_json":
+ if (
+ "response_format" in scope._kwargs
+ and scope._kwargs["response_format"] == "b64_json"
+ ):
image_format = "b64_json"
else:
image_format = "url"
- image_size = str(scope._kwargs.get("width", "1024")) + "x" + str(scope._kwargs.get("height", "1024"))
+ image_size = (
+ str(scope._kwargs.get("width", "1024"))
+ + "x"
+ + str(scope._kwargs.get("height", "1024"))
+ )
request_model = scope._kwargs.get("model", "dall-e-2")
# Calculate cost of the operation
- cost = get_image_model_cost(request_model, pricing_info, image_size,
- scope._kwargs.get("quality", "standard"))
+ cost = get_image_model_cost(
+ request_model,
+ pricing_info,
+ image_size,
+ scope._kwargs.get("quality", "standard"),
+ )
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE, SemanticConvention.GEN_AI_SYSTEM_TOGETHER,
- scope._server_address, scope._server_port, request_model, scope._response_model,
- environment, application_name, False, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE,
+ SemanticConvention.GEN_AI_SYSTEM_TOGETHER,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ False,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Image-specific span attributes
scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_ID, scope._response_id)
scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "image")
scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IMAGE_SIZE, image_size)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, len(scope._response_data) * cost)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_COST, len(scope._response_data) * cost
+ )
# Content attributes
if capture_message_content:
scope._span.add_event(
name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
attributes={
- SemanticConvention.GEN_AI_CONTENT_PROMPT: scope._kwargs.get("prompt", ""),
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: scope._kwargs.get(
+ "prompt", ""
+ ),
},
)
for images_count, item in enumerate(scope._response_data):
- attribute_name = f"{SemanticConvention.GEN_AI_RESPONSE_IMAGE}.{images_count}"
+ attribute_name = (
+ f"{SemanticConvention.GEN_AI_RESPONSE_IMAGE}.{images_count}"
+ )
scope._span.add_event(
name=attribute_name,
attributes={
- SemanticConvention.GEN_AI_CONTENT_COMPLETION: getattr(item, image_format),
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION: getattr(
+ item, image_format
+ ),
},
)
@@ -287,15 +484,40 @@ def common_image_logic(scope, pricing_info, environment, application_name, metri
# Metrics
if not disable_metrics:
record_common_metrics(
- metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE,
- SemanticConvention.GEN_AI_SYSTEM_TOGETHER, scope._server_address, scope._server_port,
- request_model, scope._response_model, environment, application_name,
- scope._start_time, scope._end_time, 0, 0, len(scope._response_data) * cost
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_IMAGE,
+ SemanticConvention.GEN_AI_SYSTEM_TOGETHER,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ scope._response_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ 0,
+ 0,
+ len(scope._response_data) * cost,
)
-def process_image_response(response, request_model, pricing_info, server_address, server_port,
- environment, application_name, metrics, start_time, end_time, span, capture_message_content,
- disable_metrics, version, **kwargs):
+
+def process_image_response(
+ response,
+ request_model,
+ pricing_info,
+ server_address,
+ server_port,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ end_time,
+ span,
+ capture_message_content,
+ disable_metrics,
+ version,
+ **kwargs,
+):
"""
Process image generation request and generate Telemetry
"""
@@ -314,7 +536,15 @@ def process_image_response(response, request_model, pricing_info, server_address
scope._tbt = 0
scope._ttft = end_time - start_time
- common_image_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version)
+ common_image_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/transformers/__init__.py b/sdk/python/src/openlit/instrumentation/transformers/__init__.py
index 00b3fc97..9297fda4 100644
--- a/sdk/python/src/openlit/instrumentation/transformers/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/transformers/__init__.py
@@ -11,6 +11,7 @@ from openlit.instrumentation.transformers.transformers import pipeline_wrapper
_instruments = ("transformers >= 4.48.0",)
+
class TransformersInstrumentor(BaseInstrumentor):
"""
An instrumentor for HuggingFace Transformer library.
@@ -40,7 +41,7 @@ class TransformersInstrumentor(BaseInstrumentor):
pricing_info,
capture_message_content,
metrics,
- disable_metrics
+ disable_metrics,
),
)
diff --git a/sdk/python/src/openlit/instrumentation/transformers/transformers.py b/sdk/python/src/openlit/instrumentation/transformers/transformers.py
index a35866a1..6c600ce0 100644
--- a/sdk/python/src/openlit/instrumentation/transformers/transformers.py
+++ b/sdk/python/src/openlit/instrumentation/transformers/transformers.py
@@ -9,8 +9,16 @@ from openlit.instrumentation.transformers.utils import process_chat_response
from openlit.semcov import SemanticConvention
-def pipeline_wrapper(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+def pipeline_wrapper(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -20,7 +28,9 @@ def pipeline_wrapper(version, environment, application_name, tracer, pricing_inf
Wraps the GenAI function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "127.0.0.1", 80)
+ server_address, server_port = set_server_address_and_port(
+ instance, "127.0.0.1", 80
+ )
request_model = instance.model.config.name_or_path
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
diff --git a/sdk/python/src/openlit/instrumentation/transformers/utils.py b/sdk/python/src/openlit/instrumentation/transformers/utils.py
index 3a8430bd..ebc7156a 100644
--- a/sdk/python/src/openlit/instrumentation/transformers/utils.py
+++ b/sdk/python/src/openlit/instrumentation/transformers/utils.py
@@ -1,6 +1,7 @@
"""
HF Transformers OpenTelemetry instrumentation utility functions
"""
+
import time
from opentelemetry.trace import Status, StatusCode
@@ -13,6 +14,7 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def format_content(content):
"""
Format content to a consistent structure.
@@ -21,8 +23,12 @@ def format_content(content):
return content
elif isinstance(content, list):
# Check if its a list of chat messages (like in the test case)
- if (len(content) > 0 and isinstance(content[0], dict) and
- "role" in content[0] and "content" in content[0]):
+ if (
+ len(content) > 0
+ and isinstance(content[0], dict)
+ and "role" in content[0]
+ and "content" in content[0]
+ ):
# Handle chat message format like Groq
formatted_messages = []
for message in content:
@@ -31,7 +37,7 @@ def format_content(content):
if isinstance(msg_content, list):
content_str = ", ".join(
- f'{item["type"]}: {item["text"] if "text" in item else item.get("image_url", str(item))}'
+ f"{item['type']}: {item['text'] if 'text' in item else item.get('image_url', str(item))}"
if isinstance(item, dict) and "type" in item
else str(item)
for item in msg_content
@@ -58,9 +64,20 @@ def format_content(content):
else:
return str(content)
-def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, args, kwargs, is_stream):
+def common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ args,
+ kwargs,
+ is_stream,
+):
"""
Process chat request and generate Telemetry
"""
@@ -75,31 +92,60 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
cost = get_chat_model_cost(request_model, pricing_info, input_tokens, output_tokens)
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_HUGGING_FACE,
- scope._server_address, scope._server_port, request_model, request_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_HUGGING_FACE,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Set request parameters from forward_params
if forward_params.get("temperature") is not None:
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, forward_params["temperature"])
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, forward_params["temperature"]
+ )
if forward_params.get("top_k") is not None:
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_K, forward_params["top_k"])
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_K, forward_params["top_k"]
+ )
if forward_params.get("top_p") is not None:
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, forward_params["top_p"])
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_P, forward_params["top_p"]
+ )
if forward_params.get("max_length") is not None:
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, forward_params["max_length"])
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, forward_params["max_length"]
+ )
# Set token usage and cost attributes
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Content
if capture_message_content:
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, scope._prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._completion)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_PROMPT, scope._prompt
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._completion
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -119,14 +165,44 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
# Record metrics using the standardized helper function
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
- SemanticConvention.GEN_AI_SYSTEM_HUGGING_FACE, scope._server_address, scope._server_port,
- request_model, request_model, environment, application_name, scope._start_time, scope._end_time,
- cost, input_tokens, output_tokens, scope._tbt, scope._ttft)
-
-def process_chat_response(instance, response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time,
- span, args, kwargs, capture_message_content=False, disable_metrics=False, version="1.0.0"):
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_HUGGING_FACE,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ cost,
+ input_tokens,
+ output_tokens,
+ scope._tbt,
+ scope._ttft,
+ )
+
+
+def process_chat_response(
+ instance,
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ args,
+ kwargs,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+):
"""
Process chat request and generate Telemetry
"""
@@ -146,11 +222,19 @@ def process_chat_response(instance, response, request_model, pricing_info, serve
scope._prompt = args[0]
else:
scope._prompt = (
- kwargs.get("text_inputs") or
- (kwargs.get("image") and kwargs.get("question") and
- ("image: " + kwargs.get("image") + " question:" + kwargs.get("question"))) or
- kwargs.get("fallback") or
- ""
+ kwargs.get("text_inputs")
+ or (
+ kwargs.get("image")
+ and kwargs.get("question")
+ and (
+ "image: "
+ + kwargs.get("image")
+ + " question:"
+ + kwargs.get("question")
+ )
+ )
+ or kwargs.get("fallback")
+ or ""
)
scope._prompt = format_content(scope._prompt)
@@ -175,13 +259,21 @@ def process_chat_response(instance, response, request_model, pricing_info, serve
scope._completion = ""
elif task == "automatic-speech-recognition":
- scope._completion = response.get("text", "") if isinstance(response, dict) else ""
+ scope._completion = (
+ response.get("text", "") if isinstance(response, dict) else ""
+ )
elif task == "image-classification":
- scope._completion = str(response[0]) if isinstance(response, list) and len(response) > 0 else ""
+ scope._completion = (
+ str(response[0]) if isinstance(response, list) and len(response) > 0 else ""
+ )
elif task == "visual-question-answering":
- if isinstance(response, list) and len(response) > 0 and isinstance(response[0], dict):
+ if (
+ isinstance(response, list)
+ and len(response) > 0
+ and isinstance(response[0], dict)
+ ):
scope._completion = response[0].get("answer", "")
else:
scope._completion = ""
@@ -193,7 +285,18 @@ def process_chat_response(instance, response, request_model, pricing_info, serve
scope._tbt = 0
scope._ttft = scope._end_time - scope._start_time
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, args, kwargs, is_stream=False)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ args,
+ kwargs,
+ is_stream=False,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/vertexai/__init__.py b/sdk/python/src/openlit/instrumentation/vertexai/__init__.py
index 5e944282..9cb91a68 100644
--- a/sdk/python/src/openlit/instrumentation/vertexai/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/vertexai/__init__.py
@@ -10,6 +10,7 @@ from openlit.instrumentation.vertexai.async_vertexai import async_send_message
_instruments = ("google-cloud-aiplatform >= 1.38.1",)
+
class VertexAIInstrumentor(BaseInstrumentor):
"""
An instrumentor for VertexAI client library.
@@ -32,60 +33,124 @@ class VertexAIInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"vertexai.generative_models",
"GenerativeModel.generate_content",
- send_message(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ send_message(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"vertexai.generative_models",
"ChatSession.send_message",
- send_message(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ send_message(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# sync language models
wrap_function_wrapper(
"vertexai.language_models",
"ChatSession.send_message",
- send_message(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ send_message(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"vertexai.language_models",
"ChatSession.send_message_streaming",
- send_message(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ send_message(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# async generative models
wrap_function_wrapper(
"vertexai.generative_models",
"GenerativeModel.generate_content_async",
- async_send_message(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_send_message(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"vertexai.generative_models",
"ChatSession.send_message_async",
- async_send_message(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_send_message(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
# async language models
wrap_function_wrapper(
"vertexai.language_models",
"ChatSession.send_message_async",
- async_send_message(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_send_message(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
wrap_function_wrapper(
"vertexai.language_models",
"ChatSession.send_message_streaming_async",
- async_send_message(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ async_send_message(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/vertexai/async_vertexai.py b/sdk/python/src/openlit/instrumentation/vertexai/async_vertexai.py
index 7758d258..b02dbfb8 100644
--- a/sdk/python/src/openlit/instrumentation/vertexai/async_vertexai.py
+++ b/sdk/python/src/openlit/instrumentation/vertexai/async_vertexai.py
@@ -19,8 +19,17 @@ from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def async_send_message(version, environment, application_name, tracer,
- pricing_info, capture_message_content, metrics, disable_metrics):
+
+def async_send_message(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for VertexAI AsyncMessages calls.
"""
@@ -31,16 +40,16 @@ def async_send_message(version, environment, application_name, tracer,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- request_model,
- args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ request_model,
+ args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -88,7 +97,7 @@ def async_send_message(version, environment, application_name, tracer,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
handle_exception(self._span, e)
@@ -108,7 +117,16 @@ def async_send_message(version, environment, application_name, tracer,
awaited_wrapped = await wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedAsyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port, request_model, args)
+ return TracedAsyncStream(
+ awaited_wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ request_model,
+ args,
+ )
else:
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -130,7 +148,7 @@ def async_send_message(version, environment, application_name, tracer,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/vertexai/utils.py b/sdk/python/src/openlit/instrumentation/vertexai/utils.py
index de1328b6..15373c4f 100644
--- a/sdk/python/src/openlit/instrumentation/vertexai/utils.py
+++ b/sdk/python/src/openlit/instrumentation/vertexai/utils.py
@@ -1,6 +1,7 @@
"""
VertexAI OpenTelemetry instrumentation utility functions
"""
+
import time
from opentelemetry.trace import Status, StatusCode
@@ -14,6 +15,7 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def format_content(contents):
"""
Format the VertexAI contents into a string for span events.
@@ -37,7 +39,9 @@ def format_content(contents):
if part.thought:
content_str.append(f"thought: {part.thought}")
if part.code_execution_result:
- content_str.append(f"code_execution_result: {part.code_execution_result}")
+ content_str.append(
+ f"code_execution_result: {part.code_execution_result}"
+ )
if part.executable_code:
content_str.append(f"executable_code: {part.executable_code}")
if part.file_data:
@@ -53,6 +57,7 @@ def format_content(contents):
return "\n".join(formatted_messages)
+
def process_chunk(scope, chunk):
"""
Process a chunk of response data and update state.
@@ -71,8 +76,18 @@ def process_chunk(scope, chunk):
scope._input_tokens = chunk.usage_metadata.prompt_token_count
scope._output_tokens = chunk.usage_metadata.candidates_token_count
-def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream):
+
+def common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream,
+):
"""
Process chat request and generate Telemetry
"""
@@ -86,13 +101,26 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
formatted_messages = format_content(contents)
prompt = formatted_messages or str(scope._args[0][0])
- cost = get_chat_model_cost(scope._request_model, pricing_info, scope._input_tokens, scope._output_tokens)
+ cost = get_chat_model_cost(
+ scope._request_model, pricing_info, scope._input_tokens, scope._output_tokens
+ )
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_VERTEXAI,
- scope._server_address, scope._server_port, scope._request_model, scope._request_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_VERTEXAI,
+ scope._server_address,
+ scope._server_port,
+ scope._request_model,
+ scope._request_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Request parameters (VertexAI-specific)
inference_config = scope._kwargs.get("generation_config", {})
@@ -115,18 +143,30 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
scope._span.set_attribute(attribute, value)
# Span Attributes for Response parameters
- scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text" if isinstance(scope._llmresponse, str) else "json")
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_OUTPUT_TYPE,
+ "text" if isinstance(scope._llmresponse, str) else "json",
+ )
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens + scope._output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,
+ scope._input_tokens + scope._output_tokens,
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Content
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, scope._llmresponse
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -146,23 +186,69 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
# Record metrics
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_VERTEXAI,
- scope._server_address, scope._server_port, scope._request_model, scope._request_model, environment,
- application_name, scope._start_time, scope._end_time, scope._input_tokens, scope._output_tokens,
- cost, scope._tbt, scope._ttft)
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_VERTEXAI,
+ scope._server_address,
+ scope._server_port,
+ scope._request_model,
+ scope._request_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ scope._input_tokens,
+ scope._output_tokens,
+ cost,
+ scope._tbt,
+ scope._ttft,
+ )
+
-def process_streaming_chat_response(scope, pricing_info, environment, application_name, metrics,
- capture_message_content=False, disable_metrics=False, version=""):
+def process_streaming_chat_response(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="",
+):
"""
Process streaming chat response and generate telemetry.
"""
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=True)
-
-def process_chat_response(response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time,
- span, capture_message_content=False, disable_metrics=False, version="1.0.0", **kwargs):
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=True,
+ )
+
+
+def process_chat_response(
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+ **kwargs,
+):
"""
Process non-streaming chat response and generate telemetry.
"""
@@ -182,11 +268,21 @@ def process_chat_response(response, request_model, pricing_info, server_port, se
scope._kwargs = kwargs
scope._args = [kwargs.get("contents", [])]
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=False)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=False,
+ )
return response
+
def extract_vertexai_details(instance):
"""
Extract VertexAI-specific details like location and model name.
diff --git a/sdk/python/src/openlit/instrumentation/vertexai/vertexai.py b/sdk/python/src/openlit/instrumentation/vertexai/vertexai.py
index 87702ee6..bc4028bf 100644
--- a/sdk/python/src/openlit/instrumentation/vertexai/vertexai.py
+++ b/sdk/python/src/openlit/instrumentation/vertexai/vertexai.py
@@ -19,8 +19,17 @@ from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-def send_message(version, environment, application_name, tracer,
- pricing_info, capture_message_content, metrics, disable_metrics):
+
+def send_message(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for VertexAI messages to collect metrics.
"""
@@ -31,16 +40,16 @@ def send_message(version, environment, application_name, tracer,
"""
def __init__(
- self,
- wrapped,
- span,
- span_name,
- kwargs,
- server_address,
- server_port,
- request_model,
- args,
- ):
+ self,
+ wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ request_model,
+ args,
+ ):
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
@@ -88,7 +97,7 @@ def send_message(version, environment, application_name, tracer,
metrics=metrics,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
- version=version
+ version=version,
)
except Exception as e:
handle_exception(self._span, e)
@@ -108,7 +117,16 @@ def send_message(version, environment, application_name, tracer,
awaited_wrapped = wrapped(*args, **kwargs)
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
- return TracedSyncStream(awaited_wrapped, span, span_name, kwargs, server_address, server_port, request_model, args)
+ return TracedSyncStream(
+ awaited_wrapped,
+ span,
+ span_name,
+ kwargs,
+ server_address,
+ server_port,
+ request_model,
+ args,
+ )
else:
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
@@ -130,7 +148,7 @@ def send_message(version, environment, application_name, tracer,
capture_message_content=capture_message_content,
disable_metrics=disable_metrics,
version=version,
- **kwargs
+ **kwargs,
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/vllm/__init__.py b/sdk/python/src/openlit/instrumentation/vllm/__init__.py
index a72f3b1b..eeab327a 100644
--- a/sdk/python/src/openlit/instrumentation/vllm/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/vllm/__init__.py
@@ -5,12 +5,11 @@ import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
-from openlit.instrumentation.vllm.vllm import (
- generate
-)
+from openlit.instrumentation.vllm.vllm import generate
_instruments = ("vllm >= 0.5.4",)
+
class VLLMInstrumentor(BaseInstrumentor):
"""
An instrumentor for vLLM client library.
@@ -33,8 +32,16 @@ class VLLMInstrumentor(BaseInstrumentor):
wrap_function_wrapper(
"vllm.entrypoints.llm",
"LLM.generate",
- generate(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ generate(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+ ),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/vllm/utils.py b/sdk/python/src/openlit/instrumentation/vllm/utils.py
index 7dcf318a..15bdf042 100644
--- a/sdk/python/src/openlit/instrumentation/vllm/utils.py
+++ b/sdk/python/src/openlit/instrumentation/vllm/utils.py
@@ -1,6 +1,7 @@
"""
vLLM OpenTelemetry instrumentation utility functions
"""
+
import time
from opentelemetry.trace import Status, StatusCode
@@ -13,17 +14,19 @@ from openlit.__helpers import (
)
from openlit.semcov import SemanticConvention
+
def get_inference_config(args, kwargs):
"""
Safely extract inference configuration from args or kwargs.
"""
- if 'sampling_params' in kwargs:
- return kwargs['sampling_params']
+ if "sampling_params" in kwargs:
+ return kwargs["sampling_params"]
if len(args) > 1:
return args[1]
return None
+
def format_content(prompts):
"""
Process a list of prompts to extract content.
@@ -36,8 +39,18 @@ def format_content(prompts):
else:
return str(prompts)
-def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream):
+
+def common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream,
+):
"""
Process chat request and generate Telemetry
"""
@@ -60,37 +73,75 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
cost = get_chat_model_cost(request_model, pricing_info, input_tokens, output_tokens)
# Common Span Attributes
- common_span_attributes(scope,
- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_VLLM,
- scope._server_address, scope._server_port, request_model, request_model,
- environment, application_name, is_stream, scope._tbt, scope._ttft, version)
+ common_span_attributes(
+ scope,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_VLLM,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ is_stream,
+ scope._tbt,
+ scope._ttft,
+ version,
+ )
# Span Attributes for Request parameters
inference_config = get_inference_config(scope._args, scope._kwargs)
if inference_config:
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, getattr(inference_config, 'max_tokens', -1))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES, getattr(inference_config, 'stop_sequences', []))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, getattr(inference_config, 'temperature', 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, getattr(inference_config, 'top_p', 1.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_TOP_K, getattr(inference_config, 'top_k', -1))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
- getattr(inference_config, 'presence_penalty', 0.0))
- scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
- getattr(inference_config, 'frequency_penalty', 0.0))
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS,
+ getattr(inference_config, "max_tokens", -1),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_STOP_SEQUENCES,
+ getattr(inference_config, "stop_sequences", []),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TEMPERATURE,
+ getattr(inference_config, "temperature", 1.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_P,
+ getattr(inference_config, "top_p", 1.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_TOP_K,
+ getattr(inference_config, "top_k", -1),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,
+ getattr(inference_config, "presence_penalty", 0.0),
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,
+ getattr(inference_config, "frequency_penalty", 0.0),
+ )
# Span Attributes for Response parameters
scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, "text")
# Span Attributes for Cost and Tokens
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens
+ )
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, input_tokens + output_tokens
+ )
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
# Span Attributes for Content
if capture_message_content:
scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt)
- scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_COMPLETION, completion)
+ scope._span.set_attribute(
+ SemanticConvention.GEN_AI_CONTENT_COMPLETION, completion
+ )
# To be removed once the change to span_attributes (from span events) is complete
scope._span.add_event(
@@ -110,14 +161,44 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
# Metrics
if not disable_metrics:
- record_completion_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT, SemanticConvention.GEN_AI_SYSTEM_VLLM,
- scope._server_address, scope._server_port, request_model, request_model, environment,
- application_name, scope._start_time, scope._end_time, input_tokens, output_tokens,
- cost, scope._tbt, scope._ttft)
-
-def process_chat_response(instance, response, request_model, pricing_info, server_port, server_address,
- environment, application_name, metrics, start_time, span, args, kwargs,
- capture_message_content=False, disable_metrics=False, version="1.0.0"):
+ record_completion_metrics(
+ metrics,
+ SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,
+ SemanticConvention.GEN_AI_SYSTEM_VLLM,
+ scope._server_address,
+ scope._server_port,
+ request_model,
+ request_model,
+ environment,
+ application_name,
+ scope._start_time,
+ scope._end_time,
+ input_tokens,
+ output_tokens,
+ cost,
+ scope._tbt,
+ scope._ttft,
+ )
+
+
+def process_chat_response(
+ instance,
+ response,
+ request_model,
+ pricing_info,
+ server_port,
+ server_address,
+ environment,
+ application_name,
+ metrics,
+ start_time,
+ span,
+ args,
+ kwargs,
+ capture_message_content=False,
+ disable_metrics=False,
+ version="1.0.0",
+):
"""
Process chat request and generate Telemetry
"""
@@ -137,7 +218,16 @@ def process_chat_response(instance, response, request_model, pricing_info, serve
scope._args = args
scope._kwargs = kwargs
- common_chat_logic(scope, pricing_info, environment, application_name, metrics,
- capture_message_content, disable_metrics, version, is_stream=False)
+ common_chat_logic(
+ scope,
+ pricing_info,
+ environment,
+ application_name,
+ metrics,
+ capture_message_content,
+ disable_metrics,
+ version,
+ is_stream=False,
+ )
return response
diff --git a/sdk/python/src/openlit/instrumentation/vllm/vllm.py b/sdk/python/src/openlit/instrumentation/vllm/vllm.py
index 16be3ae8..9d75884c 100644
--- a/sdk/python/src/openlit/instrumentation/vllm/vllm.py
+++ b/sdk/python/src/openlit/instrumentation/vllm/vllm.py
@@ -4,17 +4,21 @@ Module for monitoring vLLM API calls.
import time
from opentelemetry.trace import SpanKind
-from openlit.__helpers import (
- handle_exception,
- set_server_address_and_port
-)
-from openlit.instrumentation.vllm.utils import (
- process_chat_response
-)
+from openlit.__helpers import handle_exception, set_server_address_and_port
+from openlit.instrumentation.vllm.utils import process_chat_response
from openlit.semcov import SemanticConvention
-def generate(version, environment, application_name, tracer, pricing_info,
- capture_message_content, metrics, disable_metrics):
+
+def generate(
+ version,
+ environment,
+ application_name,
+ tracer,
+ pricing_info,
+ capture_message_content,
+ metrics,
+ disable_metrics,
+):
"""
Generates a telemetry wrapper for GenAI function call
"""
@@ -23,7 +27,9 @@ def generate(version, environment, application_name, tracer, pricing_info,
"""
Wraps the GenAI function call.
"""
- server_address, server_port = set_server_address_and_port(instance, "http://127.0.0.1", 443)
+ server_address, server_port = set_server_address_and_port(
+ instance, "http://127.0.0.1", 443
+ )
request_model = instance.llm_engine.model_config.model or "facebook/opt-125m"
span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
diff --git a/sdk/python/src/openlit/otel/events.py b/sdk/python/src/openlit/otel/events.py
index 1eb782b2..820389b8 100644
--- a/sdk/python/src/openlit/otel/events.py
+++ b/sdk/python/src/openlit/otel/events.py
@@ -4,10 +4,17 @@ Setups up OpenTelemetry events emitter
import os
from opentelemetry import _events, _logs
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk._events import EventLoggerProvider
-from opentelemetry.sdk._logs.export import BatchLogRecordProcessor, SimpleLogRecordProcessor
+from opentelemetry.sdk._logs.export import (
+ BatchLogRecordProcessor,
+ SimpleLogRecordProcessor,
+)
from opentelemetry.sdk._logs import LoggerProvider
from opentelemetry.sdk._logs.export import ConsoleLogExporter
@@ -19,7 +26,15 @@ else:
# Global flag to check if the events provider initialization is complete.
EVENTS_SET = False
-def setup_events(application_name, environment, event_logger, otlp_endpoint, otlp_headers, disable_batch):
+
+def setup_events(
+ application_name,
+ environment,
+ event_logger,
+ otlp_endpoint,
+ otlp_headers,
+ disable_batch,
+):
"""Setup OpenTelemetry events with the given configuration.
Args:
@@ -42,10 +57,12 @@ def setup_events(application_name, environment, event_logger, otlp_endpoint, otl
try:
if not EVENTS_SET:
# Create resource with service and environment information
- resource = Resource.create(attributes={
- SERVICE_NAME: application_name,
- DEPLOYMENT_ENVIRONMENT: environment,
- TELEMETRY_SDK_NAME: "openlit"}
+ resource = Resource.create(
+ attributes={
+ SERVICE_NAME: application_name,
+ DEPLOYMENT_ENVIRONMENT: environment,
+ TELEMETRY_SDK_NAME: "openlit",
+ }
)
# Initialize the LoggerProvider with the created resource.
@@ -57,7 +74,9 @@ def setup_events(application_name, environment, event_logger, otlp_endpoint, otl
if otlp_headers is not None:
if isinstance(otlp_headers, dict):
- headers_str = ','.join(f"{key}={value}" for key, value in otlp_headers.items())
+ headers_str = ",".join(
+ f"{key}={value}" for key, value in otlp_headers.items()
+ )
else:
headers_str = otlp_headers
@@ -67,10 +86,16 @@ def setup_events(application_name, environment, event_logger, otlp_endpoint, otl
if os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT"):
event_exporter = OTLPLogExporter()
# pylint: disable=line-too-long
- logger_provider.add_log_record_processor(SimpleLogRecordProcessor(event_exporter)) if disable_batch else logger_provider.add_log_record_processor(BatchLogRecordProcessor(event_exporter))
+ logger_provider.add_log_record_processor(
+ SimpleLogRecordProcessor(event_exporter)
+ ) if disable_batch else logger_provider.add_log_record_processor(
+ BatchLogRecordProcessor(event_exporter)
+ )
else:
event_exporter = ConsoleLogExporter()
- logger_provider.add_log_record_processor(SimpleLogRecordProcessor(event_exporter))
+ logger_provider.add_log_record_processor(
+ SimpleLogRecordProcessor(event_exporter)
+ )
_logs.set_logger_provider(logger_provider)
event_provider = EventLoggerProvider()
diff --git a/sdk/python/src/openlit/otel/metrics.py b/sdk/python/src/openlit/otel/metrics.py
index 9f24e00a..4838f1a9 100644
--- a/sdk/python/src/openlit/otel/metrics.py
+++ b/sdk/python/src/openlit/otel/metrics.py
@@ -2,33 +2,35 @@
"""
Setups up OpenTelemetry Meter
"""
+
import os
from opentelemetry import metrics
from opentelemetry.sdk.metrics import MeterProvider
-from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader, ConsoleMetricExporter
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.metrics.export import (
+ PeriodicExportingMetricReader,
+ ConsoleMetricExporter,
+)
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from opentelemetry.sdk.resources import Resource
from openlit.semcov import SemanticConvention
if os.environ.get("OTEL_EXPORTER_OTLP_PROTOCOL") == "grpc":
- from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter
+ from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import (
+ OTLPMetricExporter,
+ )
else:
- from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
+ from opentelemetry.exporter.otlp.proto.http.metric_exporter import (
+ OTLPMetricExporter,
+ )
# Global flag to check if the meter provider initialization is complete.
METER_SET = False
-_DB_CLIENT_OPERATION_DURATION_BUCKETS = [
- 0.001,
- 0.005,
- 0.01,
- 0.05,
- 0.1,
- 0.5,
- 1,
- 5,
- 10
-]
+_DB_CLIENT_OPERATION_DURATION_BUCKETS = [0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10]
_GEN_AI_CLIENT_OPERATION_DURATION_BUCKETS = [
0.01,
@@ -60,7 +62,7 @@ _GEN_AI_SERVER_TBT = [
0.5,
0.75,
1.0,
- 2.5
+ 2.5,
]
_GEN_AI_SERVER_TFTT = [
@@ -79,7 +81,7 @@ _GEN_AI_SERVER_TFTT = [
2.5,
5.0,
7.5,
- 10.0
+ 10.0,
]
_GEN_AI_CLIENT_TOKEN_USAGE_BUCKETS = [
@@ -99,6 +101,7 @@ _GEN_AI_CLIENT_TOKEN_USAGE_BUCKETS = [
67108864,
]
+
def setup_meter(application_name, environment, meter, otlp_endpoint, otlp_headers):
"""
Sets up OpenTelemetry metrics with a counter for total requests.
@@ -118,10 +121,12 @@ def setup_meter(application_name, environment, meter, otlp_endpoint, otlp_header
try:
if meter is None and not METER_SET:
# Create a resource with the service name attribute.
- resource = Resource.create(attributes={
- SERVICE_NAME: application_name,
- DEPLOYMENT_ENVIRONMENT: environment,
- TELEMETRY_SDK_NAME: "openlit"}
+ resource = Resource.create(
+ attributes={
+ SERVICE_NAME: application_name,
+ DEPLOYMENT_ENVIRONMENT: environment,
+ TELEMETRY_SDK_NAME: "openlit",
+ }
)
# Only set environment variables if you have a non-None value.
@@ -130,7 +135,9 @@ def setup_meter(application_name, environment, meter, otlp_endpoint, otlp_header
if otlp_headers is not None:
if isinstance(otlp_headers, dict):
- headers_str = ','.join(f"{key}={value}" for key, value in otlp_headers.items())
+ headers_str = ",".join(
+ f"{key}={value}" for key, value in otlp_headers.items()
+ )
else:
headers_str = otlp_headers
# Now, we have either converted the dict to a string or used the provided string.
@@ -144,7 +151,9 @@ def setup_meter(application_name, environment, meter, otlp_endpoint, otlp_header
metric_reader = PeriodicExportingMetricReader(metric_exporter)
- meter_provider = MeterProvider(resource=resource, metric_readers=[metric_reader])
+ meter_provider = MeterProvider(
+ resource=resource, metric_readers=[metric_reader]
+ )
metrics.set_meter_provider(meter_provider)
@@ -185,7 +194,6 @@ def setup_meter(application_name, environment, meter, otlp_endpoint, otlp_header
unit="s",
explicit_bucket_boundaries_advisory=_DB_CLIENT_OPERATION_DURATION_BUCKETS,
),
-
# Extra
"genai_requests": meter.create_counter(
name=SemanticConvention.GEN_AI_REQUESTS,
diff --git a/sdk/python/src/openlit/otel/tracing.py b/sdk/python/src/openlit/otel/tracing.py
index a2aa9345..1dfb0356 100644
--- a/sdk/python/src/openlit/otel/tracing.py
+++ b/sdk/python/src/openlit/otel/tracing.py
@@ -4,7 +4,11 @@ Setups up OpenTelemetry tracer
import os
from opentelemetry import trace
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.sdk.resources import (
+ SERVICE_NAME,
+ TELEMETRY_SDK_NAME,
+ DEPLOYMENT_ENVIRONMENT,
+)
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, SimpleSpanProcessor
@@ -18,7 +22,10 @@ else:
# Global flag to check if the tracer provider initialization is complete.
TRACER_SET = False
-def setup_tracing(application_name, environment, tracer, otlp_endpoint, otlp_headers, disable_batch):
+
+def setup_tracing(
+ application_name, environment, tracer, otlp_endpoint, otlp_headers, disable_batch
+):
"""
Sets up tracing with OpenTelemetry.
Initializes the tracer provider and configures the span processor and exporter.
@@ -33,15 +40,17 @@ def setup_tracing(application_name, environment, tracer, otlp_endpoint, otlp_hea
global TRACER_SET
try:
- #Disable Haystack Auto Tracing
+ # Disable Haystack Auto Tracing
os.environ["HAYSTACK_AUTO_TRACE_ENABLED"] = "false"
if not TRACER_SET:
# Create a resource with the service name attribute.
- resource = Resource.create(attributes={
- SERVICE_NAME: application_name,
- DEPLOYMENT_ENVIRONMENT: environment,
- TELEMETRY_SDK_NAME: "openlit"}
+ resource = Resource.create(
+ attributes={
+ SERVICE_NAME: application_name,
+ DEPLOYMENT_ENVIRONMENT: environment,
+ TELEMETRY_SDK_NAME: "openlit",
+ }
)
# Initialize the TracerProvider with the created resource.
@@ -53,7 +62,9 @@ def setup_tracing(application_name, environment, tracer, otlp_endpoint, otlp_hea
if otlp_headers is not None:
if isinstance(otlp_headers, dict):
- headers_str = ','.join(f"{key}={value}" for key, value in otlp_headers.items())
+ headers_str = ",".join(
+ f"{key}={value}" for key, value in otlp_headers.items()
+ )
else:
headers_str = otlp_headers
@@ -63,7 +74,11 @@ def setup_tracing(application_name, environment, tracer, otlp_endpoint, otlp_hea
if os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT"):
span_exporter = OTLPSpanExporter()
# pylint: disable=line-too-long
- span_processor = BatchSpanProcessor(span_exporter) if not disable_batch else SimpleSpanProcessor(span_exporter)
+ span_processor = (
+ BatchSpanProcessor(span_exporter)
+ if not disable_batch
+ else SimpleSpanProcessor(span_exporter)
+ )
else:
span_exporter = ConsoleSpanExporter()
span_processor = SimpleSpanProcessor(span_exporter)
diff --git a/sdk/python/src/openlit/semcov/__init__.py b/sdk/python/src/openlit/semcov/__init__.py
index 23a4d30d..6595437a 100644
--- a/sdk/python/src/openlit/semcov/__init__.py
+++ b/sdk/python/src/openlit/semcov/__init__.py
@@ -6,6 +6,8 @@ intended for use across different components of AI applications, including reque
response processing, usage metrics, and interaction with vector databases and AI systems.
The purpose is to standardize the semantics for easier integration, analytics, and maintenance.
"""
+
+
class SemanticConvention:
"""
The SemanticConvention class provides a centralized repository of constant values that
@@ -155,9 +157,15 @@ class SemanticConvention:
GEN_AI_FRAMEWORK_PIPELINE_INPUT_DATA = "gen_ai.framework.pipeline.input_data"
GEN_AI_FRAMEWORK_PIPELINE_OUTPUT_DATA = "gen_ai.framework.pipeline.output_data"
GEN_AI_FRAMEWORK_PIPELINE_METADATA = "gen_ai.framework.pipeline.metadata"
- GEN_AI_FRAMEWORK_PIPELINE_MAX_RUNS = "gen_ai.framework.pipeline.max_runs_per_component"
- GEN_AI_FRAMEWORK_PIPELINE_COMPONENT_COUNT = "gen_ai.framework.pipeline.component_count"
- GEN_AI_FRAMEWORK_PIPELINE_EXECUTION_TIME = "gen_ai.framework.pipeline.execution_time"
+ GEN_AI_FRAMEWORK_PIPELINE_MAX_RUNS = (
+ "gen_ai.framework.pipeline.max_runs_per_component"
+ )
+ GEN_AI_FRAMEWORK_PIPELINE_COMPONENT_COUNT = (
+ "gen_ai.framework.pipeline.component_count"
+ )
+ GEN_AI_FRAMEWORK_PIPELINE_EXECUTION_TIME = (
+ "gen_ai.framework.pipeline.execution_time"
+ )
# GenAI Request Attributes (Extra)
GEN_AI_REQUEST_IS_STREAM = "gen_ai.request.is_stream"
@@ -220,7 +228,6 @@ class SemanticConvention:
DB_QUERY_SUMMARY = "db.query.summary"
DB_RESPONSE_RETURNED_ROWS = "db.response.returned_rows"
-
# Vector DB Attributes (Extras)
DB_SDK_VERSION = "db.sdk.version"
DB_OPERATION_API_ENDPOINT = "db.operation.api_endpoint"
@@ -286,7 +293,6 @@ class SemanticConvention:
GEN_AI_AGENT_NAME = "gen_ai.agent.name"
GEN_AI_AGENT_DESCRIPTION = "gen_ai.agent.description"
-
GEN_AI_AGENT_TYPE = "gen_ai.agent.type"
GEN_AI_AGENT_TASK_ID = "gen_ai.agent.task.id"
GEN_AI_AGENT_ROLE = "gen_ai.agent.role"
@@ -405,6 +411,64 @@ class SemanticConvention:
GEN_AI_FRAMEWORK_INDEX_NAME = "gen_ai.framework.index.name"
GEN_AI_FRAMEWORK_INDEX_TYPE = "gen_ai.framework.index.type"
+ # === GENERAL FRAMEWORK SEMANTIC CONVENTIONS (reusable across frameworks) ===
+
+ # Framework tracing attributes (general, reusable across frameworks)
+ GEN_AI_FRAMEWORK_TAGS = "gen_ai.framework.tags"
+
+ # Framework performance tracking (general)
+ GEN_AI_FRAMEWORK_PERFORMANCE_VS_BASELINE = (
+ "gen_ai.framework.performance.vs_baseline"
+ )
+ GEN_AI_FRAMEWORK_PERFORMANCE_BASELINE_AVG = (
+ "gen_ai.framework.performance.baseline_avg"
+ )
+ GEN_AI_FRAMEWORK_PERFORMANCE_BASELINE_PERCENTILE = (
+ "gen_ai.framework.performance.baseline_percentile"
+ )
+
+ # Framework error classification (general)
+ GEN_AI_FRAMEWORK_ERROR_CLASS = "gen_ai.framework.error.class"
+ GEN_AI_FRAMEWORK_ERROR_TYPE = "gen_ai.framework.error.type"
+ GEN_AI_FRAMEWORK_ERROR_MESSAGE = "gen_ai.framework.error.message"
+
+ # Workflow attributes (general, reusable)
+ GEN_AI_WORKFLOW_TYPE = "gen_ai.workflow.type"
+ GEN_AI_WORKFLOW_INPUT = "gen_ai.workflow.input"
+ GEN_AI_WORKFLOW_OUTPUT = "gen_ai.workflow.output"
+
+ # Serialized function information (general, reusable)
+ GEN_AI_SERIALIZED_NAME = "gen_ai.serialized.name"
+ GEN_AI_SERIALIZED_SIGNATURE = "gen_ai.serialized.signature"
+ GEN_AI_SERIALIZED_DOC = "gen_ai.serialized.doc"
+ GEN_AI_SERIALIZED_MODULE = "gen_ai.serialized.module"
+
+ # Tool operation attributes (general, reusable)
+ GEN_AI_TOOL_INPUT = "gen_ai.tool.input"
+ GEN_AI_TOOL_OUTPUT = "gen_ai.tool.output"
+
+ # Retrieval operation attributes (general, reusable)
+ GEN_AI_RETRIEVAL_QUERY = "gen_ai.retrieval.query"
+ GEN_AI_RETRIEVAL_DOCUMENT_COUNT = "gen_ai.retrieval.document_count"
+ GEN_AI_RETRIEVAL_DOCUMENTS = "gen_ai.retrieval.documents"
+
+ # Provider information (general, reusable)
+ GEN_AI_REQUEST_PROVIDER = "gen_ai.request.provider"
+
+ # Enhanced token details (general, reusable across providers)
+ GEN_AI_USAGE_COMPLETION_TOKENS_DETAILS_AUDIO = (
+ "gen_ai.usage.completion_tokens_details.audio"
+ )
+ GEN_AI_USAGE_COMPLETION_TOKENS_DETAILS_REASONING = (
+ "gen_ai.usage.completion_tokens_details.reasoning"
+ )
+ GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_READ = (
+ "gen_ai.usage.prompt_tokens_details.cache_read"
+ )
+ GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_WRITE = (
+ "gen_ai.usage.prompt_tokens_details.cache_write"
+ )
+
# === STANDARD OPENTELEMETRY SEMANTIC CONVENTIONS ===
# These are framework-agnostic and reusable across all agent frameworks
@@ -415,7 +479,9 @@ class SemanticConvention:
GEN_AI_OPENAI_RUN_ID = "gen_ai.openai.run.id"
GEN_AI_OPENAI_REQUEST_SERVICE_TIER = "gen_ai.openai.request.service_tier"
GEN_AI_OPENAI_RESPONSE_SERVICE_TIER = "gen_ai.openai.response.service_tier"
- GEN_AI_OPENAI_RESPONSE_SYSTEM_FINGERPRINT = "gen_ai.openai.response.system_fingerprint"
+ GEN_AI_OPENAI_RESPONSE_SYSTEM_FINGERPRINT = (
+ "gen_ai.openai.response.system_fingerprint"
+ )
# Data Source Attributes (for RAG and knowledge retrieval)
GEN_AI_DATA_SOURCE_ID = "gen_ai.data_source.id"