Tracing
patronus.tracing
decorators
start_span
start_span(
name: str,
*,
record_exception: bool = True,
attributes: Optional[Attributes] = None,
) -> Iterator[Optional[typing.Any]]
Context manager for creating and managing a trace span.
This function is used to create a span within the current context using the tracer,
allowing you to track execution timing or events within a specific block of code.
The context is set by patronus.init()
function. If SDK was not initialized, yielded value will be None.
Example:
import patronus
patronus.init()
# Use context manager for finer-grained tracing
def complex_operation():
with patronus.start_span("Data preparation"):
# Prepare data
pass
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the span. |
required |
record_exception
|
bool
|
Whether to record exceptions that occur within the span. Default is True. |
True
|
attributes
|
Optional[Attributes]
|
Attributes to associate with the span, providing additional metadata. |
None
|
Source code in src/patronus/tracing/decorators.py
traced
traced(
span_name: Optional[str] = None,
*,
log_args: bool = True,
log_results: bool = True,
log_exceptions: bool = True,
disable_log: bool = False,
attributes: Attributes = None,
**kwargs: Any,
)
A decorator to trace function execution by recording a span for the traced function.
Example:
import patronus
patronus.init()
# Trace a function with the @traced decorator
@patronus.traced()
def process_input(user_query):
# Process the input
Parameters:
Name | Type | Description | Default |
---|---|---|---|
span_name
|
Optional[str]
|
The name of the traced span. Defaults to the function name if not provided. |
None
|
log_args
|
bool
|
Whether to log the arguments passed to the function. Default is True. |
True
|
log_results
|
bool
|
Whether to log the function's return value. Default is True. |
True
|
log_exceptions
|
bool
|
Whether to log any exceptions raised while executing the function. Default is True. |
True
|
disable_log
|
bool
|
Whether to disable logging the trace information. Default is False. |
False
|
attributes
|
Attributes
|
Attributes to attach to the traced span. Default is None. |
None
|
**kwargs
|
Any
|
Additional arguments for the decorator. |
{}
|
Source code in src/patronus/tracing/decorators.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
tracer
This module provides the implementation for tracing support using the OpenTelemetry SDK.
PatronusAttributesSpanProcessor
PatronusAttributesSpanProcessor(
project_name: str,
app: Optional[str] = None,
experiment_id: Optional[str] = None,
)
Bases: SpanProcessor
Processor that adds Patronus-specific attributes to all spans.
This processor ensures that each span includes the mandatory attributes:
project_name
, and optionally adds app
or experiment_id
attributes
if they are provided during initialization.
Source code in src/patronus/tracing/tracer.py
create_tracer_provider
cached
create_tracer_provider(
exporter_endpoint: str,
api_key: str,
scope: PatronusScope,
) -> TracerProvider
Creates and returns a cached TracerProvider configured with the specified exporter.
The function utilizes an OpenTelemetry BatchSpanProcessor and an OTLPSpanExporter to initialize the tracer. The configuration is cached for reuse.
Source code in src/patronus/tracing/tracer.py
create_tracer
Creates an OpenTelemetry (OTeL) tracer tied to the specified scope.