本指南介绍如何在 Azure Monitor Application Insights 中配置 OpenTelemetry (OTel)。 对于 .NET、Node.js和 Python,请使用 Microsoft OpenTelemetry 发行版。 对于Java,请使用 Azure Monitor OpenTelemetry 发行版及其现有的代理或本机映像集成。
首先,为你的编程语言启用 OpenTelemetry。
Microsoft OpenTelemetry 发行版包标识符和安装命令包括:
| 语言 |
Package |
安装命令 |
| Python |
microsoft-opentelemetry |
pip install microsoft-opentelemetry |
| Node.js |
@microsoft/opentelemetry |
npm install @microsoft/opentelemetry |
| .NET |
Microsoft.OpenTelemetry |
dotnet add package Microsoft.OpenTelemetry |
不要同时初始化 Microsoft OpenTelemetry 发行版和 Azure Monitor OpenTelemetry 发行版。 Azure Monitor仍然是遥测目标;独立Azure Monitor导出程序保留其现有名称。
.NET示例使用Microsoft.OpenTelemetry和 OpenTelemetry 命名空间。 在应用程序库加载之前初始化 Node.js 插桩。 ESM 应用程序必须预加载 Microsoft OpenTelemetry 发行版加载程序。 除非示例在代码中设置连接字符串,否则请在启动应用程序之前设置APPLICATIONINSIGHTS_CONNECTION_STRING。
连接字符串
在 Application Insights 中,“Connection String” 定义了用于发送遥测数据的目标地点。
使用以下三种方法之一配置连接字符串:
在UseMicrosoftOpenTelemetry()中配置Program.cs。
// Import the Microsoft OpenTelemetry Distro and supporting APIs.
using Microsoft.OpenTelemetry;
// Create the application builder.
var builder = WebApplication.CreateBuilder(args);
// Configure the Microsoft OpenTelemetry Distro.
builder.UseMicrosoftOpenTelemetry(options =>
{
options.Exporters = ExportTarget.AzureMonitor;
// Set the connection string for Azure Monitor export.
options.AzureMonitor.ConnectionString = "<ConnectionString>";
});
// Build the application with the configured telemetry services.
var app = builder.Build();
// Run the application and its telemetry providers.
app.Run();
设置环境变量。
export APPLICATIONINSIGHTS_CONNECTION_STRING="<ConnectionString>"
将以下部分添加到 appsettings.json其中,然后在初始化发行版时读取它。
{
"AzureMonitor": {
"ConnectionString": "<ConnectionString>"
}
}
// Import the Microsoft OpenTelemetry Distro and supporting APIs.
using Microsoft.OpenTelemetry;
// Create the application builder.
var builder = WebApplication.CreateBuilder(args);
// Configure the Microsoft OpenTelemetry Distro.
builder.UseMicrosoftOpenTelemetry(options =>
{
options.Exporters = ExportTarget.AzureMonitor;
// Read the environment setting before the application configuration.
options.AzureMonitor.ConnectionString =
builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"] ??
builder.Configuration["AzureMonitor:ConnectionString"];
});
// Build the application with the configured telemetry services.
var app = builder.Build();
// Run the application and its telemetry providers.
app.Run();
注意
显式 options.AzureMonitor.ConnectionString 值优先。 配置文件示例在 AzureMonitor 部分之前检查环境变量键。 读取该部分可明确避免依赖隐式配置绑定。
使用以下两种方法之一来配置连接字符串:
为应用程序启动时的所有信号配置 Microsoft OpenTelemetry 发行版:
// Import the Microsoft OpenTelemetry Distro and supporting APIs.
using Microsoft.OpenTelemetry;
using OpenTelemetry;
// Create the SDK and keep its providers alive until application shutdown.
using var sdk = OpenTelemetrySdk.Create(telemetry =>
{
// Configure the Microsoft OpenTelemetry Distro.
telemetry.UseMicrosoftOpenTelemetry(options =>
{
options.Exporters = ExportTarget.AzureMonitor;
// Set the connection string for Azure Monitor export.
options.AzureMonitor.ConnectionString = "<ConnectionString>";
});
});
在应用程序生存期内保持 sdk 活动状态。 仅在关闭时将其释放,以刷新挂起的遥测数据。
设置环境变量。
export APPLICATIONINSIGHTS_CONNECTION_STRING="<ConnectionString>"
注意
如果在多个位置设置连接字符串,则适用以下优先顺序:
- Code
- 环境变量
使用以下三种方法之一配置连接字符串:
将以下部分添加到 applicationinsights.json 配置文件。
{
"connectionString": "<ConnectionString>"
}
您还可以通过指定从文件中加载来设置连接字符串。 该文件应仅包含连接字符串,而不包含其他任何内容。 如果你指定相对路径,系统会相对于 applicationinsights-agent-3.7.9.jar 所在的目录对其进行解析。
{
"connectionString": "${file:connection-string-file.txt}"
}
设置环境变量。
APPLICATIONINSIGHTS_CONNECTION_STRING
添加 applicationinsights.connection.string 为系统属性。
java -javaagent:"<AgentDirectory>/applicationinsights-agent-3.7.9.jar" \
-Dapplicationinsights.connection.string="<ConnectionString>" \
-jar "<AppJarPath>"
注意
如果在多个位置设置连接字符串,则适用以下优先顺序:
- 系统属性
- 环境变量
- 配置文件
如果在同一Java虚拟机(JVM)中部署多个应用程序,并希望将遥测数据发送到不同的连接字符串,请参阅 Connection 字符串重写(预览版)。
请使用以下两种方法之一配置连接字符串:
设置环境变量。
export APPLICATIONINSIGHTS_CONNECTION_STRING="<ConnectionString>"
对于 Spring Boot,请在 application.properties 中设置该属性。
applicationinsights.connection.string=<ConnectionString>
提示
有关 Microsoft OpenTelemetry 发行版配置示例,请参阅 Node.js 示例。
请使用以下两种方法之一配置连接字符串:
设置环境变量。
export APPLICATIONINSIGHTS_CONNECTION_STRING="<ConnectionString>"
使用配置对象。
export class BasicConnectionSample {
static async run() {
// Import the Microsoft OpenTelemetry Distro.
const { useMicrosoftOpenTelemetry } = await import("@microsoft/opentelemetry");
// Configure Azure Monitor export and the options used by this sample.
const options = {
azureMonitor: {
azureMonitorExporterOptions: {
connectionString:
process.env.APPLICATIONINSIGHTS_CONNECTION_STRING ||
"<ConnectionString>",
},
},
};
// Initialize the Microsoft OpenTelemetry Distro.
const monitor = useMicrosoftOpenTelemetry(options);
console.log("Azure Monitor initialized");
}
}
请使用以下两种方法之一配置连接字符串:
设置环境变量。
export APPLICATIONINSIGHTS_CONNECTION_STRING="<ConnectionString>"
使用函数use_microsoft_opentelemetry并启用Azure Monitor导出。
# Import the `use_microsoft_opentelemetry()` function from the
# `microsoft.opentelemetry` package.
from microsoft.opentelemetry import use_microsoft_opentelemetry
# Configure OpenTelemetry to use Azure Monitor with the specified connection string.
use_microsoft_opentelemetry(
enable_azure_monitor=True,
azure_monitor_connection_string="<ConnectionString>",
)
设置云角色名称和云角色实例
适用于 .NET、Node.js 和 Python 的 Microsoft OpenTelemetry 发行版,以及用于 Java 的 Azure Monitor OpenTelemetry,在受支持的情况下检测资源上下文,并为 云角色名称 和云角色实例提供默认值。 你可以为你的应用程序覆盖这些值。 云角色名称在应用程序映射上显示为节点下的名称。
通过 资源 属性设置云角色名称和云角色实例。 云角色名称使用service.namespace和service.name属性,但如果未设置service.name,则回退到service.namespace。 云角色实例使用 service.instance.id 属性值。 有关资源的标准属性的信息,请参阅 OpenTelemetry 语义约定。
设置云角色名称和云角色实例
// Import the Microsoft OpenTelemetry Distro and supporting APIs.
using Microsoft.OpenTelemetry;
using OpenTelemetry;
using OpenTelemetry.Resources;
// Define the service attributes used for cloud role name and instance.
var resourceAttributes = new Dictionary<string, object>
{
["service.name"] = "<ServiceName>",
["service.namespace"] = "<ServiceNamespace>",
["service.instance.id"] = "<ServiceInstanceId>"
};
// Create the application builder.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenTelemetry()
// Configure the Microsoft OpenTelemetry Distro.
.UseMicrosoftOpenTelemetry(options =>
{
options.Exporters = ExportTarget.AzureMonitor;
})
// Apply the resource attributes to traces, metrics, and logs.
.ConfigureResource(resourceBuilder => resourceBuilder.AddAttributes(
resourceAttributes));
// Build the application with the configured telemetry services.
var app = builder.Build();
// Run the application and its telemetry providers.
app.Run();
通过 资源 属性设置云角色名称和云角色实例。 云角色名称使用service.namespace和service.name属性,但如果未设置service.name,则回退到service.namespace。 云角色实例使用 service.instance.id 属性值。 有关资源的标准属性的信息,请参阅 OpenTelemetry 语义约定。
// Import the Microsoft OpenTelemetry Distro and supporting APIs.
using Microsoft.OpenTelemetry;
using OpenTelemetry;
using OpenTelemetry.Resources;
// Define the service attributes used for cloud role name and instance.
var resourceAttributes = new Dictionary<string, object>
{
["service.name"] = "<ServiceName>",
["service.namespace"] = "<ServiceNamespace>",
["service.instance.id"] = "<ServiceInstanceId>"
};
// Create the SDK and keep its providers alive until application shutdown.
using var sdk = OpenTelemetrySdk.Create(telemetry =>
{
// Configure the Microsoft OpenTelemetry Distro.
telemetry.UseMicrosoftOpenTelemetry(options =>
options.Exporters = ExportTarget.AzureMonitor)
// Apply the resource attributes to traces, metrics, and logs.
.ConfigureResource(resourceBuilder =>
resourceBuilder.AddAttributes(resourceAttributes));
});
注意
如果未设置云角色名称和云角色实例,云角色名称默认为 Application Insights 资源的名称,云角色实例默认为计算机名称。
使用以下三种方法之一配置云角色名称和云角色实例:
将以下部分添加到 applicationinsights.json 配置文件。
将 role.name 设置为云角色名称,将 role.instance 设置为云角色实例:
{
"role": {
"name": "<CloudRoleName>",
"instance": "<CloudRoleInstance>"
}
}
设置环境变量。
APPLICATIONINSIGHTS_ROLE_NAME
APPLICATIONINSIGHTS_ROLE_INSTANCE
添加 applicationinsights.role.name 并 applicationinsights.role.instance 作为系统属性。
java -javaagent:"<AgentDirectory>/applicationinsights-agent-3.7.9.jar" \
-Dapplicationinsights.role.name="<CloudRoleName>" \
-Dapplicationinsights.role.instance="<CloudRoleInstance>" \
-jar "<AppJarPath>"
注意
如果在多个位置设置云角色名称和云角色实例,则适用以下优先顺序:
- 系统属性
- 环境变量
- 配置文件
如果在同一 JVM 中部署多个应用程序,并希望将遥测数据发送到不同的云角色名称,请参阅云角色名称替代(预览版)。
若要设置云角色名称,请执行以下操作:
- 使用
spring.application.name 用于 Spring Boot 本地映像应用程序。
- 使用
quarkus.application.name 用于 Quarkus 原生映像应用程序。
注意
Quarkus 社区支持和维护 Quarkus 扩展。 寻求帮助,请使用 Quarkus 社区支持渠道。 Azure不提供对此集成的技术支持。
通过 资源 属性设置云角色名称和云角色实例。 云角色名称使用service.namespace和service.name属性,但如果未设置service.name,则回退到service.namespace。 云角色实例使用 service.instance.id 属性值。 有关资源的标准属性的信息,请参阅 OpenTelemetry 语义约定。
设置云角色名称和云角色实例
export class CloudRoleSample {
static async run() {
// Import the Microsoft OpenTelemetry Distro.
const { useMicrosoftOpenTelemetry } = await import("@microsoft/opentelemetry");
const { resourceFromAttributes } = await import("@opentelemetry/resources");
const { ATTR_SERVICE_NAME } = await import("@opentelemetry/semantic-conventions");
const { ATTR_SERVICE_NAMESPACE, ATTR_SERVICE_INSTANCE_ID } = await import(
"@opentelemetry/semantic-conventions/incubating"
);
// Set the service attributes used for cloud role name and instance.
const customResource = resourceFromAttributes({
[ATTR_SERVICE_NAME]: process.env.OTEL_SERVICE_NAME || "<ServiceName>",
[ATTR_SERVICE_NAMESPACE]:
process.env.OTEL_SERVICE_NAMESPACE || "<ServiceNamespace>",
[ATTR_SERVICE_INSTANCE_ID]:
process.env.OTEL_SERVICE_INSTANCE_ID || "<ServiceInstanceId>",
});
// Configure Azure Monitor export and the options used by this sample.
const options = {
azureMonitor: {
azureMonitorExporterOptions: {
connectionString:
process.env.APPLICATIONINSIGHTS_CONNECTION_STRING ||
"<ConnectionString>",
},
},
resource: customResource,
};
// Initialize the Microsoft OpenTelemetry Distro.
const monitor = useMicrosoftOpenTelemetry(options);
console.log("Azure Monitor initialized (custom resource)");
}
}
通过资源属性设置云角色名称和云角色实例。 云角色名称使用 service.namespace 和 service.name 属性,但如果未设置 service.name,它将回滚到 service.namespace。 云角色实例使用 service.instance.id 属性值。 有关资源的标准属性的信息,请参阅 OpenTelemetry 语义约定。
使用 OTEL_RESOURCE_ATTRIBUTES 和/或 OTEL_SERVICE_NAME 环境变量设置资源特性。
OTEL_RESOURCE_ATTRIBUTES 采用一系列逗号分隔的键值对。 可以合并服务命名空间和服务名称来设置云角色名称,并使用服务实例 ID 设置云角色实例:
serviceName="<ServiceName>"
serviceNamespace="<ServiceNamespace>"
serviceInstanceId="<ServiceInstanceId>"
resourceAttributes="service.namespace=$serviceNamespace"
resourceAttributes+=",service.instance.id=$serviceInstanceId"
export OTEL_RESOURCE_ATTRIBUTES="$resourceAttributes"
export OTEL_SERVICE_NAME="$serviceName"
如果未设置 service.namespace Resource 属性,也可以仅通过 OTEL_SERVICE_NAME 环境变量或 service.name Resource 属性设置云角色名称:
export OTEL_RESOURCE_ATTRIBUTES="service.instance.id=<ServiceInstanceId>"
export OTEL_SERVICE_NAME="<ServiceName>"
设置资源属性
适用于 .NET、Node.js 和 Python 的 Microsoft OpenTelemetry 发行版,以及用于 Java 的 Azure Monitor OpenTelemetry,可以在受支持的 Azure 环境中启用资源检测。 有关详细信息,请参阅 自动数据收集和资源检测器。
对于手动设置,请使用标准 OpenTelemetry 选项直接设置资源属性:
# Set variables
serviceName="<ServiceName>"
location="<Location>"
subscriptionId="<SubscriptionId>"
resourceGroupName="<ResourceGroupName>"
appName="<AppName>"
# Build App Service resource ID
path="/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName"
provider="/providers/Microsoft.Web/sites/$appName"
resourceId="$path$provider"
resourceAttributes="cloud.provider=azure,cloud.region=$location"
resourceAttributes+=",cloud.resource_id=$resourceId"
export OTEL_SERVICE_NAME="$serviceName"
export OTEL_RESOURCE_ATTRIBUTES="$resourceAttributes"
在 Windows PowerShell 上:
# Set variables
$serviceName = "<ServiceName>"
$location = "<Location>"
$subscriptionId = "<SubscriptionId>"
$resourceGroupName = "<ResourceGroupName>"
$appName = "<AppName>"
# Build App Service resource ID
$path = "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName"
$provider = "/providers/Microsoft.Web/sites/$appName"
$resourceId = "$path$provider"
$resourceAttributes = "cloud.provider=azure,cloud.region=$location"
$resourceAttributes += ",cloud.resource_id=$resourceId"
$env:OTEL_SERVICE_NAME = $serviceName
$env:OTEL_RESOURCE_ATTRIBUTES = $resourceAttributes
启用采样
采样可减少遥测引入量和成本。 发行版支持Azure Monitor导出的固定百分比采样和速率限制采样。 还可以将应用程序日志与跟踪采样决策保持一致。 采样器将采样信息附加到导出的 Span,以便 Application Insights 可以调整体验次数。 有关概念性概述,请参阅 详细了解采样。
重要
- 采样决策适用于 跟踪(跨度)。
-
日志 可以跟随跟踪采样决策。 默认情况下,此行为在适用于 .NET 的 Microsoft OpenTelemetry Distro 中默认启用,并在 Microsoft OpenTelemetry Distro 的 Node.js 和 Python 版本中默认禁用。 请参阅 日志的基于跟踪的采样。
- 指标从不采样。
使用标准 OpenTelemetry 环境变量来选择采样器,并在你的发行版读取这些环境变量的位置提供其参数。 选择一种配置方法;无需在代码和环境变量中配置相同的采样设置。 按照.NET选项卡中的版本特定指南进行操作。 有关采样器名称的详细信息,请参阅 OTEL_TRACES_SAMPLER。
对于 Microsoft.OpenTelemetry 1.1.0,请使用代码示例在 UseMicrosoftOpenTelemetry() 中设置采样。 也不需要设置 OTEL_TRACES_SAMPLER 或 OTEL_TRACES_SAMPLER_ARG。
对于 Microsoft.OpenTelemetry 1.1.0,请使用 中的 OpenTelemetrySdk.Create()。 通过发行版选项配置采样一次;也不需要设置采样器环境变量。
固定百分比采样
-
APPLICATIONINSIGHTS_SAMPLING_PERCENTAGE — 采样百分比
- 值为百分比(例如,
33.333 = ~33.333%)。
限流采样
-
APPLICATIONINSIGHTS_SAMPLING_REQUESTS_PER_SECOND — 每秒最大请求数
有关配置选项和示例,请参阅 “配置采样替代”。
以下示例为 Node.js 和Python设置采样环境变量。 对于上述选项卡中涵盖的.NET版本,请改用基于代码的配置。
注意
以下示例对Java无效。 有关正确的环境变量,请参阅上一Java选项卡。
固定百分比采样(约 10%)
export OTEL_TRACES_SAMPLER="microsoft.fixed_percentage"
export OTEL_TRACES_SAMPLER_ARG=0.1
限速采样(~1.5 追踪/秒)
export OTEL_TRACES_SAMPLER="microsoft.rate_limited"
export OTEL_TRACES_SAMPLER_ARG=1.5
注意
采样器配置和优先级取决于语言。 .NET示例显式设置发行版选项。 Python使用采样器环境变量。 对于 Node.js,请避免代码和环境中的设置冲突。
固定百分比采样
// Import the Microsoft OpenTelemetry Distro and supporting APIs.
using Microsoft.OpenTelemetry;
// Create the application builder.
var builder = WebApplication.CreateBuilder(args);
// Configure the Microsoft OpenTelemetry Distro.
builder.UseMicrosoftOpenTelemetry(options =>
{
options.Exporters = ExportTarget.AzureMonitor;
// Select fixed-percentage sampling instead of rate-limited sampling.
options.AzureMonitor.TracesPerSecond = null;
// Keep approximately 10% of traces.
options.AzureMonitor.SamplingRatio = 0.1F;
});
// Build the application with the configured telemetry services.
var app = builder.Build();
// Run the application and its telemetry providers.
app.Run();
速率限制抽样
// Import the Microsoft OpenTelemetry Distro and supporting APIs.
using Microsoft.OpenTelemetry;
// Create the application builder.
var builder = WebApplication.CreateBuilder(args);
// Configure the Microsoft OpenTelemetry Distro.
builder.UseMicrosoftOpenTelemetry(options =>
{
options.Exporters = ExportTarget.AzureMonitor;
// Limit sampling to approximately 1.5 traces per second.
options.AzureMonitor.TracesPerSecond = 1.5;
});
// Build the application with the configured telemetry services.
var app = builder.Build();
// Run the application and its telemetry providers.
app.Run();
注意
适用于 .NET 的 Microsoft OpenTelemetry 发行版默认采用每秒 5 个跟踪的速率限制采样。 在代码中选择固定百分比采样时,将 TracesPerSecond 设置为 null。
固定百分比采样
// Import the Microsoft OpenTelemetry Distro and supporting APIs.
using Microsoft.OpenTelemetry;
using OpenTelemetry;
// Create the SDK and keep its providers alive until application shutdown.
using var sdk = OpenTelemetrySdk.Create(telemetry =>
{
// Configure the Microsoft OpenTelemetry Distro.
telemetry.UseMicrosoftOpenTelemetry(options =>
{
options.Exporters = ExportTarget.AzureMonitor;
// Select fixed-percentage sampling instead of rate-limited sampling.
options.AzureMonitor.TracesPerSecond = null;
// Keep approximately 10% of traces.
options.AzureMonitor.SamplingRatio = 0.1F;
});
});
速率限制抽样
// Import the Microsoft OpenTelemetry Distro and supporting APIs.
using Microsoft.OpenTelemetry;
using OpenTelemetry;
// Create the SDK and keep its providers alive until application shutdown.
using var sdk = OpenTelemetrySdk.Create(telemetry =>
{
// Configure the Microsoft OpenTelemetry Distro.
telemetry.UseMicrosoftOpenTelemetry(options =>
{
options.Exporters = ExportTarget.AzureMonitor;
// Limit sampling to approximately 1.5 traces per second.
options.AzureMonitor.TracesPerSecond = 1.5;
});
});
注意
适用于 .NET 的 Microsoft OpenTelemetry 发行版默认采用每秒 5 个跟踪的速率限制采样。 使 SDK 在应用程序生存期内保持活动状态。
Microsoft OpenTelemetry Distro for Node.js 默认为每秒 5 个跟踪的速率限制采样。 将 tracesPerSecond: 0 设置为改用 samplingRatio。
固定百分比采样
// Import the Microsoft OpenTelemetry Distro.
const { useMicrosoftOpenTelemetry } = await import("@microsoft/opentelemetry");
// Initialize the Microsoft OpenTelemetry Distro.
const monitor = useMicrosoftOpenTelemetry({
azureMonitor: {
azureMonitorExporterOptions: {
connectionString:
process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "<ConnectionString>",
},
},
// Keep approximately 10% of traces.
samplingRatio: 0.1,
// Use the sampling ratio instead of a rate limit.
tracesPerSecond: 0,
});
速率限制抽样
// Import the Microsoft OpenTelemetry Distro.
const { useMicrosoftOpenTelemetry } = await import("@microsoft/opentelemetry");
// Initialize the Microsoft OpenTelemetry Distro.
const monitor = useMicrosoftOpenTelemetry({
azureMonitor: {
azureMonitorExporterOptions: {
connectionString:
process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "<ConnectionString>",
},
},
// Limit sampling to approximately 1.5 traces per second.
tracesPerSecond: 1.5,
});
注意
如果未在代码中或通过环境变量设置采样器,Azure Monitor默认使用 RateLimitedSampler。
Microsoft OpenTelemetry Python 发行版默认采用每秒 5 个跟踪的速率限制采样。 在初始化发行版之前,请通过环境变量配置采样器。
固定百分比采样
import os
from microsoft.opentelemetry import use_microsoft_opentelemetry
# Select the sampler before initializing the distro.
os.environ["OTEL_TRACES_SAMPLER"] = "microsoft.fixed_percentage"
# Keep approximately 10% of traces.
os.environ["OTEL_TRACES_SAMPLER_ARG"] = "0.1"
# Initialize the Microsoft OpenTelemetry Distro with Azure Monitor export.
use_microsoft_opentelemetry(
enable_azure_monitor=True,
azure_monitor_connection_string="<ConnectionString>",
)
速率限制抽样
import os
from microsoft.opentelemetry import use_microsoft_opentelemetry
# Select the sampler before initializing the distro.
os.environ["OTEL_TRACES_SAMPLER"] = "microsoft.rate_limited"
# Limit sampling to approximately 1.5 traces per second.
os.environ["OTEL_TRACES_SAMPLER_ARG"] = "1.5"
# Initialize the Microsoft OpenTelemetry Distro with Azure Monitor export.
use_microsoft_opentelemetry(
enable_azure_monitor=True,
azure_monitor_connection_string="<ConnectionString>",
)
注意
使用OTEL_TRACES_SAMPLER和OTEL_TRACES_SAMPLER_ARG用于Python采样。 不要沿用较旧的 Azure Monitor 发行版的 sampling_ratio 和 traces_per_second 参数。
提示
使用固定百分比采样时,不确定为采样率设置的值,从 5% (0.05) 开始。 根据故障和性能窗格中所示操作的准确性来调整速率。 任何采样都降低了准确性,因此 对 OpenTelemetry 指标发出警报,这些指标不受采样影响。
在初始化 Microsoft OpenTelemetry 发行版时,从builder.Configuration中读取采样设置并将其分配给options.AzureMonitor.SamplingRatio或options.AzureMonitor.TracesPerSecond 如果选择百分比采样,也设置 TracesPerSecond = null。 验证从 0 到 1 和非负速率限制的比率。 此配置不需要采样器环境变量。
加载应用程序的配置,并将验证后的值传递到 OpenTelemetrySdk.Create() 内的 UseMicrosoftOpenTelemetry()。 使用与代码示例相同的采样属性;发行版不需要第二个提供程序或单独的导出程序。
如果未配置采样,则默认的速率限制采样配置为每秒最多捕获 5 个请求,以及这些请求的所有依赖项和日志。
此配置取代了以前的默认值,即捕获所有请求。 如果仍想要捕获所有请求,请使用固定百分比采样并将采样百分比设置为 100。
固定百分比采样
以下示例演示如何设置采样以捕获大约三分之一的所有请求:
{
"sampling": {
"percentage": 33.333
}
}
使用环境变量设置采样百分比。 它优先于 JSON 配置中指定的采样百分比。
提示
对于采样百分比,请选择一个接近于 100/N 的百分比,其中 N 是整数。 当前采样不支持其他值。
速率限制抽样
注意
速率限制抽样是近似的,因为内部必须随时间调整“固定”抽样百分比,以便在每个遥测记录上发出准确的项计数。 在内部,速率限制采样被调整以在0.1秒内快速适应新的应用程序负载。 因此,你不应看到其大幅或长时间超出配置速率。
以下示例演示如何将采样设置为每秒最多捕获(大约)1 个请求:
{
"sampling": {
"requestsPerSecond": 1
}
}
该值 requestsPerSecond 可以是十进制值,因此可以将其配置为捕获每秒不到一个请求(如果需要)。 例如,值 0.5 表示每 2 秒最多捕获 1 个请求。
使用环境变量设置速率限制。 它优先于 JSON 配置中指定的速率限制。
Java 本地不支持在配置文件中进行采样配置。 若要配置采样,请使用代码或环境变量。
Python不支持在配置文件中配置采样。 若要配置采样,请使用代码或环境变量。
启用此功能时,系统会删除属于 未采样跟踪的 日志记录,以便日志与跟踪采样保持一致。
- 当日志记录具有有效的
SpanId时,它是跟踪的一部分。
- 如果关联的追踪
TraceFlags 指示 未采样,该功能 将删除 日志记录。
- 不含任何跟踪上下文的日志记录不会受到影响。
- 默认值取决于语言和发行版版本。 请参阅下面的每种语言设置。
使用以下设置配置基于跟踪的日志采样:
发行版默认启用基于跟踪的日志采样。 将EnableTraceBasedLogsSampler设置为false以禁用它。
// Configure the Microsoft OpenTelemetry Distro.
builder.Services.AddOpenTelemetry().UseMicrosoftOpenTelemetry(options =>
{
options.Exporters = ExportTarget.AzureMonitor;
});
发行版默认启用基于跟踪的日志采样,包括非托管应用程序。 将EnableTraceBasedLogsSampler设置为false以禁用它。
// Import the Microsoft OpenTelemetry Distro and supporting APIs.
using Microsoft.OpenTelemetry;
using OpenTelemetry;
// Create the SDK and keep its providers alive until application shutdown.
using var sdk = OpenTelemetrySdk.Create(telemetry =>
{
// Configure the Microsoft OpenTelemetry Distro.
telemetry.UseMicrosoftOpenTelemetry(options =>
{
options.Exporters = ExportTarget.AzureMonitor;
});
});
对于 Spring Boot 原生和 Quarkus 原生应用程序,默认启用基于跟踪的采样。
注意
Quarkus 社区支持和维护 Quarkus 扩展。 寻求帮助,请使用 Quarkus 社区支持渠道。 Azure不提供对此集成的技术支持。
// Import the Microsoft OpenTelemetry Distro.
const { useMicrosoftOpenTelemetry } = await import("@microsoft/opentelemetry");
// Initialize the Microsoft OpenTelemetry Distro.
const monitor = useMicrosoftOpenTelemetry({
azureMonitor: {
// Drop logs associated with unsampled traces.
enableTraceBasedSamplingForLogs: true,
azureMonitorExporterOptions: {
connectionString:
process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "<ConnectionString>",
},
},
});
from microsoft.opentelemetry import use_microsoft_opentelemetry
# Initialize the Microsoft OpenTelemetry Distro with Azure Monitor export.
use_microsoft_opentelemetry(
enable_azure_monitor=True,
azure_monitor_connection_string="<ConnectionString>",
# Drop logs associated with unsampled traces.
enable_trace_based_sampling_for_logs=True,
)
实时指标
实时指标提供实时分析仪表板,用于深入了解应用程序活动和性能。
此功能默认启用。
配置 Microsoft OpenTelemetry 发行版时,可以禁用实时指标。
// Configure the Microsoft OpenTelemetry Distro.
builder.Services.AddOpenTelemetry().UseMicrosoftOpenTelemetry(options =>
{
options.Exporters = ExportTarget.AzureMonitor;
// Disable Live Metrics.
options.AzureMonitor.EnableLiveMetrics = false;
});
Microsoft OpenTelemetry Distro for .NET(包括非托管应用程序)中默认启用实时指标。 若要禁用它,请将options.AzureMonitor.EnableLiveMetrics设置为false:
// Import the Microsoft OpenTelemetry Distro and supporting APIs.
using Microsoft.OpenTelemetry;
using OpenTelemetry;
// Create the SDK and keep its providers alive until application shutdown.
using var sdk = OpenTelemetrySdk.Create(telemetry =>
{
// Configure the Microsoft OpenTelemetry Distro.
telemetry.UseMicrosoftOpenTelemetry(options =>
{
options.Exporters = ExportTarget.AzureMonitor;
// Disable Live Metrics.
options.AzureMonitor.EnableLiveMetrics = false;
});
});
独立的Azure Monitor OpenTelemetry 导出器不包括 Live Metrics。
实时指标默认处于启用状态。 配置 Microsoft OpenTelemetry 发行版时,可以启用或禁用它azureMonitor.enableLiveMetrics。
export class LiveMetricsSample {
static async run() {
// Import the Microsoft OpenTelemetry Distro.
const { useMicrosoftOpenTelemetry } = await import("@microsoft/opentelemetry");
// Configure Azure Monitor export and the options used by this sample.
const options = {
azureMonitor: {
azureMonitorExporterOptions: {
connectionString:
process.env.APPLICATIONINSIGHTS_CONNECTION_STRING ||
"<ConnectionString>",
},
enableLiveMetrics: true,
},
};
// Initialize the Microsoft OpenTelemetry Distro.
const monitor = useMicrosoftOpenTelemetry(options);
console.log("Azure Monitor initialized (live metrics enabled)");
}
}
实时指标默认处于启用状态。 配置发行版时,可以禁用实时指标。
...
# Initialize the Microsoft OpenTelemetry Distro with Azure Monitor export.
use_microsoft_opentelemetry(
enable_azure_monitor=True,
azure_monitor_enable_live_metrics=False, # To disable live metrics
)
...
脱机存储和自动重试
Azure Monitor 基于 OpenTelemetry 的产品在应用程序与 Application Insights 断开连接时,会缓存遥测数据,并在长达 48 小时内尝试重新发送。 有关数据处理建议,请参阅 导出和删除专用数据。 由于两个原因,高负载应用程序偶尔会删除遥测:超过允许的时间或超过最大文件大小。 如有必要,产品将最近事件优先于旧事件。
Microsoft OpenTelemetry 发行版包括 AzureMonitorExporter,默认情况下,AzureMonitorExporter 使用以下位置之一用于脱机存储(按优先级顺序列出):
Windows
- %LOCALAPPDATA%\Microsoft\AzureMonitor
- %TEMP%\Microsoft\AzureMonitor
非Windows
- %TMPDIR%/Microsoft/AzureMonitor
- /var/tmp/Microsoft/AzureMonitor
- /tmp/Microsoft/AzureMonitor
若要替代默认目录,请在配置 Microsoft OpenTelemetry 发行版时设置options.AzureMonitor.StorageDirectory。
// Import the Microsoft OpenTelemetry Distro and supporting APIs.
using Microsoft.OpenTelemetry;
// Create the application builder.
var builder = WebApplication.CreateBuilder(args);
// Configure the Microsoft OpenTelemetry Distro.
builder.UseMicrosoftOpenTelemetry(options =>
{
options.Exporters = ExportTarget.AzureMonitor;
// Store telemetry here when the exporter cannot send it immediately.
options.AzureMonitor.StorageDirectory = "<StorageDirectory>";
});
// Build the application with the configured telemetry services.
var app = builder.Build();
// Run the application and its telemetry providers.
app.Run();
若要禁用此功能,请设置 options.AzureMonitor.DisableOfflineStorage = true。
默认情况下,AzureMonitorExporter 将以下位置之一用于脱机存储,按优先级顺序列出:
Windows
- %LOCALAPPDATA%\Microsoft\AzureMonitor
- %TEMP%\Microsoft\AzureMonitor
非Windows
- %TMPDIR%/Microsoft/AzureMonitor
- /var/tmp/Microsoft/AzureMonitor
- /tmp/Microsoft/AzureMonitor
若要替代 Microsoft OpenTelemetry 发行版的默认目录,请设置 options.AzureMonitor.StorageDirectory。
// Import the Microsoft OpenTelemetry Distro and supporting APIs.
using Microsoft.OpenTelemetry;
using OpenTelemetry;
// Create the SDK and keep its providers alive until application shutdown.
using var sdk = OpenTelemetrySdk.Create(telemetry =>
{
// Configure the Microsoft OpenTelemetry Distro.
telemetry.UseMicrosoftOpenTelemetry(options =>
{
options.Exporters = ExportTarget.AzureMonitor;
// Store telemetry here when the exporter cannot send it immediately.
options.AzureMonitor.StorageDirectory = "<StorageDirectory>";
});
});
若要禁用此功能,请设置 options.AzureMonitor.DisableOfflineStorage = true。
当代理无法将遥测数据发送到Azure Monitor时,它会将遥测文件存储在磁盘上。 这些文件保存在 telemetry 系统属性指定的 java.io.tmpdir 目录下的文件夹中。 每个文件名以时间戳开头,以扩展名结尾 .trn 。 这种脱机存储机制有助于确保在临时网络中断或引入失败期间保留遥测数据。
默认情况下,代理最多存储 50 MB 的遥测数据,并允许 配置存储限制。 代理会定期尝试发送存储的遥测数据。 删除超过 48 小时的遥测文件,达到存储限制时会丢弃最早的事件。
有关可用配置的完整列表,请参阅配置选项。
当代理无法将遥测数据发送到Azure Monitor时,它会将遥测文件存储在磁盘上。 这些文件保存在 telemetry 系统属性指定的 java.io.tmpdir 目录下的文件夹中。 每个文件名以时间戳开头,以扩展名结尾 .trn 。 这种脱机存储机制有助于确保在临时网络中断或引入失败期间保留遥测数据。
默认情况下,代理最多存储 50 MB 的遥测数据。 定期尝试发送存储的遥测数据。 删除超过 48 小时的遥测文件,达到存储限制时会丢弃最早的事件。
默认情况下,AzureMonitorExporter 将使用以下位置之一进行脱机存储。
- Windows
- %TEMP%\Microsoft-AzureMonitor-
<unique-identifier>\opentelemetry-nodejs-<your-instrumentation-key>
- 非Windows
- %TMPDIR%/Microsoft/Microsoft-AzureMonitor-
<unique-identifier>/opentelemetry-nodejs-<your-instrumentation-key>
- /var/tmp/Microsoft/Microsoft-AzureMonitor-
<unique-identifier>/opentelemetry-nodejs-<your-instrumentation-key>
<unique-identifier>这是从用户环境属性(如检测密钥、进程名称、用户名和应用程序目录)创建的哈希。 此标识符解决了常见的多用户系统问题:当第一个用户创建存储目录时,其文件权限(umask 设置)可能会阻止其他用户访问同一路径。 每个用户上下文的唯一目录可确保每个用户获取自己的存储位置,并具有适当的访问权限。
若要覆盖默认目录,请设置 azureMonitor.azureMonitorExporterOptions.storageDirectory。
例如:
export class OfflineStorageSample {
static async run() {
// Import the Microsoft OpenTelemetry Distro.
const { useMicrosoftOpenTelemetry } = await import("@microsoft/opentelemetry");
// Configure Azure Monitor export and the options used by this sample.
const options = {
azureMonitor: {
azureMonitorExporterOptions: {
connectionString:
process.env.APPLICATIONINSIGHTS_CONNECTION_STRING ||
"<ConnectionString>",
// Store telemetry here when the exporter cannot send it immediately.
storageDirectory: "<StorageDirectory>",
disableOfflineStorage: false, // set to true to disable
},
},
};
// Initialize the Microsoft OpenTelemetry Distro.
const monitor = useMicrosoftOpenTelemetry(options);
console.log("Azure Monitor initialized (offline storage configured)");
}
}
若要禁用此功能,请设置 azureMonitor.azureMonitorExporterOptions.disableOfflineStorage = true。
默认情况下,Azure Monitor导出程序使用以下路径:
<tempfile.gettempdir()>/Microsoft-AzureMonitor-<unique-identifier>/opentelemetry-python-<your-instrumentation-key>
<unique-identifier>这是从用户环境属性(如检测密钥、进程名称、用户名和应用程序目录)创建的哈希。 此标识符解决了常见的多用户系统问题:当第一个用户创建存储目录时,其文件权限(umask 设置)可能会阻止其他用户访问同一路径。 每个用户上下文的唯一目录可确保每个用户获取自己的存储位置,并具有适当的访问权限。
若要覆盖默认目录,请将 azure_monitor_exporter_storage_directory 设置为所需的目录。
例如:
...
# Configure OpenTelemetry to use Azure Monitor with the specified connection string and
# storage directory.
use_microsoft_opentelemetry(
enable_azure_monitor=True,
azure_monitor_connection_string="<ConnectionString>",
azure_monitor_exporter_storage_directory="<StorageDirectory>",
)
...
若要禁用此功能,请设置为 azure_monitor_exporter_disable_offline_storageTrue. 默认值为 False。
例如:
...
# Configure OpenTelemetry to use Azure Monitor with the specified connection string and
# disable offline storage.
use_microsoft_opentelemetry(
enable_azure_monitor=True,
azure_monitor_connection_string="<ConnectionString>",
azure_monitor_exporter_disable_offline_storage=True,
)
...
启用 OTLP 导出器
你可能希望启用 OpenTelemetry 协议(OTLP)导出程序以及Azure Monitor导出程序,以便将遥测数据发送到两个位置。
注意
为方便起见,只展示了 OTLP 导出器。 Azure未正式支持 OTLP 导出程序或其下游的任何组件或第三方体验。
Microsoft OpenTelemetry 发行版包括 OTLP 导出。 使用 OTEL_EXPORTER_OTLP_ENDPOINT 配置收集器终结点,并选择这两个目标:
// Import the Microsoft OpenTelemetry Distro and supporting APIs.
using Microsoft.OpenTelemetry;
// Create the application builder.
var builder = WebApplication.CreateBuilder(args);
// Configure the Microsoft OpenTelemetry Distro.
builder.UseMicrosoftOpenTelemetry(options =>
{
// Send telemetry to Azure Monitor and the configured OTLP collector.
options.Exporters = ExportTarget.AzureMonitor | ExportTarget.Otlp;
});
// Build the application with the configured telemetry services.
var app = builder.Build();
// Run the application and its telemetry providers.
app.Run();
配置 OTEL_EXPORTER_OTLP_ENDPOINT收集器终结点,并选择这两个目标。 使 SDK 保持活动状态,直到应用程序关闭:
// Import the Microsoft OpenTelemetry Distro and supporting APIs.
using Microsoft.OpenTelemetry;
using OpenTelemetry;
// Create the SDK and keep its providers alive until application shutdown.
using var sdk = OpenTelemetrySdk.Create(telemetry =>
{
// Configure the Microsoft OpenTelemetry Distro.
telemetry.UseMicrosoftOpenTelemetry(options =>
{
// Send telemetry to Azure Monitor and the configured OTLP collector.
options.Exporters = ExportTarget.AzureMonitor | ExportTarget.Otlp;
});
});
Application Insights Java 代理不支持 OTLP。
有关受支持的配置的详细信息,请参阅 Java 补充文档。
不能与 Azure Monitor 导出程序一起启用 OpenTelemetry 协议(OTLP)导出程序,以便将遥测数据发送到两个位置。
在项目中安装 OpenTelemetry Collector Trace Exporter 和其他 OpenTelemetry 软件包。
npm install @opentelemetry/api
npm install @opentelemetry/exporter-trace-otlp-http
npm install @opentelemetry/sdk-trace-base
npm install @opentelemetry/sdk-trace-node
添加以下代码片段。 此示例假设你已经安装了一个 OpenTelemetry 收集器,并且其中正在运行一个 OTLP 接收器。 有关详细信息,请参阅 GitHub 上的示例。
启用 OTLP 导出程序
export class OtlpExporterSample {
static async run() {
// Import the Microsoft OpenTelemetry Distro.
const { useMicrosoftOpenTelemetry } = await import("@microsoft/opentelemetry");
const { BatchSpanProcessor } = await import("@opentelemetry/sdk-trace-base");
const { OTLPTraceExporter } = await import(
"@opentelemetry/exporter-trace-otlp-http"
);
// Create an OTLP trace exporter (set 'url' if your collector isn't on the default
// endpoint).
const otlpExporter = new OTLPTraceExporter({
// url: "http://localhost:4318/v1/traces",
});
// Configure Azure Monitor and add the OTLP exporter as an additional span
// processor.
const options = {
azureMonitor: {
azureMonitorExporterOptions: {
connectionString:
process.env.APPLICATIONINSIGHTS_CONNECTION_STRING ||
"<ConnectionString>",
},
},
spanProcessors: [new BatchSpanProcessor(otlpExporter)],
};
// Initialize the Microsoft OpenTelemetry Distro.
const monitor = useMicrosoftOpenTelemetry(options);
console.log("Azure Monitor initialized (OTLP exporter added)");
}
}
安装 opentelemetry-exporter-otlp 包。
添加以下代码片段。 此示例假设你已经安装了一个 OpenTelemetry 收集器,并且其中正在运行一个 OTLP 接收器。 有关详细信息,请参阅此 README。
启用 OTLP 导出程序
# Import the `use_microsoft_opentelemetry()`, `trace`, `OTLPSpanExporter`, and
# `BatchSpanProcessor` classes from the appropriate packages.
from microsoft.opentelemetry import use_microsoft_opentelemetry
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import BatchSpanProcessor
# Configure OpenTelemetry to use Azure Monitor with the specified connection string.
use_microsoft_opentelemetry(
enable_azure_monitor=True,
azure_monitor_connection_string="<ConnectionString>",
)
# Get the tracer for the current module.
tracer = trace.get_tracer(__name__)
# Create an OTLP span exporter that sends spans to the specified endpoint.
# Replace `http://localhost:4317` with the endpoint of your OTLP collector.
otlpExporter = OTLPSpanExporter(endpoint="http://localhost:4317")
# Create a batch span processor that uses the OTLP span exporter.
spanProcessor = BatchSpanProcessor(otlpExporter)
# Add the batch span processor to the tracer provider.
trace.get_tracer_provider().add_span_processor(spanProcessor)
# Start a new span with the name "test".
with tracer.start_as_current_span("test"):
print("Hello world!")
OpenTelemetry 配置
可以通过环境变量配置以下设置。 使用与你的语言和发行版对应的选项卡。
| 环境变量 |
说明 |
APPLICATIONINSIGHTS_CONNECTION_STRING |
将此变量设置为你的 Application Insights 资源的“连接字符串”。 |
APPLICATIONINSIGHTS_STATSBEAT_DISABLED |
将此变量设置为 true 选择退出内部指标集合。 |
OTEL_RESOURCE_ATTRIBUTES |
用于资源属性的键值对。 有关资源属性的详细信息,请参阅资源 SDK 规范。 |
OTEL_SERVICE_NAME |
设置 service.name 资源属性的值。 如果service.name也在OTEL_RESOURCE_ATTRIBUTES中提供,OTEL_SERVICE_NAME将优先。 |
| 环境变量 |
说明 |
APPLICATIONINSIGHTS_CONNECTION_STRING |
将此变量设置为你的 Application Insights 资源的“连接字符串”。 |
APPLICATIONINSIGHTS_STATSBEAT_DISABLED |
将此变量设置为 true 选择退出内部指标集合。 |
OTEL_RESOURCE_ATTRIBUTES |
用于资源属性的键值对。 有关资源属性的详细信息,请参阅资源 SDK 规范。 |
OTEL_SERVICE_NAME |
设置 service.name 资源属性的值。 如果service.name也在OTEL_RESOURCE_ATTRIBUTES中提供,OTEL_SERVICE_NAME将优先。 |
Redact URL 查询字符串
若要编辑 URL 查询字符串,请禁用查询字符串收集。 如果使用 SAS 令牌调用Azure存储,则建议使用此设置。
Microsoft.OpenTelemetry 发行版包括 ASP.NET Core 和 HttpClient 检测库。 对于处理敏感查询字符串的应用程序,请在初始化 distro 之前显式启用查询脱敏:
- 将
OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_DISABLE_URL_QUERY_REDACTION设置为false,用于 ASP.NET Core 检测。
- 设置
OTEL_DOTNET_EXPERIMENTAL_HTTPCLIENT_DISABLE_URL_QUERY_REDACTION 为 false,用于 HttpClient 检测。
将任一变量设置为 true 将禁用该插桩的查询字符串脱敏。
适用于 .NET 的 Microsoft OpenTelemetry 发行版包括 HttpClient 插桩。 在初始化之前将 OTEL_DOTNET_EXPERIMENTAL_HTTPCLIENT_DISABLE_URL_QUERY_REDACTION 设置为 false,以启用查询字符串隐藏。 对于 ASP.NET Core 托管,还要将 OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_DISABLE_URL_QUERY_REDACTION 设置为 false。
如果改用独立 Azure.Monitor.OpenTelemetry.Exporter 包,请单独添加和配置相关的插桩库。
将以下内容添加到 applicationinsights.json 配置文件:
隐藏 URL 查询字符串
{
"preview": {
"processors": [
{
"type": "attribute",
"actions": [
{
"key": "url.query",
"pattern": "^.*$",
"replace": "REDACTED",
"action": "mask"
}
]
},
{
"type": "attribute",
"actions": [
{
"key": "url.full",
"pattern": "[?].*$",
"replace": "?REDACTED",
"action": "mask"
}
]
}
]
}
}
OpenTelemetry 社区正在积极努力支持修订。
使用 Microsoft OpenTelemetry 发行版时,可以通过向发行版配置添加跨度处理器来对查询字符串进行脱敏。
隐藏 URL 查询字符串
export class RedactQueryStringsSample {
static async run() {
// Import the Microsoft OpenTelemetry Distro.
const { useMicrosoftOpenTelemetry } = await import("@microsoft/opentelemetry");
const { SEMATTRS_HTTP_ROUTE, SEMATTRS_HTTP_TARGET, SEMATTRS_HTTP_URL } =
await import("@opentelemetry/semantic-conventions");
class RedactQueryStringProcessor {
forceFlush() {
return Promise.resolve();
}
onStart() {}
shutdown() {
return Promise.resolve();
}
onEnd(span: any) {
const route = String(span.attributes[SEMATTRS_HTTP_ROUTE] ?? "");
const url = String(span.attributes[SEMATTRS_HTTP_URL] ?? "");
const target = String(span.attributes[SEMATTRS_HTTP_TARGET] ?? "");
const strip = (s: string) => {
const i = s.indexOf("?");
return i === -1 ? s : s.substring(0, i);
};
if (route)
span.attributes[SEMATTRS_HTTP_ROUTE] = strip(route);
if (url)
span.attributes[SEMATTRS_HTTP_URL] = strip(url);
if (target)
span.attributes[SEMATTRS_HTTP_TARGET] = strip(target);
}
}
// Configure Azure Monitor export and the options used by this sample.
const options = {
azureMonitor: {
azureMonitorExporterOptions: {
connectionString:
process.env.APPLICATIONINSIGHTS_CONNECTION_STRING ||
"<ConnectionString>",
},
},
spanProcessors: [new RedactQueryStringProcessor()],
};
// Initialize the Microsoft OpenTelemetry Distro.
const monitor = useMicrosoftOpenTelemetry(options);
console.log("Azure Monitor initialized (query strings redacted)");
}
}
OpenTelemetry 社区正在积极致力于支持修订。
指标导出间隔
可以使用环境变量配置指标导出间隔 OTEL_METRIC_EXPORT_INTERVAL 。
export OTEL_METRIC_EXPORT_INTERVAL="60000"
默认值为 60000 毫秒(60 秒)。 此设置控制 OpenTelemetry SDK 导出指标的频率。
提示
Azure Monitor指标和Azure Monitor工作区以固定的 60 秒间隔引入自定义指标。 更频繁发送的指标会被缓冲,并每隔 60 秒处理一次。 Log Analytics会根据发送的时间间隔记录指标,短时间间隔可能会增加成本,长时间间隔可能会导致可见性延迟。
有关参考,请参阅以下 OpenTelemetry 规范:
故障排除、反馈和支持
提示
以下部分适用于所有 OpenTelemetry 发行版文章。
故障排除
OpenTelemetry 反馈
若要提供反馈,请查看以下内容:
支持
选择所选语言的选项卡,以发现支持选项。
注意
Quarkus 社区支持和维护 Quarkus 扩展。 寻求帮助,请使用 Quarkus 社区支持渠道。 Azure不提供对此集成的技术支持。
后续步骤