Azure Application Insights是 Azure Monitor 的一项功能,在实时网页应用程序的应用性能管理(APM)方面表现出色。 Aspire 项目旨在将 OpenTelemetry 用于应用程序遥测。 OpenTelemetry 支持扩展模型,以支持将数据发送到不同的 APM。 Aspire 默认情况下使用 OTLP 进行遥测导出,仪表板在开发期间将使用该导出的数据。 Azure Monitor 还未支持 OTLP,因此需要修改应用程序以便使用 Azure Monitor 导出程序,并通过连接字符串进行配置。
若要使用 Application Insights,请在 AppHost 项目中指定其配置,并在服务默认项目中使用Azure Monitor 发行版。
了解遥测选项
Aspire 提供收集遥测并将其发送到 Application Insights的方式的灵活性。 有两种主要方法可用于与 Application Insights 集成。
经典 Application Insights SDK
经典 Application Insights SDK 是用于检测 .NET 应用程序的传统方法。 它提供以下功能:
- HTTP 请求、依赖项、异常等的内置遥测数据收集。
- 通过
TelemetryClientAPI 自定义遥测。
但是,此 SDK 与OpenTelemetry方法Aspire使用的AddServiceDefaults()基于的配置不兼容。
OpenTelemetry 使用 Azure 监视器
OpenTelemetry 是遥测收集的新式标准。
Aspire 使用 OpenTelemetry 通过其 AddServiceDefaults() 方法进行跟踪和度量。 使用 OpenTelemetry:
- 遥测与供应商无关,允许与多个后端集成。
- 可以使用 Azure Monitor OpenTelemetry 分发版将遥测数据发送到 Application Insights。
主要区别:
| 功能 / 特点 | 经典 Application Insights SDK | OpenTelemetry 使用 Azure 监视器 |
|---|---|---|
| 易用性 | 需要手动设置高级功能。 | 提供更现代的标准化 API。 |
| 兼容性 | 与 OpenTelemetry 设置不兼容。 | 完全兼容Aspire默认值。 |
| 可扩展性 | 仅限使用 Application Insights 功能。 | Open、vendor-neutral、支持其他后端。 |
| 检测标准 | 特定于应用程序的 API(例如, TelemetryClient)。 |
OpenTelemetry 标准。 |
有关每个方法的详细指南,请参阅:
选择如何配置 Application Insights
Aspire 能够将云资源预配为云部署的一部分,包括 Application Insights。 在 Aspire 项目中,可以决定在部署到 Aspire时是否希望 Application Insights 预配 Azure 资源。 还可以选择使用现有的 Application Insights 资源,只需提供其连接字符串。 连接信息由 AppHost 项目中的资源配置管理。
在 Azure 部署期间配置应用程序洞察
在部署应用程序时使用此选项,将通过 Application Insights (Azure Developer CLI)为你创建一个 azd 实例。
若要使用自动预配,请在 AppHost 项目中指定依赖项,并在需要将遥测数据发送到的每个项目/资源中引用它 Application Insights。 这些步骤包括:
向 .添加 Nuget 包引用 Aspire。Hosting.Azure.AppHost 项目中的 ApplicationInsights 。
更新 AppHost 代码以使用 Application Insights 资源,并从每个项目中引用它:
var builder = DistributedApplication.CreateBuilder(args);
// Automatically provision an Application Insights resource
var insights = builder.AddAzureApplicationInsights("MyApplicationInsights");
// Reference the resource from each project
var apiService = builder.AddProject<Projects.ApiService>("apiservice")
.WithReference(insights);
builder.AddProject<Projects.Web>("webfrontend")
.WithReference(apiService)
.WithReference(insights);
builder.Build().Run();
按照 《在 Azure Container Apps 部署 Aspire 项目的步骤》(深入指南),使用 Azure Developer CLI 将应用程序部署到 Azure Container Apps。
azd 将创建一个 Application Insights 资源作为同一资源组的一部分,并为每个容器配置连接字符串。
手动预配 Application Insights 资源
Application Insights 使用连接字符串告诉 OpenTelemetry 导出程序发送遥测数据的位置。 连接字符串特定于要向其发送遥测数据的 Application Insights 实例。 可以在 Application Insights 实例的“概述”页中找到它。
如果要使用手动预配的 Application Insights 实例,则应使用 AddConnectionString AppHost 项目中的 API 告知项目/容器在何处发送遥测数据。
Azure Monitor 发行版要求环境变量 APPLICATIONINSIGHTS_CONNECTION_STRING,因此在定义连接字符串时需要显式设置该变量。
var builder = DistributedApplication.CreateBuilder(args);
var insights = builder.AddConnectionString(
"myInsightsResource",
"APPLICATIONINSIGHTS_CONNECTION_STRING");
var apiService = builder.AddProject<Projects.ApiService>("apiservice")
.WithReference(insights);
builder.AddProject<Projects.Web>("webfrontend")
.WithReference(apiService)
.WithReference(insights);
builder.Build().Run();
开发期间的资源使用情况
在本地运行 Aspire 项目时,前面的代码从配置中读取连接字符串。 由于这是机密,因此应将该值存储在 应用机密中。 右键单击 AppHost 项目,然后从上下文菜单中选择 “管理机密 ”以打开 AppHost 项目的机密文件。 在文件中添加密钥和特定连接字符串,下面的示例用于说明目的。
{
"ConnectionStrings": {
"myInsightsResource": "InstrumentationKey=12345678-abcd-1234-abcd-1234abcd5678;IngestionEndpoint=https://westus3-1.in.applicationinsights.azure.com"
}
}
注释
name AppHost 代码中指定的值需要匹配设置文件中节内的键ConnectionStrings。
部署期间的资源使用情况
Aspire部署 Azure Developer CLI 应用程序时,它将识别连接字符串资源并提示输入值。 这使得可以在部署中使用与本地开发时所用的值不同的资源。
混合部署
如果要为每个执行上下文使用不同的部署机制,请有条件地使用相应的 API。 例如,以下代码在开发时使用预先提供的连接,并在部署时使用自动预配的资源。
var builder = DistributedApplication.CreateBuilder(args);
var insights = builder.ExecutionContext.IsPublishMode
? builder.AddAzureApplicationInsights("myInsightsResource")
: builder.AddConnectionString("myInsightsResource", "APPLICATIONINSIGHTS_CONNECTION_STRING");
var apiService = builder.AddProject<Projects.ApiService>("apiservice")
.WithReference(insights);
builder.AddProject<Projects.Web>("webfrontend")
.WithReference(apiService)
.WithReference(insights);
builder.Build().Run();
小窍门
在开发阶段,上述代码要求你在应用程序机密中提供连接字符串信息,而在部署时系统将通过 azd 提示你输入连接字符串。
使用 Azure Monitor 发行版
为了使导出到 Azure Monitor 更简单,此示例使用 Azure Monitor 导出工具库。 这是围绕 Azure Monitor OpenTelemetry 导出程序包的一个包装程序包,它使得通过一组常见默认配置导出到 Azure Monitor 更加方便。
将以下包添加到 ServiceDefaults 项目,以便将其包含在每个 Aspire 服务中。 有关详细信息,请参阅 Aspire 服务默认值。
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore"
Version="*" />
将 using 语句添加到项目文件的开头。
using Azure.Monitor.OpenTelemetry.AspNetCore;
取消注释 AddOpenTelemetryExporters 中的行以使用 Azure Monitor 导出程序:
private static IHostApplicationBuilder AddOpenTelemetryExporters(
this IHostApplicationBuilder builder)
{
// Omitted for brevity...
// Uncomment the following lines to enable the Azure Monitor exporter
// (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
{
builder.Services.AddOpenTelemetry().UseAzureMonitor();
}
return builder;
}
可以进一步自定义 Azure Monitor 导出程序,包括自定义资源名称和更改采样。 有关详细信息,请参阅 自定义 Azure 监控导出器。 使用无参数版本的 UseAzureMonitor(),将从环境变量中拾取连接字符串 APPLICATIONINSIGHTS_CONNECTION_STRING ,我们通过 AppHost 项目进行配置。