在 Azure 容器應用程式 中匯出 OpenTelemetry 至 Elastic

本組態指南說明如何透過受管理的 OpenTelemetry 代理程式,設定 Azure 容器應用程式 將日誌、追蹤資料和指標轉發至 Elastic。

欲了解更多關於受管理的 OpenTelemetry 代理程式的資訊,請參閱 Azure 容器應用程式 中的 OpenTelemetry 代理程式。

在本指南中,您將了解如何:

  • 建立 Elastic 擷取權杖。
  • 使用 Bicep 或 Azure 入口網站設定 Elastic OTLP 目的地。
  • 設定所需的應用程式環境變數。
  • 對你現有的容器應用程式環境和應用程式套用設定更新。
  • 檢查 Elastic 裡的遙測數據。

先決條件

  • Azure 訂用帳戶
  • 接受 OTLP 擷取的 Elastic 帳戶和目標部署或專案。
  • Azure CLI 已安裝並登入你的 Azure 帳號。
  • 已安裝 Azure 容器應用程式 CLI 擴充功能(供 Bicep 使用)。
az extension add --name containerapp --upgrade

建立 Elastic 擷取權杖

建立一個彈性 API 金鑰,能為目標部署匯入 OpenTelemetry 資料。

所需權限應允許寫入以下訊號的遙測資料:

  • 日誌接收/寫入
  • 追蹤擷取/寫入
  • 指標擷取/寫入

關於權杖權限與 API 金鑰的提供者指引,請參見 彈性 API 金鑰。

設定 OpenTelemetry 目的端

請使用以下選項之一,將 Elastic 配置為 OTel 端點,在你的容器應用程式環境中。

Important

設定受管理的 OpenTelemetry 目的地並不會自動產生遙測結果。 您的應用程式必須已經透過 OpenTelemetry SDK 進行儀器化,以發送追蹤、指標和日誌。

設定用於部署的 CLI 變數:

$RESOURCE_GROUP = "<RESOURCE_GROUP_NAME>"
$ELASTIC_OTLP_ENDPOINT = "https://<YOUR_ELASTIC_ENDPOINT>:443"
$ELASTIC_API_KEY = "<ELASTIC_API_KEY_WITHOUT_PREFIX>"

請使用彈性 OTLP 基礎端點格式:

  • https://<YOUR_ELASTIC_ENDPOINT>:443

使用授權令牌標頭格式:

  • 標頭鍵:Authorization
  • 標頭值: ApiKey <TOKEN>

使用此管理環境資源範例來配置目的地路由:

@secure()
param elasticApiKey string
param elasticOtlpEndpoint string = 'https://<YOUR_ELASTIC_ENDPOINT>:443'

var elasticDestinationName = 'elastic-otlp'
var elasticAuthHeaderValue = 'ApiKey ${elasticApiKey}'

resource environment 'Microsoft.App/managedEnvironments@2026-03-02-preview' = {
  name: '<MANAGED_ENVIRONMENT_NAME>'
  location: '<REGION>'
  properties: {
    openTelemetryConfiguration: {
      destinationsConfiguration: {
        otlpConfigurations: [
          {
            name: elasticDestinationName
            endpoint: elasticOtlpEndpoint
            protocol: 'http'
            insecure: false
            headers: [
              {
                key: 'Authorization'
                value: elasticAuthHeaderValue
              }
            ]
          }
        ]
      }
      tracesConfiguration: {
        destinations: [
          elasticDestinationName
        ]
      }
      logsConfiguration: {
        destinations: [
          elasticDestinationName
        ]
      }
      metricsConfiguration: {
        destinations: [
          elasticDestinationName
        ]
      }
    }
  }
}

請使用此應用程式資源範例來設定所需的應用程式環境變數:

resource app 'Microsoft.App/containerApps@2023-05-01' = {
  name: '<CONTAINER_APP_NAME>'
  location: '<REGION>'
  properties: {
    managedEnvironmentId: environment.id
    template: {
      containers: [
        {
          name: '<CONTAINER_NAME>'
          image: '<IMAGE_NAME>'
          env: [
            {
              name: 'OTEL_SERVICE_NAME'
              value: '<SERVICE_NAME>'
            }
            {
              name: 'OTEL_TRACES_EXPORTER'
              value: 'otlp'
            }
            {
              name: 'OTEL_METRICS_EXPORTER'
              value: 'otlp'
            }
            {
              name: 'OTEL_LOGS_EXPORTER'
              value: 'otlp'
            }
            {
              name: 'OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE'
              value: 'cumulative'
            }
          ]
        }
      ]
    }
  }
}

從儲存庫根目錄套用你的 Bicep 設定:

az deployment group create `
  --resource-group $RESOURCE_GROUP `
  --template-file infra/main.bicep `
  --parameters @infra/main.parameters.json `
  --parameters elasticOtlpEndpoint="$ELASTIC_OTLP_ENDPOINT" `
  --parameters elasticApiKey="$ELASTIC_API_KEY"

你的容器應用程式現在已經設定成將遙測資料傳送給 Elastic。

在 Elastic 中檢查遙測數據

設定完成後,檢查日誌、追蹤和指標是否從你的應用程式傳送,並對應到預期的服務身份。

使用符合你工作流程的彈性視圖。

欲了解更多關於 Elastic 探索與分析的資訊,請參閱 Elastic Observability 文件。