我想在 Prometheus 中添加 EF Core 连接池的指标。我根据文档使用了以下内容,但我没有在 Prometheus 中看到指标。此外,我没有看到任何 Prometheus 导出器的踪迹。有什么办法可以实现我想要的吗?
这是Startup.cs中的代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddOpenTelemetry()
.WithMetrics(builder => builder
.AddRuntimeInstrumentation()
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddPrometheusExporter()
.AddMeter(httpCountMeter.Name))
.WithTracing(builder => builder.AddEntityFrameworkCoreInstrumentation(options =>
{
options.SetDbStatementForText = true;
options.SetDbStatementForStoredProcedure = true;
}));
}
这些是我正在使用的库:
<PackageReference Include="NSwag.AspNetCore" Version="13.20.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.6.0-rc.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.5.1-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta.3" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.5.1-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.RunTime" Version="1.5.1" />
这些是我想在 Prometheus 中看到的指标:
# EF Core Connection Pool Metrics
# TYPE efcore_connection_pool_size gauge
# HELP efcore_connection_pool_size The current size of the EF Core connection pool.
efcore_connection_pool_size{pool="YourDbContextName"} 10
# TYPE efcore_connection_pool_busy gauge
# HELP efcore_connection_pool_busy The number of connections in the EF Core connection pool that are currently in use.
efcore_connection_pool_busy{pool="YourDbContextName"} 5
# TYPE efcore_connection_pool_idle gauge
# HELP efcore_connection_pool_idle The number of idle connections in the EF Core connection pool.
efcore_connection_pool_idle{pool="YourDbContextName"} 5
# TYPE efcore_connection_pool_pending gauge
# HELP efcore_connection_pool_pending The number of pending requests for connections in the EF Core connection pool.
efcore_connection_pool_pending{pool="YourDbContextName"} 3
# TYPE efcore_connection_pool_lifetime_seconds histogram
# UNIT seconds
# HELP efcore_connection_pool_lifetime_seconds Histogram of the lifetimes of connections in the EF Core connection pool.
efcore_connection_pool_lifetime_seconds_bucket{pool="YourDbContextName",le="0.1"} 10
efcore_connection_pool_lifetime_seconds_bucket{pool="YourDbContextName",le="1"} 20
# ... (more buckets)
efcore_connection_pool_lifetime_seconds_sum{pool="YourDbContextName"} 50
efcore_connection_pool_lifetime_seconds_count{pool="YourDbContextName"} 30
在您使用的库中,只有跟踪功能。在您的代码中,您只需在
WithTracing
方法中配置 ef core 的跟踪。