Azure Monitor OpenTelemetry Exporter for Java
將 OpenTelemetry 資料匯出至 Azure Monitor
此技能協助 Java 開發人員將 OpenTelemetry 追蹤、指標和日誌匯出至 Azure Monitor 和 Application Insights。它提供已棄用匯出器套件的程式碼範例,以及遷移至建議的 autoconfigure 解決方案的指引。
下載技能 ZIP
在 Claude 中上傳
前往 設定 → 功能 → 技能 → 上傳技能
開啟並開始使用
測試它
正在使用「Azure Monitor OpenTelemetry Exporter for Java」。 展示如何在我的 Java 應用程式中設定 Azure Monitor OpenTelemetry 匯出器
預期結果:
若要在 Java 中設定 Azure Monitor OpenTelemetry 匯出器,請將 autoconfigure 相依性新增至您的 pom.xml 並設定連接字串。建議的方法是使用 azure-monitor-opentelemetry-autoconfigure 而非已棄用的匯出器。設定 APPLICATIONINSIGHTS_CONNECTION_STRING 環境變數,並使用 AutoConfiguredOpenTelemetrySdk.builder() 初始化 SDK。
正在使用「Azure Monitor OpenTelemetry Exporter for Java」。 如何建立具有 Azure Monitor 自訂屬性的 span?
預期結果:
從 OpenTelemetry 取得 Tracer,使用 tracer.spanBuilder() 建立 Span,使用 setAttribute() 設定屬性,並確保在 finally 區塊中呼叫 span.end()。使用搭配 makeCurrent() 的 try-with-resources 以進行正確的內容傳播。使用 span.recordException() 記錄例外��以便在 Application Insights 中進行錯誤追蹤。
安全審計
安全This is a prompt-only documentation skill containing code examples for using the Azure Monitor OpenTelemetry Exporter library. Static analysis scanned 0 files and detected 0 security issues. The skill provides informational content about a deprecated Microsoft library with migration guidance. No executable code, network calls, or file system access is present in the skill itself.
品質評分
你能建構什麼
為 Java 應用程式設定 Azure 監控
設定 OpenTelemetry 將追蹤和指標從 Java 應用程式匯出至 Azure Monitor,以進行集中式可觀測性
從已棄用匯出器遷移
從已棄用的 azure-monitor-opentelemetry-exporter 移轉至新的 azure-monitor-opentelemetry-autoconfigure 套件
實作分散式追蹤
建立 span、巢狀 span 並記錄例外,以在 Azure 中追蹤跨微服務的要求流程
試試這些提示
展示如何在我的 Java 應用程式中設定 Azure Monitor OpenTelemetry 匯出器。我想使用連接字串將追蹤匯出至 Application Insights。
如何在 Java 中建立具有自訂屬性的 OpenTelemetry span 並將其匯出至 Azure Monitor?包含設定屬性和記錄例外的範例。
建立一個自訂 SpanProcessor,在匯出至 Azure Monitor 之前為每個 span 新增自訂屬性。展示如何向 OpenTelemetry SDK 註冊它。
從已棄用的 azure-monitor-opentelemetry-exporter 遷移至 azure-monitor-opentelemetry-autoconfigure 的步驟為何?展示所需的相依性變更和程式碼更新。
最佳實務
- 始終搭配 span.makeCurrent() 使用 try-with-resources 以進行正確的內容管理
- 使用 recordException() 在 span 上記錄例外,以便在 Azure 中進行錯誤追蹤
- 遵循 OpenTelemetry 語意慣例進行屬性命名
避免
- 請勿在原始程式碼中硬式編碼連接字串 - 請使用環境變數
- 避免建立未結束的 span - 始終呼叫 span.end()
- 請勿跳過例外記錄 - 始終在 catch 區塊中呼叫 recordException()