iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐶

Using OpenTelemetry Collector to Send Trace Data to Datadog

に公開

This is a technical supplementary article about the presentation given at Japan Datadog User Group #12.

https://datadog-jp.connpass.com/event/360923/

The presentation slides are available here.

https://speakerdeck.com/tetsuya28/datadog-distribution-of-opentelemetry-collector-intro

Introduction

In this article, we will try sending trace data to Datadog using OpenTelemetry-based instrumentation with the OpenTelemetry Collector, instead of using the official SDKs provided by Datadog.

By intentionally using OpenTelemetry-based instrumentation instead of Datadog's official SDK, you gain the benefit of minimizing application-side changes when switching to other observability tools in the future or using a different tool for the local development environment.

For the OpenTelemetry Collector, we will use the Datadog Distribution of OpenTelemetry Collector (hereafter referred to as DDOT), which is the official distribution provided by Datadog.

https://docs.datadoghq.com/ja/opentelemetry/setup/ddot_collector/

For an explanation of DDOT and selection criteria, please refer to the presentation slides above.

Goal

The goal is to send trace data to Datadog using the OpenTelemetry Collector.

Prerequisites

Assume the application is implemented in Go and runs on Kubernetes.

Assume the Datadog Container Agent is deployed on Kubernetes using the Datadog Operator.

Configuration Steps

Configuring the Datadog Operator

First, update the Datadog Operator to a version that supports DDOT (v1.15.0 or later).

https://github.com/DataDog/datadog-operator/releases/tag/v1.15.0

Use the following Helm repository.

https://helm.datadoghq.com

values.yaml
image:
  repository: gcr.io/datadoghq/operator
  tag: 1.15.0

Enabling DDOT in the Datadog Container Agent

Next, configure the Datadog Container Agent.

You can check the Custom Resource configuration at the following URL.

https://github.com/DataDog/datadog-operator/blob/main/docs/configuration.v2alpha1.md

To enable DDOT, configure the following two settings:

  1. Set spec.override.nodeAgent.image.tag to a -full image version 7.65.0 or later.
  2. Set spec.features.otelCollector.enabled to true.
datadog-container-agent.yaml
apiVersion: datadoghq.com/v2alpha1
kind: DatadogAgent
metadata:
  name: datadog
spec:
  global:
    clusterName: xxx
    tags:
      - env:prd
    credentials:
      apiSecret:
        secretName: datadog-secret
        keyName: api-key
    logLevel: warn
  override:
    clusterAgent:
      createPodDisruptionBudget: true
      replicas: 2
    nodeAgent:
      env:
        - name: DD_LOG_FORMAT_JSON
          value: "true"
      image:
        tag: 7.66.0-full
  features:
    otelCollector:
      enabled: true
      ports:
        - containerPort: 4317
          hostPort: 4317
          name: otel-grpc
      conf:
        configData: ""

When deployed on Kubernetes in this state, an otel-agent container will be added to the Pod of the Datadog Container Agent.

Configuration for Datadog in DDOT

Write the DDOT configuration in YAML format in the spec.features.otelCollector.conf.configData of the Datadog resource mentioned above.

receivers:
  otlp:
    protocols:
      grpc:
         endpoint: 0.0.0.0:4317
exporters:
  datadog:
    api:
      key: ${env:DD_API_KEY}
      site: ${env:DD_SITE}
processors:
  # Details described later
  infraattributes:
    allow_hostname_override: true
  resource:
    attributes:
      - key: deployment.environment
        value: prd
        action: upsert
  batch:
    timeout: 10s
  k8sattributes:
    auth_type: serviceAccount
    passthrough: false
    pod_association:
    - sources:
        - from: connection
connectors:
  datadog/connector:
service:
  pipelines:
    traces:
      receivers: [otlp]
      processors: [resource, k8sattributes, infraattributes, batch]
      exporters: [datadog/connector, datadog]
    metrics:
      receivers: [datadog/connector]
      processors: [resource, k8sattributes, infraattributes, batch]
      exporters: [datadog]

Instrumenting the Application

Instrument the application using OpenTelemetry.

Details of the instrumentation method are omitted, but the implementation example is as follows.

main.go
import (
	"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
	otelresource "go.opentelemetry.io/otel/sdk/resource"
	otelsdktrace "go.opentelemetry.io/otel/sdk/trace"
	otelsemconv "go.opentelemetry.io/otel/semconv/v1.12.0"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"google.golang.org/grpc/keepalive"
)

func main() {
	resource := otelresource.NewWithAttributes(
		otelsemconv.SchemaURL,
	)

	sampler := newOtelCustomSampler(
		otelsdktrace.ParentBased(),
	)

	tracerOptions := []otelsdktrace.TracerProviderOption{
		otelsdktrace.WithResource(resource),
		otelsdktrace.WithSampler(sampler),
	}

	dialOptions := []grpc.DialOption{
		grpc.WithTransportCredentials(insecure.NewCredentials()),
		grpc.WithDefaultCallOptions(
			grpc.MaxCallRecvMsgSize(math.MaxInt32),
			grpc.MaxCallSendMsgSize(math.MaxInt32),
		),
		grpc.WithKeepaliveParams(keepalive.ClientParameters{
			Time:                defaultKeepaliveTime,
			Timeout:             defaultKeepaliveTimeout,
			PermitWithoutStream: false,
		}),
	}

	addr := fmt.Sprintf("%s:%s", os.Getenv("DD_OTEL_HOST"), os.Getenv("DD_OTEL_PORT"))

	conn, err := grpc.NewClient(addr, dialOptions...)
	if err != nil {
		return nil, err
	}

	exporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn))
	if err != nil {
		return nil, err
	}
}

Configuring the Application Manifest

To send trace data to the otel-agent container of the Datadog Container Agent running as a DaemonSet, set the environment variables in the application manifest to include the hostIP and the OTLP port specified in the DDOT configuration above.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: xxx
spec:
  selector:
    matchLabels:
      name: xxx
  template:
    metadata:
      labels:
        name: xxx
    spec:
      containers:
        - name: xxx
          image: xxx
          env:
            - name: DD_OTEL_HOST
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: DD_OTEL_PORT
              value: 4317

Other Notes

Usage Cautions

Compatibility with OpenTelemetry semconv

Some additional configuration is needed to align the OpenTelemetry semconv with the Datadog semconv. We will add more information to this article as needed.

Possibility of Double Billing

When DDOT is enabled in the Datadog Container Agent, the hostname collected by the Datadog Container Agent and the hostname collected by DDOT may differ. This can cause the same Kubernetes node to be counted as a separate host, potentially resulting in double billing for Infrastructure hosts.

To address this issue, you can align the hostnames by adding the following configuration to the DDOT processor:

processors:
  infraattributes:
    allow_hostname_override: true

https://github.com/DataDog/datadog-agent/pull/35621

Conclusion

In this article, we intentionally introduced a method to send trace data to Datadog using OpenTelemetry-based instrumentation with the OpenTelemetry Collector, rather than using the official SDK provided by Datadog.

Although not verified in this article, there is also the advantage of unifying instrumentation to OpenTelemetry-based by sending metrics, logs, and profiles via the OpenTelemetry Collector as well.

Discussion