Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Added logging and distributed tracing with opentelemetry #108

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions aisuite/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
import importlib
import os
import functools
import logging
import time
from opentelemetry import trace


# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


class LLMError(Exception):
Expand All @@ -13,6 +21,37 @@ def __init__(self, message):


class Provider(ABC):
tracer = trace.get_tracer(__name__)

def log_request(self, method_name, **kwargs):
"""Log details of the API call."""
logger.info(f"API call to {method_name} with parameters: {kwargs}")

def log_error(self, method_name, error):
"""Log errors for debugging."""
logger.error(f"Error in {method_name}: {error}")

def trace_execution(self, method_name):
"""Wrap a method with tracing."""

def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
with self.tracer.start_as_current_span(method_name):
start_time = time.time()
try:
result = func(*args, **kwargs)
latency = time.time() - start_time
logger.info(f"{method_name} executed in {latency:.2f}s")
return result
except Exception as e:
self.log_error(method_name, e)
raise e

return wrapper

return decorator

@abstractmethod
def chat_completions_create(self, model, messages):
"""Abstract method for chat completion calls, to be implemented by each provider."""
Expand Down