在 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 (https://repo.anaconda.com/pkgs/snowflake).
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.
通过代码记录之前,您必须:
-
设置事件表来收集通过处理程序代码记录的消息。
For more information, see Event table overview.
-
请确保已设置跟踪级别,以便将所需数据存储在事件表中。
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.
添加对遥测包的支持
To use telemetry package, you must make the open source Snowflake telemetry package (https://github.com/snowflakedb/snowflake-telemetry-python), which is included with Snowflake, available to your handler code. The package is available from the Anaconda Snowflake channel (https://repo.anaconda.com/pkgs/snowflake).
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.
添加跟踪事件
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:
其中
nameis a Python string that specifies the name of the trace event.attributesis an OpenTelemetry Attributes object (https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-api/src/opentelemetry/util/types.py) 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.
添加这些事件会在事件表中添加两行,每行在列中 RECORD 具有不同的值:
The FunctionEventWithAttributes event row includes the following attributes in the row’s RECORD_ATTRIBUTES column:
添加 span 属性¶
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:
其中:
keyis a Python string that specifies the key for an attribute.valueis an OpenTelemetry AttributeValue object (https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-api/src/opentelemetry/util/types.py) that specifies the value of the attribute.
以下示例中的代码创建四个属性并设置其值:
设置这些属性会导致事件表的 RECORD_ATTRIBUTES 列中出现以下内容:
添加自定义 span¶
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 (https://opentelemetry-python.readthedocs.io/en/latest/api/index.html) 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 (https://opentelemetry-python.readthedocs.io/en/latest/api/index.html).
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 示例¶
以下各节提供了从 Python 代码中为跟踪事件添加支持的示例。
存储过程示例
Streamlit 示例¶
UDF 示例¶
当您通过处理输入行的 Python 函数调用跟踪事件 API 时,将为 UDF 处理的 每一行 调用该 API。
例如,以下语句对 50 行调用上一示例中定义的 Python 函数,从而产生 100 个跟踪事件(每行两个):
UDTF 示例¶
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: