diff --git a/src/Components/Aspire.Npgsql.EntityFrameworkCore.PostgreSQL/Aspire.Npgsql.EntityFrameworkCore.PostgreSQL.csproj b/src/Components/Aspire.Npgsql.EntityFrameworkCore.PostgreSQL/Aspire.Npgsql.EntityFrameworkCore.PostgreSQL.csproj index 9ad44ab009..47f0494300 100644 --- a/src/Components/Aspire.Npgsql.EntityFrameworkCore.PostgreSQL/Aspire.Npgsql.EntityFrameworkCore.PostgreSQL.csproj +++ b/src/Components/Aspire.Npgsql.EntityFrameworkCore.PostgreSQL/Aspire.Npgsql.EntityFrameworkCore.PostgreSQL.csproj @@ -8,6 +8,10 @@ $(SharedDir)PostgreSQL_logo.3colors.540x557.png + + + + diff --git a/src/Components/Aspire.Npgsql.EntityFrameworkCore.PostgreSQL/AspireEFPostgreSqlExtensions.cs b/src/Components/Aspire.Npgsql.EntityFrameworkCore.PostgreSQL/AspireEFPostgreSqlExtensions.cs index bfe9a02621..65008bf65d 100644 --- a/src/Components/Aspire.Npgsql.EntityFrameworkCore.PostgreSQL/AspireEFPostgreSqlExtensions.cs +++ b/src/Components/Aspire.Npgsql.EntityFrameworkCore.PostgreSQL/AspireEFPostgreSqlExtensions.cs @@ -125,8 +125,7 @@ public static partial class AspireEFPostgreSqlExtensions eventCountersInstrumentationOptions.AddEventSources("Microsoft.EntityFrameworkCore"); }); - // https://github.com/npgsql/npgsql/blob/4c9921de2dfb48fb5a488787fc7422add3553f50/src/Npgsql/MetricsReporter.cs#L48 - meterProviderBuilder.AddMeter("Npgsql"); + NpgsqlCommon.AddNpgsqlMetrics(meterProviderBuilder); }); } diff --git a/src/Components/Aspire.Npgsql/AspirePostgreSqlNpgsqlExtensions.cs b/src/Components/Aspire.Npgsql/AspirePostgreSqlNpgsqlExtensions.cs index 5b11e3c7b9..666cb5e84f 100644 --- a/src/Components/Aspire.Npgsql/AspirePostgreSqlNpgsqlExtensions.cs +++ b/src/Components/Aspire.Npgsql/AspirePostgreSqlNpgsqlExtensions.cs @@ -9,7 +9,6 @@ using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Logging; using Npgsql; -using OpenTelemetry.Metrics; namespace Microsoft.Extensions.Hosting; @@ -98,14 +97,7 @@ private static void AddNpgsqlDataSource(IHostApplicationBuilder builder, string if (settings.Metrics) { builder.Services.AddOpenTelemetry() - .WithMetrics(meterProviderBuilder => - { - // https://github.com/npgsql/npgsql/blob/4c9921de2dfb48fb5a488787fc7422add3553f50/src/Npgsql/MetricsReporter.cs#L48 - meterProviderBuilder.AddMeter("Npgsql"); - - // disable "prepared_ratio" until https://github.com/dotnet/aspire/issues/629 is fixed. - meterProviderBuilder.AddView(instrumentName: "db.client.commands.prepared_ratio", MetricStreamConfiguration.Drop); - }); + .WithMetrics(NpgsqlCommon.AddNpgsqlMetrics); } } diff --git a/src/Components/Aspire.Npgsql/NpgsqlCommon.cs b/src/Components/Aspire.Npgsql/NpgsqlCommon.cs new file mode 100644 index 0000000000..fe877ee313 --- /dev/null +++ b/src/Components/Aspire.Npgsql/NpgsqlCommon.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using OpenTelemetry.Metrics; + +internal static class NpgsqlCommon +{ + public static void AddNpgsqlMetrics(MeterProviderBuilder meterProviderBuilder) + { + double[] secondsBuckets = [0, 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10]; + + // https://github.com/npgsql/npgsql/blob/4c9921de2dfb48fb5a488787fc7422add3553f50/src/Npgsql/MetricsReporter.cs#L48 + meterProviderBuilder + .AddMeter("Npgsql") + // Npgsql's histograms are in seconds, not milliseconds. + .AddView("db.client.commands.duration", + new ExplicitBucketHistogramConfiguration + { + Boundaries = secondsBuckets + }) + .AddView("db.client.connections.create_time", + new ExplicitBucketHistogramConfiguration + { + Boundaries = secondsBuckets + }); + } +}