.net core-利用OpenObserve 实现OpenTelemetry可观测性标准(Logs,Traces,Metrics)
1. 什么是 OpenObserve?[*]OpenObserve 是一个开源的可观测性平台(支持日志、指标、追踪),旨在成为 Elasticsearch/Datadog 的轻量级替代方案。
[*]它支持通过 OpenTelemetry(OTLP)协议接收数据。
2. 搭建 OpenObserve
[*]docker 本地运行或部署 OpenObserve(参考 官方文档):
[*]docker run -d --name openobserve -p 5080:5080 -e ZO_ROOT_USER_EMAIL="root@example.com" -e ZO_ROOT_USER_PASSWORD="root123" public.ecr.aws/zinclabs/openobserve:latest
[*]访问 http://localhost:5080(默认账号:root@example.com,密码:root123)。
3. .NET Core 的 OpenTelemetry 配置
[*]安装必要的 NuGet 包:
4. 在 .NET Core 中配置 OpenTelemetry
var builder = WebApplication.CreateBuilder(args);<br>//base
var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes("root@example.com: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
{
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 数据。
[*]认证:默认使用基本认证(账号 root@example.com,密码 root123)。需将凭据编码为 Base64:
// 将 "root@example.com:root123" 转为 Base64
var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes("root@example.com: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() //将追踪数据输出到控制台
更多参考:
[*]OpenObserve 文档
[*]OpenTelemetry .NET 文档
[*]https://github.com/openobserve/dotnet-opentelemetry-tracing-application.git
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]