Skip to content

Default metrics pool name to Application Name from connection string#6533

Merged
roji merged 1 commit intomainfrom
roji/default-pool-name-to-app-name
Apr 8, 2026
Merged

Default metrics pool name to Application Name from connection string#6533
roji merged 1 commit intomainfrom
roji/default-pool-name-to-app-name

Conversation

@roji
Copy link
Copy Markdown
Member

@roji roji commented Apr 8, 2026

When no explicit NpgsqlDataSourceBuilder.Name is set, the db.client.connection.pool.name metric tag currently defaults to the full connection string, making metrics hard to distinguish in monitoring dashboards. This change defaults it to the Application Name from the connection string instead, falling back to the connection string only if Application Name is also unset.

The fallback priority is now:

  1. Explicit NpgsqlDataSourceBuilder.Name (unchanged)
  2. ApplicationName from the connection string (new)
  3. Connection string (existing fallback)

This is done by inserting settings.ApplicationName in the Name assignment chain in NpgsqlDataSource.cs. No new connection string property is needed.

Tests:

  • Added Pool_name_defaults_to_application_name verifying the new fallback behavior
  • Updated Password_does_not_leak_via_datasource_name to test the connection-string fallback path (no ApplicationName set)

Closes #6531

When no explicit NpgsqlDataSourceBuilder.Name is set, default the data
source name (used as db.client.connection.pool.name in metrics) to the
Application Name from the connection string, falling back to the
connection string itself only if Application Name is also unset.

Closes #6531

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@roji roji requested a review from vonzshik as a code owner April 8, 2026 09:56
Copilot AI review requested due to automatic review settings April 8, 2026 09:56
@roji roji enabled auto-merge (squash) April 8, 2026 09:57
@roji roji requested a review from NinoFloris April 8, 2026 09:57
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR changes how NpgsqlDataSource.Name is derived when no explicit NpgsqlDataSourceBuilder.Name is provided, so that the db.client.connection.pool.name metric tag defaults to the connection string’s Application Name (and falls back to the password-scrubbed connection string when Application Name is unset).

Changes:

  • Update NpgsqlDataSource default naming fallback to prefer settings.ApplicationName.
  • Add a metrics test verifying the new Application Name fallback behavior.
  • Adjust the password-leak test to cover the connection-string fallback path (no Application Name set).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/Npgsql/NpgsqlDataSource.cs Changes the default Name derivation chain to include ApplicationName before falling back to the (password-scrubbed) connection string.
test/Npgsql.Tests/MetricTests.cs Adds/updates tests for the new pool-name fallback behavior and password scrubbing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 125 to +135
if (settings.PersistSecurityInfo)
{
ConnectionString = settings.ToString();

// The data source name is reported in tracing/metrics, so avoid leaking the password through there.
Name = name ?? settings.ToStringWithoutPassword();
Name = name ?? settings.ApplicationName ?? settings.ToStringWithoutPassword();
}
else
{
ConnectionString = settings.ToStringWithoutPassword();
Name = name ?? ConnectionString;
Name = name ?? settings.ApplicationName ?? ConnectionString;
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

settings.ApplicationName can be an empty string (e.g., connection string contains Application Name=). With the current null-coalescing chain, an empty application name becomes the data source Name, resulting in an empty db.client.connection.pool.name tag and skipping the intended fallback to the (password-scrubbed) connection string. Consider treating empty/whitespace application names as unset (e.g., use a length/IsNullOrEmpty check before selecting it).

Copilot uses AI. Check for mistakes.
Comment on lines +139 to +143
var applicationName = "MetricsDataSource" + Interlocked.Increment(ref _dataSourceCounter);
var dataSourceBuilder = base.CreateDataSourceBuilder();
dataSourceBuilder.ConnectionStringBuilder.ApplicationName = applicationName;
// Do not set the data source name - this makes the pool name default to the Application Name
await using var dataSource = dataSourceBuilder.Build();
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test uses an instance-level counter to generate applicationName. Since the test assembly is parallelizable, different MetricTests instances can run concurrently and produce the same MetricsDataSource1 name, which can cause metric aggregation across tests (same db.client.connection.pool.name) and lead to flaky assertions. Use a process-wide unique value (e.g., a static counter, Guid, or include TestContext.CurrentContext.Test.ID) to avoid collisions under parallel execution.

Copilot uses AI. Check for mistakes.
@roji roji merged commit 8d90c32 into main Apr 8, 2026
30 of 31 checks passed
@roji roji deleted the roji/default-pool-name-to-app-name branch April 8, 2026 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow setting pool.name via Connection String for easier metrics identification

2 participants