You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Aspire中添加外部服务后,指标、链路追踪与日志无法同步至仪表板的问题求助

在Aspire中添加外部服务后,指标、链路追踪与日志无法同步至仪表板的问题求助

各位好,我最近在尝试把一个ASP.NET Core应用接入Aspire仪表板,已经通过代码成功添加了外部服务,能在仪表板上看到应用处于运行状态,但指标(Metrics)、链路追踪(Traces)和日志(Logs)都没有同步到Aspire仪表板,想请教下大家哪里出了问题?

我在Aspire中添加外部服务的代码

builder.AddExternalService("FirstApp", new Uri("http://localhost:5027"))
       .WithHttpHealthCheck("/healthz")
       .WithExplicitStart();

我的ASP.NET Core应用Program.cs完整代码

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();

var oltpEndpoint = new Uri("http://localhost:19131");
var resourceBuilder = ResourceBuilder.CreateDefault()
    .AddService("FirstApi", serviceVersion: "1.0.0");

// OpenTelemetry 日志配置
builder.Logging.AddOpenTelemetry(options => {
    options
        .SetResourceBuilder(resourceBuilder)
        .AddConsoleExporter()
        .AddOtlpExporter(oltpOptions => {
            oltpOptions.Endpoint = oltpEndpoint;
            oltpOptions.Protocol = OtlpExportProtocol.Grpc;
        });
});

// OpenTelemetry 指标与追踪配置
builder.Services.AddOpenTelemetry()
    .WithMetrics(metrics => {
        metrics
            .SetResourceBuilder(resourceBuilder)
            .AddAspNetCoreInstrumentation()
            .AddHttpClientInstrumentation()
            .AddRuntimeInstrumentation()
            .AddConsoleExporter()
            .AddOtlpExporter(options => {
                options.Endpoint = oltpEndpoint;
                options.Protocol = OtlpExportProtocol.Grpc;
            });
    })
    .WithTracing(tracing => {
        tracing
            .SetResourceBuilder(resourceBuilder)
            .AddAspNetCoreInstrumentation()
            .AddHttpClientInstrumentation()
            .AddConsoleExporter()
            .AddOtlpExporter(options => {
                options.Endpoint = oltpEndpoint;
                options.Protocol = OtlpExportProtocol.Grpc;
            });
    });

// 服务发现
builder.Services.AddServiceDiscovery();

var app = builder.Build();
app.MapScalarApiReference();

// HTTP 请求管道配置
if (app.Environment.IsDevelopment()) {
    app.MapOpenApi();
}

app.UseHttpsRedirection();

var summaries = new[] {
    "Freezing", "Bracing", "Chilly", "Cool", "Mild",
    "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", () => {
    var forecast = Enumerable.Range(1, 5).Select(index =>
        new WeatherForecast (
            DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
            Random.Shared.Next(-20, 55),
            summaries[Random.Shared.Next(summaries.Length)]
        ))
        .ToArray();
    return forecast;
})
.WithName("GetWeatherForecast");

app.MapHealthChecks("/healthz");
app.Run();

record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) {
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

Aspire项目的launchSettings.json配置

{
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "profiles": {
    "http": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "http://localhost:15004",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "DOTNET_ENVIRONMENT": "Development",
        "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19131",
        "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20150"
      }
    }
  }
}

补充说明

  • 我已经在ASP.NET Core应用里配置了OpenTelemetry,指定的Otlp端点和Aspire launchSettings里的ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL完全一致,协议用的是gRPC
  • 日志、指标、追踪都能正常输出到控制台,但就是没同步到Aspire仪表板
  • 我**不想使用builder.AddProject<>()**的方式,必须用外部服务接入的形式

有没有大佬能帮我排查下问题出在哪?比如是不是OpenTelemetry的配置有遗漏,或者Aspire那边还需要额外的设置?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 08:19:34