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

Renaming Guard Methods to make their action more explicit #2762

Merged
merged 2 commits into from
Jan 12, 2022
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
2 changes: 1 addition & 1 deletion src/OpenTelemetry.Api/Baggage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public IReadOnlyDictionary<string, string> GetBaggage()
/// <returns>Baggage item or <see langword="null"/> if nothing was found.</returns>
public string GetBaggage(string name)
{
Guard.NullOrEmpty(name, nameof(name));
Guard.ThrowIfNullOrEmpty(name, nameof(name));

return this.baggage != null && this.baggage.TryGetValue(name, out string value)
? value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class CompositeTextMapPropagator : TextMapPropagator
/// <param name="propagators">List of <see cref="TextMapPropagator"/> wire context propagator.</param>
public CompositeTextMapPropagator(IEnumerable<TextMapPropagator> propagators)
{
Guard.Null(propagators, nameof(propagators));
Guard.ThrowIfNull(propagators, nameof(propagators));

this.propagators = new List<TextMapPropagator>(propagators);
}
Expand Down
14 changes: 7 additions & 7 deletions src/OpenTelemetry.Api/Context/RuntimeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static class RuntimeContext
/// <returns>The slot registered.</returns>
public static RuntimeContextSlot<T> RegisterSlot<T>(string slotName)
{
Guard.NullOrEmpty(slotName, nameof(slotName));
Guard.ThrowIfNullOrEmpty(slotName, nameof(slotName));

lock (Slots)
{
Expand All @@ -66,9 +66,9 @@ public static RuntimeContextSlot<T> RegisterSlot<T>(string slotName)
/// <returns>The slot previously registered.</returns>
public static RuntimeContextSlot<T> GetSlot<T>(string slotName)
{
Guard.NullOrEmpty(slotName, nameof(slotName));
Guard.ThrowIfNullOrEmpty(slotName, nameof(slotName));
var slot = GuardNotFound(slotName);
var contextSlot = Guard.Type<RuntimeContextSlot<T>>(slot, nameof(slot));
var contextSlot = Guard.ThrowIfNotOfType<RuntimeContextSlot<T>>(slot, nameof(slot));
return contextSlot;
}

Expand Down Expand Up @@ -127,9 +127,9 @@ public static T GetValue<T>(string slotName)
/// <param name="value">The value to be set.</param>
public static void SetValue(string slotName, object value)
{
Guard.NullOrEmpty(slotName, nameof(slotName));
Guard.ThrowIfNullOrEmpty(slotName, nameof(slotName));
var slot = GuardNotFound(slotName);
var runtimeContextSlotValueAccessor = Guard.Type<IRuntimeContextSlotValueAccessor>(slot, nameof(slot));
var runtimeContextSlotValueAccessor = Guard.ThrowIfNotOfType<IRuntimeContextSlotValueAccessor>(slot, nameof(slot));
runtimeContextSlotValueAccessor.Value = value;
}

Expand All @@ -140,9 +140,9 @@ public static void SetValue(string slotName, object value)
/// <returns>The value retrieved from the context slot.</returns>
public static object GetValue(string slotName)
{
Guard.NullOrEmpty(slotName, nameof(slotName));
Guard.ThrowIfNullOrEmpty(slotName, nameof(slotName));
var slot = GuardNotFound(slotName);
var runtimeContextSlotValueAccessor = Guard.Type<IRuntimeContextSlotValueAccessor>(slot, nameof(slot));
var runtimeContextSlotValueAccessor = Guard.ThrowIfNotOfType<IRuntimeContextSlotValueAccessor>(slot, nameof(slot));
return runtimeContextSlotValueAccessor.Value;
}

Expand Down
22 changes: 11 additions & 11 deletions src/OpenTelemetry.Api/Internal/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal static class Guard
/// <param name="paramName">The parameter name to use in the thrown exception.</param>
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Null(object value, string paramName = DefaultParamName)
public static void ThrowIfNull(object value, string paramName = DefaultParamName)
{
if (value is null)
{
Expand All @@ -50,7 +50,7 @@ public static void Null(object value, string paramName = DefaultParamName)
/// <param name="paramName">The parameter name to use in the thrown exception.</param>
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void NullOrEmpty(string value, string paramName = DefaultParamName)
public static void ThrowIfNullOrEmpty(string value, string paramName = DefaultParamName)
{
if (string.IsNullOrEmpty(value))
{
Expand All @@ -65,7 +65,7 @@ public static void NullOrEmpty(string value, string paramName = DefaultParamName
/// <param name="paramName">The parameter name to use in the thrown exception.</param>
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void NullOrWhitespace(string value, string paramName = DefaultParamName)
public static void ThrowIfNullOrWhitespace(string value, string paramName = DefaultParamName)
{
if (string.IsNullOrWhiteSpace(value))
{
Expand All @@ -81,7 +81,7 @@ public static void NullOrWhitespace(string value, string paramName = DefaultPara
/// <param name="paramName">The parameter name to use in the thrown exception.</param>
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Zero(int value, string message = "Must not be zero", string paramName = DefaultParamName)
public static void ThrowIfZero(int value, string message = "Must not be zero", string paramName = DefaultParamName)
{
if (value == 0)
{
Expand All @@ -96,9 +96,9 @@ public static void Zero(int value, string message = "Must not be zero", string p
/// <param name="paramName">The parameter name to use in the thrown exception.</param>
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InvalidTimeout(int value, string paramName = DefaultParamName)
public static void ThrowIfInvalidTimeout(int value, string paramName = DefaultParamName)
{
Range(value, paramName, min: Timeout.Infinite, message: $"Must be non-negative or '{nameof(Timeout)}.{nameof(Timeout.Infinite)}'");
ThrowIfOutOfRange(value, paramName, min: Timeout.Infinite, message: $"Must be non-negative or '{nameof(Timeout)}.{nameof(Timeout.Infinite)}'");
}

/// <summary>
Expand All @@ -113,9 +113,9 @@ public static void InvalidTimeout(int value, string paramName = DefaultParamName
/// <param name="message">An optional custom message to use in the thrown exception.</param>
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Range(int value, string paramName = DefaultParamName, int min = int.MinValue, int max = int.MaxValue, string minName = null, string maxName = null, string message = null)
public static void ThrowIfOutOfRange(int value, string paramName = DefaultParamName, int min = int.MinValue, int max = int.MaxValue, string minName = null, string maxName = null, string message = null)
{
Range<int>(value, paramName, min, max, minName, maxName, message);
Range(value, paramName, min, max, minName, maxName, message);
}

/// <summary>
Expand All @@ -130,9 +130,9 @@ public static void Range(int value, string paramName = DefaultParamName, int min
/// <param name="message">An optional custom message to use in the thrown exception.</param>
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Range(double value, string paramName = DefaultParamName, double min = double.MinValue, double max = double.MaxValue, string minName = null, string maxName = null, string message = null)
public static void ThrowIfOutOfRange(double value, string paramName = DefaultParamName, double min = double.MinValue, double max = double.MaxValue, string minName = null, string maxName = null, string message = null)
{
Range<double>(value, paramName, min, max, minName, maxName, message);
Range(value, paramName, min, max, minName, maxName, message);
}

/// <summary>
Expand All @@ -144,7 +144,7 @@ public static void Range(double value, string paramName = DefaultParamName, doub
/// <returns>The value casted to the specified type.</returns>
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Type<T>(object value, string paramName = DefaultParamName)
public static T ThrowIfNotOfType<T>(object value, string paramName = DefaultParamName)
{
if (value is not T result)
{
Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry.Api/Trace/SpanAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public SpanAttributes()
public SpanAttributes(IEnumerable<KeyValuePair<string, object>> attributes)
: this()
{
Guard.Null(attributes, nameof(attributes));
Guard.ThrowIfNull(attributes, nameof(attributes));

foreach (KeyValuePair<string, object> kvp in attributes)
{
Expand Down Expand Up @@ -133,7 +133,7 @@ public void Add(string key, double[] values)

private void AddInternal(string key, object value)
{
Guard.Null(key, nameof(key));
Guard.ThrowIfNull(key, nameof(key));

this.Attributes[key] = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static class ConsoleExporterHelperExtensions
[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The objects should not be disposed.")]
public static TracerProviderBuilder AddConsoleExporter(this TracerProviderBuilder builder, Action<ConsoleExporterOptions> configure = null)
{
Guard.Null(builder, nameof(builder));
Guard.ThrowIfNull(builder, nameof(builder));

if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static class ConsoleExporterLoggingExtensions
/// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
public static OpenTelemetryLoggerOptions AddConsoleExporter(this OpenTelemetryLoggerOptions loggerOptions, Action<ConsoleExporterOptions> configure = null)
{
Guard.Null(loggerOptions, nameof(loggerOptions));
Guard.ThrowIfNull(loggerOptions, nameof(loggerOptions));

var options = new ConsoleExporterOptions();
configure?.Invoke(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static class ConsoleExporterMetricsExtensions
[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The objects should not be disposed.")]
public static MeterProviderBuilder AddConsoleExporter(this MeterProviderBuilder builder, Action<ConsoleExporterOptions> configure = null)
{
Guard.Null(builder, nameof(builder));
Guard.ThrowIfNull(builder, nameof(builder));

var options = new ConsoleExporterOptions();
configure?.Invoke(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public static class InMemoryExporterHelperExtensions
[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The objects should not be disposed.")]
public static TracerProviderBuilder AddInMemoryExporter(this TracerProviderBuilder builder, ICollection<Activity> exportedItems)
{
Guard.Null(builder, nameof(builder));
Guard.Null(exportedItems, nameof(exportedItems));
Guard.ThrowIfNull(builder, nameof(builder));
Guard.ThrowIfNull(exportedItems, nameof(exportedItems));

if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public static class InMemoryExporterLoggingExtensions
{
public static OpenTelemetryLoggerOptions AddInMemoryExporter(this OpenTelemetryLoggerOptions loggerOptions, ICollection<LogRecord> exportedItems)
{
Guard.Null(loggerOptions, nameof(loggerOptions));
Guard.Null(exportedItems, nameof(exportedItems));
Guard.ThrowIfNull(loggerOptions, nameof(loggerOptions));
Guard.ThrowIfNull(exportedItems, nameof(exportedItems));

return loggerOptions.AddProcessor(new SimpleLogRecordExportProcessor(new InMemoryExporter<LogRecord>(exportedItems)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public static class InMemoryExporterMetricsExtensions
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddInMemoryExporter(this MeterProviderBuilder builder, ICollection<Metric> exportedItems)
{
Guard.Null(builder, nameof(builder));
Guard.Null(exportedItems, nameof(exportedItems));
Guard.ThrowIfNull(builder, nameof(builder));
Guard.ThrowIfNull(exportedItems, nameof(exportedItems));

return builder.AddReader(new BaseExportingMetricReader(new InMemoryExporter<Metric>(exportedItems)));
}
Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry.Exporter.Jaeger/JaegerExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public JaegerExporter(JaegerExporterOptions options)

internal JaegerExporter(JaegerExporterOptions options, TProtocolFactory protocolFactory = null, IJaegerClient client = null)
{
Guard.Null(options, nameof(options));
Guard.ThrowIfNull(options, nameof(options));

this.maxPayloadSizeInBytes = (!options.MaxPayloadSizeInBytes.HasValue || options.MaxPayloadSizeInBytes <= 0)
? JaegerExporterOptions.DefaultMaxPayloadSizeInBytes
Expand Down Expand Up @@ -122,7 +122,7 @@ public override ExportResult Export(in Batch<Activity> activityBatch)

internal void SetResourceAndInitializeBatch(Resource resource)
{
Guard.Null(resource, nameof(resource));
Guard.ThrowIfNull(resource, nameof(resource));

var process = this.Process;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static class JaegerExporterHelperExtensions
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddJaegerExporter(this TracerProviderBuilder builder, Action<JaegerExporterOptions> configure = null)
{
Guard.Null(builder, nameof(builder));
Guard.ThrowIfNull(builder, nameof(builder));

if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static class OtlpLogExporterHelperExtensions
/// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
public static OpenTelemetryLoggerOptions AddOtlpExporter(this OpenTelemetryLoggerOptions loggerOptions, Action<OtlpExporterOptions> configure = null)
{
Guard.Null(loggerOptions);
Guard.ThrowIfNull(loggerOptions);

return AddOtlpExporter(loggerOptions, new OtlpExporterOptions(), configure);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ internal abstract class BaseOtlpGrpcExportClient<TRequest> : IExportClient<TRequ
{
protected BaseOtlpGrpcExportClient(OtlpExporterOptions options)
{
Guard.Null(options, nameof(options));
Guard.InvalidTimeout(options.TimeoutMilliseconds, nameof(options.TimeoutMilliseconds));
Guard.ThrowIfNull(options, nameof(options));
Guard.ThrowIfInvalidTimeout(options.TimeoutMilliseconds, nameof(options.TimeoutMilliseconds));

ExporterClientValidation.EnsureUnencryptedSupportIsEnabled(options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ internal abstract class BaseOtlpHttpExportClient<TRequest> : IExportClient<TRequ
{
protected BaseOtlpHttpExportClient(OtlpExporterOptions options, HttpClient httpClient)
{
Guard.Null(options, nameof(options));
Guard.Null(httpClient, nameof(httpClient));
Guard.InvalidTimeout(options.TimeoutMilliseconds, $"{nameof(options)}.{nameof(options.TimeoutMilliseconds)}");
Guard.ThrowIfNull(options, nameof(options));
Guard.ThrowIfNull(httpClient, nameof(httpClient));
Guard.ThrowIfInvalidTimeout(options.TimeoutMilliseconds, $"{nameof(options)}.{nameof(options.TimeoutMilliseconds)}");

this.Options = options;
this.Headers = options.GetHeaders<Dictionary<string, string>>((d, k, v) => d.Add(k, v));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static class OtlpMetricExporterExtensions
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddOtlpExporter(this MeterProviderBuilder builder, Action<OtlpExporterOptions> configure = null)
{
Guard.Null(builder, nameof(builder));
Guard.ThrowIfNull(builder, nameof(builder));

if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static class OtlpTraceExporterHelperExtensions
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddOtlpExporter(this TracerProviderBuilder builder, Action<OtlpExporterOptions> configure = null)
{
Guard.Null(builder, nameof(builder));
Guard.ThrowIfNull(builder, nameof(builder));

if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal sealed class PrometheusExporterHttpServer : IDisposable
/// <param name="exporter">The <see cref="PrometheusExporter"/> instance.</param>
public PrometheusExporterHttpServer(PrometheusExporter exporter)
{
Guard.Null(exporter, nameof(exporter));
Guard.ThrowIfNull(exporter, nameof(exporter));

this.exporter = exporter;
if ((exporter.Options.HttpListenerPrefixes?.Count ?? 0) <= 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal sealed class PrometheusExporterMiddleware
/// <param name="next"><see cref="RequestDelegate"/>.</param>
public PrometheusExporterMiddleware(MeterProvider meterProvider, RequestDelegate next)
{
Guard.Null(meterProvider, nameof(meterProvider));
Guard.ThrowIfNull(meterProvider, nameof(meterProvider));

if (!meterProvider.TryFindExporter(out PrometheusExporter exporter))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static class PrometheusExporterMeterProviderBuilderExtensions
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddPrometheusExporter(this MeterProviderBuilder builder, Action<PrometheusExporterOptions> configure = null)
{
Guard.Null(builder, nameof(builder));
Guard.ThrowIfNull(builder, nameof(builder));

if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public int ScrapeResponseCacheDurationMilliseconds
get => this.scrapeResponseCacheDurationMilliseconds;
set
{
Guard.Range(value, nameof(value), min: 0);
Guard.ThrowIfOutOfRange(value, nameof(value), min: 0);

this.scrapeResponseCacheDurationMilliseconds = value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry.Exporter.ZPages/ZPagesExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ZPagesExporter : BaseExporter<Activity>
/// <param name="options">Options for the exporter.</param>
public ZPagesExporter(ZPagesExporterOptions options)
{
Guard.Null(options?.RetentionTime, $"{nameof(options)}?.{nameof(options.RetentionTime)}");
Guard.ThrowIfNull(options?.RetentionTime, $"{nameof(options)}?.{nameof(options.RetentionTime)}");

ZPagesActivityTracker.RetentionTime = options.RetentionTime;

Expand Down
Loading