Emitting trace events in Python¶
You can use the Snowflake telemetry package to emit trace events from a function or procedure handler written in Python.
The package is available from the Anaconda Snowflake channel.
You can access stored trace event data by executing a SELECT command on the event table. For more information, see Viewing trace data.
Note
For guidelines to keep in mind when adding trace events, see General guidelines for adding trace events.
For general information about setting up logging and retrieving messages in Snowflake, see Logging messages from functions and procedures.
Before logging from code, you must:
Set up an event table to collect messages logged from handler code.
For more information, see Event table overview.
Be sure you have the trace level set so that the data you want are stored in the event table.
For more information, see Setting levels for logging, metrics, and tracing.
Note
For guidelines to keep in mind when adding trace events, see General guidelines for adding trace events.
Adding support for the telemetry package¶
To use telemetry package, you must make the open source Snowflake telemetry package, which is included with Snowflake, available to your handler code. The package is available from the Anaconda Snowflake channel.
By default, the telemetry package is included when you create a Python handler for a stored procedure or function. However, if you
specify a package policy to allow or disallow specific packages explicitly, Snowflake doesn’t automatically include the
snowflake-telemetry-python package. In this case, you must specify the package in the PACKAGES clause.
For a Streamlit app. You can add the
snowflake-telemetry-pythonpackage to your app by using Snowsight or anenvironment.yml.file.Code in the following example uses the PACKAGES clause to reference the telemetry package as well as the Snowpark library (which is required for stored procedures written in Python – for more information, see Writing stored procedures with SQL and Python).
Import the
telemetrypackage in your code.
Adding trace events¶
You can add trace events by calling the telemetry.add_event method, passing a name for the event. You can also optionally associate
attributes – key-value pairs – with an event.
The add_event method is available in the following form:
where
nameis a Python string that specifies the name of the trace event.attributesis an OpenTelemetry Attributes object that specifies the attributes for this trace event. This argument is optional. Omit the argument if you do not have any attributes to specify for this trace event.
Handler code in the following example adds two events, FunctionEmptyEvent and FunctionEventWithAttributes. With
FunctionEventWithAttributes, the code also adds two attributes: key1 and key2.
Adding these events results in two rows in the event table, each with a different value in the RECORD column:
The FunctionEventWithAttributes event row includes the following attributes in the row’s RECORD_ATTRIBUTES column:
Adding span attributes¶
You can set attributes – key-value pairs – associated with spans by calling the telemetry.set_span_attribute method.
For details on spans, see How Snowflake represents trace events.
The set_span_attribute method is available in the following form:
where:
keyis a Python string that specifies the key for an attribute.
valueis an OpenTelemetry AttributeValue object that specifies the value of the attribute.
Code in the following example creates four attributes and sets their values:
Setting these attributes results in the following in the event table’s RECORD_ATTRIBUTES column:
Adding custom spans¶
You can add custom spans that are separate from the default span created by Snowflake. For details on custom spans, see Adding custom spans to a trace.
Code in the following example uses the OpenTelemetry Python API to create the my.span span as the current span with
start_as_current_span. It then adds an event with attributes to the new span using the OpenTelemetry Python API.
Event data won’t be captured by the event table unless the span ends before your handler completes execution. In this example, closing the
span happens automatically when the with statement concludes.
Python examples¶
The following sections provide examples of adding support for trace events from Python code.
Stored procedure example¶
Streamlit example¶
UDF example¶
When you call the trace event API from a Python function that processes an input row, the API will be called for every row processed by the UDF.
For example, the following statement calls the Python function defined in the previous example for 50 rows, resulting in 100 trace events (two for each row):
UDTF example¶
When you call the trace event API in the process() method of a UDTF handler class, the API will be called for every row processed.
For example, the following statement calls the process() method defined in the previous example for 50 rows, resulting in 100 trace
events (two for each row) added by the process() method: