Set up tracing
How to collect data from a live LLM app.
Quickstart: For a simple end-to-end example, check the Tutorial.
Installation
Install the tracely
package from PyPi:
Initialize tracing
You must first connect to Evidently Cloud and create a Project.
To start sending traces, use init_tracing
:
You can also set parameters using environment variables.
init_tracing()
Function Arguments
Parameter | Description | Environment Variable |
---|---|---|
address | Trace collector address. Defaults to https://app.evidently.cloud/ . | EVIDENTLY_TRACE_COLLECTOR |
api_key | Evidently Cloud API key. | EVIDENTLY_TRACE_COLLECTOR_API_KEY |
export_name | Tracing dataset name. Traces with the same name are grouped into a single dataset. | EVIDENTLY_TRACE_COLLECTOR_EXPORT_NAME |
project_id | Destination Project ID in Evidently Cloud. | EVIDENTLY_TRACE_COLLECTOR_PROJECT_ID |
exporter_type | Trace export protocol: grpc or http . | - |
as_global | Registers the tracing provider (opentelemetry.trace.TracerProvider ) globally (True ) or use locally (False ). Default: True . | - |
Decorator
Once Tracely
is initialized, you can decorate your functions with trace_event
to start collecting traces for a specific function:
You can also specify which function arguments should be included in the trace.
Example 1. To log all arguments of the function:
Example 2. To log only input arguments of the function:
Example 3. To log only “arg1” and “arg2”:
trace_event
Decorator Arguments
Parameter | Description | Default |
---|---|---|
span_name: Optional[str] | The name of the span to send in the event. | Function name |
track_args: Optional[List[str]] | A list of function arguments to include in the event. | None (all arguments included) |
ignore_args: Optional[List[str]] | A list of function arguments to exclude, e.g., arguments that contain sensitive data. | None (no arguments ignored) |
track_output: Optional[bool] | Indicates whether to track the function’s return value. | True |
parse_output: Optional[bool] | Indicates whether the result should be parsed, e.g., dict , list , and tuple types will be split into separate fields. | True |
Context manager
To create a trace event without using a decorator (e.g., for a specific piece of code), you can use the context manager:
create_trace_event
Function Arguments
Parameter | Description | Default |
---|---|---|
name | Span name. | - |
parse_output | Whether to parse the result into separate fields for dict , list , or tuple types. | True |
params | Key-value parameters to set as attributes. | - |
event
Object Methods
Method | Description |
---|---|
set_attribute | Sets a custom attribute for the event. |
set_result | Sets a result for the event. Only one result can be set per event. |
Add event attributes
If you want to add a new attribute to an active event span, you can use get_current_span()
to get access to the current span:
get_current_span()
Object Methods
Method | Description |
---|---|
set_attribute | Adds a new attribute to the active span. |
set_result | Sets a result field for the active span. (Has no effect in decorated functions that define return values). |