Unsure when to use OpenTelemetry with Datadog? Start with Custom Instrumentation with the OpenTelemetry API to learn more.

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 트레이스 공급자를 사용하려면,

  1. OpenTelemetry API 패키지를 설치합니다.
composer require open-telemetry/sdk
  1. OpenTelemetry PHP 매뉴얼 계측 설명서에 따라 PHP 코드에 원하는 OpenTelemetry 계측을 수동으로 추가하세요.

  2. Datadog PHP 추적 라이브러리]6을 설치합니다.

  3. DD_TRACE_OTEL_ENABLEDtrue로 설정합니다.

Datadog는 이러한 OpenTelemetry 스팬(span)을 다른 Datadog 애플리케이션 성능 모니터링(APM) 스팬(span)과 결합하여 애플리케이션의 단일 트레이스로 만듭니다.

스팬(span) 태그 추가하기

스팬(span)을 시작하는 바로 그 순간에 속성을 추가할 수 있습니다:

$span = $tracer->spanBuilder('mySpan')
    ->setAttribute('key', 'value')
    ->startSpan();

또는 스팬(span) 이 활성화되어 있는 동안,

$activeSpan = OpenTelemetry\API\Trace\Span::getCurrent();

$activeSpan->setAttribute('key', 'value');

스팬(span)에 오류 설정

예외가 발생할 때 활성화되어 있는 경우 예외 정보가 캡처되고 스팬(span)에 첨부됩니다.

// Create a span
$span = $tracer->spanBuilder('mySpan')->startSpan();

throw new \Exception('Oops!');

// 'mySpan' will be flagged as erroneous and have 
// the stack trace and exception message attached as tags

트레이스에 오류 플래그를 지정하는 것도 수동으로 할 수 있습니다:

use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\Context\Context;

// Can only be done after the setup steps, such as initializing the tracer.

try {
    throw new \Exception('Oops!');
} catch (\Exception $e) {
    $rootSpan = Span::fromContext(Context::getRoot());
    $rootSpan->recordException($e);
}

스팬(span) 추가

스팬(span)을 추가하려면,

// Get a tracer or use an existing one
$tracerProvider = \OpenTelemetry\API\Globals::tracerProvider();
$tracer = $tracerProvider->getTracer('datadog')

// Create a span
$span = $tracer->spanBuilder('mySpan')->startSpan();

// ... do stuff

// Close the span
$span->end();

활성 스팬에 액세스(스팬(span))

현재 활성화된 스팬(span) 에 액세스하려면:

스팬(span) = OpenTelemetry\API\추적하다\스팬(span)::getCurrent();

참고 자료