Skip to content

Commit

Permalink
remove terminal prints, removed debug.py, make logs 'more' uniform
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-agenta committed Nov 21, 2024
1 parent 26882d0 commit 8a882ef
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 161 deletions.
60 changes: 9 additions & 51 deletions agenta-cli/agenta/sdk/decorators/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ def __init__(
_MIDDLEWARES = False

except: # pylint: disable=bare-except
log.error("------------------------------------")
log.error("Agenta SDK - failed to secure route: %s", route_path)
log.error("------------------------------------")
log.warning("Agenta SDK - failed to secure route: %s", route_path)
### --- Update Middleware --- #

DEFAULT_PATH = "generate"
Expand Down Expand Up @@ -354,9 +352,7 @@ async def execute_function(
*args,
**func_params,
):
log.info("---------------------------")
log.info(f"Agenta SDK - running route: {repr(self.route_path or '/')}")
log.info("---------------------------")
log.info("Agenta SDK - handling route: %s", repr(self.route_path or "/"))

tracing_context.set(routing_context.get())

Expand All @@ -382,28 +378,20 @@ async def handle_success(self, result: Any, inline_trace: bool):
if inline_trace:
trace = await self.fetch_inline_trace(inline_trace)

log.info(f"----------------------------------")
log.info(f"Agenta SDK - exiting with success: 200")
log.info(f"----------------------------------")

return BaseResponse(data=data, trace=trace)

def handle_failure(self, error: Exception):
log.error("--------------------------------------------------")
log.error("Agenta SDK - handling application exception below:")
log.error("--------------------------------------------------")
log.error(format_exc().strip("\n"))
log.error("--------------------------------------------------")
log.warning("--------------------------------------------------")
log.warning("Agenta SDK - handling application exception below:")
log.warning("--------------------------------------------------")
log.warning(format_exc().strip("\n"))
log.warning("--------------------------------------------------")

status_code = error.status_code if hasattr(error, "status_code") else 500
message = str(error)
stacktrace = format_exception(error, value=error, tb=error.__traceback__) # type: ignore
detail = {"message": message, "stacktrace": stacktrace}

log.error(f"----------------------------------")
log.error(f"Agenta SDK - exiting with failure: {status_code}")
log.error(f"----------------------------------")

raise HTTPException(status_code=status_code, detail=detail)

def patch_result(self, result: Any):
Expand Down Expand Up @@ -680,45 +668,15 @@ def handle_terminal_run(

loop = get_event_loop()

with routing_context_manager(
config=args_config_params,
environment="terminal",
):
result = loop.run_until_complete(
with routing_context_manager(config=args_config_params):
loop.run_until_complete(
self.execute_function(
func,
True, # inline trace: True
**{"params": args_func_params, "config_params": args_config_params},
)
)

SHOW_DETAILS = True
SHOW_DATA = False
SHOW_TRACE = False

if result.trace:
log.info("\n========= Result =========\n")

log.info(f"trace_id: {result.trace['trace_id']}")
if SHOW_DETAILS:
log.info(f"latency: {result.trace.get('latency')}")
log.info(f"cost: {result.trace.get('cost')}")
log.info(f"usage: {list(result.trace.get('usage', {}).values())}")

if SHOW_DATA:
log.info(" ")
log.info(f"data:")
log.info(dumps(result.data, indent=2))

if SHOW_TRACE:
log.info(" ")
log.info(f"trace:")
log.info(f"----------------")
log.info(dumps(result.trace.get("spans", []), indent=2))
log.info(f"----------------")

log.info("\n==========================\n")

def override_config_in_schema(
self,
openapi_schema: dict,
Expand Down
14 changes: 7 additions & 7 deletions agenta-cli/agenta/sdk/litellm/litellm.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def log_pre_api_call(
)

if not self.span:
log.error("LiteLLM callback error: span not found.")
log.warning("Agenta SDK - litellm tracing failed")
return

self.span.set_attributes(
Expand All @@ -86,7 +86,7 @@ def log_stream_event(
end_time,
):
if not self.span:
log.error("LiteLLM callback error: span not found.")
log.warning("Agenta SDK - litellm tracing failed")
return

try:
Expand Down Expand Up @@ -132,7 +132,7 @@ def log_success_event(
end_time,
):
if not self.span:
log.error("LiteLLM callback error: span not found.")
log.warning("Agenta SDK - litellm tracing failed")
return

try:
Expand Down Expand Up @@ -178,7 +178,7 @@ def log_failure_event(
end_time,
):
if not self.span:
log.error("LiteLLM callback error: span not found.")
log.warning("Agenta SDK - litellm tracing failed")
return

self.span.record_exception(kwargs["exception"])
Expand All @@ -195,7 +195,7 @@ async def async_log_stream_event(
end_time,
):
if not self.span:
log.error("LiteLLM callback error: span not found.")
log.warning("Agenta SDK - litellm tracing failed")
return

try:
Expand Down Expand Up @@ -241,7 +241,7 @@ async def async_log_success_event(
end_time,
):
if not self.span:
log.error("LiteLLM callback error: span not found.")
log.warning("Agenta SDK - litellm tracing failed")
return

try:
Expand Down Expand Up @@ -287,7 +287,7 @@ async def async_log_failure_event(
end_time,
):
if not self.span:
log.error("LiteLLM callback error: span not found.")
log.warning("Agenta SDK - litellm tracing failed")
return

self.span.record_exception(kwargs["exception"])
Expand Down
10 changes: 5 additions & 5 deletions agenta-cli/agenta/sdk/middleware/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ async def dispatch(
return await call_next(request)

except: # pylint: disable=bare-except
log.error("------------------------------------------------------")
log.error("Agenta SDK - handling auth middleware exception below:")
log.error("------------------------------------------------------")
log.error(format_exc().strip("\n"))
log.error("------------------------------------------------------")
log.warning("------------------------------------------------------")
log.warning("Agenta SDK - handling auth middleware exception below:")
log.warning("------------------------------------------------------")
log.warning(format_exc().strip("\n"))
log.warning("------------------------------------------------------")

return Deny()
12 changes: 6 additions & 6 deletions agenta-cli/agenta/sdk/tracing/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def tracing_context_manager():
token = tracing_context.set(_tracing_context)
try:
yield
except Exception as e:
log.error("----------------------------------------------")
log.error("Agenta SDK - handling tracing exception below:")
log.error("----------------------------------------------")
log.error(format_exc().strip("\n"))
log.error("----------------------------------------------")
except: # pylint: disable=bare-except
log.warning("----------------------------------------------")
log.warning("Agenta SDK - handling tracing exception below:")
log.warning("----------------------------------------------")
log.warning(format_exc().strip("\n"))
log.warning("----------------------------------------------")
finally:
tracing_context.reset(token)
4 changes: 1 addition & 3 deletions agenta-cli/agenta/sdk/tracing/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ def force_flush(
ret = super().force_flush(timeout_millis)

if not ret:
log.error("--------------------------------------------")
log.error("Agenta SDK - skipping export due to timeout.")
log.error("--------------------------------------------")
log.warning("Agenta SDK - skipping export due to timeout.")

def is_ready(
self,
Expand Down
13 changes: 1 addition & 12 deletions agenta-cli/agenta/sdk/tracing/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,6 @@ def configure(
self.tracer_provider.add_span_processor(self.inline)
# TRACE PROCESSORS -- OTLP
try:
log.info("--------------------------------------------")
log.info(
"Agenta SDK - connecting to otlp receiver at: %s",
self.otlp_url,
)
log.info("--------------------------------------------")

check(
self.otlp_url,
headers=self.headers,
Expand All @@ -107,12 +100,8 @@ def configure(

self.tracer_provider.add_span_processor(_otlp)

log.info("Success: traces will be exported.")
log.info("--------------------------------------------")

except: # pylint: disable=bare-except
log.warning("Failure: traces will not be exported.")
log.warning("--------------------------------------------")
log.warning("Agenta SDK - traces will not be exported.")

# GLOBAL TRACER PROVIDER -- INSTRUMENTATION LIBRARIES
set_tracer_provider(self.tracer_provider)
Expand Down
68 changes: 0 additions & 68 deletions agenta-cli/agenta/sdk/utils/debug.py

This file was deleted.

24 changes: 15 additions & 9 deletions agenta-cli/agenta/sdk/utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ def __exit__(self, exc_type, exc_value, exc_tb):
if exc_type is None:
return True
else:
log.error("-------------------------------------------------")
log.error("Agenta SDK - suppressing tracing exception below:")
log.error("-------------------------------------------------")
log.error(format_exc().strip("\n"))
log.error("-------------------------------------------------")
log.warning("-------------------------------------------------")
log.warning("Agenta SDK - suppressing tracing exception below:")
log.warning("-------------------------------------------------")
log.warning(format_exc().strip("\n"))
log.warning("-------------------------------------------------")
return True


Expand All @@ -34,17 +34,23 @@ async def async_wrapper(*args, **kwargs):
try:
return await func(*args, **kwargs)
except Exception as e:
log.error("--- HANDLING EXCEPTION ---")
log.error("--------------------------")
log.warning("------------------------------------------")
log.warning("Agenta SDK - intercepting exception below:")
log.warning("------------------------------------------")
log.warning(format_exc().strip("\n"))
log.warning("------------------------------------------")
raise e

@wraps(func)
def sync_wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
log.error("--- HANDLING EXCEPTION ---")
log.error("--------------------------")
log.warning("------------------------------------------")
log.warning("Agenta SDK - intercepting exception below:")
log.warning("------------------------------------------")
log.warning(format_exc().strip("\n"))
log.warning("------------------------------------------")
raise e

return async_wrapper if is_coroutine_function else sync_wrapper
Expand Down

0 comments on commit 8a882ef

Please sign in to comment.