Skip to content

Commit 0124e0a

Browse files
authored
Enable the new OpenTelemetry metrics for net6.0 (#5174)
Closes #3960
1 parent b3282aa commit 0124e0a

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

Directory.Packages.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<ItemGroup>
3-
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
3+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
44
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
55
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.4" />
66

@@ -49,10 +49,10 @@
4949
<PackageVersion Include="System.Text.Json" Version="7.0.3" />
5050
<PackageVersion Include="System.Threading.Channels" Version="7.0.0" />
5151
<PackageVersion Include="System.Collections.Immutable" Version="7.0.0" />
52-
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="6.0.0" />
5352
</ItemGroup>
5453

5554
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'netstandard2.1' OR '$(TargetFramework)' == 'net6.0' ">
5655
<PackageVersion Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
56+
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="7.0.2" />
5757
</ItemGroup>
5858
</Project>

src/Npgsql/MetricsReporter.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Npgsql;
44

5-
#if NET7_0_OR_GREATER
5+
#if NET6_0_OR_GREATER
66
using System.Collections.Generic;
77
using System.Diagnostics;
88
using System.Diagnostics.Metrics;
@@ -114,7 +114,11 @@ internal void ReportCommandStop(long startTimestamp)
114114

115115
if (CommandDuration.Enabled && startTimestamp > 0)
116116
{
117+
#if NET7_0_OR_GREATER
117118
var duration = Stopwatch.GetElapsedTime(startTimestamp);
119+
#else
120+
var duration = new TimeSpan((long)((Stopwatch.GetTimestamp() - startTimestamp) * StopWatchTickFrequency));
121+
#endif
118122
CommandDuration.Record(duration.TotalMilliseconds, _poolNameTag);
119123
}
120124
}
@@ -216,9 +220,15 @@ public void Dispose()
216220
Reporters.Remove(this);
217221
}
218222
}
223+
224+
#if !NET7_0_OR_GREATER
225+
const long TicksPerMicrosecond = 10;
226+
const long TicksPerMillisecond = TicksPerMicrosecond * 1000;
227+
const long TicksPerSecond = TicksPerMillisecond * 1000; // 10,000,000
228+
static readonly double StopWatchTickFrequency = (double)TicksPerSecond / Stopwatch.Frequency;
229+
#endif
219230
}
220231
#else
221-
// Unfortunately, UpDownCounter is only supported starting with net7.0, and since a lot of the metrics rely on it,
222232
sealed class MetricsReporter : IDisposable
223233
{
224234
public MetricsReporter(NpgsqlDataSource _) {}

src/Npgsql/Npgsql.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
<PackageReference Include="System.Text.Json" />
3131
<PackageReference Include="System.Threading.Channels" />
3232
<PackageReference Include="System.Collections.Immutable" />
33-
<PackageReference Include="System.Diagnostics.DiagnosticSource" />
3433
</ItemGroup>
3534

3635
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'netstandard2.1' OR '$(TargetFramework)' == 'net6.0' ">
3736
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" />
37+
<PackageReference Include="System.Diagnostics.DiagnosticSource" />
3838
</ItemGroup>
3939

4040
<ItemGroup>

0 commit comments

Comments
 (0)