Python Tracer SDK
Install
poetry add autoblocksai
AutoblocksTracer
The AutoblocksTracer
class is used to send events to Autoblocks.
It can be initialized without any arguments if your ingestion key
is set as an environment variable named AUTOBLOCKS_INGESTION_KEY
.
Python
from autoblocks.tracer import AutoblocksTracer
tracer = AutoblocksTracer()
Since an instance of AutoblocksTracer
contains internal state,
we recommend creating a new instance on each request or operation
you are tracing.
All AutoblocksTracer
instances share the same httpx
client
instance under the hood, so you don't need to worry about inefficient
usage of network resources when creating multiple tracer instances.
Otherwise, you can pass the ingestion key as an argument:
Python
tracer = AutoblocksTracer("your-ingestion-key")
There are additional arguments that you can use to further configure the tracer:
- Name
trace_id
- Type
- Optional[str]
- Description
The
trace_id
for all events sent through the tracer instance.
- Name
properties
- Type
- Optional[dict]
- Description
The properties that will be sent for all events sent through the tracer instance.
- Name
timeout
- Type
- timedelta | undefined
- Description
The timeout for each
send_event
call, which is aPOST
request to the Autoblocks ingestion API. Default value istimedelta(seconds=5)
.The
timeout
property should be an instance ofdatetime.timedelta
.
Python
from datetime import timedelta
tracer = AutoblocksTracer(
trace_id="my-trace-id",
properties={"source": "my-app"},
timeout=timedelta(seconds=1),
)
send_event
Send an event to Autoblocks.
Python
tracer.send_event(
"ai.request",
properties={
"provider": "openai",
"prompt": "Hello!",
},
)
The send_event
method is "fire and forget"; it will return immediately and the event will be sent in the background.
Arguments
- Name
message
- Type
- str
- Description
The name of the event.
- Name
trace_id
- Type
- Optional[str]
- Description
Used to group related events together. If not provided, defaults to a random UUID.
trace_id
set viasend_event
trace_id
set viaset_trace_id
trace_id
set via the constructor- Name
properties
- Type
- Optional[dict]
- Description
Custom properties to attach to the event. The values in this dictionary can be anything, including other dictionaries.
The
properties
set withinsendEvent
take precedence over any other properties that have been set.The order of precedence is:
properties
set viasend_event
properties
set viaset_properties
orupdate_properties
properties
set via the constructor
- Name
span_id
- Type
- Optional[str]
- Description
Within a trace, connect related events together using span IDs. This is useful to connect request and response events together, for example.
- Name
parent_span_id
- Type
- Optional[str]
- Description
Establish parent / child relationships between your spans using parent span IDs. For example, if some events represent work being done on behalf of a higher level span in your trace, you should set that span's ID as the parent span ID for those events.
- Name
timestamp
- Type
- Optional[str]
- Description
An ISO date time string of when the event happened. If not provided, defaults to the current time.
The trace_id
set within send_event
takes precedence over a trace_id
that has been previously set.
The order of precedence is:
Read more about spans and the Trace Tree UI.
Returns
set_trace_id
Use this method to set the trace_id
for all subsequent calls of send_event
.
Python
tracer.set_trace_id('my-trace-id')
Arguments
- Name
trace_id
- Type
- str
- Description
The
trace_id
to set.
set_properties
Use this method to set the properties
for all subsequent calls of send_event
.
This method overwrites any existing properties that were previously set.
If a property that already exists is not specified in the argument to
set_properties
, it will be removed.
Python
tracer.set_properties({"x": 1, "y": 2}) # --> {"x": 1, "y": 2}
tracer.set_properties({"x": 2}) # --> {"x": 2}
Arguments
- Name
properties
- Type
- dict
- Description
The
properties
to set.
update_properties
Use this method to update the properties
for all subsequent calls of send_event
.
This method merges any existing properties that were previously set. If a property already exists, it will be overwritten.
Python
tracer.update_properties({"x": 1, "y": 2}) # --> {"x": 1, "y": 2}
tracer.update_properties({"x": 2}) # --> {"x": 2, "y": 2}
Arguments
- Name
properties
- Type
- dict
- Description
The
properties
to update.