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

Enable logging #229

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions portkey_ai/api_resources/client.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from __future__ import annotations

import sys
from typing import List, Mapping, Optional, Union
import httpx
import sentry_sdk
import sentry_sdk.integrations
import sentry_sdk.integrations.excepthook
from sentry_sdk.integrations.asyncio import AsyncioIntegration
from portkey_ai.api_resources import apis
from portkey_ai.api_resources.base_client import APIClient, AsyncAPIClient

# from openai import AsyncOpenAI, OpenAI
from portkey_ai.api_resources.utils import enable_logging
from portkey_ai.version import VERSION
from .._vendor.openai import OpenAI, AsyncOpenAI
from portkey_ai.api_resources.global_constants import (
OPEN_AI_API_KEY,
SENTRY_DSN,
SENTRY_ENVIRONMENT,
)


Expand Down Expand Up @@ -117,6 +124,19 @@ def __init__(
**kwargs,
)

if enable_logging():
sentry_sdk.init(
dsn=SENTRY_DSN,
environment=SENTRY_ENVIRONMENT,
release=VERSION,
integrations=[
sentry_sdk.integrations.excepthook.ExcepthookIntegration(
always_run=True
),
],
)
self._set_global_exception_handler()

self.openai_client = OpenAI(
api_key=OPEN_AI_API_KEY,
base_url=self.base_url,
Expand Down Expand Up @@ -221,6 +241,13 @@ def copy(
**kwargs,
)

def _set_global_exception_handler(self):
def global_exception_handler(exctype, value, traceback):
sentry_sdk.capture_exception(value)
sys.__excepthook__(exctype, value, traceback)

sys.excepthook = global_exception_handler

def post(self, url: str, **kwargs):
return apis.Post(self).create(url=url, **kwargs)

Expand Down Expand Up @@ -332,6 +359,17 @@ def __init__(
**kwargs,
)

if enable_logging():
sentry_sdk.init(
dsn=SENTRY_DSN,
environment=SENTRY_ENVIRONMENT,
release=VERSION,
integrations=[
AsyncioIntegration(),
],
)
self._set_global_exception_handler()

self.openai_client = AsyncOpenAI(
api_key=OPEN_AI_API_KEY,
base_url=self.base_url,
Expand Down Expand Up @@ -436,6 +474,17 @@ def copy(
**kwargs,
)

def _set_global_exception_handler(self):
import asyncio

def global_exception_handler(loop, context):
exception = context.get("exception")
if exception:
sentry_sdk.capture_exception(exception)
loop.default_exception_handler(context)

asyncio.get_event_loop().set_exception_handler(global_exception_handler)

async def post(self, url: str, **kwargs):
return await apis.AsyncPost(self).create(url=url, **kwargs)

Expand Down
4 changes: 4 additions & 0 deletions portkey_ai/api_resources/global_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@
PORTKEY_API_KEY_ENV = "PORTKEY_API_KEY"
PORTKEY_PROXY_ENV = "PORTKEY_PROXY"
OPEN_AI_API_KEY = "DUMMY-KEY"

ENABLE_LOGGING = "ENABLE_LOGGING"
SENTRY_DSN = "https://e3110dbfe36a48d7ab1964a4cea081ba@o4505023873220608.ingest.us.sentry.io/4505089620967424"
SENTRY_ENVIRONMENT = "production"
5 changes: 5 additions & 0 deletions portkey_ai/api_resources/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
InternalServerError,
)
from .global_constants import (
ENABLE_LOGGING,
MISSING_API_KEY_ERROR_MESSAGE,
MISSING_BASE_URL,
MISSING_MODE_MESSAGE,
Expand Down Expand Up @@ -480,3 +481,7 @@ def parse_headers_generic(headers: Optional[httpx.Headers]) -> dict:
_headers[k] = v

return _headers


def enable_logging() -> bool:
return os.environ.get(ENABLE_LOGGING, "true").lower() == "true"
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ install_requires =
tqdm>4
types-requests
jiter<1,>=0.4.0
sentry-sdk

[options.entry_points]
console_scripts =
Expand Down
Loading