azure-monitor-opentelemetry-exporter-java
microsoft/skills
使用已弃用的导出器或推荐的自动配置包,将 OpenTelemetry 跟踪、指标和日志导出到 Azure Monitor/Application Insights。
...展开全部Azure Monitor OpenTelemetry Java 导出器
⚠️ 弃用通知:此软件包已弃用。请迁移至
azure-monitor-opentelemetry-autoconfigure。请参阅迁移指南以获取详细说明。
将 OpenTelemetry 遥测数据导出到 Azure Monitor / Application Insights。
安装(已弃用)
<dependency><groupid>com.azure</groupid><artifactid>azure-monitor-opentelemetry-exporter</artifactid><version>1.0.0-beta.x</version></dependency>
推荐做法:改用自动配置
<dependency><groupid>com.azure</groupid><artifactid>azure-monitor-opentelemetry-autoconfigure</artifactid><version>LATEST</version></dependency>
环境变量
APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=xxx;IngestionEndpoint=https://xxx.in.applicationinsights.azure.com/
使用自动配置进行基本设置(推荐)
使用环境变量
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder;
import io.opentelemetry.api.OpenTelemetry;
import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporter;
// 来自 APPLICATIONINSIGHTS_CONNECTION_STRING 环境变量的连接字符串
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();
AzureMonitorExporter.customize(sdkBuilder);
OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk();
使用显式连接字符串
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();
AzureMonitorExporter.customize(sdkBuilder, "{connection-string}");
OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk();
创建跨度(Span)
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Scope;
// 获取追踪器
Tracer tracer = openTelemetry.getTracer("com.example.myapp");
// 创建跨度
Span span = tracer.spanBuilder("myOperation").startSpan();
try (Scope scope = span.makeCurrent()) {
// 您的应用程序逻辑
doWork();
} catch (Throwable t) {
span.recordException(t);
throw t;
} finally {
span.end();
}
添加跨度属性
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
Span span = tracer.spanBuilder("processOrder")
.setAttribute("order.id", "12345")
.setAttribute("customer.tier", "premium")
.startSpan();
try (Scope scope = span.makeCurrent()) {
// 在执行期间添加属性
span.setAttribute("items.count", 3);
span.setAttribute("total.amount", 99.99);
processOrder();
} finally {
span.end();
}
自定义跨度处理器
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.context.Context;
private static final AttributeKey<string> CUSTOM_ATTR = AttributeKey.stringKey("custom.attribute");
SpanProcessor customProcessor = new SpanProcessor() {
@Override
public void onStart(Context context, ReadWriteSpan span) {
// 向每个跨度添加自定义属性
span.setAttribute(CUSTOM_ATTR, "customValue");
}
@Override
public boolean isStartRequired() {
return true;
}
@Override
public void onEnd(ReadableSpan span) {
// 如有需要,进行后处理
}
@Override
public boolean isEndRequired() {
return false;
}
};
// 注册处理器
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();
AzureMonitorExporter.customize(sdkBuilder);
sdkBuilder.addTracerProviderCustomizer(
(sdkTracerProviderBuilder, configProperties) ->
sdkTracerProviderBuilder.addSpanProcessor(customProcessor)
);
OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk();
</string>
嵌套跨度
public void parentOperation() {
Span parentSpan = tracer.spanBuilder("parentOperation").startSpan();
try (Scope scope = parentSpan.makeCurrent()) {
childOperation();
} finally {
parentSpan.end();
}
}
public void childOperation() {
// 通过上下文自动链接到父跨度
Span childSpan = tracer.spanBuilder("childOperation").startSpan();
try (Scope scope = childSpan.makeCurrent()) {
// 子任务
} finally {
childSpan.end();
}
}
记录异常
Span span = tracer.spanBuilder("riskyOperation").startSpan();
try (Scope scope = span.makeCurrent()) {
performRiskyWork();
} catch (Exception e) {
span.recordException(e);
span.setStatus(StatusCode.ERROR, e.getMessage());
throw e;
} finally {
span.end();
}
指标(通过 OpenTelemetry)
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.metrics.LongHistogram;
Meter meter = openTelemetry.getMeter("com.example.myapp");
// 计数器
LongCounter requestCounter = meter.counterBuilder("http.requests")
.setDescription("总 HTTP 请求数")
.setUnit("requests")
.build();
requestCounter.add(1, Attributes.of(
AttributeKey.stringKey("http.method"), "GET",
AttributeKey.longKey("http.status_code"), 200L
));
// 直方图
LongHistogram latencyHistogram = meter.histogramBuilder("http.latency")
.setDescription("请求延迟")
.setUnit("ms")
.ofLongs()
.build();
latencyHistogram.record(150, Attributes.of(
AttributeKey.stringKey("http.route"), "/api/users"
));
关键概念
| 概念 | 描述 |
|---|---|
| 连接字符串 | 包含仪器密钥的 Application Insights 连接字符串 |
| 追踪器(Tracer) | 为分布式跟踪创建跨度 |
| 跨度(Span) | 表示带有计时和属性的工作单元 |
| 跨度处理器(SpanProcessor) | 拦截跨度生命周期以进行自定义 |
| 导出器(Exporter) | 将遥测数据发送到 Azure Monitor |
迁移到自动配置
azure-monitor-opentelemetry-autoconfigure 软件包提供:
- 对常用库的自动插桩
- 简化的配置
- 与 OpenTelemetry SDK 更好的集成
迁移步骤
-
替换依赖项:
<dependency><groupid>com.azure</groupid><artifactid>azure-monitor-opentelemetry-exporter</artifactid></dependency><dependency><groupid>com.azure</groupid><artifactid>azure-monitor-opentelemetry-autoconfigure</artifactid></dependency> -
根据迁移指南更新初始化代码
最佳实践
- 使用自动配置 — 迁移至
azure-monitor-opentelemetry-autoconfigure - 设置有意义的跨度名称 — 使用描述性的操作名称
- 添加相关属性 — 包含用于调试的上下文数据
- 处理异常 — 始终在跨度上记录异常
- 使用语义约定 — 遵循 OpenTelemetry 语义约定
- 在 finally 块中结束跨度 — 确保跨度始终被结束
- 使用 try-with-resources — 使用 try-with-resources 模式进行范围管理
参考链接
| 资源 | URL |
|---|---|
| Maven 软件包 | https://central.sonatype.com/artifact/com.azure/azure-monitor-opentelemetry-exporter |
| GitHub | https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/monitor/azure-monitor-opentelemetry-exporter |
| 迁移指南 | https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/MIGRATION.md |
| 自动配置软件包 | https://central.sonatype.com/artifact/com.azure/azure-monitor-opentelemetry-autoconfigure |
| OpenTelemetry Java | https://opentelemetry.io/docs/languages/java/ |
| Application Insights | https://learn.microsoft.com/azure/azure-monitor/app/app-insights-overview |
---
name: azure-monitor-opentelemetry-exporter-java
description: Export OpenTelemetry traces, metrics, and logs to Azure Monitor/Application Insights using the deprecated exporter or the recommended autoconfigure package.
license: MIT
---
# Azure Monitor OpenTelemetry Exporter for Java
> **⚠️ DEPRECATION NOTICE**: This package is deprecated. Migrate to `azure-monitor-opentelemetry-autoconfigure`.
>
> See [Migration Guide](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/MIGRATION.md) for detailed instructions.
Export OpenTelemetry telemetry data to Azure Monitor / Application Insights.
## Installation (Deprecated)
```xml
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-monitor-opentelemetry-exporter</artifactId>
<version>1.0.0-beta.x</version>
</dependency>
```
## Recommended: Use Autoconfigure Instead
```xml
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-monitor-opentelemetry-autoconfigure</artifactId>
<version>LATEST</version>
</dependency>
```
## Environment Variables
```bash
APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=xxx;IngestionEndpoint=https://xxx.in.applicationinsights.azure.com/
```
## Basic Setup with Autoconfigure (Recommended)
### Using Environment Variable
```java
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder;
import io.opentelemetry.api.OpenTelemetry;
import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporter;
// Connection string from APPLICATIONINSIGHTS_CONNECTION_STRING env var
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();
AzureMonitorExporter.customize(sdkBuilder);
OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk();
```
### With Explicit Connection String
```java
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();
AzureMonitorExporter.customize(sdkBuilder, "{connection-string}");
OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk();
```
## Creating Spans
```java
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Scope;
// Get tracer
Tracer tracer = openTelemetry.getTracer("com.example.myapp");
// Create span
Span span = tracer.spanBuilder("myOperation").startSpan();
try (Scope scope = span.makeCurrent()) {
// Your application logic
doWork();
} catch (Throwable t) {
span.recordException(t);
throw t;
} finally {
span.end();
}
```
## Adding Span Attributes
```java
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
Span span = tracer.spanBuilder("processOrder")
.setAttribute("order.id", "12345")
.setAttribute("customer.tier", "premium")
.startSpan();
try (Scope scope = span.makeCurrent()) {
// Add attributes during execution
span.setAttribute("items.count", 3);
span.setAttribute("total.amount", 99.99);
processOrder();
} finally {
span.end();
}
```
## Custom Span Processor
```java
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.context.Context;
private static final AttributeKey<String> CUSTOM_ATTR = AttributeKey.stringKey("custom.attribute");
SpanProcessor customProcessor = new SpanProcessor() {
@Override
public void onStart(Context context, ReadWriteSpan span) {
// Add custom attribute to every span
span.setAttribute(CUSTOM_ATTR, "customValue");
}
@Override
public boolean isStartRequired() {
return true;
}
@Override
public void onEnd(ReadableSpan span) {
// Post-processing if needed
}
@Override
public boolean isEndRequired() {
return false;
}
};
// Register processor
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();
AzureMonitorExporter.customize(sdkBuilder);
sdkBuilder.addTracerProviderCustomizer(
(sdkTracerProviderBuilder, configProperties) ->
sdkTracerProviderBuilder.addSpanProcessor(customProcessor)
);
OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk();
```
## Nested Spans
```java
public void parentOperation() {
Span parentSpan = tracer.spanBuilder("parentOperation").startSpan();
try (Scope scope = parentSpan.makeCurrent()) {
childOperation();
} finally {
parentSpan.end();
}
}
public void childOperation() {
// Automatically links to parent via Context
Span childSpan = tracer.spanBuilder("childOperation").startSpan();
try (Scope scope = childSpan.makeCurrent()) {
// Child work
} finally {
childSpan.end();
}
}
```
## Recording Exceptions
```java
Span span = tracer.spanBuilder("riskyOperation").startSpan();
try (Scope scope = span.makeCurrent()) {
performRiskyWork();
} catch (Exception e) {
span.recordException(e);
span.setStatus(StatusCode.ERROR, e.getMessage());
throw e;
} finally {
span.end();
}
```
## Metrics (via OpenTelemetry)
```java
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.metrics.LongHistogram;
Meter meter = openTelemetry.getMeter("com.example.myapp");
// Counter
LongCounter requestCounter = meter.counterBuilder("http.requests")
.setDescription("Total HTTP requests")
.setUnit("requests")
.build();
requestCounter.add(1, Attributes.of(
AttributeKey.stringKey("http.method"), "GET",
AttributeKey.longKey("http.status_code"), 200L
));
// Histogram
LongHistogram latencyHistogram = meter.histogramBuilder("http.latency")
.setDescription("Request latency")
.setUnit("ms")
.ofLongs()
.build();
latencyHistogram.record(150, Attributes.of(
AttributeKey.stringKey("http.route"), "/api/users"
));
```
## Key Concepts
| Concept | Description |
|---------|-------------|
| Connection String | Application Insights connection string with instrumentation key |
| Tracer | Creates spans for distributed tracing |
| Span | Represents a unit of work with timing and attributes |
| SpanProcessor | Intercepts span lifecycle for customization |
| Exporter | Sends telemetry to Azure Monitor |
## Migration to Autoconfigure
The `azure-monitor-opentelemetry-autoconfigure` package provides:
- Automatic instrumentation of common libraries
- Simplified configuration
- Better integration with OpenTelemetry SDK
### Migration Steps
1. Replace dependency:
```xml
<!-- Remove -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-monitor-opentelemetry-exporter</artifactId>
</dependency>
<!-- Add -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-monitor-opentelemetry-autoconfigure</artifactId>
</dependency>
```
2. Update initialization code per [Migration Guide](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/MIGRATION.md)
## Best Practices
1. **Use autoconfigure** — Migrate to `azure-monitor-opentelemetry-autoconfigure`
2. **Set meaningful span names** — Use descriptive operation names
3. **Add relevant attributes** — Include contextual data for debugging
4. **Handle exceptions** — Always record exceptions on spans
5. **Use semantic conventions** — Follow OpenTelemetry semantic conventions
6. **End spans in finally** — Ensure spans are always ended
7. **Use try-with-resources** — Scope management with try-with-resources pattern
## Reference Links
| Resource | URL |
|----------|-----|
| Maven Package | https://central.sonatype.com/artifact/com.azure/azure-monitor-opentelemetry-exporter |
| GitHub | https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/monitor/azure-monitor-opentelemetry-exporter |
| Migration Guide | https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/MIGRATION.md |
| Autoconfigure Package | https://central.sonatype.com/artifact/com.azure/azure-monitor-opentelemetry-autoconfigure |
| OpenTelemetry Java | https://opentelemetry.io/docs/languages/java/ |
| Application Insights | https://learn.microsoft.com/azure/azure-monitor/app/app-insights-overview |
所有文件
0 个文件安装 azure-monitor-opentelemetry-exporter-java
将技能文件下载并解压至你的 .claude/skills/ 目录。
下载ZIP克隆仓库并复制技能文件到您的项目中。
git clone https://github.com/microsoft/skills/tree/main/.github/plugins/azure-sdk-java/skills/azure-monitor-opentelemetry-exporter-java # Copy SKILL.md to your .claude/skills/ directory
复制





首页
