|
| 1 | +import importlib |
| 2 | +import logging |
| 3 | +import sys |
| 4 | +from typing import Any, Callable, Dict, List, Optional |
| 5 | + |
| 6 | +from uipath.tracing import traced |
| 7 | + |
| 8 | +# Original module and traceable function references |
| 9 | +original_langsmith: Any = None |
| 10 | +original_traceable: Any = None |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +# Apply the patch |
| 16 | +def _map_traceable_to_traced_args( |
| 17 | + run_type: Optional[str] = None, |
| 18 | + name: Optional[str] = None, |
| 19 | + tags: Optional[List[str]] = None, |
| 20 | + metadata: Optional[Dict[str, Any]] = None, |
| 21 | + **kwargs: Any, |
| 22 | +) -> Dict[str, Any]: |
| 23 | + """ |
| 24 | + Map LangSmith @traceable arguments to UiPath @traced() arguments. |
| 25 | +
|
| 26 | + Args: |
| 27 | + run_type: Function type (tool, chain, llm, retriever, etc.) |
| 28 | + name: Custom name for the traced function |
| 29 | + tags: List of tags for categorization |
| 30 | + metadata: Additional metadata dictionary |
| 31 | + **kwargs: Additional arguments (ignored) |
| 32 | +
|
| 33 | + Returns: |
| 34 | + Dict containing mapped arguments for @traced() |
| 35 | + """ |
| 36 | + traced_args = {} |
| 37 | + |
| 38 | + # Direct mappings |
| 39 | + if name is not None: |
| 40 | + traced_args["name"] = name |
| 41 | + |
| 42 | + # Pass through run_type directly to UiPath @traced() |
| 43 | + if run_type: |
| 44 | + traced_args["run_type"] = run_type |
| 45 | + |
| 46 | + # For span_type, we can derive from run_type or use a default |
| 47 | + if run_type: |
| 48 | + # Map run_type to appropriate span_type for OpenTelemetry |
| 49 | + span_type_mapping = { |
| 50 | + "tool": "tool_call", |
| 51 | + "chain": "chain_execution", |
| 52 | + "llm": "llm_call", |
| 53 | + "retriever": "retrieval", |
| 54 | + "embedding": "embedding", |
| 55 | + "prompt": "prompt_template", |
| 56 | + "parser": "output_parser", |
| 57 | + } |
| 58 | + traced_args["span_type"] = span_type_mapping.get(run_type, run_type) |
| 59 | + |
| 60 | + # Note: UiPath @traced() doesn't support custom attributes directly |
| 61 | + # Tags and metadata information is lost in the current mapping |
| 62 | + # This could be enhanced in future versions |
| 63 | + |
| 64 | + return traced_args |
| 65 | + |
| 66 | + |
| 67 | +def otel_traceable_adapter( |
| 68 | + func: Optional[Callable[..., Any]] = None, |
| 69 | + *, |
| 70 | + run_type: Optional[str] = None, |
| 71 | + name: Optional[str] = None, |
| 72 | + tags: Optional[List[str]] = None, |
| 73 | + metadata: Optional[Dict[str, Any]] = None, |
| 74 | + **kwargs: Any, |
| 75 | +): |
| 76 | + """ |
| 77 | + OTEL-based adapter that converts LangSmith @traceable decorator calls to UiPath @traced(). |
| 78 | +
|
| 79 | + This function maintains the same interface as LangSmith's @traceable but uses |
| 80 | + UiPath's OpenTelemetry-based tracing system underneath. |
| 81 | +
|
| 82 | + Args: |
| 83 | + func: Function to be decorated (when used without parentheses) |
| 84 | + run_type: Type of function (tool, chain, llm, etc.) |
| 85 | + name: Custom name for tracing |
| 86 | + tags: List of tags for categorization |
| 87 | + metadata: Additional metadata dictionary |
| 88 | + **kwargs: Additional arguments (for future compatibility) |
| 89 | +
|
| 90 | + Returns: |
| 91 | + Decorated function or decorator function |
| 92 | + """ |
| 93 | + |
| 94 | + def decorator(f: Callable[..., Any]) -> Callable[..., Any]: |
| 95 | + # Map arguments to @traced() format |
| 96 | + traced_args = _map_traceable_to_traced_args( |
| 97 | + run_type=run_type, name=name, tags=tags, metadata=metadata, **kwargs |
| 98 | + ) |
| 99 | + |
| 100 | + # Apply UiPath @traced() decorator |
| 101 | + return traced(**traced_args)(f) |
| 102 | + |
| 103 | + # Handle both @traceable and @traceable(...) usage patterns |
| 104 | + if func is None: |
| 105 | + # Called as @traceable(...) - return decorator |
| 106 | + return decorator |
| 107 | + else: |
| 108 | + # Called as @traceable - apply decorator directly |
| 109 | + return decorator(func) |
| 110 | + |
| 111 | + |
| 112 | +def _instrument_traceable_attributes(): |
| 113 | + """Apply the patch to langsmith module at import time.""" |
| 114 | + global original_langsmith, original_traceable |
| 115 | + |
| 116 | + # Import the original module if not already done |
| 117 | + if original_langsmith is None: |
| 118 | + # Temporarily remove our custom module from sys.modules |
| 119 | + if "langsmith" in sys.modules: |
| 120 | + original_langsmith = sys.modules["langsmith"] |
| 121 | + del sys.modules["langsmith"] |
| 122 | + |
| 123 | + # Import the original module |
| 124 | + original_langsmith = importlib.import_module("langsmith") |
| 125 | + |
| 126 | + # Store the original traceable |
| 127 | + original_traceable = original_langsmith.traceable |
| 128 | + |
| 129 | + # Replace the traceable function with our patched version |
| 130 | + original_langsmith.traceable = otel_traceable_adapter |
| 131 | + |
| 132 | + # Put our modified module back |
| 133 | + sys.modules["langsmith"] = original_langsmith |
| 134 | + |
| 135 | + return original_langsmith |
0 commit comments