Decorator for tracing CRUD operations
def trace_crud(operation_type):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
# Start span
with tracer.start_as_current_span(f"crud_{operation_type}") as span:
# Add attributes
span.set_attribute("crud.operation", operation_type)
# Extract entity ID if present
entity_id = kwargs.get('employee_id', None)
if entity_id:
span.set_attribute("crud.entity_id", entity_id)
# Start MLflow run
with mlflow.start_run(run_name=f"crud_{operation_type}", nested=True) as run:
# Log operation details
mlflow.log_params({
"crud.operation": operation_type,
"crud.function": func.__name__
})
if entity_id:
mlflow.log_param("crud.entity_id", entity_id)
start_time = time.time()
try:
# Execute function
result = func(*args, **kwargs)
# Log success
mlflow.log_param("crud.result", "success")
# Log execution time
execution_time = (time.time() - start_time) * 1000
mlflow.log_metric("crud.execution_time_ms", execution_time)
# Log result count if applicable
if isinstance(result, list):
mlflow.log_metric("crud.result_count", len(result))
elif result is not None and operation_type == "get":
mlflow.log_param("crud.found", True)
elif result is None and operation_type == "get":
mlflow.log_param("crud.found", False)
return result
except Exception as e:
# Log failure
span.record_exception(e)
mlflow.log_params({
"crud.result": "error",
"crud.error_type": type(e).__name__,
"crud.error_message": str(e)[:250]
})
# Log execution time
execution_time = (time.time() - start_time) * 1000
mlflow.log_metric("crud.execution_time_ms", execution_time)
raise
return wrapper
return decorator
xchange the decorator in such a way that we can get the table too