|
1 |
| -# Tracing |
2 |
| - |
3 |
| -The `traced()` decorator enables automatic tracing of function calls, inputs, and outputs. It is designed to help you monitor, debug, and audit your code by capturing detailed information about function executions, including arguments, return values, and exceptions. |
4 |
| - |
5 |
| -You can view the traces of an Orchestrator job by going to the Jobs page, click a job, a side panel will open, and they will be available under the `Trace` tab. These can also be seen in UiPath Maestro when your agent is part of a larger process orchestration. |
6 |
| - |
7 |
| -## Usage |
8 |
| - |
9 |
| -Apply the `@traced()` decorator to any function (sync, async, generator, or async generator) to automatically record its execution as a trace span. |
10 |
| - |
11 |
| -```python |
12 |
| -from uipath.tracing._traced import traced |
13 |
| - |
14 |
| -@traced() |
15 |
| -def my_function(x, y): |
16 |
| - return x + y |
17 |
| - |
18 |
| -@traced(name="custom_span", run_type="my_type") |
19 |
| -async def my_async_function(a, b): |
20 |
| - return a * b |
21 |
| -``` |
22 |
| - |
23 |
| -## Parameters |
24 |
| - |
25 |
| -| Parameter | Type | Description | |
26 |
| -|------------------|-------------------------------------|---------------------------------------------------------------------------------------------------| |
27 |
| -| name | Optional[str] | Custom name for the trace span. Defaults to the function name. | |
28 |
| -| run_type | Optional[str] | Category for the run (e.g., "uipath"). Useful for filtering traces. | |
29 |
| -| span_type | Optional[str] | Custom type for the span. Defaults to function type (sync/async/generator). | |
30 |
| -| input_processor | Optional[Callable[[dict], dict]] | Function to process/transform inputs before recording. Receives a dict of arguments. | |
31 |
| -| output_processor | Optional[Callable[[Any], Any]] | Function to process/transform outputs before recording. Receives the function's return value. | |
32 |
| -| hide_input | bool | If True, input data is redacted in the trace for privacy/security. | |
33 |
| -| hide_output | bool | If True, output data is redacted in the trace for privacy/security. | |
34 |
| - |
35 |
| -## Input and Output Processors |
36 |
| - |
37 |
| -Processors allow you to mask, redact, or transform sensitive data before it is recorded in the trace. For example: |
38 |
| - |
39 |
| -```python |
40 |
| -def mask_inputs(inputs): |
41 |
| - inputs = inputs.copy() |
42 |
| - if 'password' in inputs: |
43 |
| - inputs['password'] = '***REDACTED***' |
44 |
| - return inputs |
45 |
| - |
46 |
| -def anonymize_output(output): |
47 |
| - if isinstance(output, dict) and 'email' in output: |
48 |
| - output = output.copy() |
49 |
| - output['email'] = 'anonymous@example.com' |
50 |
| - return output |
51 |
| - |
52 |
| -@traced(input_processor=mask_inputs, output_processor=anonymize_output) |
53 |
| -def login(user, password): |
54 |
| - # ... |
55 |
| - return {"email": user + "@example.com"} |
56 |
| -``` |
57 |
| - |
58 |
| -## Privacy Controls |
59 |
| - |
60 |
| -- Set `hide_input=True` to prevent input data from being logged. |
61 |
| -- Set `hide_output=True` to prevent output data from being logged. |
62 |
| - |
63 |
| -```python |
64 |
| -@traced(hide_input=True, hide_output=True) |
65 |
| -def sensitive_operation(secret): |
66 |
| - ... |
67 |
| -``` |
68 |
| - |
69 |
| -## Supported Function Types |
70 |
| - |
71 |
| -- Regular functions (sync/async) |
72 |
| -- Generator functions (sync/async) |
73 |
| - |
74 |
| -## Example with plain python agents |
75 |
| - |
76 |
| -When used with plain python agents please call `wait_for_tracers()` at the end of the script to ensure all traces are sent, if this is not called the agent could end without sending all the traces. |
77 |
| - |
78 |
| -```python |
79 |
| - |
80 |
| -from uipath.tracing import traced, wait_for_tracers |
81 |
| - |
82 |
| -@traced(name="process_payment", run_type="payment", hide_input=True) |
83 |
| -def process_payment(card_number, amount): |
84 |
| - # Sensitive input will not be logged |
85 |
| - return {"status": "success", "amount": amount} |
86 |
| - |
87 |
| -@traced() |
88 |
| -def main(): |
89 |
| - process_payment() |
90 |
| - |
91 |
| -def main_wait_traces(): |
92 |
| - try: |
93 |
| - main() |
94 |
| - finally: |
95 |
| - # this needs to be called after the last `traced` function is done |
96 |
| - # to ensure the trace associated with main is saved |
97 |
| - wait_for_tracers() |
98 |
| - |
99 |
| -if __name__ == "__main__": |
100 |
| - main_wait_traces() |
101 |
| -``` |
102 |
| - |
103 |
| - |
104 |
| -## Example with langchain agents |
105 |
| - |
106 |
| -When using `uipath-langchain` there is no need to call wait_for_tracers our framework will ensure that is called. |
107 |
| - |
108 |
| -```python |
109 |
| -@traced() |
110 |
| -def my_custom_traced_function(input: str) -> str: |
111 |
| - return { "x": "some-output" } |
112 |
| -``` |
113 |
| - |
114 |
| -You can also use `@traceable()` attribute from langchain, but we recommend using `@traced()` attribute instead. |
115 |
| - |
116 |
| -```python |
117 |
| -@traceable() |
118 |
| -# @traced() ---> do not use both at the same time or it will duplicate spans. |
119 |
| -def my_custom_traced_function(input: str) -> str: |
120 |
| - return { "x": "some-output" } |
| 1 | +# Tracing |
| 2 | + |
| 3 | +The `traced()` decorator enables automatic tracing of function calls, inputs, and outputs. It is designed to help you monitor, debug, and audit your code by capturing detailed information about function executions, including arguments, return values, and exceptions. |
| 4 | + |
| 5 | +You can view the traces of an Orchestrator job by going to the Jobs page, click a job, a side panel will open, and they will be available under the `Trace` tab. These can also be seen in UiPath Maestro when your agent is part of a larger process orchestration. |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +Apply the `@traced()` decorator to any function (sync, async, generator, or async generator) to automatically record its execution as a trace span. |
| 10 | + |
| 11 | +```python hl_lines="3 7" |
| 12 | +from uipath.tracing._traced import traced |
| 13 | + |
| 14 | +@traced() |
| 15 | +def my_function(x, y): |
| 16 | + return x + y |
| 17 | + |
| 18 | +@traced(name="custom_span", run_type="my_type") |
| 19 | +async def my_async_function(a, b): |
| 20 | + return a * b |
| 21 | +``` |
| 22 | + |
| 23 | +## Parameters |
| 24 | + |
| 25 | +| Parameter | Type | Description | |
| 26 | +|------------------|-------------------------------------|---------------------------------------------------------------------------------------------------| |
| 27 | +| name | Optional[str] | Custom name for the trace span. Defaults to the function name. | |
| 28 | +| run_type | Optional[str] | Category for the run (e.g., "uipath"). Useful for filtering traces. | |
| 29 | +| span_type | Optional[str] | Custom type for the span. Defaults to function type (sync/async/generator). | |
| 30 | +| input_processor | Optional[Callable[[dict], dict]] | Function to process/transform inputs before recording. Receives a dict of arguments. | |
| 31 | +| output_processor | Optional[Callable[[Any], Any]] | Function to process/transform outputs before recording. Receives the function's return value. | |
| 32 | +| hide_input | bool | If True, input data is redacted in the trace for privacy/security. | |
| 33 | +| hide_output | bool | If True, output data is redacted in the trace for privacy/security. | |
| 34 | + |
| 35 | +## Input and Output Processors |
| 36 | + |
| 37 | +Processors allow you to mask, redact, or transform sensitive data before it is recorded in the trace. For example: |
| 38 | + |
| 39 | +```python hl_lines="13" |
| 40 | +def mask_inputs(inputs): |
| 41 | + inputs = inputs.copy() |
| 42 | + if 'password' in inputs: |
| 43 | + inputs['password'] = '***REDACTED***' |
| 44 | + return inputs |
| 45 | + |
| 46 | +def anonymize_output(output): |
| 47 | + if isinstance(output, dict) and 'email' in output: |
| 48 | + output = output.copy() |
| 49 | + output['email'] = 'anonymous@example.com' |
| 50 | + return output |
| 51 | + |
| 52 | +@traced(input_processor=mask_inputs, output_processor=anonymize_output) |
| 53 | +def login(user, password): |
| 54 | + # ... |
| 55 | + return {"email": user + "@example.com"} |
| 56 | +``` |
| 57 | + |
| 58 | +## Privacy Controls |
| 59 | + |
| 60 | +- Set `hide_input=True` to prevent input data from being logged. |
| 61 | +- Set `hide_output=True` to prevent output data from being logged. |
| 62 | + |
| 63 | +```python hl_lines="1" |
| 64 | +@traced(hide_input=True, hide_output=True) |
| 65 | +def sensitive_operation(secret): |
| 66 | + ... |
| 67 | +``` |
| 68 | + |
| 69 | +## Supported Function Types |
| 70 | + |
| 71 | +- Regular functions (sync/async) |
| 72 | +- Generator functions (sync/async) |
| 73 | + |
| 74 | +## Example with plain python agents |
| 75 | + |
| 76 | +When used with plain python agents please call `wait_for_tracers()` at the end of the script to ensure all traces are sent, if this is not called the agent could end without sending all the traces. |
| 77 | + |
| 78 | +```python hl_lines="3 8" |
| 79 | + |
| 80 | +from uipath.tracing import traced, wait_for_tracers |
| 81 | + |
| 82 | +@traced(name="process_payment", run_type="payment", hide_input=True) |
| 83 | +def process_payment(card_number, amount): |
| 84 | + # Sensitive input will not be logged |
| 85 | + return {"status": "success", "amount": amount} |
| 86 | + |
| 87 | +@traced() |
| 88 | +def main(): |
| 89 | + process_payment() |
| 90 | + |
| 91 | +def main_wait_traces(): |
| 92 | + try: |
| 93 | + main() |
| 94 | + finally: |
| 95 | + # this needs to be called after the last `traced` function is done |
| 96 | + # to ensure the trace associated with main is saved |
| 97 | + wait_for_tracers() |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + main_wait_traces() |
| 101 | +``` |
| 102 | + |
| 103 | + |
| 104 | +## Example with langchain agents |
| 105 | + |
| 106 | +When using `uipath-langchain` there is no need to call wait_for_tracers our framework will ensure that is called. |
| 107 | + |
| 108 | +```python hl_lines="1" |
| 109 | +@traced() |
| 110 | +def my_custom_traced_function(input: str) -> str: |
| 111 | + return { "x": "some-output" } |
| 112 | +``` |
| 113 | + |
| 114 | +You can also use `@traceable()` attribute from langchain, but we recommend using `@traced()` attribute instead. |
| 115 | + |
| 116 | +```python hl_lines="1" |
| 117 | +@traceable() |
| 118 | +# @traced() ---> do not use both at the same time or it will duplicate spans. |
| 119 | +def my_custom_traced_function(input: str) -> str: |
| 120 | + return { "x": "some-output" } |
121 | 121 | ```
|
0 commit comments