Python에서 추적 이벤트 내보내기

Snowflake telemetry 패키지를 사용하여 Python으로 작성된 함수 또는 프로시저 처리기에서 추적 이벤트를 내보낼 수 있습니다. 패키지는 Anaconda Snowflake 채널 에서 구할 수 있습니다.

이벤트 테이블에서 SELECT 명령을 실행하여 저장된 추적 이벤트 데이터에 액세스할 수 있습니다. 자세한 내용은 추적 데이터에 액세스하기 섹션을 참조하십시오.

참고

추적 이벤트를 추가할 때 염두에 두어야 할 지침은 추적 이벤트를 추가하는 일반 지침 섹션을 참조하십시오.

Snowflake에서 로깅 설정 및 메시지 검색에 대한 일반적인 정보는 함수 및 프로시저의 메시지 로깅하기 섹션을 참조하십시오.

코드에서 로깅하기 전에 다음을 수행해야 합니다.

  • 처리기 코드에서 로깅된 메시지를 수집하도록 이벤트 테이블을 설정합니다.

    자세한 내용은 이벤트 테이블 설정하기 섹션을 참조하십시오.

  • 원하는 데이터가 이벤트 테이블에 저장되도록 추적 수준을 설정했는지 확인합니다.

    자세한 내용은 추적 수준 설정하기 섹션을 참조하십시오.

참고

추적 이벤트를 추가할 때 염두에 두어야 할 지침은 추적 이벤트를 추가하는 일반 지침 섹션을 참조하십시오.

원격 분석 패키지에 대한 지원 추가하기

원격 분석 패키지를 사용하려면 Snowflake에 포함된 오픈 소스 Snowflake 원격 분석 패키지 를 처리기 코드에서 사용 가능하도록 만들어야 합니다. 패키지는 Anaconda Snowflake 채널 에서 구할 수 있습니다.

  • CREATE PROCEDURE 또는 CREATE FUNCTION 문의 PACKAGES 절에 snowflake-telemetry-python 패키지를 포함합니다. PACKAGES 절은 포함된 Snowflake 원격 분석 패키지를 코드에 사용할 수 있도록 합니다.

    다음 예제의 코드에서는 PACKAGES 절을 사용하여 원격 분석 패키지와 Snowpark 라이브러리(Python으로 작성된 저장 프로시저에 필요함 – 자세한 내용은 Python으로 저장 프로시저 작성하기 참조)를 참조합니다.

    CREATE OR REPLACE FUNCTION my_function(...)
      RETURNS ...
      LANGUAGE PYTHON
      ...
      PACKAGES = ('snowflake-telemetry-python')
      ...
    
    Copy
  • 처리기 코드에서 telemetry 패키지를 가져옵니다.

    from snowflake import telemetry
    
    Copy

추적 이벤트 추가하기

telemetry.add_event 메서드를 호출하고 이벤트 이름을 전달하여 추적 이벤트를 추가할 수 있습니다. 선택적으로 특성(키-값 페어)을 이벤트와 연결할 수도 있습니다.

add_event 메서드는 다음 형식으로 사용할 수 있습니다.

telemetry.add_event(<name>, <attributes>)
Copy

여기서

  • name 은 추적 이벤트의 이름을 지정하는 Python 문자열입니다.

  • attributes 는 이 추적 이벤트의 특성을 지정하는 OpenTelemetry Attributes 오브젝트 입니다. 이 인자는 선택 사항입니다. 이 추적 이벤트에 대해 지정할 특성이 없으면 인자를 생략하십시오.

다음 예제의 처리기 코드에서는 FunctionEmptyEventFunctionEventWithAttributes 의 두 이벤트가 추가됩니다. 이 코드에서 FunctionEventWithAttributes 를 사용하면 key1key2 의 두 가지 특성도 추가됩니다.

telemetry.add_event("FunctionEmptyEvent")
telemetry.add_event("FunctionEventWithAttributes", {"key1": "value1", "key2": "value2"})
Copy

이러한 이벤트를 추가하면 이벤트 테이블에 두 개의 행이 생기는데, RECORD 열의 값이 각각 다릅니다.

{
  "name": "FunctionEmptyEvent"
}
Copy
{
  "name": "FunctionEventWithAttributes"
}
Copy

FunctionEventWithAttributes 이벤트 행의 RECORD_ATTRIBUTES 열에 다음 특성을 포함합니다.

{
  "key1": "value1",
  "key2": "value2"
}
Copy

범위 특성 추가하기

telemetry.set_span_attribute 메서드를 호출하여 범위와 연결된 특성(키-값 페어)을 설정할 수 있습니다.

범위에 대한 자세한 내용은 Snowflake가 추적 이벤트를 표시하는 방법 섹션을 참조하십시오.

set_span_attribute 메서드는 다음 형식으로 사용할 수 있습니다.

telemetry.set_span_attribute(<key>, <value>)
Copy

여기서,

다음 예제의 코드는 4가지 특성을 만들고 해당 값을 설정합니다.

// Setting span attributes.
telemetry.set_span_attribute("example.boolean", true);
telemetry.set_span_attribute("example.long", 2);
telemetry.set_span_attribute("example.double", 2.5);
telemetry.set_span_attribute("example.string", "testAttribute");
Copy

이러한 특성을 설정하면 이벤트 테이블의 RECORD_ATTRIBUTES 열에 다음 내용이 표시됩니다.

{
  "example.boolean": true,
  "example.long": 2,
  "example.double": 2.5,
  "example.string": "testAttribute"
}
Copy

Python 예제

다음 섹션에서는 Python 저장 프로시저함수 에서 추적 이벤트를 추가하는 예제를 소개합니다.

저장 프로시저 예제

CREATE OR REPLACE PROCEDURE do_tracing()
RETURNS VARIANT
LANGUAGE PYTHON
PACKAGES=('snowflake-snowpark-python', 'snowflake-telemetry-python')
RUNTIME_VERSION=3.8
HANDLER='run'
AS $$
from snowflake import telemetry
def run(session):
  telemetry.set_span_attribute("example.proc.do_tracing", "begin")
  telemetry.add_event("event_with_attributes", {"example.key1": "value1", "example.key2": "value2"})
  return "SUCCESS"
$$;
Copy

UDF의 예

CREATE OR REPLACE FUNCTION times_two(x number)
RETURNS NUMBER
LANGUAGE PYTHON
PACKAGES=('snowflake-telemetry-python')
RUNTIME_VERSION=3.8
HANDLER = 'times_two'
AS $$
from snowflake import telemetry
def times_two(x):
  telemetry.set_span_attribute("example.func.times_two", "begin")
  telemetry.add_event("event_without_attributes")
  telemetry.add_event("event_with_attributes", {"example.key1": "value1", "example.key2": "value2"})

  response = 2 * x

  telemetry.set_span_attribute("example.func.times_two.response", response)

  return response
$$;
Copy

입력 행을 처리하는 Python 함수에서 추적 이벤트 API를 호출하면 UDF에서 처리하는 모든 행에 대해 API가 호출됩니다.

예를 들어 다음 문은 50개의 행에 대해 이전 예제에서 정의한 Python 함수를 호출하므로 100개의 추적 이벤트(각 행당 2개)가 생성됩니다.

select count(times_two(seq8())) from table(generator(rowcount => 50));
Copy

UDTF의 예

CREATE OR REPLACE FUNCTION digits_of_number(input number)
RETURNS TABLE(result number)
LANGUAGE PYTHON
PACKAGES=('snowflake-telemetry-python')
RUNTIME_VERSION=3.8
HANDLER = 'TableFunctionHandler'
AS $$
from snowflake import telemetry

class TableFunctionHandler:

  def __init__(self):
    telemetry.add_event("test_udtf_init")

  def process(self, input):
    telemetry.add_event("test_udtf_process", {"input": str(input)})
    response = input

    while input > 0:
      response = input % 10
      input /= 10
      yield (response,)

  def end_partition(self):
    telemetry.add_event("test_udtf_end_partition")
$$;
Copy

UDTF 처리기 클래스의 process() 메서드에서 추적 이벤트 API를 호출하면 처리된 모든 행에 대해 API가 호출됩니다.

예를 들어 다음 문은 50개의 행에 대해 이전 예제에서 정의한 process() 메서드를 호출하므로 process() 메서드에 의해 100개의 추적 이벤트(각 행당 2개)가 추가됩니다.

select * from table(generator(rowcount => 50)), table(digits_of_number(seq8())) order by 1;
Copy