Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
108 changes: 84 additions & 24 deletions src/Sentry/BuiltInSystemDiagnosticsMeters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ public static partial class BuiltInSystemDiagnosticsMeters
/// <summary>
/// Matches the built-in Microsoft.AspNetCore.Hosting metrics
/// </summary>
#if NET8_0_OR_GREATER
#if NET9_0_OR_GREATER
public static readonly StringOrRegex MicrosoftAspNetCoreHosting = MicrosoftAspNetCoreHostingRegex;

[GeneratedRegex(MicrosoftAspNetCoreHostingPattern)]
private static partial Regex MicrosoftAspNetCoreHostingRegex { get; }
#elif NET8_0
public static readonly StringOrRegex MicrosoftAspNetCoreHosting = MicrosoftAspNetCoreHostingRegex();

[GeneratedRegex(MicrosoftAspNetCoreHostingPattern, RegexOptions.Compiled)]
[GeneratedRegex(MicrosoftAspNetCoreHostingPattern)]
private static partial Regex MicrosoftAspNetCoreHostingRegex();
#else
public static readonly StringOrRegex MicrosoftAspNetCoreHosting = new Regex(MicrosoftAspNetCoreHostingPattern, RegexOptions.Compiled);
Expand All @@ -36,10 +41,15 @@ public static partial class BuiltInSystemDiagnosticsMeters
/// <summary>
/// Matches the built-in Microsoft.AspNetCore.Routing metrics
/// </summary>
#if NET8_0_OR_GREATER
#if NET9_0_OR_GREATER
public static readonly StringOrRegex MicrosoftAspNetCoreRouting = MicrosoftAspNetCoreRoutingRegex;

[GeneratedRegex(MicrosoftAspNetCoreRoutingPattern)]
private static partial Regex MicrosoftAspNetCoreRoutingRegex { get; }
#elif NET8_0
public static readonly StringOrRegex MicrosoftAspNetCoreRouting = MicrosoftAspNetCoreRoutingRegex();

[GeneratedRegex(MicrosoftAspNetCoreRoutingPattern, RegexOptions.Compiled)]
[GeneratedRegex(MicrosoftAspNetCoreRoutingPattern)]
private static partial Regex MicrosoftAspNetCoreRoutingRegex();
#else
public static readonly StringOrRegex MicrosoftAspNetCoreRouting = new Regex(MicrosoftAspNetCoreRoutingPattern, RegexOptions.Compiled);
Expand All @@ -48,10 +58,15 @@ public static partial class BuiltInSystemDiagnosticsMeters
/// <summary>
/// Matches the built-in Microsoft.AspNetCore.Diagnostics metrics
/// </summary>
#if NET8_0_OR_GREATER
#if NET9_0_OR_GREATER
public static readonly StringOrRegex MicrosoftAspNetCoreDiagnostics = MicrosoftAspNetCoreDiagnosticsRegex;

[GeneratedRegex(MicrosoftAspNetCoreDiagnosticsPattern)]
private static partial Regex MicrosoftAspNetCoreDiagnosticsRegex { get; }
#elif NET8_0
public static readonly StringOrRegex MicrosoftAspNetCoreDiagnostics = MicrosoftAspNetCoreDiagnosticsRegex();

[GeneratedRegex(MicrosoftAspNetCoreDiagnosticsPattern, RegexOptions.Compiled)]
[GeneratedRegex(MicrosoftAspNetCoreDiagnosticsPattern)]
private static partial Regex MicrosoftAspNetCoreDiagnosticsRegex();
#else
public static readonly StringOrRegex MicrosoftAspNetCoreDiagnostics = new Regex(MicrosoftAspNetCoreDiagnosticsPattern, RegexOptions.Compiled);
Expand All @@ -60,10 +75,15 @@ public static partial class BuiltInSystemDiagnosticsMeters
/// <summary>
/// Matches the built-in Microsoft.AspNetCore.RateLimiting metrics
/// </summary>
#if NET8_0_OR_GREATER
#if NET9_0_OR_GREATER
public static readonly StringOrRegex MicrosoftAspNetCoreRateLimiting = MicrosoftAspNetCoreRateLimitingRegex;

[GeneratedRegex(MicrosoftAspNetCoreRateLimitingPattern)]
private static partial Regex MicrosoftAspNetCoreRateLimitingRegex { get; }
#elif NET8_0
public static readonly StringOrRegex MicrosoftAspNetCoreRateLimiting = MicrosoftAspNetCoreRateLimitingRegex();

[GeneratedRegex(MicrosoftAspNetCoreRateLimitingPattern, RegexOptions.Compiled)]
[GeneratedRegex(MicrosoftAspNetCoreRateLimitingPattern)]
private static partial Regex MicrosoftAspNetCoreRateLimitingRegex();
#else
public static readonly StringOrRegex MicrosoftAspNetCoreRateLimiting = new Regex(MicrosoftAspNetCoreRateLimitingPattern, RegexOptions.Compiled);
Expand All @@ -72,10 +92,15 @@ public static partial class BuiltInSystemDiagnosticsMeters
/// <summary>
/// Matches the built-in Microsoft.AspNetCore.HeaderParsing metrics
/// </summary>
#if NET8_0_OR_GREATER
#if NET9_0_OR_GREATER
public static readonly StringOrRegex MicrosoftAspNetCoreHeaderParsing = MicrosoftAspNetCoreHeaderParsingRegex;

[GeneratedRegex(MicrosoftAspNetCoreHeaderParsingPattern)]
private static partial Regex MicrosoftAspNetCoreHeaderParsingRegex { get; }
#elif NET8_0
public static readonly StringOrRegex MicrosoftAspNetCoreHeaderParsing = MicrosoftAspNetCoreHeaderParsingRegex();

[GeneratedRegex(MicrosoftAspNetCoreHeaderParsingPattern, RegexOptions.Compiled)]
[GeneratedRegex(MicrosoftAspNetCoreHeaderParsingPattern)]
private static partial Regex MicrosoftAspNetCoreHeaderParsingRegex();
#else
public static readonly StringOrRegex MicrosoftAspNetCoreHeaderParsing = new Regex(MicrosoftAspNetCoreHeaderParsingPattern, RegexOptions.Compiled);
Expand All @@ -84,10 +109,15 @@ public static partial class BuiltInSystemDiagnosticsMeters
/// <summary>
/// Matches the built-in Microsoft.AspNetCore.Server.Kestrel metrics
/// </summary>
#if NET8_0_OR_GREATER
#if NET9_0_OR_GREATER
public static readonly StringOrRegex MicrosoftAspNetCoreServerKestrel = MicrosoftAspNetCoreServerKestrelRegex;

[GeneratedRegex(MicrosoftAspNetCoreServerKestrelPattern)]
private static partial Regex MicrosoftAspNetCoreServerKestrelRegex { get; }
#elif NET8_0
public static readonly StringOrRegex MicrosoftAspNetCoreServerKestrel = MicrosoftAspNetCoreServerKestrelRegex();

[GeneratedRegex(MicrosoftAspNetCoreServerKestrelPattern, RegexOptions.Compiled)]
[GeneratedRegex(MicrosoftAspNetCoreServerKestrelPattern)]
private static partial Regex MicrosoftAspNetCoreServerKestrelRegex();
#else
public static readonly StringOrRegex MicrosoftAspNetCoreServerKestrel = new Regex(MicrosoftAspNetCoreServerKestrelPattern, RegexOptions.Compiled);
Expand All @@ -96,10 +126,15 @@ public static partial class BuiltInSystemDiagnosticsMeters
/// <summary>
/// Matches the built-in Microsoft.AspNetCore.Http.Connections metrics
/// </summary>
#if NET8_0_OR_GREATER
#if NET9_0_OR_GREATER
public static readonly StringOrRegex MicrosoftAspNetCoreHttpConnections = MicrosoftAspNetCoreHttpConnectionsRegex;

[GeneratedRegex(MicrosoftAspNetCoreHttpConnectionsPattern)]
private static partial Regex MicrosoftAspNetCoreHttpConnectionsRegex { get; }
#elif NET8_0
public static readonly StringOrRegex MicrosoftAspNetCoreHttpConnections = MicrosoftAspNetCoreHttpConnectionsRegex();

[GeneratedRegex(MicrosoftAspNetCoreHttpConnectionsPattern, RegexOptions.Compiled)]
[GeneratedRegex(MicrosoftAspNetCoreHttpConnectionsPattern)]
private static partial Regex MicrosoftAspNetCoreHttpConnectionsRegex();
#else
public static readonly StringOrRegex MicrosoftAspNetCoreHttpConnections = new Regex(MicrosoftAspNetCoreHttpConnectionsPattern, RegexOptions.Compiled);
Expand All @@ -108,10 +143,15 @@ public static partial class BuiltInSystemDiagnosticsMeters
/// <summary>
/// Matches the built-in Microsoft.Extensions.Diagnostics.HealthChecks metrics
/// </summary>
#if NET8_0_OR_GREATER
#if NET9_0_OR_GREATER
public static readonly StringOrRegex MicrosoftExtensionsDiagnosticsHealthChecks = MicrosoftExtensionsDiagnosticsHealthChecksRegex;

[GeneratedRegex(MicrosoftExtensionsDiagnosticsHealthChecksPattern)]
private static partial Regex MicrosoftExtensionsDiagnosticsHealthChecksRegex { get; }
#elif NET8_0
public static readonly StringOrRegex MicrosoftExtensionsDiagnosticsHealthChecks = MicrosoftExtensionsDiagnosticsHealthChecksRegex();

[GeneratedRegex(MicrosoftExtensionsDiagnosticsHealthChecksPattern, RegexOptions.Compiled)]
[GeneratedRegex(MicrosoftExtensionsDiagnosticsHealthChecksPattern)]
private static partial Regex MicrosoftExtensionsDiagnosticsHealthChecksRegex();
#else
public static readonly StringOrRegex MicrosoftExtensionsDiagnosticsHealthChecks = new Regex(MicrosoftExtensionsDiagnosticsHealthChecksPattern, RegexOptions.Compiled);
Expand All @@ -120,10 +160,15 @@ public static partial class BuiltInSystemDiagnosticsMeters
/// <summary>
/// Matches the built-in Microsoft.Extensions.Diagnostics.ResourceMonitoring metrics
/// </summary>
#if NET8_0_OR_GREATER
#if NET9_0_OR_GREATER
public static readonly StringOrRegex MicrosoftExtensionsDiagnosticsResourceMonitoring = MicrosoftExtensionsDiagnosticsResourceMonitoringRegex;

[GeneratedRegex(MicrosoftExtensionsDiagnosticsResourceMonitoringPattern)]
private static partial Regex MicrosoftExtensionsDiagnosticsResourceMonitoringRegex { get; }
#elif NET8_0
public static readonly StringOrRegex MicrosoftExtensionsDiagnosticsResourceMonitoring = MicrosoftExtensionsDiagnosticsResourceMonitoringRegex();

[GeneratedRegex(MicrosoftExtensionsDiagnosticsResourceMonitoringPattern, RegexOptions.Compiled)]
[GeneratedRegex(MicrosoftExtensionsDiagnosticsResourceMonitoringPattern)]
private static partial Regex MicrosoftExtensionsDiagnosticsResourceMonitoringRegex();
#else
public static readonly StringOrRegex MicrosoftExtensionsDiagnosticsResourceMonitoring = new Regex(MicrosoftExtensionsDiagnosticsResourceMonitoringPattern, RegexOptions.Compiled);
Expand All @@ -132,10 +177,15 @@ public static partial class BuiltInSystemDiagnosticsMeters
/// <summary>
/// Matches the built-in System.Net.NameResolution metrics
/// </summary>
#if NET8_0_OR_GREATER
#if NET9_0_OR_GREATER
public static readonly StringOrRegex OpenTelemetryInstrumentationRuntime = OpenTelemetryInstrumentationRuntimeRegex;

[GeneratedRegex(OpenTelemetryInstrumentationRuntimePattern)]
private static partial Regex OpenTelemetryInstrumentationRuntimeRegex { get; }
#elif NET8_0
public static readonly StringOrRegex OpenTelemetryInstrumentationRuntime = OpenTelemetryInstrumentationRuntimeRegex();

[GeneratedRegex(OpenTelemetryInstrumentationRuntimePattern, RegexOptions.Compiled)]
[GeneratedRegex(OpenTelemetryInstrumentationRuntimePattern)]
private static partial Regex OpenTelemetryInstrumentationRuntimeRegex();
#else
public static readonly StringOrRegex OpenTelemetryInstrumentationRuntime = new Regex(OpenTelemetryInstrumentationRuntimePattern, RegexOptions.Compiled);
Expand All @@ -144,10 +194,15 @@ public static partial class BuiltInSystemDiagnosticsMeters
/// <summary>
/// Matches the built-in System.Net.NameResolution metrics
/// </summary>
#if NET8_0_OR_GREATER
#if NET9_0_OR_GREATER
public static readonly StringOrRegex SystemNetNameResolution = SystemNetNameResolutionRegex;

[GeneratedRegex(SystemNetNameResolutionPattern)]
private static partial Regex SystemNetNameResolutionRegex { get; }
#elif NET8_0
public static readonly StringOrRegex SystemNetNameResolution = SystemNetNameResolutionRegex();

[GeneratedRegex(SystemNetNameResolutionPattern, RegexOptions.Compiled)]
[GeneratedRegex(SystemNetNameResolutionPattern)]
private static partial Regex SystemNetNameResolutionRegex();
#else
public static readonly StringOrRegex SystemNetNameResolution = new Regex(SystemNetNameResolutionPattern, RegexOptions.Compiled);
Expand All @@ -156,10 +211,15 @@ public static partial class BuiltInSystemDiagnosticsMeters
/// <summary>
/// Matches the built-in <see cref="System.Net.Http"/> metrics
/// </summary>
#if NET8_0_OR_GREATER
#if NET9_0_OR_GREATER
public static readonly StringOrRegex SystemNetHttp = SystemNetHttpRegex;

[GeneratedRegex(SystemNetHttpPattern)]
private static partial Regex SystemNetHttpRegex { get; }
#elif NET8_0
public static readonly StringOrRegex SystemNetHttp = SystemNetHttpRegex();

[GeneratedRegex(SystemNetHttpPattern, RegexOptions.Compiled)]
[GeneratedRegex(SystemNetHttpPattern)]
private static partial Regex SystemNetHttpRegex();
#else
public static readonly StringOrRegex SystemNetHttp = new Regex(SystemNetHttpPattern, RegexOptions.Compiled);
Expand Down
13 changes: 8 additions & 5 deletions src/Sentry/Internal/OriginHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ internal static partial class OriginHelper
internal const string Manual = "manual";
private const string ValidOriginPattern = @"^(auto|manual)(\.[\w]+){0,3}$";

#if NET8_0_OR_GREATER
[GeneratedRegex(ValidOriginPattern, RegexOptions.Compiled)]
private static partial Regex ValidOriginRegEx();
private static readonly Regex ValidOrigin = ValidOriginRegEx();
#if NET9_0_OR_GREATER
[GeneratedRegex(ValidOriginPattern)]
private static partial Regex ValidOrigin { get; }
#elif NET8_0
[GeneratedRegex(ValidOriginPattern)]
private static partial Regex ValidOriginRegex();
private static readonly Regex ValidOrigin = ValidOriginRegex();
#else
private static readonly Regex ValidOrigin = new (ValidOriginPattern, RegexOptions.Compiled);
private static readonly Regex ValidOrigin = new(ValidOriginPattern, RegexOptions.Compiled);
#endif

public static bool IsValidOrigin(string? value) => value == null || ValidOrigin.IsMatch(value);
Expand Down
57 changes: 34 additions & 23 deletions src/Sentry/MetricHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,40 @@ internal static partial class MetricHelper
private static readonly DateTimeOffset UnixEpoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);
#endif

#if NET9_0_OR_GREATER
[GeneratedRegex(InvalidMetricKeyOrNameCharactersPattern)]
private static partial Regex InvalidMetricKeyOrNameCharacters { get; }

[GeneratedRegex(InvalidTagKeyCharactersPattern)]
private static partial Regex InvalidTagKeyCharacters { get; }

[GeneratedRegex(InvalidMetricUnitCharactersPattern)]
private static partial Regex InvalidMetricUnitCharacters { get; }
#elif NET8_0
[GeneratedRegex(InvalidMetricKeyOrNameCharactersPattern)]
private static partial Regex InvalidMetricKeyOrNameCharacters();

[GeneratedRegex(InvalidTagKeyCharactersPattern)]
private static partial Regex InvalidTagKeyCharacters();

[GeneratedRegex(InvalidMetricUnitCharactersPattern)]
private static partial Regex InvalidMetricUnitCharacters();
#else
private static readonly Regex InvalidMetricKeyOrNameCharacters = new(InvalidMetricKeyOrNameCharactersPattern, RegexOptions.Compiled);
private static readonly Regex InvalidTagKeyCharacters = new(InvalidTagKeyCharactersPattern, RegexOptions.Compiled);
private static readonly Regex InvalidMetricUnitCharacters = new(InvalidMetricUnitCharactersPattern, RegexOptions.Compiled);
#endif

#if NET8_0
internal static string SanitizeMetricKeyOrName(string input) => InvalidMetricKeyOrNameCharacters().Replace(input, "_");
internal static string SanitizeTagKey(string input) => InvalidTagKeyCharacters().Replace(input, "");
internal static string SanitizeMetricUnit(string input) => InvalidMetricUnitCharacters().Replace(input, "");
#else
internal static string SanitizeMetricKeyOrName(string input) => InvalidMetricKeyOrNameCharacters.Replace(input, "_");
internal static string SanitizeTagKey(string input) => InvalidTagKeyCharacters.Replace(input, "");
internal static string SanitizeMetricUnit(string input) => InvalidMetricUnitCharacters.Replace(input, "");
#endif

internal static long GetDayBucketKey(this DateTimeOffset timestamp)
{
var utc = timestamp.ToUniversalTime();
Expand All @@ -43,29 +77,6 @@ internal static DateTimeOffset GetCutoff() => DateTimeOffset.UtcNow
.Subtract(TimeSpan.FromSeconds(RollupInSeconds))
.Subtract(TimeSpan.FromMilliseconds(FlushShift));

#if NET7_0_OR_GREATER
[GeneratedRegex(InvalidMetricKeyOrNameCharactersPattern, RegexOptions.Compiled)]
private static partial Regex InvalidMetricKeyOrNameCharacters();
internal static string SanitizeMetricKeyOrName(string input) => InvalidMetricKeyOrNameCharacters().Replace(input, "_");

[GeneratedRegex(InvalidTagKeyCharactersPattern, RegexOptions.Compiled)]
private static partial Regex InvalidTagKeyCharacters();
internal static string SanitizeTagKey(string input) => InvalidTagKeyCharacters().Replace(input, "");

[GeneratedRegex(InvalidMetricUnitCharactersPattern, RegexOptions.Compiled)]
private static partial Regex InvalidMetricUnitCharacters();
internal static string SanitizeMetricUnit(string input) => InvalidMetricUnitCharacters().Replace(input, "");
#else
private static readonly Regex InvalidMetricKeyOrNameCharacters = new(InvalidMetricKeyOrNameCharactersPattern, RegexOptions.Compiled);
internal static string SanitizeMetricKeyOrName(string input) => InvalidMetricKeyOrNameCharacters.Replace(input, "_");

private static readonly Regex InvalidTagKeyCharacters = new(InvalidTagKeyCharactersPattern, RegexOptions.Compiled);
internal static string SanitizeTagKey(string input) => InvalidTagKeyCharacters.Replace(input, "");

private static readonly Regex InvalidMetricUnitCharacters = new(InvalidMetricUnitCharactersPattern, RegexOptions.Compiled);
internal static string SanitizeMetricUnit(string input) => InvalidMetricUnitCharacters.Replace(input, "");
#endif

private static readonly Lazy<KeyValuePair<string, string>[]> LazyTagValueReplacements = new(() =>
[
new KeyValuePair<string, string>("\n", @"\n"),
Expand Down
33 changes: 16 additions & 17 deletions src/Sentry/SentryMonitorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,29 @@ public enum SentryMonitorInterval
/// </summary>
public partial class SentryMonitorOptions : ISentryJsonSerializable
{
private SentryMonitorScheduleType _type = SentryMonitorScheduleType.None;
private string? _crontab;
private int? _interval;
private SentryMonitorInterval? _unit;

// Breakdown of the validation
// ^(\*|([0-5]?\d)) Minute 0 - 59
// (\s+)(\*|([01]?\d|2[0-3])) Hour 0 - 23
// (\s+)(\*|([1-9]|[12]\d|3[01])) Day 1 - 31
// (\s+)(\*|([1-9]|1[0-2])) Month 1 - 12
// (\s+)(\*|([0-7])) Weekday 0 - 7
// $ End of string
#if NET7_0_OR_GREATER
[GeneratedRegex(@"^(\*|([0-5]?\d))(\s+)(\*|([01]?\d|2[0-3]))(\s+)(\*|([1-9]|[12]\d|3[01]))(\s+)(\*|([1-9]|1[0-2]))(\s+)(\*|([0-7]))$", RegexOptions.IgnoreCase)]
private static partial Regex CrontabValidation();
#else
private static Regex? CrontabValidationInstance;
private const string ValidCrontabPattern = @"^(\*|([0-5]?\d))(\s+)(\*|([01]?\d|2[0-3]))(\s+)(\*|([1-9]|[12]\d|3[01]))(\s+)(\*|([1-9]|1[0-2]))(\s+)(\*|([0-7]))$";

private static Regex CrontabValidation()
{
return CrontabValidationInstance ??= new Regex(
@"^(\*|([0-5]?\d))(\s+)(\*|([01]?\d|2[0-3]))(\s+)(\*|([1-9]|[12]\d|3[01]))(\s+)(\*|([1-9]|1[0-2]))(\s+)(\*|([0-7]))$",
RegexOptions.Compiled | RegexOptions.CultureInvariant);
}
private SentryMonitorScheduleType _type = SentryMonitorScheduleType.None;
private string? _crontab;
private int? _interval;
private SentryMonitorInterval? _unit;

#if NET9_0_OR_GREATER
[GeneratedRegex(ValidCrontabPattern, RegexOptions.IgnoreCase)]
private static partial Regex ValidCrontab { get; }
#elif NET8_0
[GeneratedRegex(ValidCrontabPattern, RegexOptions.IgnoreCase)]
private static partial Regex ValidCrontabRegex();
private static readonly Regex ValidCrontab = ValidCrontabRegex();
#else
private static readonly Regex ValidCrontab = new(ValidCrontabPattern, RegexOptions.Compiled | RegexOptions.CultureInvariant);
#endif

/// <summary>
Expand All @@ -88,7 +87,7 @@ public void Interval(string crontab)
throw new ArgumentException("You tried to set the interval twice. The Check-Ins interval is supposed to be set only once.");
}

if (!CrontabValidation().IsMatch(crontab))
if (!ValidCrontab.IsMatch(crontab))
{
throw new ArgumentException("The provided crontab does not match the expected format of '* * * * *' " +
"translating to 'minute', 'hour', 'day of the month', 'month', and 'day of the week'.");
Expand Down
Loading