EF Core connection pool metrics in Prometheus

111 Views Asked by At

I want to add metrics for EF Core connection pool in Prometheus. I have used the following based on the documentation but I don't see the metrics in Prometheus. Moreover, I don't see any Prometheus exporter for traces. Is there a way I can achieve what I want?

This is the code in 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;
            }));
}

These are the libraries that I am using:

<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" />

These are the kind of metrics I want to see in 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
0

There are 0 best solutions below