Fix: Agrega logs para las operaciones en la base de datos #4
4
main.py
4
main.py
@@ -15,7 +15,7 @@ from google import genai
|
|||||||
from google.genai import types as genai_types
|
from google.genai import types as genai_types
|
||||||
from mcp.server.fastmcp import Context, FastMCP
|
from mcp.server.fastmcp import Context, FastMCP
|
||||||
|
|
||||||
from utils import Settings, _args, log_structured_entry
|
from utils import Settings, _args, cfg, log_structured_entry
|
||||||
|
|
||||||
HTTP_TOO_MANY_REQUESTS = 429
|
HTTP_TOO_MANY_REQUESTS = 429
|
||||||
HTTP_SERVER_ERROR = 500
|
HTTP_SERVER_ERROR = 500
|
||||||
@@ -624,8 +624,6 @@ async def lifespan(_server: FastMCP) -> AsyncIterator[AppContext]:
|
|||||||
log_structured_entry("MCP server lifespan ending", "INFO")
|
log_structured_entry("MCP server lifespan ending", "INFO")
|
||||||
|
|
||||||
|
|
||||||
cfg = Settings.model_validate({})
|
|
||||||
|
|
||||||
mcp = FastMCP(
|
mcp = FastMCP(
|
||||||
"knowledge-search",
|
"knowledge-search",
|
||||||
host=_args.host,
|
host=_args.host,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from .config import Settings, _args
|
from .config import Settings, _args, cfg
|
||||||
from .logging_setup import log_structured_entry
|
from .logging_setup import log_structured_entry
|
||||||
|
|
||||||
__all__ = ['Settings', '_args', 'log_structured_entry']
|
__all__ = ['Settings', '_args', 'cfg', 'log_structured_entry']
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ class Settings(BaseSettings):
|
|||||||
endpoint_domain: str
|
endpoint_domain: str
|
||||||
embedding_model: str = "gemini-embedding-001"
|
embedding_model: str = "gemini-embedding-001"
|
||||||
search_limit: int = 10
|
search_limit: int = 10
|
||||||
|
log_name: str = "va_agent_evaluation_logs"
|
||||||
|
log_level: str = "INFO"
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def settings_customise_sources(
|
def settings_customise_sources(
|
||||||
@@ -52,3 +54,7 @@ class Settings(BaseSettings):
|
|||||||
YamlConfigSettingsSource(settings_cls),
|
YamlConfigSettingsSource(settings_cls),
|
||||||
file_secret_settings,
|
file_secret_settings,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# Singleton instance of Settings
|
||||||
|
cfg = Settings.model_validate({})
|
||||||
|
|||||||
@@ -9,26 +9,24 @@ from typing import Optional, Dict, Literal
|
|||||||
import google.cloud.logging
|
import google.cloud.logging
|
||||||
from google.cloud.logging.handlers import CloudLoggingHandler
|
from google.cloud.logging.handlers import CloudLoggingHandler
|
||||||
|
|
||||||
from .config import Settings
|
from .config import cfg
|
||||||
|
|
||||||
|
|
||||||
def _setup_logger() -> logging.Logger:
|
def _setup_logger() -> logging.Logger:
|
||||||
"""Create or return the singleton evaluation logger."""
|
"""Create or return the singleton evaluation logger."""
|
||||||
log_name = "va_agent-evaluation-logs"
|
logger = logging.getLogger(cfg.log_name)
|
||||||
|
A8080816 marked this conversation as resolved
Outdated
|
|||||||
logger = logging.getLogger(log_name)
|
|
||||||
cfg = Settings.model_validate({})
|
|
||||||
if any(isinstance(h, CloudLoggingHandler) for h in logger.handlers):
|
if any(isinstance(h, CloudLoggingHandler) for h in logger.handlers):
|
||||||
return logger
|
return logger
|
||||||
|
A8080816 marked this conversation as resolved
A8065384
commented
Se instancia Se instancia `Settings` dos veces, podemos centralizarlo en `config`
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
client = google.cloud.logging.Client(project=cfg.project_id)
|
client = google.cloud.logging.Client(project=cfg.project_id)
|
||||||
handler = CloudLoggingHandler(client, name=log_name) # async transport
|
handler = CloudLoggingHandler(client, name=cfg.log_name) # async transport
|
||||||
logger.addHandler(handler)
|
logger.addHandler(handler)
|
||||||
logger.setLevel(logging.INFO)
|
logger.setLevel(getattr(logging, cfg.log_level.upper()))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Fallback to console if Cloud Logging is unavailable (local dev)
|
# Fallback to console if Cloud Logging is unavailable (local dev)
|
||||||
|
A8080816 marked this conversation as resolved
A8065384
commented
Convertir LOG LEVEL en parámetro de ambiente también Convertir LOG LEVEL en parámetro de ambiente también
|
|||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=getattr(logging, cfg.log_level.upper()))
|
||||||
logger = logging.getLogger(log_name)
|
logger = logging.getLogger(cfg.log_name)
|
||||||
logger.warning("Cloud Logging setup failed; using console. Error: %s", e)
|
logger.warning("Cloud Logging setup failed; using console. Error: %s", e)
|
||||||
|
|
||||||
return logger
|
return logger
|
||||||
@@ -43,7 +41,7 @@ def log_structured_entry(message: str, severity: Literal["INFO", "WARNING", "ERR
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
message: Short label for the row (e.g., "Final agent turn").
|
message: Short label for the row (e.g., "Final agent turn").
|
||||||
severity: "INFO" | "WARNING" | "ERROR" etc.
|
severity: "INFO" | "WARNING" | "ERROR"
|
||||||
custom_log: A dict with your structured payload.
|
custom_log: A dict with your structured payload.
|
||||||
"""
|
"""
|
||||||
level = getattr(logging, severity.upper(), logging.INFO)
|
level = getattr(logging, severity.upper(), logging.INFO)
|
||||||
|
|||||||
Reference in New Issue
Block a user
Convertir en parametro dentro de
Settings