.net core-利用OpenObserve 实现OpenTelemetry可观测性标准(Logs,Traces,Metrics)

1. 什么是 OpenObserve?

  • OpenObserve 是一个开源的可观测性平台(支持日志、指标、追踪),旨在成为 Elasticsearch/Datadog 的轻量级替代方案。

  • 它支持通过 OpenTelemetry(OTLP)协议接收数据。

2. 搭建 OpenObserve

3. .NET Core 的 OpenTelemetry 配置

  • 安装必要的 NuGet 包:

    <PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.10.0" />

    <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.10.0" />

    <PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.10.0" />

    <PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.10.0" />

    <PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.10.0" />

    <PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.10.0" />

4. 在 .NET Core 中配置 OpenTelemetry

复制代码
var builder = WebApplication.CreateBuilder(args);
//base 
var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes("[email protected]:root123"));
// 配置OpenTelemetry 日志Log
builder.Logging.ClearProviders();

var resourceBuilder = ResourceBuilder.CreateDefault()
    .AddService("xxxapi")
    .AddAttributes(new Dictionary<string, object>
    {
        ["environment"] = "development",
        ["service.version"] = "1.0.0"
    });

builder.Logging.AddOpenTelemetry(logging => {
    logging.IncludeFormattedMessage = true;
    logging.SetResourceBuilder(resourceBuilder)
        .AddConsoleExporter()  // Keep console logging for debugging
        .AddOtlpExporter(otlpOptions => {
            otlpOptions.BatchExportProcessorOptions = new BatchExportProcessorOptions<Activity>
            {
                MaxQueueSize = 100,        // 减小队列避免堆积
                MaxExportBatchSize = 10,   // 减小批量大小
                ScheduledDelayMilliseconds = 500, // 更频繁导出
            };
            otlpOptions.Endpoint = new Uri("http://localhost:5080/api/default/v1/logs");
            otlpOptions.Headers = $"Authorization=Basic {credentials}";
            otlpOptions.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
        });
});

// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "xxx API", Version = "v1" });
});
builder.Services.AddHttpClient();

// 配置 OpenTelemetry 追踪Tracing
builder.Services.AddOpenTelemetry()
    .WithTracing(builder => builder
        .SetResourceBuilder(resourceBuilder)
        .AddSource("xxxapi")
        .AddAspNetCoreInstrumentation()
        .AddHttpClientInstrumentation()
        .AddOtlpExporter(opts =>
        {
            opts.Endpoint = new Uri("http://localhost:5080/api/default/v1/traces");
            opts.Headers = $"Authorization=Basic {credentials}";
            opts.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
        }));

var app = builder.Build();
       
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "xxx API v1"));
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.Run(); 

5. 关键细节

  • OTLP 端点 :OpenObserve 使用 /api/default/接收 OTLP 数据。

  • 认证 :默认使用基本认证(账号 [email protected],密码 root123)。需将凭据编码为 Base64:

    复制代码
    // 将 "[email protected]:root123" 转为 Base64
    var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes("[email protected]:root123"));
    opt.Headers = $"Authorization=Basic {credentials}";
  • 数据类型:上述配置会向 OpenObserve 发送追踪(Traces)、指标(Metrics)和日志(Logs)。

6. 在 OpenObserve 中验证数据

  • 访问 http://localhost:5080

    • 日志 :在 Logs Explorer 中查看。

    • 追踪 :在 Traces Explorer 中查看。

    • 指标 :在 Metrics Explorer 中查看。

7. 故障排除

  • 确保 OpenObserve 正在运行。

  • 检查 OTLP 端点和认证头是否正确。

  • 启用 OpenTelemetry 的诊断输出(调试用):

    logging.AddConsoleExporter() //将追踪数据输出到控制台

    tracing.AddConsoleExporter() //将追踪数据输出到控制台

    复制代码
     

更多参考: