OpenTelemetry API를 사용한 파이썬(Python) 커스텀 계측
Overview
There are a few reasons to manually instrument your applications with the OpenTelemetry API:
- You are not using Datadog supported library instrumentation.
- You want to extend the
ddtrace library’s functionality. - You need finer control over instrumenting your applications.
The ddtrace library provides several techniques to help you achieve these goals. The following sections demonstrate how to use the OpenTelemetry API for custom instrumentation to use with Datadog.
설정
OpenTelemetry를 설정하여 Datadog 트레이스 공급자를 사용하려면,
자동-계측 및 설정에 대한 지침을 아직 읽지 않았다면 파이썬 설정 지침부터 시작하세요.
DD_TRACE_OTEL_ENABLED 환경 변수를 true로 설정합니다.
커스텀 스팬(span) 생성하기
기존 트레이스 컨텍스트 내에서 커스텀 스팬을 생성하려면 다음을 수행하세요.
from opentelemetry import trace
tracer = trace.get_tracer(__name__)
def do_work():
with tracer.start_as_current_span("operation_name") as span:
# Perform the work that you want to track with the span
print("Doing work...")
# When the 'with' block ends, the span is automatically closed
활성 스팬에 액세스(스팬(span))
현재 활성화된 스팬(span)에 액세스하려면 get_current_span() 함수를 사용합니다:
from opentelemetry import trace
current_span = trace.get_current_span()
# enrich 'current_span' with information
스팬(span) 태그 추가하기
스팬에 속성을 추가해 추가 컨텍스트 또는 메타데이터를 제공합니다.
현재 스팬에 속성을 추가하는 방법에 대한 예시를 제공합니다.
from opentelemetry import trace
current_span = trace.get_current_span()
current_span.set_attribute("attribute_key1", 1)
참고 자료