|
| 1 | +from functools import wraps |
| 2 | +from typing import Any, Callable, Dict, Optional |
| 3 | + |
| 4 | +from opentelemetry.trace import Span |
| 5 | + |
| 6 | +from guardrails.types.primitives import PrimitiveTypes |
| 7 | +from guardrails.utils.safe_get import safe_get |
| 8 | +from guardrails.utils.hub_telemetry_utils import HubTelemetry |
| 9 | + |
| 10 | + |
| 11 | +def get_guard_attributes(attrs: Dict[str, Any], guard_self: Any) -> Dict[str, Any]: |
| 12 | + attrs["guard_id"] = guard_self.id |
| 13 | + attrs["user_id"] = guard_self._user_id |
| 14 | + attrs["custom_reask_prompt"] = guard_self._exec_opts.reask_prompt is not None |
| 15 | + attrs["custom_reask_instructions"] = ( |
| 16 | + guard_self._exec_opts.reask_instructions is not None |
| 17 | + ) |
| 18 | + attrs["custom_reask_messages"] = guard_self._exec_opts.reask_messages is not None |
| 19 | + attrs["output_type"] = ( |
| 20 | + "unstructured" |
| 21 | + if PrimitiveTypes.is_primitive(guard_self.output_schema.type.actual_instance) |
| 22 | + else "structured" |
| 23 | + ) |
| 24 | + return attrs |
| 25 | + |
| 26 | + |
| 27 | +def get_guard_call_attributes(attrs: Dict[str, Any], *args, **kwargs) -> Dict[str, Any]: |
| 28 | + guard_self = safe_get(args, 0) |
| 29 | + if guard_self is not None: |
| 30 | + attrs = get_guard_attributes(attrs, guard_self) |
| 31 | + |
| 32 | + llm_api_str = "" # noqa |
| 33 | + llm_api = safe_get(args, 1, kwargs.get("llm_api")) |
| 34 | + if llm_api: |
| 35 | + llm_api_module_name = ( |
| 36 | + llm_api.__module__ if hasattr(llm_api, "__module__") else "" |
| 37 | + ) |
| 38 | + llm_api_name = ( |
| 39 | + llm_api.__name__ if hasattr(llm_api, "__name__") else type(llm_api).__name__ |
| 40 | + ) |
| 41 | + llm_api_str = f"{llm_api_module_name}.{llm_api_name}" |
| 42 | + attrs["llm_api"] = llm_api_str if llm_api_str else "None" |
| 43 | + |
| 44 | + return attrs |
| 45 | + |
| 46 | + |
| 47 | +def add_attributes(name: str, span: Span, origin: str, *args, **kwargs): |
| 48 | + attrs = {"origin": origin} |
| 49 | + if origin == "Guard.__call__": |
| 50 | + attrs = get_guard_call_attributes(attrs, *args, **kwargs) |
| 51 | + |
| 52 | + for key, value in attrs.items(): |
| 53 | + span.set_attribute(key, value) |
| 54 | + |
| 55 | + |
| 56 | +def trace(*, name: str, origin: str, is_parent: Optional[bool] = False): |
| 57 | + def decorator(fn: Callable[..., Any]): |
| 58 | + @wraps(fn) |
| 59 | + def wrapper(*args, **kwargs): |
| 60 | + hub_telemetry = HubTelemetry() |
| 61 | + if hub_telemetry._enabled and hub_telemetry._tracer is not None: |
| 62 | + context = ( |
| 63 | + hub_telemetry.extract_current_context() if not is_parent else None |
| 64 | + ) |
| 65 | + with hub_telemetry._tracer.start_as_current_span( |
| 66 | + name, context=context |
| 67 | + ) as span: # noqa |
| 68 | + if is_parent: |
| 69 | + # Inject the current context |
| 70 | + hub_telemetry.inject_current_context() |
| 71 | + |
| 72 | + add_attributes(name, span, origin, *args, **kwargs) |
| 73 | + return fn(*args, **kwargs) |
| 74 | + else: |
| 75 | + return fn(*args, **kwargs) |
| 76 | + |
| 77 | + return wrapper |
| 78 | + |
| 79 | + return decorator |
0 commit comments