Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Npgsql Metrics #658

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<PackageIconFullPath>$(SharedDir)PostgreSQL_logo.3colors.540x557.png</PackageIconFullPath>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\Aspire.Npgsql\NpgsqlCommon.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,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);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Npgsql;
using OpenTelemetry.Metrics;

namespace Microsoft.Extensions.Hosting;

Expand Down Expand Up @@ -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);
}
}

Expand Down
27 changes: 27 additions & 0 deletions src/Components/Aspire.Npgsql/NpgsqlCommon.cs
Original file line number Diff line number Diff line change
@@ -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
});
}
}