共用方式為


為 Databricks 應用程式設定遙測

這很重要

應用程式遙測目前仍處於 測試階段

Databricks Apps 遙測使用 OpenTelemetry(OTel)協定收集追蹤、日誌和指標,並將這些數據持久化至 Unity Catalog 資料表。 啟用應用程式遙測後,Databricks 會自動擷取系統日誌與使用事件,如使用者登入和直接 API 請求。 你也可以使用 OpenTelemetry SDK 為你的框架新增自訂儀器。

需求規格

  • 你的工作區必須位於支援區域:australiaeastbrazilsouthcanadacentralcentralindiacentraluseastuseastus2germanywestcentralnorthcentralusnortheuropesouthcentralussoutheastasiaswedencentralswitzerlandnorthuksouthwesteuropewestuswestus2, 。 westus3
  • 要在 Unity Catalog 建立新的遙測目標資料表,你需要 CAN MANAGE 對目標目錄和結構,以及 CREATE TABLE 該結構的權限。
  • 要寫入 Unity Catalog 中的現有遙測目標資料表,您需要對CAN MANAGE目標目錄和結構描述擁有權限,或者所有帳號使用者必須在目標資料表上擁有 USE CATALOGUSE SCHEMASELECTMODIFY 權限。
  • 目標資料表必須是受管理的 Delta 資料表,且位於與你的工作區相同的區域內。
  • Databricks 建議在遙測目標表啟用 預測優化 ,以提升查詢效能。

啟用應用程式遙測

備註

如果您在應用程式遙測測試版之前建立了應用程式,必須先停止並重新啟動該應用程式,才能進行以下設定步驟。

要開啟應用程式的遙測,請在應用程式設定中設定遙測表的目錄和架構。

  1. 在你的 Azure Databricks 工作區開啟應用程式細節頁面。
  2. 在概覽標籤中,找到 應用程式遙測 設定區塊,點選 新增
  3. 輸入或瀏覽以選擇目錄與架構。 Azure Databricks 會將遙測資料寫入選定位置的三個資料表:otel_metrics、、 otel_spansotel_logs和 。
  4. (可選)指定一個表格前綴,使表格被命名 <prefix>_otel_metrics為 、 <prefix>_otel_spans<prefix>_otel_logs和 。 Azure Databricks 會附加到現有的資料表上,或如果沒有資料表就會建立它們。
  5. 點選 [儲存]。
  6. 重新部署應用程式,讓遙測資料開始流向 Unity Catalog。

驗證遙測資料

重新部署後,該 otel_logs 表格會自動填入。 這些otel_spansotel_metrics表格只有在您為應用程式新增自訂儀器後才會填入資料。

重新部署應用程式後:

  1. 造訪應用程式網址以產生活動。

  2. 等幾秒鐘,等第一批資料出現。

  3. 請在 Databricks SQL 執行以下查詢以確認資料是否在流動:

    SELECT * FROM <catalog>.<schema>.otel_logs
    LIMIT 10;
    

查詢遙測資料

用於篩選與關聯遙測資料的有用欄位包括 timeservice_nametrace_idspan_idattributes、 和 。 欄位 attributes 是一個包含事件特定元資料的映射,例如 event.name

要查看任何遙測表的完整結構,請執行:

DESCRIBE TABLE <catalog>.<schema>.otel_logs;

以下範例會在otel_logs 表中查詢過去一小時內的系統或自訂 OpenTelemetry 錯誤日誌:

SELECT time, body
FROM <catalog>.<schema>.otel_logs
WHERE service_name = '<app-name>'
  AND severity_text = "ERROR"
  AND time >= current_timestamp() - INTERVAL 1 HOUR
ORDER BY time DESC
LIMIT 100;

新增自訂儀器

新增 OpenTelemetry 自動儀器化功能,以產生自訂的追蹤、指標與日誌。 依照所示更新您的 app.yaml 和相依檔案以符合您的架構。

Streamlit

更新 app.yaml

command: ['opentelemetry-instrument', 'streamlit', 'run', 'app.py']
env:
  - name: OTEL_TRACES_SAMPLER
    value: 'always_on'

更新 requirements.txt

streamlit==1.38.0

# Auto-instrumentation
opentelemetry-distro
opentelemetry-exporter-otlp-proto-grpc

# Required for Streamlit
opentelemetry-instrumentation-tornado

# Host metrics (CPU, memory)
opentelemetry-instrumentation-system-metrics

破折號

更新 app.yaml

command: ['opentelemetry-instrument', 'python', 'app.py']
env:
  - name: OTEL_TRACES_SAMPLER
    value: 'always_on'

更新 requirements.txt

dash
dash-bootstrap-components
pandas
plotly
databricks-sql-connector
databricks-sdk
python-dotenv
dash-ag-grid
opentelemetry-distro[otlp]
opentelemetry-instrumentation-flask
opentelemetry-exporter-otlp-proto-grpc

Flask

更新 app.yaml

command: ['opentelemetry-instrument', 'flask', '--app', 'app.py', 'run', '--no-reload']
env:
  - name: OTEL_TRACES_SAMPLER
    value: 'always_on'

更新 requirements.txt

opentelemetry-distro
opentelemetry-exporter-otlp-proto-grpc
opentelemetry-instrumentation-flask

FastAPI

更新 app.yaml

command: ['opentelemetry-instrument', 'uvicorn', 'app:app', '--host', '0.0.0.0', '--port', '8000']
env:
  - name: OTEL_TRACES_SAMPLER
    value: 'always_on'

更新 requirements.txt

fastapi
uvicorn
opentelemetry-distro
opentelemetry-exporter-otlp-proto-grpc
opentelemetry-instrumentation-fastapi

Node.js

建立 otel.js 檔案︰

'use strict';

import { NodeSDK } from '@opentelemetry/sdk-node';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-proto';
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-proto';

const sdk = new NodeSDK({
  traceExporter: new OTLPTraceExporter(),
  metricReader: new PeriodicExportingMetricReader({
    exporter: new OTLPMetricExporter(),
    exportIntervalMillis: 10000,
  }),
  instrumentations: [
    getNodeAutoInstrumentations({
      '@opentelemetry/instrumentation-fs': { enabled: false },
    }),
  ],
});

try {
  sdk.start();
} catch (e) {
  console.error('OTel SDK failed to start', e);
}

async function shutdown() {
  try {
    await sdk.shutdown();
  } catch (e) {
    console.error('OTel SDK shutdown failed', e);
  } finally {
    process.exit(0);
  }
}
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);

更新 package.json

{
  "name": "nodejs-otel",
  "version": "0.1.0",
  "private": true,
  "main": "app.js",
  "scripts": {
    "start": "node -r ./otel.js app.js"
  },
  "dependencies": {
    "express": "^4.21.2",
    "morgan": "^1.10.0",
    "@opentelemetry/api": "^1.9.0",
    "@opentelemetry/sdk-node": "0.203.0",
    "@opentelemetry/auto-instrumentations-node": "0.67.3",
    "@opentelemetry/exporter-trace-otlp-proto": "0.203.0",
    "@opentelemetry/exporter-metrics-otlp-proto": "0.203.0",
    "@opentelemetry/exporter-logs-otlp-proto": "0.203.0",
    "@opentelemetry/sdk-metrics": "2.0.1"
  }
}

環境變數

啟用應用程式遙測時,Databricks 會自動在應用程式執行環境中設定 OTLP 收集端點、匯出協定、資源屬性及批次處理的環境變數。 欲了解完整的 OTel 環境變數清單,請參見 App 遙測環境變數。

限制與局限

應用程式遙測使用 Zerobus Ingest 連接器將資料寫入 Unity 目錄資料表。 所有 Zerobus Ingest 連接器的限制 都適用於應用程式遙測,包括記錄大小、吞吐量、交付保證及目標表要求的限制。

除了 Zerobus 限制外,應用程式遙測還強制執行每條日誌行的最大大小為 1 MB。