feat: add OpenTelemetry instrumentation (tracing + metrics + logging)

- OTLP gRPC exporter to 192.168.1.154:4316
- ASP.NET Core, HttpClient, EF Core, Runtime instrumentation
- Log export via AddOpenTelemetry logging
This commit is contained in:
向宁 2026-05-17 22:02:50 +08:00
parent 54db985fa5
commit c2d114535d
3 changed files with 43 additions and 0 deletions

View File

@ -1,10 +1,20 @@
using Microsoft.EntityFrameworkCore;
using OpenTelemetry.Logs;
using RAG.Api;
using RAG.Infrastructure.Persistence;
using Volo.Abp.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
// OTel 日志导出
builder.Logging.AddOpenTelemetry(logging =>
{
logging.AddOtlpExporter(options =>
{
options.Endpoint = new Uri("http://192.168.1.154:4316");
});
});
// ABP 需要同时注册 ObjectAccessor 的具体类型和接口类型
var appBuilderAccessor = new ObjectAccessor<Microsoft.AspNetCore.Builder.IApplicationBuilder>();
var webAppAccessor = new ObjectAccessor<Microsoft.AspNetCore.Builder.WebApplication>();

View File

@ -4,6 +4,7 @@
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<NoWarn>$(NoWarn);NU1902</NoWarn>
</PropertyGroup>
<ItemGroup>
@ -18,6 +19,14 @@
</PackageReference>
<PackageReference Include="Volo.Abp.AspNetCore" Version="10.3.0" />
<PackageReference Include="Volo.Abp.Core" Version="10.3.0" />
<PackageReference Include="OpenTelemetry" Version="1.11.2" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.11.2" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.11.2" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.11.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.11.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.11.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.11.0-beta.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.11.1" />
</ItemGroup>
<ItemGroup>

View File

@ -1,6 +1,9 @@
using FastEndpoints;
using FastEndpoints.Security;
using FastEndpoints.Swagger;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using RAG.Api.Grpc;
using RAG.Api.Middleware;
using RAG.Api.Services;
@ -23,6 +26,27 @@ public class RAGApiModule : AbpModule
var services = context.Services;
var config = services.GetConfiguration();
// OpenTelemetry
services.AddOpenTelemetry()
.ConfigureResource(r => r
.AddService(serviceName: "rag-backend", serviceVersion: "1.0.0"))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddEntityFrameworkCoreInstrumentation()
.AddOtlpExporter(options =>
{
options.Endpoint = new Uri("http://192.168.1.154:4316");
}))
.WithMetrics(metrics => metrics
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddRuntimeInstrumentation()
.AddOtlpExporter((exporterOptions, readerOptions) =>
{
exporterOptions.Endpoint = new Uri("http://192.168.1.154:4316");
}));
// FastEndpoints + Swagger
services.AddFastEndpoints();
services.SwaggerDocument();