Skip to content

Commit 09861a1

Browse files
committed
Fix db.client.connections.create_time metric with .NET 6 (#6248)
Fixes #6247 (cherry picked from commit 3fb8541)
1 parent 03e95a8 commit 09861a1

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/Npgsql/MetricsReporter.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,15 @@ internal void ReportPendingConnectionRequestStart()
165165
internal void ReportPendingConnectionRequestStop()
166166
=> PendingConnectionRequests.Add(-1, _poolNameTag);
167167

168-
internal void ReportConnectionCreateTime(TimeSpan duration)
169-
=> ConnectionCreateTime.Record(duration.TotalSeconds, _poolNameTag);
168+
internal void ReportConnectionCreateTime(long startTimestamp)
169+
{
170+
#if NET7_0_OR_GREATER
171+
var duration = Stopwatch.GetElapsedTime(startTimestamp);
172+
#else
173+
var duration = new TimeSpan((long)((Stopwatch.GetTimestamp() - startTimestamp) * StopWatchTickFrequency));
174+
#endif
175+
ConnectionCreateTime.Record(duration.TotalSeconds, _poolNameTag);
176+
}
170177

171178
static IEnumerable<Measurement<int>> GetConnectionUsage()
172179
{

src/Npgsql/PoolingDataSource.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,10 @@ bool CheckIdleConnector([NotNullWhen(true)] NpgsqlConnector? connector)
268268
try
269269
{
270270
// We've managed to increase the open counter, open a physical connections.
271-
#if NET7_0_OR_GREATER
272271
var startTime = Stopwatch.GetTimestamp();
273-
#endif
274272
var connector = new NpgsqlConnector(this, conn) { ClearCounter = _clearCounter };
275273
await connector.Open(timeout, async, cancellationToken).ConfigureAwait(false);
276-
#if NET7_0_OR_GREATER
277-
MetricsReporter.ReportConnectionCreateTime(Stopwatch.GetElapsedTime(startTime));
278-
#endif
274+
MetricsReporter.ReportConnectionCreateTime(startTime);
279275

280276
var i = 0;
281277
for (; i < MaxConnections; i++)

0 commit comments

Comments
 (0)