diff --git a/src/OpenTelemetry.Api/Baggage.cs b/src/OpenTelemetry.Api/Baggage.cs index ce1b4d4c1ce..b9804921964 100644 --- a/src/OpenTelemetry.Api/Baggage.cs +++ b/src/OpenTelemetry.Api/Baggage.cs @@ -239,7 +239,7 @@ public IReadOnlyDictionary GetBaggage() /// Baggage item or if nothing was found. 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 diff --git a/src/OpenTelemetry.Api/Context/Propagation/CompositeTextMapPropagator.cs b/src/OpenTelemetry.Api/Context/Propagation/CompositeTextMapPropagator.cs index 7897ff8210c..5523db486ac 100644 --- a/src/OpenTelemetry.Api/Context/Propagation/CompositeTextMapPropagator.cs +++ b/src/OpenTelemetry.Api/Context/Propagation/CompositeTextMapPropagator.cs @@ -35,7 +35,7 @@ public class CompositeTextMapPropagator : TextMapPropagator /// List of wire context propagator. public CompositeTextMapPropagator(IEnumerable propagators) { - Guard.Null(propagators, nameof(propagators)); + Guard.ThrowIfNull(propagators, nameof(propagators)); this.propagators = new List(propagators); } diff --git a/src/OpenTelemetry.Api/Context/RuntimeContext.cs b/src/OpenTelemetry.Api/Context/RuntimeContext.cs index 1520f64bbdd..9f0b022f951 100644 --- a/src/OpenTelemetry.Api/Context/RuntimeContext.cs +++ b/src/OpenTelemetry.Api/Context/RuntimeContext.cs @@ -41,7 +41,7 @@ public static class RuntimeContext /// The slot registered. public static RuntimeContextSlot RegisterSlot(string slotName) { - Guard.NullOrEmpty(slotName, nameof(slotName)); + Guard.ThrowIfNullOrEmpty(slotName, nameof(slotName)); lock (Slots) { @@ -66,9 +66,9 @@ public static RuntimeContextSlot RegisterSlot(string slotName) /// The slot previously registered. public static RuntimeContextSlot GetSlot(string slotName) { - Guard.NullOrEmpty(slotName, nameof(slotName)); + Guard.ThrowIfNullOrEmpty(slotName, nameof(slotName)); var slot = GuardNotFound(slotName); - var contextSlot = Guard.Type>(slot, nameof(slot)); + var contextSlot = Guard.ThrowIfNotOfType>(slot, nameof(slot)); return contextSlot; } @@ -127,9 +127,9 @@ public static T GetValue(string slotName) /// The value to be set. 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(slot, nameof(slot)); + var runtimeContextSlotValueAccessor = Guard.ThrowIfNotOfType(slot, nameof(slot)); runtimeContextSlotValueAccessor.Value = value; } @@ -140,9 +140,9 @@ public static void SetValue(string slotName, object value) /// The value retrieved from the context slot. public static object GetValue(string slotName) { - Guard.NullOrEmpty(slotName, nameof(slotName)); + Guard.ThrowIfNullOrEmpty(slotName, nameof(slotName)); var slot = GuardNotFound(slotName); - var runtimeContextSlotValueAccessor = Guard.Type(slot, nameof(slot)); + var runtimeContextSlotValueAccessor = Guard.ThrowIfNotOfType(slot, nameof(slot)); return runtimeContextSlotValueAccessor.Value; } diff --git a/src/OpenTelemetry.Api/Internal/Guard.cs b/src/OpenTelemetry.Api/Internal/Guard.cs index ae6e816ae06..46d57c03aa5 100644 --- a/src/OpenTelemetry.Api/Internal/Guard.cs +++ b/src/OpenTelemetry.Api/Internal/Guard.cs @@ -35,7 +35,7 @@ internal static class Guard /// The parameter name to use in the thrown exception. [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) { @@ -50,7 +50,7 @@ public static void Null(object value, string paramName = DefaultParamName) /// The parameter name to use in the thrown exception. [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)) { @@ -65,7 +65,7 @@ public static void NullOrEmpty(string value, string paramName = DefaultParamName /// The parameter name to use in the thrown exception. [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)) { @@ -81,7 +81,7 @@ public static void NullOrWhitespace(string value, string paramName = DefaultPara /// The parameter name to use in the thrown exception. [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) { @@ -96,9 +96,9 @@ public static void Zero(int value, string message = "Must not be zero", string p /// The parameter name to use in the thrown exception. [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)}'"); } /// @@ -113,9 +113,9 @@ public static void InvalidTimeout(int value, string paramName = DefaultParamName /// An optional custom message to use in the thrown exception. [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(value, paramName, min, max, minName, maxName, message); + Range(value, paramName, min, max, minName, maxName, message); } /// @@ -130,9 +130,9 @@ public static void Range(int value, string paramName = DefaultParamName, int min /// An optional custom message to use in the thrown exception. [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(value, paramName, min, max, minName, maxName, message); + Range(value, paramName, min, max, minName, maxName, message); } /// @@ -144,7 +144,7 @@ public static void Range(double value, string paramName = DefaultParamName, doub /// The value casted to the specified type. [DebuggerHidden] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T Type(object value, string paramName = DefaultParamName) + public static T ThrowIfNotOfType(object value, string paramName = DefaultParamName) { if (value is not T result) { diff --git a/src/OpenTelemetry.Api/Trace/SpanAttributes.cs b/src/OpenTelemetry.Api/Trace/SpanAttributes.cs index 14d2a481fa1..f247d291cea 100644 --- a/src/OpenTelemetry.Api/Trace/SpanAttributes.cs +++ b/src/OpenTelemetry.Api/Trace/SpanAttributes.cs @@ -41,7 +41,7 @@ public SpanAttributes() public SpanAttributes(IEnumerable> attributes) : this() { - Guard.Null(attributes, nameof(attributes)); + Guard.ThrowIfNull(attributes, nameof(attributes)); foreach (KeyValuePair kvp in attributes) { @@ -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; } diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleExporterHelperExtensions.cs b/src/OpenTelemetry.Exporter.Console/ConsoleExporterHelperExtensions.cs index db7413bd261..4a0c8a6017d 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleExporterHelperExtensions.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleExporterHelperExtensions.cs @@ -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 configure = null) { - Guard.Null(builder, nameof(builder)); + Guard.ThrowIfNull(builder, nameof(builder)); if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder) { diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleExporterLoggingExtensions.cs b/src/OpenTelemetry.Exporter.Console/ConsoleExporterLoggingExtensions.cs index eee27b3820e..1c9a2d9bce5 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleExporterLoggingExtensions.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleExporterLoggingExtensions.cs @@ -30,7 +30,7 @@ public static class ConsoleExporterLoggingExtensions /// The instance of to chain the calls. public static OpenTelemetryLoggerOptions AddConsoleExporter(this OpenTelemetryLoggerOptions loggerOptions, Action configure = null) { - Guard.Null(loggerOptions, nameof(loggerOptions)); + Guard.ThrowIfNull(loggerOptions, nameof(loggerOptions)); var options = new ConsoleExporterOptions(); configure?.Invoke(options); diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleExporterMetricsExtensions.cs b/src/OpenTelemetry.Exporter.Console/ConsoleExporterMetricsExtensions.cs index 3a19817d054..b07ed46366d 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleExporterMetricsExtensions.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleExporterMetricsExtensions.cs @@ -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 configure = null) { - Guard.Null(builder, nameof(builder)); + Guard.ThrowIfNull(builder, nameof(builder)); var options = new ConsoleExporterOptions(); configure?.Invoke(options); diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterHelperExtensions.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterHelperExtensions.cs index 7fffc434464..3a49d915075 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterHelperExtensions.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterHelperExtensions.cs @@ -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 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) { diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterLoggingExtensions.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterLoggingExtensions.cs index e7ab23e8b1f..4a64390f303 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterLoggingExtensions.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterLoggingExtensions.cs @@ -24,8 +24,8 @@ public static class InMemoryExporterLoggingExtensions { public static OpenTelemetryLoggerOptions AddInMemoryExporter(this OpenTelemetryLoggerOptions loggerOptions, ICollection 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(exportedItems))); } diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs index 64d56303815..97d3a3eabf8 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporterMetricsExtensions.cs @@ -30,8 +30,8 @@ public static class InMemoryExporterMetricsExtensions /// The instance of to chain the calls. public static MeterProviderBuilder AddInMemoryExporter(this MeterProviderBuilder builder, ICollection 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(exportedItems))); } diff --git a/src/OpenTelemetry.Exporter.Jaeger/JaegerExporter.cs b/src/OpenTelemetry.Exporter.Jaeger/JaegerExporter.cs index 4305319b8cc..3de88ba15b5 100644 --- a/src/OpenTelemetry.Exporter.Jaeger/JaegerExporter.cs +++ b/src/OpenTelemetry.Exporter.Jaeger/JaegerExporter.cs @@ -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 @@ -122,7 +122,7 @@ public override ExportResult Export(in Batch activityBatch) internal void SetResourceAndInitializeBatch(Resource resource) { - Guard.Null(resource, nameof(resource)); + Guard.ThrowIfNull(resource, nameof(resource)); var process = this.Process; diff --git a/src/OpenTelemetry.Exporter.Jaeger/JaegerExporterHelperExtensions.cs b/src/OpenTelemetry.Exporter.Jaeger/JaegerExporterHelperExtensions.cs index f7b762195a4..7d3e641d5be 100644 --- a/src/OpenTelemetry.Exporter.Jaeger/JaegerExporterHelperExtensions.cs +++ b/src/OpenTelemetry.Exporter.Jaeger/JaegerExporterHelperExtensions.cs @@ -35,7 +35,7 @@ public static class JaegerExporterHelperExtensions /// The instance of to chain the calls. public static TracerProviderBuilder AddJaegerExporter(this TracerProviderBuilder builder, Action configure = null) { - Guard.Null(builder, nameof(builder)); + Guard.ThrowIfNull(builder, nameof(builder)); if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder) { diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol.Logs/OtlpLogExporterHelperExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol.Logs/OtlpLogExporterHelperExtensions.cs index 1b66d4f6807..b99be335bac 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol.Logs/OtlpLogExporterHelperExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol.Logs/OtlpLogExporterHelperExtensions.cs @@ -33,7 +33,7 @@ public static class OtlpLogExporterHelperExtensions /// The instance of to chain the calls. public static OpenTelemetryLoggerOptions AddOtlpExporter(this OpenTelemetryLoggerOptions loggerOptions, Action configure = null) { - Guard.Null(loggerOptions); + Guard.ThrowIfNull(loggerOptions); return AddOtlpExporter(loggerOptions, new OtlpExporterOptions(), configure); } diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/BaseOtlpGrpcExportClient.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/BaseOtlpGrpcExportClient.cs index cc5a5be13da..69c0b03e749 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/BaseOtlpGrpcExportClient.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/BaseOtlpGrpcExportClient.cs @@ -30,8 +30,8 @@ internal abstract class BaseOtlpGrpcExportClient : IExportClient : IExportClient>((d, k, v) => d.Add(k, v)); diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpMetricExporterExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpMetricExporterExtensions.cs index 80a551aeacf..fc0956d0ad5 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpMetricExporterExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpMetricExporterExtensions.cs @@ -33,7 +33,7 @@ public static class OtlpMetricExporterExtensions /// The instance of to chain the calls. public static MeterProviderBuilder AddOtlpExporter(this MeterProviderBuilder builder, Action configure = null) { - Guard.Null(builder, nameof(builder)); + Guard.ThrowIfNull(builder, nameof(builder)); if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder) { diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpTraceExporterHelperExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpTraceExporterHelperExtensions.cs index 33fd4676d68..d9e66316919 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpTraceExporterHelperExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpTraceExporterHelperExtensions.cs @@ -33,7 +33,7 @@ public static class OtlpTraceExporterHelperExtensions /// The instance of to chain the calls. public static TracerProviderBuilder AddOtlpExporter(this TracerProviderBuilder builder, Action configure = null) { - Guard.Null(builder, nameof(builder)); + Guard.ThrowIfNull(builder, nameof(builder)); if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder) { diff --git a/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusExporterHttpServer.cs b/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusExporterHttpServer.cs index 473044571ea..eada5dc0164 100644 --- a/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusExporterHttpServer.cs +++ b/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusExporterHttpServer.cs @@ -40,7 +40,7 @@ internal sealed class PrometheusExporterHttpServer : IDisposable /// The instance. public PrometheusExporterHttpServer(PrometheusExporter exporter) { - Guard.Null(exporter, nameof(exporter)); + Guard.ThrowIfNull(exporter, nameof(exporter)); this.exporter = exporter; if ((exporter.Options.HttpListenerPrefixes?.Count ?? 0) <= 0) diff --git a/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusExporterMiddleware.cs b/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusExporterMiddleware.cs index 38955036995..b76342ca501 100644 --- a/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusExporterMiddleware.cs +++ b/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusExporterMiddleware.cs @@ -38,7 +38,7 @@ internal sealed class PrometheusExporterMiddleware /// . public PrometheusExporterMiddleware(MeterProvider meterProvider, RequestDelegate next) { - Guard.Null(meterProvider, nameof(meterProvider)); + Guard.ThrowIfNull(meterProvider, nameof(meterProvider)); if (!meterProvider.TryFindExporter(out PrometheusExporter exporter)) { diff --git a/src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterMeterProviderBuilderExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterMeterProviderBuilderExtensions.cs index c2e6e26515b..a7d5da46285 100644 --- a/src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterMeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterMeterProviderBuilderExtensions.cs @@ -30,7 +30,7 @@ public static class PrometheusExporterMeterProviderBuilderExtensions /// The instance of to chain the calls. public static MeterProviderBuilder AddPrometheusExporter(this MeterProviderBuilder builder, Action configure = null) { - Guard.Null(builder, nameof(builder)); + Guard.ThrowIfNull(builder, nameof(builder)); if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder) { diff --git a/src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterOptions.cs b/src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterOptions.cs index e42fcb89bc3..eb4ff27685a 100644 --- a/src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterOptions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterOptions.cs @@ -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; } diff --git a/src/OpenTelemetry.Exporter.ZPages/ZPagesExporter.cs b/src/OpenTelemetry.Exporter.ZPages/ZPagesExporter.cs index a7b2eba27b9..c02e5905731 100644 --- a/src/OpenTelemetry.Exporter.ZPages/ZPagesExporter.cs +++ b/src/OpenTelemetry.Exporter.ZPages/ZPagesExporter.cs @@ -37,7 +37,7 @@ public class ZPagesExporter : BaseExporter /// Options for the exporter. 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; diff --git a/src/OpenTelemetry.Exporter.ZPages/ZPagesExporterHelperExtensions.cs b/src/OpenTelemetry.Exporter.ZPages/ZPagesExporterHelperExtensions.cs index a27e956eaef..7dd316a5c4c 100644 --- a/src/OpenTelemetry.Exporter.ZPages/ZPagesExporterHelperExtensions.cs +++ b/src/OpenTelemetry.Exporter.ZPages/ZPagesExporterHelperExtensions.cs @@ -37,7 +37,7 @@ public static TracerProviderBuilder AddZPagesExporter( this TracerProviderBuilder builder, Action configure = null) { - Guard.Null(builder, nameof(builder)); + Guard.ThrowIfNull(builder, nameof(builder)); var exporterOptions = new ZPagesExporterOptions(); configure?.Invoke(exporterOptions); diff --git a/src/OpenTelemetry.Exporter.ZPages/ZPagesExporterStatsHttpServer.cs b/src/OpenTelemetry.Exporter.ZPages/ZPagesExporterStatsHttpServer.cs index 6b8ab4c1ed5..906d81674c2 100644 --- a/src/OpenTelemetry.Exporter.ZPages/ZPagesExporterStatsHttpServer.cs +++ b/src/OpenTelemetry.Exporter.ZPages/ZPagesExporterStatsHttpServer.cs @@ -42,7 +42,7 @@ public class ZPagesExporterStatsHttpServer : IDisposable /// The instance. public ZPagesExporterStatsHttpServer(ZPagesExporter exporter) { - Guard.Null(exporter?.Options?.Url, $"{nameof(exporter)}?.{nameof(exporter.Options)}?.{nameof(exporter.Options.Url)}"); + Guard.ThrowIfNull(exporter?.Options?.Url, $"{nameof(exporter)}?.{nameof(exporter.Options)}?.{nameof(exporter.Options.Url)}"); this.httpListener.Prefixes.Add(exporter.Options.Url); } diff --git a/src/OpenTelemetry.Exporter.Zipkin/Implementation/ZipkinSpan.cs b/src/OpenTelemetry.Exporter.Zipkin/Implementation/ZipkinSpan.cs index 42b9914249a..a555a90587e 100644 --- a/src/OpenTelemetry.Exporter.Zipkin/Implementation/ZipkinSpan.cs +++ b/src/OpenTelemetry.Exporter.Zipkin/Implementation/ZipkinSpan.cs @@ -41,8 +41,8 @@ public ZipkinSpan( bool? debug, bool? shared) { - Guard.NullOrWhitespace(traceId, nameof(traceId)); - Guard.NullOrWhitespace(id, nameof(id)); + Guard.ThrowIfNullOrWhitespace(traceId, nameof(traceId)); + Guard.ThrowIfNullOrWhitespace(id, nameof(id)); this.TraceId = traceId; this.ParentId = parentId; diff --git a/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs b/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs index afa00ea5189..899147ac9c8 100644 --- a/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs +++ b/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs @@ -48,7 +48,7 @@ public class ZipkinExporter : BaseExporter /// Http client to use to upload telemetry. public ZipkinExporter(ZipkinExporterOptions options, HttpClient client = null) { - Guard.Null(options, nameof(options)); + Guard.ThrowIfNull(options, nameof(options)); this.options = options; this.maxPayloadSizeInBytes = (!options.MaxPayloadSizeInBytes.HasValue || options.MaxPayloadSizeInBytes <= 0) ? ZipkinExporterOptions.DefaultMaxPayloadSizeInBytes : options.MaxPayloadSizeInBytes.Value; diff --git a/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporterHelperExtensions.cs b/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporterHelperExtensions.cs index 05773ebe1fa..ce57441e182 100644 --- a/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporterHelperExtensions.cs +++ b/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporterHelperExtensions.cs @@ -36,7 +36,7 @@ public static class ZipkinExporterHelperExtensions [System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The objects should not be disposed.")] public static TracerProviderBuilder AddZipkinExporter(this TracerProviderBuilder builder, Action configure = null) { - Guard.Null(builder, nameof(builder)); + Guard.ThrowIfNull(builder, nameof(builder)); if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder) { diff --git a/src/OpenTelemetry.Extensions.Hosting/Implementation/MeterProviderBuilderHosting.cs b/src/OpenTelemetry.Extensions.Hosting/Implementation/MeterProviderBuilderHosting.cs index 9519bbd40fa..4ee28ab5aac 100644 --- a/src/OpenTelemetry.Extensions.Hosting/Implementation/MeterProviderBuilderHosting.cs +++ b/src/OpenTelemetry.Extensions.Hosting/Implementation/MeterProviderBuilderHosting.cs @@ -30,7 +30,7 @@ internal sealed class MeterProviderBuilderHosting : MeterProviderBuilderBase, ID public MeterProviderBuilderHosting(IServiceCollection services) { - Guard.Null(services, nameof(services)); + Guard.ThrowIfNull(services, nameof(services)); this.Services = services; } @@ -39,7 +39,7 @@ public MeterProviderBuilderHosting(IServiceCollection services) public MeterProviderBuilder Configure(Action configure) { - Guard.Null(configure, nameof(configure)); + Guard.ThrowIfNull(configure, nameof(configure)); this.configurationActions.Add(configure); return this; @@ -47,7 +47,7 @@ public MeterProviderBuilder Configure(Action configure) { - Guard.Null(configure, nameof(configure)); + Guard.ThrowIfNull(configure, nameof(configure)); this.configurationActions.Add(configure); return this; @@ -47,7 +47,7 @@ public TracerProviderBuilder Configure(ActionThe so that additional calls can be chained. public static IServiceCollection AddOpenTelemetryTracing(this IServiceCollection services, Action configure) { - Guard.Null(configure, nameof(configure)); + Guard.ThrowIfNull(configure, nameof(configure)); var builder = new TracerProviderBuilderHosting(services); configure(builder); @@ -73,7 +73,7 @@ public static IServiceCollection AddOpenTelemetryMetrics(this IServiceCollection /// The so that additional calls can be chained. public static IServiceCollection AddOpenTelemetryMetrics(this IServiceCollection services, Action configure) { - Guard.Null(configure, nameof(configure)); + Guard.ThrowIfNull(configure, nameof(configure)); var builder = new MeterProviderBuilderHosting(services); configure(builder); @@ -88,8 +88,8 @@ public static IServiceCollection AddOpenTelemetryMetrics(this IServiceCollection /// The so that additional calls can be chained. private static IServiceCollection AddOpenTelemetryTracing(this IServiceCollection services, Func createTracerProvider) { - Guard.Null(services, nameof(services)); - Guard.Null(createTracerProvider, nameof(createTracerProvider)); + Guard.ThrowIfNull(services, nameof(services)); + Guard.ThrowIfNull(createTracerProvider, nameof(createTracerProvider)); try { diff --git a/src/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule/TelemetryHttpModuleOptions.cs b/src/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule/TelemetryHttpModuleOptions.cs index 2b2b14945cb..258ac86de74 100644 --- a/src/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule/TelemetryHttpModuleOptions.cs +++ b/src/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule/TelemetryHttpModuleOptions.cs @@ -42,7 +42,7 @@ public TextMapPropagator TextMapPropagator get => this.textMapPropagator; set { - Guard.Null(value, nameof(value)); + Guard.ThrowIfNull(value, nameof(value)); this.textMapPropagator = value; } diff --git a/src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInListener.cs index 7d2f4335563..8bb28b9b45f 100644 --- a/src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInListener.cs @@ -32,7 +32,7 @@ internal sealed class HttpInListener : IDisposable public HttpInListener(AspNetInstrumentationOptions options) { - Guard.Null(options, nameof(options)); + Guard.ThrowIfNull(options, nameof(options)); this.options = options; diff --git a/src/OpenTelemetry.Instrumentation.AspNet/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.AspNet/TracerProviderBuilderExtensions.cs index 28f43041e80..39f41ef969e 100644 --- a/src/OpenTelemetry.Instrumentation.AspNet/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.AspNet/TracerProviderBuilderExtensions.cs @@ -35,7 +35,7 @@ public static TracerProviderBuilder AddAspNetInstrumentation( this TracerProviderBuilder builder, Action configureAspNetInstrumentationOptions = null) { - Guard.Null(builder, nameof(builder)); + Guard.ThrowIfNull(builder, nameof(builder)); var aspnetOptions = new AspNetInstrumentationOptions(); configureAspNetInstrumentationOptions?.Invoke(aspnetOptions); diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs index 1291a63fb0d..eb25f895cce 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs @@ -54,7 +54,7 @@ internal class HttpInListener : ListenerHandler public HttpInListener(AspNetCoreInstrumentationOptions options) : base(DiagnosticSourceName) { - Guard.Null(options, nameof(options)); + Guard.ThrowIfNull(options, nameof(options)); this.options = options; } diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/MeterProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/MeterProviderBuilderExtensions.cs index 7b813462c0f..50ed827437d 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/MeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/MeterProviderBuilderExtensions.cs @@ -32,7 +32,7 @@ public static class MeterProviderBuilderExtensions public static MeterProviderBuilder AddAspNetCoreInstrumentation( this MeterProviderBuilder builder) { - Guard.Null(builder, nameof(builder)); + Guard.ThrowIfNull(builder, nameof(builder)); // TODO: Implement an IDeferredMeterProviderBuilder diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/TracerProviderBuilderExtensions.cs index 08df61076ab..1df5ec14ac2 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/TracerProviderBuilderExtensions.cs @@ -36,7 +36,7 @@ public static TracerProviderBuilder AddAspNetCoreInstrumentation( this TracerProviderBuilder builder, Action configureAspNetCoreInstrumentationOptions = null) { - Guard.Null(builder, nameof(builder)); + Guard.ThrowIfNull(builder, nameof(builder)); if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder) { diff --git a/src/OpenTelemetry.Instrumentation.GrpcNetClient/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.GrpcNetClient/TracerProviderBuilderExtensions.cs index 0e09f180e46..9c4580a39ad 100644 --- a/src/OpenTelemetry.Instrumentation.GrpcNetClient/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.GrpcNetClient/TracerProviderBuilderExtensions.cs @@ -37,7 +37,7 @@ public static TracerProviderBuilder AddGrpcClientInstrumentation( this TracerProviderBuilder builder, Action configure = null) { - Guard.Null(builder, nameof(builder)); + Guard.ThrowIfNull(builder, nameof(builder)); var grpcOptions = new GrpcClientInstrumentationOptions(); configure?.Invoke(grpcOptions); diff --git a/src/OpenTelemetry.Instrumentation.Http/MeterProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.Http/MeterProviderBuilderExtensions.cs index 83c8e60ea24..e92ad676897 100644 --- a/src/OpenTelemetry.Instrumentation.Http/MeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.Http/MeterProviderBuilderExtensions.cs @@ -32,7 +32,7 @@ public static class MeterProviderBuilderExtensions public static MeterProviderBuilder AddHttpClientInstrumentation( this MeterProviderBuilder builder) { - Guard.Null(builder, nameof(builder)); + Guard.ThrowIfNull(builder, nameof(builder)); // TODO: Implement an IDeferredMeterProviderBuilder diff --git a/src/OpenTelemetry.Instrumentation.Http/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.Http/TracerProviderBuilderExtensions.cs index 4716f0a1281..feef1d353de 100644 --- a/src/OpenTelemetry.Instrumentation.Http/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.Http/TracerProviderBuilderExtensions.cs @@ -61,7 +61,7 @@ public static TracerProviderBuilder AddHttpClientInstrumentation( this TracerProviderBuilder builder, Action configureHttpClientInstrumentationOptions = null) { - Guard.Null(builder, nameof(builder)); + Guard.ThrowIfNull(builder, nameof(builder)); var httpClientOptions = new HttpClientInstrumentationOptions(); diff --git a/src/OpenTelemetry.Instrumentation.SqlClient/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.SqlClient/TracerProviderBuilderExtensions.cs index 262bc7d18d1..de11906f615 100644 --- a/src/OpenTelemetry.Instrumentation.SqlClient/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.SqlClient/TracerProviderBuilderExtensions.cs @@ -36,7 +36,7 @@ public static TracerProviderBuilder AddSqlClientInstrumentation( this TracerProviderBuilder builder, Action configureSqlClientInstrumentationOptions = null) { - Guard.Null(builder, nameof(builder)); + Guard.ThrowIfNull(builder, nameof(builder)); var sqlOptions = new SqlClientInstrumentationOptions(); configureSqlClientInstrumentationOptions?.Invoke(sqlOptions); diff --git a/src/OpenTelemetry.Instrumentation.StackExchangeRedis/StackExchangeRedisCallsInstrumentation.cs b/src/OpenTelemetry.Instrumentation.StackExchangeRedis/StackExchangeRedisCallsInstrumentation.cs index 72cb7755057..81c2e790d6d 100644 --- a/src/OpenTelemetry.Instrumentation.StackExchangeRedis/StackExchangeRedisCallsInstrumentation.cs +++ b/src/OpenTelemetry.Instrumentation.StackExchangeRedis/StackExchangeRedisCallsInstrumentation.cs @@ -59,7 +59,7 @@ internal class StackExchangeRedisCallsInstrumentation : IDisposable /// Configuration options for redis instrumentation. public StackExchangeRedisCallsInstrumentation(IConnectionMultiplexer connection, StackExchangeRedisCallsInstrumentationOptions options) { - Guard.Null(connection, nameof(connection)); + Guard.ThrowIfNull(connection, nameof(connection)); this.options = options ?? new StackExchangeRedisCallsInstrumentationOptions(); diff --git a/src/OpenTelemetry.Instrumentation.StackExchangeRedis/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.StackExchangeRedis/TracerProviderBuilderExtensions.cs index 68d3e30861d..7814ed11671 100644 --- a/src/OpenTelemetry.Instrumentation.StackExchangeRedis/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.StackExchangeRedis/TracerProviderBuilderExtensions.cs @@ -43,7 +43,7 @@ public static TracerProviderBuilder AddRedisInstrumentation( IConnectionMultiplexer connection = null, Action configure = null) { - Guard.Null(builder, nameof(builder)); + Guard.ThrowIfNull(builder, nameof(builder)); if (builder is not IDeferredTracerProviderBuilder deferredTracerProviderBuilder) { diff --git a/src/OpenTelemetry.Shims.OpenTracing/ScopeManagerShim.cs b/src/OpenTelemetry.Shims.OpenTracing/ScopeManagerShim.cs index 6f435d93ad8..e184fc69c66 100644 --- a/src/OpenTelemetry.Shims.OpenTracing/ScopeManagerShim.cs +++ b/src/OpenTelemetry.Shims.OpenTracing/ScopeManagerShim.cs @@ -37,7 +37,7 @@ internal sealed class ScopeManagerShim : IScopeManager public ScopeManagerShim(Tracer tracer) { - Guard.Null(tracer, nameof(tracer)); + Guard.ThrowIfNull(tracer, nameof(tracer)); this.tracer = tracer; } @@ -69,7 +69,7 @@ public IScope Active /// public IScope Activate(ISpan span, bool finishSpanOnDispose) { - var shim = Guard.Type(span, nameof(span)); + var shim = Guard.ThrowIfNotOfType(span, nameof(span)); var scope = Tracer.WithSpan(shim.Span); diff --git a/src/OpenTelemetry.Shims.OpenTracing/SpanBuilderShim.cs b/src/OpenTelemetry.Shims.OpenTracing/SpanBuilderShim.cs index 177d0969b71..6a27eefe635 100644 --- a/src/OpenTelemetry.Shims.OpenTracing/SpanBuilderShim.cs +++ b/src/OpenTelemetry.Shims.OpenTracing/SpanBuilderShim.cs @@ -81,8 +81,8 @@ internal sealed class SpanBuilderShim : ISpanBuilder public SpanBuilderShim(Tracer tracer, string spanName, IList rootOperationNamesForActivityBasedAutoInstrumentations = null) { - Guard.Null(tracer, nameof(tracer)); - Guard.Null(spanName, nameof(spanName)); + Guard.ThrowIfNull(tracer, nameof(tracer)); + Guard.ThrowIfNull(spanName, nameof(spanName)); this.tracer = tracer; this.spanName = spanName; @@ -130,7 +130,7 @@ public ISpanBuilder AddReference(string referenceType, ISpanContext referencedCo return this; } - Guard.Null(referenceType, nameof(referenceType)); + Guard.ThrowIfNull(referenceType, nameof(referenceType)); // TODO There is no relation between OpenTracing.References (referenceType) and OpenTelemetry Link var actualContext = GetOpenTelemetrySpanContext(referencedContext); @@ -279,7 +279,7 @@ public ISpanBuilder WithTag(string key, double value) /// public ISpanBuilder WithTag(global::OpenTracing.Tag.BooleanTag tag, bool value) { - Guard.Null(tag?.Key, $"{nameof(tag)}?.{nameof(tag.Key)}"); + Guard.ThrowIfNull(tag?.Key, $"{nameof(tag)}?.{nameof(tag.Key)}"); return this.WithTag(tag.Key, value); } @@ -287,7 +287,7 @@ public ISpanBuilder WithTag(global::OpenTracing.Tag.BooleanTag tag, bool value) /// public ISpanBuilder WithTag(global::OpenTracing.Tag.IntOrStringTag tag, string value) { - Guard.Null(tag?.Key, $"{nameof(tag)}?.{nameof(tag.Key)}"); + Guard.ThrowIfNull(tag?.Key, $"{nameof(tag)}?.{nameof(tag.Key)}"); if (int.TryParse(value, out var result)) { @@ -300,7 +300,7 @@ public ISpanBuilder WithTag(global::OpenTracing.Tag.IntOrStringTag tag, string v /// public ISpanBuilder WithTag(global::OpenTracing.Tag.IntTag tag, int value) { - Guard.Null(tag?.Key, $"{nameof(tag)}?.{nameof(tag.Key)}"); + Guard.ThrowIfNull(tag?.Key, $"{nameof(tag)}?.{nameof(tag.Key)}"); return this.WithTag(tag.Key, value); } @@ -308,7 +308,7 @@ public ISpanBuilder WithTag(global::OpenTracing.Tag.IntTag tag, int value) /// public ISpanBuilder WithTag(global::OpenTracing.Tag.StringTag tag, string value) { - Guard.Null(tag?.Key, $"{nameof(tag)}?.{nameof(tag.Key)}"); + Guard.ThrowIfNull(tag?.Key, $"{nameof(tag)}?.{nameof(tag.Key)}"); return this.WithTag(tag.Key, value); } @@ -321,7 +321,7 @@ public ISpanBuilder WithTag(global::OpenTracing.Tag.StringTag tag, string value) /// span is not a valid SpanShim object. private static TelemetrySpan GetOpenTelemetrySpan(ISpan span) { - var shim = Guard.Type(span, nameof(span)); + var shim = Guard.ThrowIfNotOfType(span, nameof(span)); return shim.Span; } @@ -334,7 +334,7 @@ private static TelemetrySpan GetOpenTelemetrySpan(ISpan span) /// context is not a valid SpanContextShim object. private static SpanContext GetOpenTelemetrySpanContext(ISpanContext spanContext) { - var shim = Guard.Type(spanContext, nameof(spanContext)); + var shim = Guard.ThrowIfNotOfType(spanContext, nameof(spanContext)); return shim.SpanContext; } diff --git a/src/OpenTelemetry.Shims.OpenTracing/SpanShim.cs b/src/OpenTelemetry.Shims.OpenTracing/SpanShim.cs index e85c115b760..56aa2738d01 100644 --- a/src/OpenTelemetry.Shims.OpenTracing/SpanShim.cs +++ b/src/OpenTelemetry.Shims.OpenTracing/SpanShim.cs @@ -46,7 +46,7 @@ internal sealed class SpanShim : ISpan public SpanShim(TelemetrySpan span) { - Guard.Null(span, nameof(span)); + Guard.ThrowIfNull(span, nameof(span)); if (!span.Context.IsValid) { @@ -80,7 +80,7 @@ public string GetBaggageItem(string key) /// public ISpan Log(DateTimeOffset timestamp, IEnumerable> fields) { - Guard.Null(fields, nameof(fields)); + Guard.ThrowIfNull(fields, nameof(fields)); var payload = ConvertToEventPayload(fields); var eventName = payload.Item1; @@ -141,7 +141,7 @@ public ISpan Log(IEnumerable> fields) /// public ISpan Log(string @event) { - Guard.Null(@event, nameof(@event)); + Guard.ThrowIfNull(@event, nameof(@event)); this.Span.AddEvent(@event); return this; @@ -150,7 +150,7 @@ public ISpan Log(string @event) /// public ISpan Log(DateTimeOffset timestamp, string @event) { - Guard.Null(@event, nameof(@event)); + Guard.ThrowIfNull(@event, nameof(@event)); this.Span.AddEvent(@event, timestamp); return this; @@ -166,7 +166,7 @@ public ISpan SetBaggageItem(string key, string value) /// public ISpan SetOperationName(string operationName) { - Guard.Null(operationName, nameof(operationName)); + Guard.ThrowIfNull(operationName, nameof(operationName)); this.Span.UpdateName(operationName); return this; @@ -175,7 +175,7 @@ public ISpan SetOperationName(string operationName) /// public ISpan SetTag(string key, string value) { - Guard.Null(key, nameof(key)); + Guard.ThrowIfNull(key, nameof(key)); this.Span.SetAttribute(key, value); return this; @@ -184,7 +184,7 @@ public ISpan SetTag(string key, string value) /// public ISpan SetTag(string key, bool value) { - Guard.Null(key, nameof(key)); + Guard.ThrowIfNull(key, nameof(key)); // Special case the OpenTracing Error Tag // see https://opentracing.io/specification/conventions/ @@ -203,7 +203,7 @@ public ISpan SetTag(string key, bool value) /// public ISpan SetTag(string key, int value) { - Guard.Null(key, nameof(key)); + Guard.ThrowIfNull(key, nameof(key)); this.Span.SetAttribute(key, value); return this; @@ -212,7 +212,7 @@ public ISpan SetTag(string key, int value) /// public ISpan SetTag(string key, double value) { - Guard.Null(key, nameof(key)); + Guard.ThrowIfNull(key, nameof(key)); this.Span.SetAttribute(key, value); return this; diff --git a/src/OpenTelemetry.Shims.OpenTracing/TracerShim.cs b/src/OpenTelemetry.Shims.OpenTracing/TracerShim.cs index 45a1ce7db9f..c702c1252d5 100644 --- a/src/OpenTelemetry.Shims.OpenTracing/TracerShim.cs +++ b/src/OpenTelemetry.Shims.OpenTracing/TracerShim.cs @@ -28,8 +28,8 @@ public class TracerShim : global::OpenTracing.ITracer public TracerShim(Trace.Tracer tracer, TextMapPropagator textFormat) { - Guard.Null(tracer, nameof(tracer)); - Guard.Null(textFormat, nameof(textFormat)); + Guard.ThrowIfNull(tracer, nameof(tracer)); + Guard.ThrowIfNull(textFormat, nameof(textFormat)); this.tracer = tracer; this.propagator = textFormat; @@ -51,8 +51,8 @@ public TracerShim(Trace.Tracer tracer, TextMapPropagator textFormat) /// public global::OpenTracing.ISpanContext Extract(IFormat format, TCarrier carrier) { - Guard.Null(format, nameof(format)); - Guard.Null(carrier, nameof(carrier)); + Guard.ThrowIfNull(format, nameof(format)); + Guard.ThrowIfNull(carrier, nameof(carrier)); PropagationContext propagationContext = default; @@ -92,10 +92,10 @@ public void Inject( IFormat format, TCarrier carrier) { - Guard.Null(spanContext, nameof(spanContext)); - var shim = Guard.Type(spanContext, nameof(spanContext)); - Guard.Null(format, nameof(format)); - Guard.Null(carrier, nameof(carrier)); + Guard.ThrowIfNull(spanContext, nameof(spanContext)); + var shim = Guard.ThrowIfNotOfType(spanContext, nameof(spanContext)); + Guard.ThrowIfNull(format, nameof(format)); + Guard.ThrowIfNull(carrier, nameof(carrier)); if ((format == BuiltinFormats.TextMap || format == BuiltinFormats.HttpHeaders) && carrier is ITextMap textMapCarrier) { diff --git a/src/OpenTelemetry/BaseExportProcessor.cs b/src/OpenTelemetry/BaseExportProcessor.cs index df07767da40..de8678524f0 100644 --- a/src/OpenTelemetry/BaseExportProcessor.cs +++ b/src/OpenTelemetry/BaseExportProcessor.cs @@ -35,7 +35,7 @@ public abstract class BaseExportProcessor : BaseProcessor /// Exporter instance. protected BaseExportProcessor(BaseExporter exporter) { - Guard.Null(exporter, nameof(exporter)); + Guard.ThrowIfNull(exporter, nameof(exporter)); this.exporter = exporter; } diff --git a/src/OpenTelemetry/BaseExporter.cs b/src/OpenTelemetry/BaseExporter.cs index e520699d2a8..cb83c28ac93 100644 --- a/src/OpenTelemetry/BaseExporter.cs +++ b/src/OpenTelemetry/BaseExporter.cs @@ -76,7 +76,7 @@ public abstract class BaseExporter : IDisposable /// public bool ForceFlush(int timeoutMilliseconds = Timeout.Infinite) { - Guard.InvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); + Guard.ThrowIfInvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); try { @@ -109,7 +109,7 @@ public bool ForceFlush(int timeoutMilliseconds = Timeout.Infinite) /// public bool Shutdown(int timeoutMilliseconds = Timeout.Infinite) { - Guard.InvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); + Guard.ThrowIfInvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); if (Interlocked.Increment(ref this.shutdownCount) > 1) { diff --git a/src/OpenTelemetry/BaseProcessor.cs b/src/OpenTelemetry/BaseProcessor.cs index 2da70a50ef2..bf7ba828acf 100644 --- a/src/OpenTelemetry/BaseProcessor.cs +++ b/src/OpenTelemetry/BaseProcessor.cs @@ -82,7 +82,7 @@ public virtual void OnEnd(T data) /// public bool ForceFlush(int timeoutMilliseconds = Timeout.Infinite) { - Guard.InvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); + Guard.ThrowIfInvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); try { @@ -115,7 +115,7 @@ public bool ForceFlush(int timeoutMilliseconds = Timeout.Infinite) /// public bool Shutdown(int timeoutMilliseconds = Timeout.Infinite) { - Guard.InvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); + Guard.ThrowIfInvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); if (Interlocked.CompareExchange(ref this.shutdownCount, 1, 0) != 0) { diff --git a/src/OpenTelemetry/Batch.cs b/src/OpenTelemetry/Batch.cs index 48e75e39f57..a77e5c973c5 100644 --- a/src/OpenTelemetry/Batch.cs +++ b/src/OpenTelemetry/Batch.cs @@ -41,8 +41,8 @@ namespace OpenTelemetry /// The number of items in the batch. public Batch(T[] items, int count) { - Guard.Null(items, nameof(items)); - Guard.Range(count, nameof(count), 0, items.Length); + Guard.ThrowIfNull(items, nameof(items)); + Guard.ThrowIfOutOfRange(count, nameof(count), 0, items.Length); this.item = null; this.circularBuffer = null; diff --git a/src/OpenTelemetry/BatchExportProcessor.cs b/src/OpenTelemetry/BatchExportProcessor.cs index af43ad8da4f..0e6a532e2f2 100644 --- a/src/OpenTelemetry/BatchExportProcessor.cs +++ b/src/OpenTelemetry/BatchExportProcessor.cs @@ -61,10 +61,10 @@ protected BatchExportProcessor( int maxExportBatchSize = DefaultMaxExportBatchSize) : base(exporter) { - Guard.Range(maxQueueSize, nameof(maxQueueSize), min: 1); - Guard.Range(maxExportBatchSize, nameof(maxExportBatchSize), min: 1, max: maxQueueSize, maxName: nameof(maxQueueSize)); - Guard.Range(scheduledDelayMilliseconds, nameof(scheduledDelayMilliseconds), min: 1); - Guard.Range(exporterTimeoutMilliseconds, nameof(exporterTimeoutMilliseconds), min: 0); + Guard.ThrowIfOutOfRange(maxQueueSize, nameof(maxQueueSize), min: 1); + Guard.ThrowIfOutOfRange(maxExportBatchSize, nameof(maxExportBatchSize), min: 1, max: maxQueueSize, maxName: nameof(maxQueueSize)); + Guard.ThrowIfOutOfRange(scheduledDelayMilliseconds, nameof(scheduledDelayMilliseconds), min: 1); + Guard.ThrowIfOutOfRange(exporterTimeoutMilliseconds, nameof(exporterTimeoutMilliseconds), min: 0); this.circularBuffer = new CircularBuffer(maxQueueSize); this.scheduledDelayMilliseconds = scheduledDelayMilliseconds; diff --git a/src/OpenTelemetry/CompositeProcessor.cs b/src/OpenTelemetry/CompositeProcessor.cs index 43eb5df0e91..19260b1dd6c 100644 --- a/src/OpenTelemetry/CompositeProcessor.cs +++ b/src/OpenTelemetry/CompositeProcessor.cs @@ -30,7 +30,7 @@ public class CompositeProcessor : BaseProcessor public CompositeProcessor(IEnumerable> processors) { - Guard.Null(processors, nameof(processors)); + Guard.ThrowIfNull(processors, nameof(processors)); using var iter = processors.GetEnumerator(); if (!iter.MoveNext()) @@ -49,7 +49,7 @@ public CompositeProcessor(IEnumerable> processors) public CompositeProcessor AddProcessor(BaseProcessor processor) { - Guard.Null(processor, nameof(processor)); + Guard.ThrowIfNull(processor, nameof(processor)); var node = new DoublyLinkedListNode(processor) { diff --git a/src/OpenTelemetry/DiagnosticSourceInstrumentation/DiagnosticSourceListener.cs b/src/OpenTelemetry/DiagnosticSourceInstrumentation/DiagnosticSourceListener.cs index e5791cdb4ea..ad5852878d8 100644 --- a/src/OpenTelemetry/DiagnosticSourceInstrumentation/DiagnosticSourceListener.cs +++ b/src/OpenTelemetry/DiagnosticSourceInstrumentation/DiagnosticSourceListener.cs @@ -27,7 +27,7 @@ internal class DiagnosticSourceListener : IObserver public DiagnosticSourceListener(ListenerHandler handler) { - Guard.Null(handler, nameof(handler)); + Guard.ThrowIfNull(handler, nameof(handler)); this.handler = handler; } diff --git a/src/OpenTelemetry/DiagnosticSourceInstrumentation/DiagnosticSourceSubscriber.cs b/src/OpenTelemetry/DiagnosticSourceInstrumentation/DiagnosticSourceSubscriber.cs index 1a21408cd19..1e1a15dce5f 100644 --- a/src/OpenTelemetry/DiagnosticSourceInstrumentation/DiagnosticSourceSubscriber.cs +++ b/src/OpenTelemetry/DiagnosticSourceInstrumentation/DiagnosticSourceSubscriber.cs @@ -42,7 +42,7 @@ public DiagnosticSourceSubscriber( Func diagnosticSourceFilter, Func isEnabledFilter) { - Guard.Null(handlerFactory, nameof(handlerFactory)); + Guard.ThrowIfNull(handlerFactory, nameof(handlerFactory)); this.listenerSubscriptions = new List(); this.handlerFactory = handlerFactory; diff --git a/src/OpenTelemetry/DiagnosticSourceInstrumentation/PropertyFetcher.cs b/src/OpenTelemetry/DiagnosticSourceInstrumentation/PropertyFetcher.cs index 31539e284c5..1bcafb11dbf 100644 --- a/src/OpenTelemetry/DiagnosticSourceInstrumentation/PropertyFetcher.cs +++ b/src/OpenTelemetry/DiagnosticSourceInstrumentation/PropertyFetcher.cs @@ -46,7 +46,7 @@ public PropertyFetcher(string propertyName) /// Property fetched. public T Fetch(object obj) { - Guard.Null(obj, nameof(obj)); + Guard.ThrowIfNull(obj, nameof(obj)); if (!this.TryFetch(obj, out T value, true)) { diff --git a/src/OpenTelemetry/Internal/CircularBuffer.cs b/src/OpenTelemetry/Internal/CircularBuffer.cs index b5aec5ede55..8cc1f22ff2c 100644 --- a/src/OpenTelemetry/Internal/CircularBuffer.cs +++ b/src/OpenTelemetry/Internal/CircularBuffer.cs @@ -36,7 +36,7 @@ internal class CircularBuffer /// The capacity of the circular buffer, must be a positive integer. public CircularBuffer(int capacity) { - Guard.Range(capacity, nameof(capacity), min: 1); + Guard.ThrowIfOutOfRange(capacity, nameof(capacity), min: 1); this.Capacity = capacity; this.trait = new T[capacity]; @@ -79,7 +79,7 @@ public int Count /// public bool Add(T value) { - Guard.Null(value, nameof(value)); + Guard.ThrowIfNull(value, nameof(value)); while (true) { @@ -119,7 +119,7 @@ public bool TryAdd(T value, int maxSpinCount) return this.Add(value); } - Guard.Null(value, nameof(value)); + Guard.ThrowIfNull(value, nameof(value)); var spinCountDown = maxSpinCount; diff --git a/src/OpenTelemetry/Internal/PooledList.cs b/src/OpenTelemetry/Internal/PooledList.cs index c0f0b4491bb..52eea3d3eb4 100644 --- a/src/OpenTelemetry/Internal/PooledList.cs +++ b/src/OpenTelemetry/Internal/PooledList.cs @@ -52,7 +52,7 @@ public static PooledList Create() public static void Add(ref PooledList list, T item) { - Guard.Null(list.buffer, $"{nameof(list)}.{nameof(list.buffer)}"); + Guard.ThrowIfNull(list.buffer, $"{nameof(list)}.{nameof(list.buffer)}"); var buffer = list.buffer; diff --git a/src/OpenTelemetry/Internal/SelfDiagnosticsEventListener.cs b/src/OpenTelemetry/Internal/SelfDiagnosticsEventListener.cs index 55138732b04..5581f5ee2ab 100644 --- a/src/OpenTelemetry/Internal/SelfDiagnosticsEventListener.cs +++ b/src/OpenTelemetry/Internal/SelfDiagnosticsEventListener.cs @@ -43,7 +43,7 @@ internal class SelfDiagnosticsEventListener : EventListener public SelfDiagnosticsEventListener(EventLevel logLevel, SelfDiagnosticsConfigRefresher configRefresher) { - Guard.Null(configRefresher, nameof(configRefresher)); + Guard.ThrowIfNull(configRefresher, nameof(configRefresher)); this.logLevel = logLevel; this.configRefresher = configRefresher; diff --git a/src/OpenTelemetry/Logs/OpenTelemetryLogger.cs b/src/OpenTelemetry/Logs/OpenTelemetryLogger.cs index 8647545190f..2a81f1dde48 100644 --- a/src/OpenTelemetry/Logs/OpenTelemetryLogger.cs +++ b/src/OpenTelemetry/Logs/OpenTelemetryLogger.cs @@ -29,8 +29,8 @@ internal class OpenTelemetryLogger : ILogger internal OpenTelemetryLogger(string categoryName, OpenTelemetryLoggerProvider provider) { - Guard.Null(categoryName, nameof(categoryName)); - Guard.Null(provider, nameof(provider)); + Guard.ThrowIfNull(categoryName, nameof(categoryName)); + Guard.ThrowIfNull(provider, nameof(provider)); this.categoryName = categoryName; this.provider = provider; diff --git a/src/OpenTelemetry/Logs/OpenTelemetryLoggerOptions.cs b/src/OpenTelemetry/Logs/OpenTelemetryLoggerOptions.cs index 7c93853b5e2..6b6b1542ead 100644 --- a/src/OpenTelemetry/Logs/OpenTelemetryLoggerOptions.cs +++ b/src/OpenTelemetry/Logs/OpenTelemetryLoggerOptions.cs @@ -58,7 +58,7 @@ public class OpenTelemetryLoggerOptions /// Returns for chaining. public OpenTelemetryLoggerOptions AddProcessor(BaseProcessor processor) { - Guard.Null(processor, nameof(processor)); + Guard.ThrowIfNull(processor, nameof(processor)); this.Processors.Add(processor); @@ -73,7 +73,7 @@ public OpenTelemetryLoggerOptions AddProcessor(BaseProcessor processo /// Returns for chaining. public OpenTelemetryLoggerOptions SetResourceBuilder(ResourceBuilder resourceBuilder) { - Guard.Null(resourceBuilder, nameof(resourceBuilder)); + Guard.ThrowIfNull(resourceBuilder, nameof(resourceBuilder)); this.ResourceBuilder = resourceBuilder; return this; diff --git a/src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs b/src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs index a411b703f35..4e1dd445cf2 100644 --- a/src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs +++ b/src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs @@ -46,7 +46,7 @@ public OpenTelemetryLoggerProvider(IOptionsMonitor o internal OpenTelemetryLoggerProvider(OpenTelemetryLoggerOptions options) { - Guard.Null(options, nameof(options)); + Guard.ThrowIfNull(options, nameof(options)); this.Options = options; this.Resource = options.ResourceBuilder.Build(); @@ -97,7 +97,7 @@ public ILogger CreateLogger(string categoryName) internal OpenTelemetryLoggerProvider AddProcessor(BaseProcessor processor) { - Guard.Null(processor, nameof(processor)); + Guard.ThrowIfNull(processor, nameof(processor)); processor.SetParentProvider(this); diff --git a/src/OpenTelemetry/Logs/OpenTelemetryLoggingExtensions.cs b/src/OpenTelemetry/Logs/OpenTelemetryLoggingExtensions.cs index f117fff6dc2..0b48ea5d35e 100644 --- a/src/OpenTelemetry/Logs/OpenTelemetryLoggingExtensions.cs +++ b/src/OpenTelemetry/Logs/OpenTelemetryLoggingExtensions.cs @@ -28,7 +28,7 @@ public static class OpenTelemetryLoggingExtensions { public static ILoggingBuilder AddOpenTelemetry(this ILoggingBuilder builder, Action configure = null) { - Guard.Null(builder, nameof(builder)); + Guard.ThrowIfNull(builder, nameof(builder)); builder.AddConfiguration(); builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton()); diff --git a/src/OpenTelemetry/Metrics/BaseExportingMetricReader.cs b/src/OpenTelemetry/Metrics/BaseExportingMetricReader.cs index ea10f020a74..4b7b845a754 100644 --- a/src/OpenTelemetry/Metrics/BaseExportingMetricReader.cs +++ b/src/OpenTelemetry/Metrics/BaseExportingMetricReader.cs @@ -29,7 +29,7 @@ public class BaseExportingMetricReader : MetricReader public BaseExportingMetricReader(BaseExporter exporter) { - Guard.Null(exporter, nameof(exporter)); + Guard.ThrowIfNull(exporter, nameof(exporter)); this.exporter = exporter; diff --git a/src/OpenTelemetry/Metrics/CompositeMetricReader.cs b/src/OpenTelemetry/Metrics/CompositeMetricReader.cs index f666f24632c..537cec59385 100644 --- a/src/OpenTelemetry/Metrics/CompositeMetricReader.cs +++ b/src/OpenTelemetry/Metrics/CompositeMetricReader.cs @@ -34,7 +34,7 @@ internal sealed partial class CompositeMetricReader : MetricReader public CompositeMetricReader(IEnumerable readers) { - Guard.Null(readers, nameof(readers)); + Guard.ThrowIfNull(readers, nameof(readers)); using var iter = readers.GetEnumerator(); if (!iter.MoveNext()) @@ -54,7 +54,7 @@ public CompositeMetricReader(IEnumerable readers) public CompositeMetricReader AddReader(MetricReader reader) { - Guard.Null(reader, nameof(reader)); + Guard.ThrowIfNull(reader, nameof(reader)); var node = new DoublyLinkedListNode(reader) { diff --git a/src/OpenTelemetry/Metrics/MeterProviderBuilderBase.cs b/src/OpenTelemetry/Metrics/MeterProviderBuilderBase.cs index 97101c8ef46..18817cf5b66 100644 --- a/src/OpenTelemetry/Metrics/MeterProviderBuilderBase.cs +++ b/src/OpenTelemetry/Metrics/MeterProviderBuilderBase.cs @@ -47,7 +47,7 @@ protected MeterProviderBuilderBase() /// public override MeterProviderBuilder AddInstrumentation(Func instrumentationFactory) { - Guard.Null(instrumentationFactory, nameof(instrumentationFactory)); + Guard.ThrowIfNull(instrumentationFactory, nameof(instrumentationFactory)); this.instrumentationFactories.Add( new InstrumentationFactory( @@ -61,11 +61,11 @@ public override MeterProviderBuilder AddInstrumentation(Func public override MeterProviderBuilder AddMeter(params string[] names) { - Guard.Null(names, nameof(names)); + Guard.ThrowIfNull(names, nameof(names)); foreach (var name in names) { - Guard.NullOrWhitespace(name, nameof(name)); + Guard.ThrowIfNullOrWhitespace(name, nameof(name)); this.meterSources.Add(name); } @@ -106,7 +106,7 @@ internal MeterProviderBuilder AddView(Func public static bool ForceFlush(this MeterProvider provider, int timeoutMilliseconds = Timeout.Infinite) { - Guard.Null(provider, nameof(provider)); - Guard.InvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); + Guard.ThrowIfNull(provider, nameof(provider)); + Guard.ThrowIfInvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); if (provider is MeterProviderSdk meterProviderSdk) { @@ -82,8 +82,8 @@ public static bool ForceFlush(this MeterProvider provider, int timeoutMillisecon /// public static bool Shutdown(this MeterProvider provider, int timeoutMilliseconds = Timeout.Infinite) { - Guard.Null(provider, nameof(provider)); - Guard.InvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); + Guard.ThrowIfNull(provider, nameof(provider)); + Guard.ThrowIfInvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); if (provider is MeterProviderSdk meterProviderSdk) { diff --git a/src/OpenTelemetry/Metrics/MeterProviderSdk.cs b/src/OpenTelemetry/Metrics/MeterProviderSdk.cs index 992061f38a8..6b4956dde74 100644 --- a/src/OpenTelemetry/Metrics/MeterProviderSdk.cs +++ b/src/OpenTelemetry/Metrics/MeterProviderSdk.cs @@ -50,7 +50,7 @@ internal MeterProviderSdk( foreach (var reader in readers) { - Guard.Null(reader, nameof(reader)); + Guard.ThrowIfNull(reader, nameof(reader)); reader.SetParentProvider(this); reader.SetMaxMetricStreams(maxMetricStreams); diff --git a/src/OpenTelemetry/Metrics/MetricPointsAccessor.cs b/src/OpenTelemetry/Metrics/MetricPointsAccessor.cs index 1fd3cac7cd2..ef5c1dc0054 100644 --- a/src/OpenTelemetry/Metrics/MetricPointsAccessor.cs +++ b/src/OpenTelemetry/Metrics/MetricPointsAccessor.cs @@ -33,7 +33,7 @@ public readonly struct MetricPointsAccessor internal MetricPointsAccessor(MetricPoint[] metricsPoints, int[] metricPointsToProcess, long targetCount, DateTimeOffset start, DateTimeOffset end) { - Guard.Null(metricsPoints, nameof(metricsPoints)); + Guard.ThrowIfNull(metricsPoints, nameof(metricsPoints)); this.metricsPoints = metricsPoints; this.metricPointsToProcess = metricPointsToProcess; diff --git a/src/OpenTelemetry/Metrics/MetricReader.cs b/src/OpenTelemetry/Metrics/MetricReader.cs index 469d61bcdd1..759010a5ed9 100644 --- a/src/OpenTelemetry/Metrics/MetricReader.cs +++ b/src/OpenTelemetry/Metrics/MetricReader.cs @@ -83,7 +83,7 @@ public AggregationTemporality Temporality /// public bool Collect(int timeoutMilliseconds = Timeout.Infinite) { - Guard.InvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); + Guard.ThrowIfInvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); var shouldRunCollect = false; var tcs = this.collectionTcs; @@ -146,7 +146,7 @@ public bool Collect(int timeoutMilliseconds = Timeout.Infinite) /// public bool Shutdown(int timeoutMilliseconds = Timeout.Infinite) { - Guard.InvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); + Guard.ThrowIfInvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); if (Interlocked.CompareExchange(ref this.shutdownCount, 1, 0) != 0) { diff --git a/src/OpenTelemetry/Metrics/PeriodicExportingMetricReader.cs b/src/OpenTelemetry/Metrics/PeriodicExportingMetricReader.cs index 67bc35d2898..f6cdcdff155 100644 --- a/src/OpenTelemetry/Metrics/PeriodicExportingMetricReader.cs +++ b/src/OpenTelemetry/Metrics/PeriodicExportingMetricReader.cs @@ -39,8 +39,8 @@ public PeriodicExportingMetricReader( int exportTimeoutMilliseconds = DefaultExportTimeoutMilliseconds) : base(exporter) { - Guard.Range(exportIntervalMilliseconds, nameof(exportIntervalMilliseconds), min: 1); - Guard.Range(exportTimeoutMilliseconds, nameof(exportTimeoutMilliseconds), min: 0); + Guard.ThrowIfOutOfRange(exportIntervalMilliseconds, nameof(exportIntervalMilliseconds), min: 1); + Guard.ThrowIfOutOfRange(exportTimeoutMilliseconds, nameof(exportTimeoutMilliseconds), min: 0); if ((this.SupportedExportModes & ExportModes.Push) != ExportModes.Push) { diff --git a/src/OpenTelemetry/Metrics/ThreadStaticStorage.cs b/src/OpenTelemetry/Metrics/ThreadStaticStorage.cs index 66c48710c44..af193f7f7ad 100644 --- a/src/OpenTelemetry/Metrics/ThreadStaticStorage.cs +++ b/src/OpenTelemetry/Metrics/ThreadStaticStorage.cs @@ -52,7 +52,7 @@ internal static ThreadStaticStorage GetStorage() [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void SplitToKeysAndValues(ReadOnlySpan> tags, int tagLength, out string[] tagKeys, out object[] tagValues) { - Guard.Zero(tagLength, $"There must be at least one tag to use {nameof(ThreadStaticStorage)}", $"{nameof(tagLength)}"); + Guard.ThrowIfZero(tagLength, $"There must be at least one tag to use {nameof(ThreadStaticStorage)}", $"{nameof(tagLength)}"); if (tagLength <= MaxTagCacheSize) { diff --git a/src/OpenTelemetry/Resources/Resource.cs b/src/OpenTelemetry/Resources/Resource.cs index 7bf14bc1804..72cb6cab4e4 100644 --- a/src/OpenTelemetry/Resources/Resource.cs +++ b/src/OpenTelemetry/Resources/Resource.cs @@ -109,7 +109,7 @@ private static KeyValuePair SanitizeAttribute(KeyValuePair resourceAttributes = new Dictionary(); - Guard.NullOrEmpty(serviceName, nameof(serviceName)); + Guard.ThrowIfNullOrEmpty(serviceName, nameof(serviceName)); resourceAttributes.Add(ResourceSemanticConventions.AttributeServiceName, serviceName); diff --git a/src/OpenTelemetry/Trace/ParentBasedSampler.cs b/src/OpenTelemetry/Trace/ParentBasedSampler.cs index fff7c623a00..e83e94b8d12 100644 --- a/src/OpenTelemetry/Trace/ParentBasedSampler.cs +++ b/src/OpenTelemetry/Trace/ParentBasedSampler.cs @@ -43,7 +43,7 @@ public sealed class ParentBasedSampler : Sampler /// The to be called for root span/activity. public ParentBasedSampler(Sampler rootSampler) { - Guard.Null(rootSampler, nameof(rootSampler)); + Guard.ThrowIfNull(rootSampler, nameof(rootSampler)); this.rootSampler = rootSampler; this.Description = $"ParentBased{{{rootSampler.Description}}}"; diff --git a/src/OpenTelemetry/Trace/TraceIdRatioBasedSampler.cs b/src/OpenTelemetry/Trace/TraceIdRatioBasedSampler.cs index 936ff7781f2..049c620e48d 100644 --- a/src/OpenTelemetry/Trace/TraceIdRatioBasedSampler.cs +++ b/src/OpenTelemetry/Trace/TraceIdRatioBasedSampler.cs @@ -37,7 +37,7 @@ public sealed class TraceIdRatioBasedSampler /// public TraceIdRatioBasedSampler(double probability) { - Guard.Range(probability, nameof(probability), min: 0.0, max: 1.0); + Guard.ThrowIfOutOfRange(probability, nameof(probability), min: 0.0, max: 1.0); this.probability = probability; diff --git a/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs b/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs index 01f87cba712..1502dd4e18f 100644 --- a/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs +++ b/src/OpenTelemetry/Trace/TracerProviderBuilderBase.cs @@ -43,7 +43,7 @@ public override TracerProviderBuilder AddInstrumentation( Func instrumentationFactory) where TInstrumentation : class { - Guard.Null(instrumentationFactory, nameof(instrumentationFactory)); + Guard.ThrowIfNull(instrumentationFactory, nameof(instrumentationFactory)); this.instrumentationFactories.Add( new InstrumentationFactory( @@ -57,11 +57,11 @@ public override TracerProviderBuilder AddInstrumentation( /// public override TracerProviderBuilder AddSource(params string[] names) { - Guard.Null(names, nameof(names)); + Guard.ThrowIfNull(names, nameof(names)); foreach (var name in names) { - Guard.NullOrWhitespace(name, nameof(name)); + Guard.ThrowIfNullOrWhitespace(name, nameof(name)); // TODO: We need to fix the listening model. // Today it ignores version. @@ -74,7 +74,7 @@ public override TracerProviderBuilder AddSource(params string[] names) /// public override TracerProviderBuilder AddLegacySource(string operationName) { - Guard.NullOrWhitespace(operationName, nameof(operationName)); + Guard.ThrowIfNullOrWhitespace(operationName, nameof(operationName)); this.legacyActivityOperationNames.Add(operationName); @@ -129,7 +129,7 @@ internal TracerProviderBuilder SetErrorStatusOnException(bool enabled) /// Returns for chaining. internal TracerProviderBuilder SetSampler(Sampler sampler) { - Guard.Null(sampler, nameof(sampler)); + Guard.ThrowIfNull(sampler, nameof(sampler)); this.sampler = sampler; return this; @@ -143,7 +143,7 @@ internal TracerProviderBuilder SetSampler(Sampler sampler) /// Returns for chaining. internal TracerProviderBuilder SetResourceBuilder(ResourceBuilder resourceBuilder) { - Guard.Null(resourceBuilder, nameof(resourceBuilder)); + Guard.ThrowIfNull(resourceBuilder, nameof(resourceBuilder)); this.resourceBuilder = resourceBuilder; return this; @@ -156,7 +156,7 @@ internal TracerProviderBuilder SetResourceBuilder(ResourceBuilder resourceBuilde /// Returns for chaining. internal TracerProviderBuilder AddProcessor(BaseProcessor processor) { - Guard.Null(processor, nameof(processor)); + Guard.ThrowIfNull(processor, nameof(processor)); this.processors.Add(processor); diff --git a/src/OpenTelemetry/Trace/TracerProviderExtensions.cs b/src/OpenTelemetry/Trace/TracerProviderExtensions.cs index ed02b7f30ae..555c1cfe6e1 100644 --- a/src/OpenTelemetry/Trace/TracerProviderExtensions.cs +++ b/src/OpenTelemetry/Trace/TracerProviderExtensions.cs @@ -25,8 +25,8 @@ public static class TracerProviderExtensions { public static TracerProvider AddProcessor(this TracerProvider provider, BaseProcessor processor) { - Guard.Null(provider, nameof(provider)); - Guard.Null(processor, nameof(processor)); + Guard.ThrowIfNull(provider, nameof(provider)); + Guard.ThrowIfNull(processor, nameof(processor)); if (provider is TracerProviderSdk tracerProviderSdk) { @@ -56,8 +56,8 @@ public static TracerProvider AddProcessor(this TracerProvider provider, BaseProc /// public static bool ForceFlush(this TracerProvider provider, int timeoutMilliseconds = Timeout.Infinite) { - Guard.Null(provider, nameof(provider)); - Guard.InvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); + Guard.ThrowIfNull(provider, nameof(provider)); + Guard.ThrowIfInvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); if (provider is TracerProviderSdk tracerProviderSdk) { @@ -96,8 +96,8 @@ public static bool ForceFlush(this TracerProvider provider, int timeoutMilliseco /// public static bool Shutdown(this TracerProvider provider, int timeoutMilliseconds = Timeout.Infinite) { - Guard.Null(provider, nameof(provider)); - Guard.InvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); + Guard.ThrowIfNull(provider, nameof(provider)); + Guard.ThrowIfInvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds)); if (provider is TracerProviderSdk tracerProviderSdk) { diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index 200cd33e791..46cc99892bd 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -282,7 +282,7 @@ Regex GetWildcardRegex(IEnumerable collection) internal TracerProviderSdk AddProcessor(BaseProcessor processor) { - Guard.Null(processor, nameof(processor)); + Guard.ThrowIfNull(processor, nameof(processor)); processor.SetParentProvider(this); diff --git a/test/OpenTelemetry.Tests/Internal/GuardTest.cs b/test/OpenTelemetry.Tests/Internal/GuardTest.cs index 64bd41accbe..66807540905 100644 --- a/test/OpenTelemetry.Tests/Internal/GuardTest.cs +++ b/test/OpenTelemetry.Tests/Internal/GuardTest.cs @@ -27,13 +27,13 @@ public class GuardTest public void NullTest() { // Valid - Guard.Null(1); - Guard.Null(1.0); - Guard.Null(new object()); - Guard.Null("hello"); + Guard.ThrowIfNull(1); + Guard.ThrowIfNull(1.0); + Guard.ThrowIfNull(new object()); + Guard.ThrowIfNull("hello"); // Invalid - var ex1 = Assert.Throws(() => Guard.Null(null, "null")); + var ex1 = Assert.Throws(() => Guard.ThrowIfNull(null, "null")); Assert.Contains("Must not be null", ex1.Message); } @@ -41,14 +41,14 @@ public void NullTest() public void NullOrEmptyTest() { // Valid - Guard.NullOrEmpty("a"); - Guard.NullOrEmpty(" "); + Guard.ThrowIfNullOrEmpty("a"); + Guard.ThrowIfNullOrEmpty(" "); // Invalid - var ex1 = Assert.Throws(() => Guard.NullOrEmpty(null)); + var ex1 = Assert.Throws(() => Guard.ThrowIfNullOrEmpty(null)); Assert.Contains("Must not be null or empty", ex1.Message); - var ex2 = Assert.Throws(() => Guard.NullOrEmpty(string.Empty)); + var ex2 = Assert.Throws(() => Guard.ThrowIfNullOrEmpty(string.Empty)); Assert.Contains("Must not be null or empty", ex2.Message); } @@ -56,16 +56,16 @@ public void NullOrEmptyTest() public void NullOrWhitespaceTest() { // Valid - Guard.NullOrWhitespace("a"); + Guard.ThrowIfNullOrWhitespace("a"); // Invalid - var ex1 = Assert.Throws(() => Guard.NullOrWhitespace(null)); + var ex1 = Assert.Throws(() => Guard.ThrowIfNullOrWhitespace(null)); Assert.Contains("Must not be null or whitespace", ex1.Message); - var ex2 = Assert.Throws(() => Guard.NullOrWhitespace(string.Empty)); + var ex2 = Assert.Throws(() => Guard.ThrowIfNullOrWhitespace(string.Empty)); Assert.Contains("Must not be null or whitespace", ex2.Message); - var ex3 = Assert.Throws(() => Guard.NullOrWhitespace(" \t\n\r")); + var ex3 = Assert.Throws(() => Guard.ThrowIfNullOrWhitespace(" \t\n\r")); Assert.Contains("Must not be null or whitespace", ex3.Message); } @@ -73,12 +73,12 @@ public void NullOrWhitespaceTest() public void InvalidTimeoutTest() { // Valid - Guard.InvalidTimeout(Timeout.Infinite); - Guard.InvalidTimeout(0); - Guard.InvalidTimeout(100); + Guard.ThrowIfInvalidTimeout(Timeout.Infinite); + Guard.ThrowIfInvalidTimeout(0); + Guard.ThrowIfInvalidTimeout(100); // Invalid - var ex1 = Assert.Throws(() => Guard.InvalidTimeout(-100)); + var ex1 = Assert.Throws(() => Guard.ThrowIfInvalidTimeout(-100)); Assert.Contains("Must be non-negative or 'Timeout.Infinite'", ex1.Message); } @@ -86,17 +86,17 @@ public void InvalidTimeoutTest() public void RangeIntTest() { // Valid - Guard.Range(0); - Guard.Range(0, min: 0, max: 0); - Guard.Range(5, min: -10, max: 10); - Guard.Range(int.MinValue, min: int.MinValue, max: 10); - Guard.Range(int.MaxValue, min: 10, max: int.MaxValue); + Guard.ThrowIfOutOfRange(0); + Guard.ThrowIfOutOfRange(0, min: 0, max: 0); + Guard.ThrowIfOutOfRange(5, min: -10, max: 10); + Guard.ThrowIfOutOfRange(int.MinValue, min: int.MinValue, max: 10); + Guard.ThrowIfOutOfRange(int.MaxValue, min: 10, max: int.MaxValue); // Invalid - var ex1 = Assert.Throws(() => Guard.Range(-1, min: 0, max: 100, minName: "empty", maxName: "full")); + var ex1 = Assert.Throws(() => Guard.ThrowIfOutOfRange(-1, min: 0, max: 100, minName: "empty", maxName: "full")); Assert.Contains("Must be in the range: [0: empty, 100: full]", ex1.Message); - var ex2 = Assert.Throws(() => Guard.Range(-1, min: 0, max: 100, message: "error")); + var ex2 = Assert.Throws(() => Guard.ThrowIfOutOfRange(-1, min: 0, max: 100, message: "error")); Assert.Contains("error", ex2.Message); } @@ -104,15 +104,15 @@ public void RangeIntTest() public void RangeDoubleTest() { // Valid - Guard.Range(1.0, min: 1.0, max: 1.0); - Guard.Range(double.MinValue, min: double.MinValue, max: 10.0); - Guard.Range(double.MaxValue, min: 10.0, max: double.MaxValue); + Guard.ThrowIfOutOfRange(1.0, min: 1.0, max: 1.0); + Guard.ThrowIfOutOfRange(double.MinValue, min: double.MinValue, max: 10.0); + Guard.ThrowIfOutOfRange(double.MaxValue, min: 10.0, max: double.MaxValue); // Invalid - var ex3 = Assert.Throws(() => Guard.Range(-1.1, min: 0.1, max: 99.9, minName: "empty", maxName: "full")); + var ex3 = Assert.Throws(() => Guard.ThrowIfOutOfRange(-1.1, min: 0.1, max: 99.9, minName: "empty", maxName: "full")); Assert.Contains("Must be in the range: [0.1: empty, 99.9: full]", ex3.Message); - var ex4 = Assert.Throws(() => Guard.Range(-1.1, min: 0.0, max: 100.0)); + var ex4 = Assert.Throws(() => Guard.ThrowIfOutOfRange(-1.1, min: 0.0, max: 100.0)); Assert.Contains("Must be in the range: [0, 100]", ex4.Message); } @@ -120,12 +120,12 @@ public void RangeDoubleTest() public void TypeTest() { // Valid - Guard.Type(0); - Guard.Type(new object()); - Guard.Type("hello"); + Guard.ThrowIfNotOfType(0); + Guard.ThrowIfNotOfType(new object()); + Guard.ThrowIfNotOfType("hello"); // Invalid - var ex1 = Assert.Throws(() => Guard.Type(100)); + var ex1 = Assert.Throws(() => Guard.ThrowIfNotOfType(100)); Assert.Equal("Cannot cast 'N/A' from 'Int32' to 'Double'", ex1.Message); } @@ -133,10 +133,10 @@ public void TypeTest() public void ZeroTest() { // Valid - Guard.Zero(1); + Guard.ThrowIfZero(1); // Invalid - var ex1 = Assert.Throws(() => Guard.Zero(0)); + var ex1 = Assert.Throws(() => Guard.ThrowIfZero(0)); Assert.Contains("Must not be zero", ex1.Message); } }