Skip to content

Commit e382fb0

Browse files
Added OpenTelemetry.Instrumentation.Runtime metrics to built in metrics (#3133)
1 parent d32c2af commit e382fb0

File tree

9 files changed

+45
-7
lines changed

9 files changed

+45
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Features
6+
7+
- The SDK now automatically collects metrics coming from `OpenTelemetry.Instrumentation.Runtime` ([#3133](https://github.com/getsentry/sentry-dotnet/pull/3133))
8+
59
### Fixes
610

711
- "No service for type 'Sentry.IHub' has been registered" exception when using OpenTelemetry and initializing Sentry via `SentrySdk.Init` ([#3129](https://github.com/getsentry/sentry-dotnet/pull/3129))

samples/Sentry.Samples.OpenTelemetry.AspNetCore/Program.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics;
22
using Microsoft.AspNetCore.Authentication;
3+
using OpenTelemetry.Metrics;
34
using OpenTelemetry.Resources;
45
using OpenTelemetry.Trace;
56
using Sentry.OpenTelemetry;
@@ -10,6 +11,16 @@
1011
// OpenTelemetry Configuration
1112
// See https://opentelemetry.io/docs/instrumentation/net/getting-started/
1213
builder.Services.AddOpenTelemetry()
14+
.WithMetrics(metrics =>
15+
{
16+
metrics
17+
.AddRuntimeInstrumentation() // <-- Requires the OpenTelemetry.Instrumentation.Runtime package
18+
// Collect some of the built-in ASP.NET Core metrics
19+
.AddMeter(
20+
"Microsoft.AspNetCore.Hosting",
21+
"Microsoft.AspNetCore.Server.Kestrel",
22+
"System.Net.Http");
23+
})
1324
.WithTracing(tracerProviderBuilder =>
1425
tracerProviderBuilder
1526
.AddSource(Telemetry.ActivitySource.Name)
@@ -21,10 +32,15 @@
2132

2233
builder.WebHost.UseSentry(options =>
2334
{
24-
//options.Dsn = "...Your DSN...";
35+
// options.Dsn = "...Your DSN...";
2536
options.Debug = builder.Environment.IsDevelopment();
2637
options.SendDefaultPii = true;
2738
options.TracesSampleRate = 1.0;
39+
// This shows experimental support for capturing OpenTelemetry metrics with Sentry
40+
options.ExperimentalMetrics = new ExperimentalMetricsOptions()
41+
{
42+
CaptureSystemDiagnosticsMeters = BuiltInSystemDiagnosticsMeters.All
43+
};
2844
options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information
2945
});
3046

samples/Sentry.Samples.OpenTelemetry.AspNetCore/Sentry.Samples.OpenTelemetry.AspNetCore.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.5.0" />
11-
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.5.0-beta.1" />
12-
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.5.0-beta.1" />
10+
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
11+
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.0" />
12+
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.7.0" />
13+
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.7.0" />
1314
</ItemGroup>
1415

1516
<ItemGroup>

src/Sentry.AspNetCore/ApplicationBuilderExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public static IApplicationBuilder UseSentry(this IApplicationBuilder app)
3939
{
4040
o.UseStackTraceFactory(stackTraceFactory);
4141
}
42-
4342
}
4443

4544
var lifetime = app.ApplicationServices.GetService<IHostApplicationLifetime>();

src/Sentry/BuiltInSystemDiagnosticsMeters.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public static partial class BuiltInSystemDiagnosticsMeters
1515
private const string MicrosoftAspNetCoreHttpConnectionsPattern = @"^Microsoft\.AspNetCore\.Http\.Connections$";
1616
private const string MicrosoftExtensionsDiagnosticsHealthChecksPattern = @"^Microsoft\.Extensions\.Diagnostics\.HealthChecks$";
1717
private const string MicrosoftExtensionsDiagnosticsResourceMonitoringPattern = @"^Microsoft\.Extensions\.Diagnostics\.ResourceMonitoring$";
18+
private const string OpenTelemetryInstrumentationRuntimePattern = @"^OpenTelemetry\.Instrumentation\.Runtime$";
1819
private const string SystemNetNameResolutionPattern = @"^System\.Net\.NameResolution$";
1920
private const string SystemNetHttpPattern = @"^System\.Net\.Http$";
2021

@@ -126,6 +127,18 @@ public static partial class BuiltInSystemDiagnosticsMeters
126127
public static readonly SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsResourceMonitoring = new Regex(MicrosoftExtensionsDiagnosticsResourceMonitoringPattern, RegexOptions.Compiled);
127128
#endif
128129

130+
/// <summary>
131+
/// Matches the built in System.Net.NameResolution metrics
132+
/// </summary>
133+
#if NET8_0_OR_GREATER
134+
public static readonly SubstringOrRegexPattern OpenTelemetryInstrumentationRuntime = OpenTelemetryInstrumentationRuntimeRegex();
135+
136+
[GeneratedRegex(OpenTelemetryInstrumentationRuntimePattern, RegexOptions.Compiled)]
137+
private static partial Regex OpenTelemetryInstrumentationRuntimeRegex();
138+
#else
139+
public static readonly SubstringOrRegexPattern OpenTelemetryInstrumentationRuntime = new Regex(OpenTelemetryInstrumentationRuntimePattern, RegexOptions.Compiled);
140+
#endif
141+
129142
/// <summary>
130143
/// Matches the built in System.Net.NameResolution metrics
131144
/// </summary>
@@ -159,10 +172,11 @@ public static partial class BuiltInSystemDiagnosticsMeters
159172
MicrosoftAspNetCoreHeaderParsing,
160173
MicrosoftAspNetCoreServerKestrel,
161174
MicrosoftAspNetCoreHttpConnections,
175+
MicrosoftExtensionsDiagnosticsHealthChecks,
176+
MicrosoftExtensionsDiagnosticsResourceMonitoring,
177+
OpenTelemetryInstrumentationRuntime,
162178
SystemNetNameResolution,
163179
SystemNetHttp,
164-
MicrosoftExtensionsDiagnosticsHealthChecks,
165-
MicrosoftExtensionsDiagnosticsResourceMonitoring
166180
});
167181

168182
/// <summary>

test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ namespace Sentry
5151
public static readonly Sentry.SubstringOrRegexPattern MicrosoftAspNetCoreServerKestrel;
5252
public static readonly Sentry.SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsHealthChecks;
5353
public static readonly Sentry.SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsResourceMonitoring;
54+
public static readonly Sentry.SubstringOrRegexPattern OpenTelemetryInstrumentationRuntime;
5455
public static readonly Sentry.SubstringOrRegexPattern SystemNetHttp;
5556
public static readonly Sentry.SubstringOrRegexPattern SystemNetNameResolution;
5657
public static System.Collections.Generic.IList<Sentry.SubstringOrRegexPattern> All { get; }

test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ namespace Sentry
5151
public static readonly Sentry.SubstringOrRegexPattern MicrosoftAspNetCoreServerKestrel;
5252
public static readonly Sentry.SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsHealthChecks;
5353
public static readonly Sentry.SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsResourceMonitoring;
54+
public static readonly Sentry.SubstringOrRegexPattern OpenTelemetryInstrumentationRuntime;
5455
public static readonly Sentry.SubstringOrRegexPattern SystemNetHttp;
5556
public static readonly Sentry.SubstringOrRegexPattern SystemNetNameResolution;
5657
public static System.Collections.Generic.IList<Sentry.SubstringOrRegexPattern> All { get; }

test/Sentry.Tests/ApiApprovalTests.Run.DotNet8_0.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ namespace Sentry
5151
public static readonly Sentry.SubstringOrRegexPattern MicrosoftAspNetCoreServerKestrel;
5252
public static readonly Sentry.SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsHealthChecks;
5353
public static readonly Sentry.SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsResourceMonitoring;
54+
public static readonly Sentry.SubstringOrRegexPattern OpenTelemetryInstrumentationRuntime;
5455
public static readonly Sentry.SubstringOrRegexPattern SystemNetHttp;
5556
public static readonly Sentry.SubstringOrRegexPattern SystemNetNameResolution;
5657
public static System.Collections.Generic.IList<Sentry.SubstringOrRegexPattern> All { get; }

test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ namespace Sentry
5151
public static readonly Sentry.SubstringOrRegexPattern MicrosoftAspNetCoreServerKestrel;
5252
public static readonly Sentry.SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsHealthChecks;
5353
public static readonly Sentry.SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsResourceMonitoring;
54+
public static readonly Sentry.SubstringOrRegexPattern OpenTelemetryInstrumentationRuntime;
5455
public static readonly Sentry.SubstringOrRegexPattern SystemNetHttp;
5556
public static readonly Sentry.SubstringOrRegexPattern SystemNetNameResolution;
5657
public static System.Collections.Generic.IList<Sentry.SubstringOrRegexPattern> All { get; }

0 commit comments

Comments
 (0)