-
Notifications
You must be signed in to change notification settings - Fork 847
Improve instrument name validation and log messages #6457
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
454f7c8
d61dc99
210bfed
8972d9d
3933f30
7895f9e
2734a39
ed6a109
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
using System.Runtime.CompilerServices; | ||
using System.Text.RegularExpressions; | ||
|
||
#pragma warning disable SA1402 // File may only contain a single type | ||
#pragma warning disable SA1403 // File may only contain a single namespace | ||
|
@@ -48,6 +49,14 @@ namespace OpenTelemetry.Internal | |
/// </summary> | ||
internal static class Guard | ||
{ | ||
// Note: We don't use static readonly here because some customers | ||
|
||
// replace this using reflection which is not allowed on initonly static | ||
// fields. See: https://github.com/dotnet/runtime/issues/11571. | ||
// Customers: This is not guaranteed to work forever. We may change this | ||
// mechanism in the future do this at your own risk. | ||
|
||
public static Regex InstrumentNameRegex { get; set; } = new( | ||
@"^[a-z][a-z0-9-._/]{0,254}$", RegexOptions.IgnoreCase | RegexOptions.Compiled); | ||
martincostello marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
/// <summary> | ||
/// Throw an exception if the value is null. | ||
/// </summary> | ||
|
@@ -178,6 +187,74 @@ public static T ThrowIfNotOfType<T>([NotNull] object? value, [CallerArgumentExpr | |
return result; | ||
} | ||
|
||
/// <summary> | ||
/// Throws an exception if the given view name is invalid according to the specification. | ||
/// Null is valid because the instrument name will be used as the view name. | ||
/// </summary> | ||
/// <remarks>See specification: <see href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument"/>.</remarks> | ||
/// <param name="viewName">The view name.</param> | ||
[DebuggerHidden] | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static void ThrowIfInvalidViewName(string? viewName) | ||
{ | ||
if (!IsValidViewName(viewName)) | ||
{ | ||
throw new ArgumentException($"View name {viewName} is invalid.", nameof(viewName)); | ||
martincostello marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
/// <summary> | ||
/// Throws an exception if the given custom view name is invalid according to the specification. | ||
/// </summary> | ||
/// <remarks>See specification: <see href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument"/>.</remarks> | ||
/// <param name="viewName">The view name.</param> | ||
[DebuggerHidden] | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static void ThrowIfInvalidCustomViewName(string viewName) | ||
{ | ||
if (!IsValidInstrumentName(viewName)) | ||
{ | ||
throw new ArgumentException($"Custom view name {viewName} is invalid.", nameof(viewName)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Returns whether the given instrument name is valid according to the specification. | ||
/// </summary> | ||
/// <remarks>See specification: <see href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument"/>.</remarks> | ||
/// <param name="instrumentName">The instrument name.</param> | ||
/// <returns>Boolean indicating if the instrument is valid.</returns> | ||
[DebuggerHidden] | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool IsValidInstrumentName(string instrumentName) | ||
{ | ||
if (string.IsNullOrWhiteSpace(instrumentName)) | ||
{ | ||
return false; | ||
} | ||
|
||
return InstrumentNameRegex.IsMatch(instrumentName); | ||
} | ||
|
||
/// <summary> | ||
/// Returns whether the given custom view name is valid according to the specification. | ||
/// </summary> | ||
/// <remarks>See specification: <see href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument"/>.</remarks> | ||
/// <param name="customViewName">The view name.</param> | ||
/// <returns>Boolean indicating if the instrument is valid.</returns> | ||
[DebuggerHidden] | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static bool IsValidViewName(string? customViewName) | ||
{ | ||
// Only validate the view name in case it's not null. In case it's null, the view name will be the instrument name as per the spec. | ||
if (customViewName == null) | ||
{ | ||
return true; | ||
} | ||
|
||
return InstrumentNameRegex.IsMatch(customViewName); | ||
} | ||
|
||
[DebuggerHidden] | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static void Range<T>(T value, string? paramName, T min, T max, string? minName, string? maxName, string? message) | ||
|
Uh oh!
There was an error while loading. Please reload this page.