Fix: Agrega logs para las operaciones en la base de datos #4
81
main.py
81
main.py
@@ -448,8 +448,11 @@ async def lifespan(_server: FastMCP) -> AsyncIterator[AppContext]:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Validate credentials and configuration by testing actual resources
|
# Validate credentials and configuration by testing actual resources
|
||||||
|
# These validations are non-blocking - errors are logged but won't stop startup
|
||||||
log_structured_entry("Starting validation of credentials and resources", "INFO")
|
log_structured_entry("Starting validation of credentials and resources", "INFO")
|
||||||
|
|
||||||
|
validation_errors = []
|
||||||
|
|
||||||
# 1. Validate GenAI embedding access
|
# 1. Validate GenAI embedding access
|
||||||
log_structured_entry("Validating GenAI embedding access", "INFO")
|
log_structured_entry("Validating GenAI embedding access", "INFO")
|
||||||
try:
|
try:
|
||||||
@@ -468,15 +471,15 @@ async def lifespan(_server: FastMCP) -> AsyncIterator[AppContext]:
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
msg = "Embedding validation returned empty response"
|
msg = "Embedding validation returned empty response"
|
||||||
log_structured_entry(msg, "ERROR")
|
log_structured_entry(msg, "WARNING")
|
||||||
raise RuntimeError(msg)
|
validation_errors.append(msg)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log_structured_entry(
|
log_structured_entry(
|
||||||
"Failed to validate GenAI embedding access",
|
"Failed to validate GenAI embedding access - service may not work correctly",
|
||||||
"ERROR",
|
"WARNING",
|
||||||
{"error": str(e), "error_type": type(e).__name__}
|
{"error": str(e), "error_type": type(e).__name__}
|
||||||
)
|
)
|
||||||
raise
|
validation_errors.append(f"GenAI: {str(e)}")
|
||||||
|
|
||||||
# 2. Validate GCS bucket access
|
# 2. Validate GCS bucket access
|
||||||
log_structured_entry(
|
log_structured_entry(
|
||||||
@@ -500,43 +503,41 @@ async def lifespan(_server: FastMCP) -> AsyncIterator[AppContext]:
|
|||||||
if response.status == 403:
|
if response.status == 403:
|
||||||
msg = f"Access denied to bucket '{cfg.bucket}'. Check permissions."
|
msg = f"Access denied to bucket '{cfg.bucket}'. Check permissions."
|
||||||
log_structured_entry(
|
log_structured_entry(
|
||||||
"GCS bucket validation failed - access denied",
|
"GCS bucket validation failed - access denied - service may not work correctly",
|
||||||
"ERROR",
|
"WARNING",
|
||||||
{"bucket": cfg.bucket, "status": response.status}
|
{"bucket": cfg.bucket, "status": response.status}
|
||||||
)
|
)
|
||||||
raise RuntimeError(msg)
|
validation_errors.append(msg)
|
||||||
elif response.status == 404:
|
elif response.status == 404:
|
||||||
msg = f"Bucket '{cfg.bucket}' not found. Check bucket name and project."
|
msg = f"Bucket '{cfg.bucket}' not found. Check bucket name and project."
|
||||||
log_structured_entry(
|
log_structured_entry(
|
||||||
"GCS bucket validation failed - not found",
|
"GCS bucket validation failed - not found - service may not work correctly",
|
||||||
"ERROR",
|
"WARNING",
|
||||||
{"bucket": cfg.bucket, "status": response.status}
|
{"bucket": cfg.bucket, "status": response.status}
|
||||||
)
|
)
|
||||||
raise RuntimeError(msg)
|
validation_errors.append(msg)
|
||||||
elif not response.ok:
|
elif not response.ok:
|
||||||
body = await response.text()
|
body = await response.text()
|
||||||
msg = f"Failed to access bucket '{cfg.bucket}': {response.status} - {body}"
|
msg = f"Failed to access bucket '{cfg.bucket}': {response.status}"
|
||||||
log_structured_entry(
|
log_structured_entry(
|
||||||
"GCS bucket validation failed",
|
"GCS bucket validation failed - service may not work correctly",
|
||||||
"ERROR",
|
"WARNING",
|
||||||
{"bucket": cfg.bucket, "status": response.status, "response": body}
|
{"bucket": cfg.bucket, "status": response.status, "response": body}
|
||||||
)
|
)
|
||||||
raise RuntimeError(msg)
|
validation_errors.append(msg)
|
||||||
|
else:
|
||||||
log_structured_entry(
|
log_structured_entry(
|
||||||
"GCS bucket validation successful",
|
"GCS bucket validation successful",
|
||||||
"INFO",
|
"INFO",
|
||||||
{"bucket": cfg.bucket}
|
{"bucket": cfg.bucket}
|
||||||
)
|
)
|
||||||
except RuntimeError:
|
|
||||||
raise
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log_structured_entry(
|
log_structured_entry(
|
||||||
"Failed to validate GCS bucket access",
|
"Failed to validate GCS bucket access - service may not work correctly",
|
||||||
"ERROR",
|
"WARNING",
|
||||||
{"error": str(e), "error_type": type(e).__name__, "bucket": cfg.bucket}
|
{"error": str(e), "error_type": type(e).__name__, "bucket": cfg.bucket}
|
||||||
)
|
)
|
||||||
raise
|
validation_errors.append(f"GCS: {str(e)}")
|
||||||
|
|
||||||
# 3. Validate vector search endpoint access
|
# 3. Validate vector search endpoint access
|
||||||
log_structured_entry(
|
log_structured_entry(
|
||||||
@@ -556,44 +557,50 @@ async def lifespan(_server: FastMCP) -> AsyncIterator[AppContext]:
|
|||||||
if response.status == 403:
|
if response.status == 403:
|
||||||
msg = f"Access denied to endpoint '{cfg.endpoint_name}'. Check permissions."
|
msg = f"Access denied to endpoint '{cfg.endpoint_name}'. Check permissions."
|
||||||
log_structured_entry(
|
log_structured_entry(
|
||||||
"Vector search endpoint validation failed - access denied",
|
"Vector search endpoint validation failed - access denied - service may not work correctly",
|
||||||
"ERROR",
|
"WARNING",
|
||||||
{"endpoint": cfg.endpoint_name, "status": response.status}
|
{"endpoint": cfg.endpoint_name, "status": response.status}
|
||||||
)
|
)
|
||||||
raise RuntimeError(msg)
|
validation_errors.append(msg)
|
||||||
elif response.status == 404:
|
elif response.status == 404:
|
||||||
msg = f"Endpoint '{cfg.endpoint_name}' not found. Check endpoint name and project."
|
msg = f"Endpoint '{cfg.endpoint_name}' not found. Check endpoint name and project."
|
||||||
log_structured_entry(
|
log_structured_entry(
|
||||||
"Vector search endpoint validation failed - not found",
|
"Vector search endpoint validation failed - not found - service may not work correctly",
|
||||||
"ERROR",
|
"WARNING",
|
||||||
{"endpoint": cfg.endpoint_name, "status": response.status}
|
{"endpoint": cfg.endpoint_name, "status": response.status}
|
||||||
)
|
)
|
||||||
raise RuntimeError(msg)
|
validation_errors.append(msg)
|
||||||
elif not response.ok:
|
elif not response.ok:
|
||||||
body = await response.text()
|
body = await response.text()
|
||||||
msg = f"Failed to access endpoint '{cfg.endpoint_name}': {response.status} - {body}"
|
msg = f"Failed to access endpoint '{cfg.endpoint_name}': {response.status}"
|
||||||
log_structured_entry(
|
log_structured_entry(
|
||||||
"Vector search endpoint validation failed",
|
"Vector search endpoint validation failed - service may not work correctly",
|
||||||
"ERROR",
|
"WARNING",
|
||||||
{"endpoint": cfg.endpoint_name, "status": response.status, "response": body}
|
{"endpoint": cfg.endpoint_name, "status": response.status, "response": body}
|
||||||
)
|
)
|
||||||
raise RuntimeError(msg)
|
validation_errors.append(msg)
|
||||||
|
else:
|
||||||
log_structured_entry(
|
log_structured_entry(
|
||||||
"Vector search endpoint validation successful",
|
"Vector search endpoint validation successful",
|
||||||
"INFO",
|
"INFO",
|
||||||
{"endpoint": cfg.endpoint_name}
|
{"endpoint": cfg.endpoint_name}
|
||||||
)
|
)
|
||||||
except RuntimeError:
|
|
||||||
raise
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log_structured_entry(
|
log_structured_entry(
|
||||||
"Failed to validate vector search endpoint access",
|
"Failed to validate vector search endpoint access - service may not work correctly",
|
||||||
"ERROR",
|
"WARNING",
|
||||||
{"error": str(e), "error_type": type(e).__name__, "endpoint": cfg.endpoint_name}
|
{"error": str(e), "error_type": type(e).__name__, "endpoint": cfg.endpoint_name}
|
||||||
)
|
)
|
||||||
raise
|
validation_errors.append(f"Vector Search: {str(e)}")
|
||||||
|
|
||||||
|
# Summary of validations
|
||||||
|
if validation_errors:
|
||||||
|
log_structured_entry(
|
||||||
|
"MCP server started with validation errors - service may not work correctly",
|
||||||
|
"WARNING",
|
||||||
|
{"validation_errors": validation_errors, "error_count": len(validation_errors)}
|
||||||
|
)
|
||||||
|
else:
|
||||||
log_structured_entry("All validations passed - MCP server initialization complete", "INFO")
|
log_structured_entry("All validations passed - MCP server initialization complete", "INFO")
|
||||||
|
|
||||||
yield AppContext(
|
yield AppContext(
|
||||||
|
|||||||
Reference in New Issue
Block a user