Skip to content

Commit

Permalink
Merge pull request #2286 from Agenta-AI/chore/silence-tracing-output
Browse files Browse the repository at this point in the history
[Fix] verbosity in tracing
  • Loading branch information
mmabrouk authored Nov 25, 2024
2 parents 32dfe6e + 8b887e8 commit 89093b1
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 156 deletions.
13 changes: 4 additions & 9 deletions agenta-cli/agenta/sdk/agenta_init.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
import toml
from os import getenv
from typing import Optional, Callable, Any
Expand All @@ -11,10 +10,6 @@
from agenta.client.exceptions import APIRequestError


logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


class AgentaSingleton:
"""Singleton class to save all the "global variables" for the sdk."""

Expand Down Expand Up @@ -164,7 +159,7 @@ def default(self, overwrite=False, **kwargs):
try:
self.push(config_name="default", overwrite=overwrite, **kwargs)
except Exception as ex:
logger.warning(
log.warning(
"Unable to push the default configuration to the server. %s", str(ex)
)

Expand All @@ -185,7 +180,7 @@ def push(self, config_name: str, overwrite=True, **kwargs):
overwrite=overwrite,
)
except Exception as ex:
logger.warning(
log.warning(
"Failed to push the configuration to the server with error: %s", ex
)

Expand All @@ -212,14 +207,14 @@ def pull(
config_name=config_name,
)
except Exception as ex:
logger.warning(
log.warning(
"Failed to pull the configuration from the server with error: %s",
str(ex),
)
try:
self.set(**{"current_version": config.current_version, **config.parameters})
except Exception as ex:
logger.warning("Failed to set the configuration with error: %s", str(ex))
log.warning("Failed to set the configuration with error: %s", str(ex))

def all(self):
"""Returns all the parameters for the app variant"""
Expand Down
64 changes: 21 additions & 43 deletions agenta-cli/agenta/sdk/decorators/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,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 @@ -357,9 +355,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 @@ -385,28 +381,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 @@ -683,10 +671,7 @@ def handle_terminal_run(

loop = get_event_loop()

with routing_context_manager(
config=args_config_params,
environment="terminal",
):
with routing_context_manager(config=args_config_params):
result = loop.run_until_complete(
self.execute_function(
func,
Expand All @@ -695,30 +680,23 @@ def handle_terminal_run(
)
)

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(f"latency: {result.trace.get('latency')}")
log.info(f"cost: {result.trace.get('cost')}")
log.info(f"usage: {list(result.trace.get('usage', {}).values())}")

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

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

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

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

if not self.span.is_recording():
Expand Down Expand Up @@ -90,7 +90,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

if not self.span.is_recording():
Expand All @@ -107,7 +107,7 @@ def log_success_event(
return

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

if not self.span.is_recording():
Expand Down Expand Up @@ -156,7 +156,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

if not self.span.is_recording():
Expand All @@ -176,7 +176,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

if not self.span.is_recording():
Expand All @@ -190,7 +190,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

if not self.span.is_recording():
Expand Down Expand Up @@ -239,7 +239,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

if not self.span.is_recording():
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 @@ -133,10 +133,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
6 changes: 1 addition & 5 deletions agenta-cli/agenta/sdk/tracing/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def configure(
self.otlp_url,
)
log.info("--------------------------------------------")

check(
self.otlp_url,
headers=self.headers,
Expand All @@ -106,13 +105,10 @@ 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.

Loading

0 comments on commit 89093b1

Please sign in to comment.