Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added OpenTelemetry.Instrumentation.Runtime metrics to built in metrics #3133

Merged
merged 6 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Features

- Added OpenTelemetry.Instrumentation.Runtime to built in metrics ([#3133](https://github.com/getsentry/sentry-dotnet/pull/3133))
jamescrosswell marked this conversation as resolved.
Show resolved Hide resolved

### 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))
Expand Down
18 changes: 17 additions & 1 deletion samples/Sentry.Samples.OpenTelemetry.AspNetCore/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Authentication;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using Sentry.OpenTelemetry;
Expand All @@ -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)
Expand All @@ -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
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.5.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.5.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.5.0-beta.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.7.0" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/Sentry.AspNetCore/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public static IApplicationBuilder UseSentry(this IApplicationBuilder app)
{
o.UseStackTraceFactory(stackTraceFactory);
}

}

var lifetime = app.ApplicationServices.GetService<IHostApplicationLifetime>();
Expand Down
18 changes: 16 additions & 2 deletions src/Sentry/BuiltInSystemDiagnosticsMeters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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$";

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

/// <summary>
/// Matches the built in System.Net.NameResolution metrics
/// </summary>
#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

/// <summary>
/// Matches the built in System.Net.NameResolution metrics
/// </summary>
Expand Down Expand Up @@ -159,10 +172,11 @@ public static partial class BuiltInSystemDiagnosticsMeters
MicrosoftAspNetCoreHeaderParsing,
MicrosoftAspNetCoreServerKestrel,
MicrosoftAspNetCoreHttpConnections,
MicrosoftExtensionsDiagnosticsHealthChecks,
MicrosoftExtensionsDiagnosticsResourceMonitoring,
OpenTelemetryInstrumentationRuntime,
SystemNetNameResolution,
SystemNetHttp,
MicrosoftExtensionsDiagnosticsHealthChecks,
MicrosoftExtensionsDiagnosticsResourceMonitoring
});

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sentry.SubstringOrRegexPattern> All { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sentry.SubstringOrRegexPattern> All { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sentry.SubstringOrRegexPattern> All { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sentry.SubstringOrRegexPattern> All { get; }
Expand Down
Loading