diff --git a/CHANGELOG.md b/CHANGELOG.md index e2b36edafb..be7b9bee75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Features + +- The SDK now automatically collects metrics coming from `OpenTelemetry.Instrumentation.Runtime` ([#3133](https://github.com/getsentry/sentry-dotnet/pull/3133)) + ### Fixes - "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)) diff --git a/samples/Sentry.Samples.OpenTelemetry.AspNetCore/Program.cs b/samples/Sentry.Samples.OpenTelemetry.AspNetCore/Program.cs index a44eac4a17..ec6db81935 100644 --- a/samples/Sentry.Samples.OpenTelemetry.AspNetCore/Program.cs +++ b/samples/Sentry.Samples.OpenTelemetry.AspNetCore/Program.cs @@ -1,5 +1,6 @@ using System.Diagnostics; using Microsoft.AspNetCore.Authentication; +using OpenTelemetry.Metrics; using OpenTelemetry.Resources; using OpenTelemetry.Trace; using Sentry.OpenTelemetry; @@ -10,6 +11,16 @@ // OpenTelemetry Configuration // See https://opentelemetry.io/docs/instrumentation/net/getting-started/ builder.Services.AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics + .AddRuntimeInstrumentation() // <-- Requires the OpenTelemetry.Instrumentation.Runtime package + // Collect some of the built-in ASP.NET Core metrics + .AddMeter( + "Microsoft.AspNetCore.Hosting", + "Microsoft.AspNetCore.Server.Kestrel", + "System.Net.Http"); + }) .WithTracing(tracerProviderBuilder => tracerProviderBuilder .AddSource(Telemetry.ActivitySource.Name) @@ -21,10 +32,15 @@ builder.WebHost.UseSentry(options => { - //options.Dsn = "...Your DSN..."; + // options.Dsn = "...Your DSN..."; options.Debug = builder.Environment.IsDevelopment(); options.SendDefaultPii = true; options.TracesSampleRate = 1.0; + // This shows experimental support for capturing OpenTelemetry metrics with Sentry + options.ExperimentalMetrics = new ExperimentalMetricsOptions() + { + CaptureSystemDiagnosticsMeters = BuiltInSystemDiagnosticsMeters.All + }; options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information }); diff --git a/samples/Sentry.Samples.OpenTelemetry.AspNetCore/Sentry.Samples.OpenTelemetry.AspNetCore.csproj b/samples/Sentry.Samples.OpenTelemetry.AspNetCore/Sentry.Samples.OpenTelemetry.AspNetCore.csproj index e2f0d0f360..f7ebe624b8 100644 --- a/samples/Sentry.Samples.OpenTelemetry.AspNetCore/Sentry.Samples.OpenTelemetry.AspNetCore.csproj +++ b/samples/Sentry.Samples.OpenTelemetry.AspNetCore/Sentry.Samples.OpenTelemetry.AspNetCore.csproj @@ -7,9 +7,10 @@ - - - + + + + diff --git a/src/Sentry.AspNetCore/ApplicationBuilderExtensions.cs b/src/Sentry.AspNetCore/ApplicationBuilderExtensions.cs index 6ac6c0903e..6a92a06f40 100644 --- a/src/Sentry.AspNetCore/ApplicationBuilderExtensions.cs +++ b/src/Sentry.AspNetCore/ApplicationBuilderExtensions.cs @@ -39,7 +39,6 @@ public static IApplicationBuilder UseSentry(this IApplicationBuilder app) { o.UseStackTraceFactory(stackTraceFactory); } - } var lifetime = app.ApplicationServices.GetService(); diff --git a/src/Sentry/BuiltInSystemDiagnosticsMeters.cs b/src/Sentry/BuiltInSystemDiagnosticsMeters.cs index be846b67d1..df5b2849fa 100644 --- a/src/Sentry/BuiltInSystemDiagnosticsMeters.cs +++ b/src/Sentry/BuiltInSystemDiagnosticsMeters.cs @@ -15,6 +15,7 @@ public static partial class BuiltInSystemDiagnosticsMeters private const string MicrosoftAspNetCoreHttpConnectionsPattern = @"^Microsoft\.AspNetCore\.Http\.Connections$"; private const string MicrosoftExtensionsDiagnosticsHealthChecksPattern = @"^Microsoft\.Extensions\.Diagnostics\.HealthChecks$"; private const string MicrosoftExtensionsDiagnosticsResourceMonitoringPattern = @"^Microsoft\.Extensions\.Diagnostics\.ResourceMonitoring$"; + private const string OpenTelemetryInstrumentationRuntimePattern = @"^OpenTelemetry\.Instrumentation\.Runtime$"; private const string SystemNetNameResolutionPattern = @"^System\.Net\.NameResolution$"; private const string SystemNetHttpPattern = @"^System\.Net\.Http$"; @@ -126,6 +127,18 @@ public static partial class BuiltInSystemDiagnosticsMeters public static readonly SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsResourceMonitoring = new Regex(MicrosoftExtensionsDiagnosticsResourceMonitoringPattern, RegexOptions.Compiled); #endif + /// + /// Matches the built in System.Net.NameResolution metrics + /// +#if NET8_0_OR_GREATER + public static readonly SubstringOrRegexPattern OpenTelemetryInstrumentationRuntime = OpenTelemetryInstrumentationRuntimeRegex(); + + [GeneratedRegex(OpenTelemetryInstrumentationRuntimePattern, RegexOptions.Compiled)] + private static partial Regex OpenTelemetryInstrumentationRuntimeRegex(); +#else + public static readonly SubstringOrRegexPattern OpenTelemetryInstrumentationRuntime = new Regex(OpenTelemetryInstrumentationRuntimePattern, RegexOptions.Compiled); +#endif + /// /// Matches the built in System.Net.NameResolution metrics /// @@ -159,10 +172,11 @@ public static partial class BuiltInSystemDiagnosticsMeters MicrosoftAspNetCoreHeaderParsing, MicrosoftAspNetCoreServerKestrel, MicrosoftAspNetCoreHttpConnections, + MicrosoftExtensionsDiagnosticsHealthChecks, + MicrosoftExtensionsDiagnosticsResourceMonitoring, + OpenTelemetryInstrumentationRuntime, SystemNetNameResolution, SystemNetHttp, - MicrosoftExtensionsDiagnosticsHealthChecks, - MicrosoftExtensionsDiagnosticsResourceMonitoring }); /// diff --git a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt index fb1c9b2f95..855b387110 100644 --- a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt +++ b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt @@ -51,6 +51,7 @@ namespace Sentry public static readonly Sentry.SubstringOrRegexPattern MicrosoftAspNetCoreServerKestrel; public static readonly Sentry.SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsHealthChecks; public static readonly Sentry.SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsResourceMonitoring; + public static readonly Sentry.SubstringOrRegexPattern OpenTelemetryInstrumentationRuntime; public static readonly Sentry.SubstringOrRegexPattern SystemNetHttp; public static readonly Sentry.SubstringOrRegexPattern SystemNetNameResolution; public static System.Collections.Generic.IList All { get; } diff --git a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt index fb1c9b2f95..855b387110 100644 --- a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt +++ b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt @@ -51,6 +51,7 @@ namespace Sentry public static readonly Sentry.SubstringOrRegexPattern MicrosoftAspNetCoreServerKestrel; public static readonly Sentry.SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsHealthChecks; public static readonly Sentry.SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsResourceMonitoring; + public static readonly Sentry.SubstringOrRegexPattern OpenTelemetryInstrumentationRuntime; public static readonly Sentry.SubstringOrRegexPattern SystemNetHttp; public static readonly Sentry.SubstringOrRegexPattern SystemNetNameResolution; public static System.Collections.Generic.IList All { get; } diff --git a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet8_0.verified.txt b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet8_0.verified.txt index 7663be3716..53b5a74c95 100644 --- a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet8_0.verified.txt +++ b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet8_0.verified.txt @@ -51,6 +51,7 @@ namespace Sentry public static readonly Sentry.SubstringOrRegexPattern MicrosoftAspNetCoreServerKestrel; public static readonly Sentry.SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsHealthChecks; public static readonly Sentry.SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsResourceMonitoring; + public static readonly Sentry.SubstringOrRegexPattern OpenTelemetryInstrumentationRuntime; public static readonly Sentry.SubstringOrRegexPattern SystemNetHttp; public static readonly Sentry.SubstringOrRegexPattern SystemNetNameResolution; public static System.Collections.Generic.IList All { get; } diff --git a/test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt b/test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt index 7564580e9a..67fd8a5703 100644 --- a/test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt +++ b/test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt @@ -51,6 +51,7 @@ namespace Sentry public static readonly Sentry.SubstringOrRegexPattern MicrosoftAspNetCoreServerKestrel; public static readonly Sentry.SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsHealthChecks; public static readonly Sentry.SubstringOrRegexPattern MicrosoftExtensionsDiagnosticsResourceMonitoring; + public static readonly Sentry.SubstringOrRegexPattern OpenTelemetryInstrumentationRuntime; public static readonly Sentry.SubstringOrRegexPattern SystemNetHttp; public static readonly Sentry.SubstringOrRegexPattern SystemNetNameResolution; public static System.Collections.Generic.IList All { get; }