Skip to content

Commit 8d90c32

Browse files
rojiCopilot
andauthored
Default metrics pool name to Application Name from connection string (#6533)
Closes #6531 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 77d1fb9 commit 8d90c32

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

src/Npgsql/NpgsqlDataSource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ internal NpgsqlDataSource(NpgsqlConnectionStringBuilder settings, NpgsqlDataSour
127127
ConnectionString = settings.ToString();
128128

129129
// The data source name is reported in tracing/metrics, so avoid leaking the password through there.
130-
Name = name ?? settings.ToStringWithoutPassword();
130+
Name = name ?? settings.ApplicationName ?? settings.ToStringWithoutPassword();
131131
}
132132
else
133133
{
134134
ConnectionString = settings.ToStringWithoutPassword();
135-
Name = name ?? ConnectionString;
135+
Name = name ?? settings.ApplicationName ?? ConnectionString;
136136
}
137137

138138
_password = settings.Password;

test/Npgsql.Tests/MetricTests.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,29 @@ public async Task ConnectionMax()
127127
Assert.That(tags["db.client.connection.pool.name"], Is.EqualTo(dataSource.Name));
128128
}
129129

130+
[Test]
131+
public async Task Pool_name_defaults_to_application_name()
132+
{
133+
var exportedItems = new List<Metric>();
134+
using var meterProvider = Sdk.CreateMeterProviderBuilder()
135+
.AddMeter("Npgsql")
136+
.AddInMemoryExporter(exportedItems)
137+
.Build();
138+
139+
var applicationName = "MetricsDataSource" + Interlocked.Increment(ref _dataSourceCounter);
140+
var dataSourceBuilder = base.CreateDataSourceBuilder();
141+
dataSourceBuilder.ConnectionStringBuilder.ApplicationName = applicationName;
142+
// Do not set the data source name - this makes the pool name default to the Application Name
143+
await using var dataSource = dataSourceBuilder.Build();
144+
145+
meterProvider.ForceFlush();
146+
147+
var metric = exportedItems.Single(m => m.Name == "db.client.connection.max");
148+
var point = GetFilteredPoints(metric.GetMetricPoints(), dataSource.Name).First();
149+
var tags = ToDictionary(point.Tags);
150+
Assert.That(tags["db.client.connection.pool.name"], Is.EqualTo(applicationName));
151+
}
152+
130153
[Test]
131154
public async Task Password_does_not_leak_via_datasource_name([Values] bool persistSecurityInfo)
132155
{
@@ -137,10 +160,9 @@ public async Task Password_does_not_leak_via_datasource_name([Values] bool persi
137160
.Build();
138161

139162
var dataSourceBuilder = base.CreateDataSourceBuilder();
140-
dataSourceBuilder.ConnectionStringBuilder.ApplicationName = "MetricsDataSource" + Interlocked.Increment(ref _dataSourceCounter);
141163
dataSourceBuilder.ConnectionStringBuilder.PersistSecurityInfo = persistSecurityInfo;
142-
// Do not set the data source name - this makes it default to the connection string, but without
143-
// the password (even when Persist Security Info is true)
164+
// Do not set the data source name or the application name - this makes the pool name default to the
165+
// connection string, but without the password (even when Persist Security Info is true)
144166
await using var dataSource = dataSourceBuilder.Build();
145167

146168
meterProvider.ForceFlush();

0 commit comments

Comments
 (0)