在 .NET 中使用 OpenTelemetry 通过 Azure AppInsights 实现分布式跟踪

问题描述 投票:0回答:1

我一直致力于使用 OpenTelemetry 在我的 .NET 8 应用程序中实现分布式跟踪。我参考了这篇文章:https://www.milanjovanovic.tech/blog/introduction-to-distributed-tracing-with-opentelemetry-in-dotnet并且我已成功查看指标和跟踪信息使用 Jaeger UI。

但是,我希望扩展这个概念并使用 Azure Application Insights 而不是 Jaeger UI。我的目标是解释 Azure AppInsights 中的跟踪和指标信息。

这是我用来配置 OpenTelemetry 的代码:

private static void ConfigureOpenTelemetry(WebApplicationBuilder builder, ConfigurationManager config)
{
    var appConfig = builder.Services.BindValidateReturn<DemoCloudServiceOptions>(config);
    builder.Services.AddOpenTelemetry().UseAzureMonitor();
    builder.Services.AddOpenTelemetry(appConfig.AppInsightsConnString);

    builder.Services.AddOpenTelemetry()
    .ConfigureResource(resource => resource.AddService("MyApp"))
    .WithMetrics(metrics =>
    {
        metrics
            .AddAspNetCoreInstrumentation()
            .AddHttpClientInstrumentation();

        metrics.AddMeter("MyApp");

        metrics.AddOtlpExporter();
    })
    .WithTracing(tracing =>
    {
        tracing
            .AddAspNetCoreInstrumentation()
            .AddHttpClientInstrumentation()
            .AddEntityFrameworkCoreInstrumentation();

        tracing.AddOtlpExporter();
    });

    builder.Logging.AddOpenTelemetry(logging => logging.AddOtlpExporter());
}

任何人都可以通过提供指导来帮助我。任何帮助将不胜感激。

c# azure azure-application-insights open-telemetry jaeger
1个回答
0
投票

我能够将 Open telemetry 记录到 Application Insights。

我已按照此MSDoc在.NET Core 8应用程序中配置Opentelemetry。

使用

appsettings.json
文件中的配置,如我的 SOThread

中所述

我的

.csproj
文件:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <ApplicationInsightsResourceId>/subscriptions/b83c1ed3-c5b6-44fb-b5ba-2b83a074c23f/resourceGroups/****/providers/microsoft.insights/components/SampleAppInsights</ApplicationInsightsResourceId>
    <UserSecretsId>****</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.2.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
    <PackageReference Include="OpenTelemetry" Version="1.8.1" />
    <PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.8.1" />
    <PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" />
    <PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
  </ItemGroup>
</Project>

感谢@Rahul Rai的清晰解释。

我的Program.cs文件:

using Azure.Monitor.OpenTelemetry.Exporter;
using OpenTelemetry;
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System.Diagnostics;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();
var conn = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];

builder.Logging.ClearProviders()
    .AddOpenTelemetry(loggerOptions =>
    {
        loggerOptions
            .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("MyApp"))         
            .AddAzureMonitorLogExporter(options =>
                options.ConnectionString = conn)     
            .AddConsoleExporter();

        loggerOptions.IncludeFormattedMessage = true;
        loggerOptions.IncludeScopes = true;
        loggerOptions.ParseStateValues = true;
    });

builder.Services.AddApplicationInsightsTelemetry(new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions
{
    ConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]
});

var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
 
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();

app.Run();
  • 即使下面的代码也对我有用。
 builder.Services.AddOpenTelemetry()
    .WithTracing(builder =>
    {
        builder.AddAspNetCoreInstrumentation();
        builder.AddConsoleExporter();
        builder.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("MyApp"));
        builder.AddAzureMonitorTraceExporter(options =>
           {
               options.ConnectionString = conn;
           });
        builder.AddConsoleExporter();
    });

当地痕迹:

Activity.TraceId:            729cc92ac67fe948aaeeaaa250af5431
Activity.SpanId:             2b2e1eb570dbfe44
Activity.TraceFlags:         Recorded
Activity.ActivitySourceName: Microsoft.AspNetCore
Activity.DisplayName:        GET
Activity.Kind:               Server
Activity.StartTime:          2024-05-24T14:51:20.6775264Z
Activity.Duration:           00:00:00.0011907
Activity.Tags:
    server.address: localhost
    server.port: 7285
    http.request.method: GET
    url.scheme: https
    url.path: /_framework/aspnetcore-browser-refresh.js
    network.protocol.version: 2
    user_agent.original: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0
    http.response.status_code: 200

应用见解: enter image description here

日志: enter image description here

  • 您可以看到本地遥测 TraceID 在 ApplicationInsights 中显示为操作 ID。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.