Use Guard class and Debug.Assert for recent developer changes (#2540)
Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
This commit is contained in:
parent
87601de2da
commit
2a221722a1
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
using System;
|
||||
using OpenTelemetry.Exporter;
|
||||
using OpenTelemetry.Internal;
|
||||
|
||||
namespace OpenTelemetry.Logs
|
||||
{
|
||||
|
|
@ -32,10 +33,7 @@ namespace OpenTelemetry.Logs
|
|||
/// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
|
||||
public static OpenTelemetryLoggerOptions AddOtlpExporter(this OpenTelemetryLoggerOptions loggerOptions, Action<OtlpExporterOptions> configure = null)
|
||||
{
|
||||
if (loggerOptions == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(loggerOptions));
|
||||
}
|
||||
Guard.Null(loggerOptions);
|
||||
|
||||
return AddOtlpExporter(loggerOptions, new OtlpExporterOptions(), configure);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using OpenTelemetry.Internal;
|
||||
|
||||
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient
|
||||
{
|
||||
|
|
@ -27,15 +28,11 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClie
|
|||
{
|
||||
protected BaseOtlpHttpExportClient(OtlpExporterOptions options, HttpClient httpClient = null)
|
||||
{
|
||||
this.Options = options ?? throw new ArgumentNullException(nameof(options));
|
||||
|
||||
if (this.Options.TimeoutMilliseconds <= 0)
|
||||
{
|
||||
throw new ArgumentException("Timeout value provided is not a positive number.", nameof(this.Options.TimeoutMilliseconds));
|
||||
}
|
||||
Guard.Null(options, nameof(options));
|
||||
Guard.InvalidTimeout(options.TimeoutMilliseconds, $"{nameof(options)}.{nameof(options.TimeoutMilliseconds)}");
|
||||
|
||||
this.Options = options;
|
||||
this.Headers = options.GetHeaders<Dictionary<string, string>>((d, k, v) => d.Add(k, v));
|
||||
|
||||
this.HttpClient = httpClient ?? new HttpClient { Timeout = TimeSpan.FromMilliseconds(this.Options.TimeoutMilliseconds) };
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ using System.Runtime.CompilerServices;
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using OpenTelemetry.Internal;
|
||||
using OpenTelemetry.Metrics;
|
||||
|
||||
namespace OpenTelemetry.Exporter.Prometheus
|
||||
|
|
@ -39,10 +40,7 @@ namespace OpenTelemetry.Exporter.Prometheus
|
|||
/// <param name="next"><see cref="RequestDelegate"/>.</param>
|
||||
public PrometheusExporterMiddleware(MeterProvider meterProvider, RequestDelegate next)
|
||||
{
|
||||
if (meterProvider == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(meterProvider));
|
||||
}
|
||||
Guard.Null(meterProvider, nameof(meterProvider));
|
||||
|
||||
if (!meterProvider.TryFindExporter(out PrometheusExporter exporter))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using OpenTelemetry.Internal;
|
||||
|
||||
namespace OpenTelemetry.Metrics
|
||||
{
|
||||
|
|
@ -29,17 +30,16 @@ namespace OpenTelemetry.Metrics
|
|||
|
||||
public MeterProviderBuilderHosting(IServiceCollection services)
|
||||
{
|
||||
this.Services = services ?? throw new ArgumentNullException(nameof(services));
|
||||
Guard.Null(services, nameof(services));
|
||||
|
||||
this.Services = services;
|
||||
}
|
||||
|
||||
public IServiceCollection Services { get; }
|
||||
|
||||
public MeterProviderBuilder Configure(Action<IServiceProvider, MeterProviderBuilder> configure)
|
||||
{
|
||||
if (configure == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(configure));
|
||||
}
|
||||
Guard.Null(configure, nameof(configure));
|
||||
|
||||
this.configurationActions.Add(configure);
|
||||
return this;
|
||||
|
|
@ -47,10 +47,7 @@ namespace OpenTelemetry.Metrics
|
|||
|
||||
public MeterProvider Build(IServiceProvider serviceProvider)
|
||||
{
|
||||
if (serviceProvider == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(serviceProvider));
|
||||
}
|
||||
Guard.Null(serviceProvider, nameof(serviceProvider));
|
||||
|
||||
// Note: Not using a foreach loop because additional actions can be
|
||||
// added during each call.
|
||||
|
|
|
|||
|
|
@ -47,10 +47,7 @@ namespace OpenTelemetry.Trace
|
|||
|
||||
public TracerProvider Build(IServiceProvider serviceProvider)
|
||||
{
|
||||
if (serviceProvider == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(serviceProvider));
|
||||
}
|
||||
Guard.Null(serviceProvider, nameof(serviceProvider));
|
||||
|
||||
// Note: Not using a foreach loop because additional actions can be
|
||||
// added during each call.
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@
|
|||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using OpenTelemetry.Extensions.Hosting.Implementation;
|
||||
|
|
@ -72,10 +73,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
|
||||
public static IServiceCollection AddOpenTelemetryMetrics(this IServiceCollection services, Action<MeterProviderBuilder> configure)
|
||||
{
|
||||
if (configure is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(configure));
|
||||
}
|
||||
Guard.Null(configure, nameof(configure));
|
||||
|
||||
var builder = new MeterProviderBuilderHosting(services);
|
||||
configure(builder);
|
||||
|
|
@ -114,15 +112,8 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
|
||||
private static IServiceCollection AddOpenTelemetryMetrics(this IServiceCollection services, Func<IServiceProvider, MeterProvider> createMeterProvider)
|
||||
{
|
||||
if (services is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(services));
|
||||
}
|
||||
|
||||
if (createMeterProvider is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(createMeterProvider));
|
||||
}
|
||||
Debug.Assert(services != null, $"{nameof(services)} must not be null");
|
||||
Debug.Assert(createMeterProvider != null, $"{nameof(createMeterProvider)} must not be null");
|
||||
|
||||
try
|
||||
{
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using global::OpenTracing;
|
||||
using OpenTracing;
|
||||
|
||||
namespace OpenTelemetry.Shims.OpenTracing
|
||||
{
|
||||
|
|
|
|||
|
|
@ -47,6 +47,7 @@ namespace OpenTelemetry.Shims.OpenTracing
|
|||
public SpanShim(TelemetrySpan span)
|
||||
{
|
||||
Guard.Null(span, nameof(span));
|
||||
|
||||
if (!span.Context.IsValid)
|
||||
{
|
||||
throw new ArgumentException($"Invalid '{nameof(SpanContext)}'", nameof(span.Context));
|
||||
|
|
|
|||
|
|
@ -16,8 +16,10 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.Metrics;
|
||||
using System.Text.RegularExpressions;
|
||||
using OpenTelemetry.Internal;
|
||||
using OpenTelemetry.Resources;
|
||||
|
||||
namespace OpenTelemetry.Metrics
|
||||
|
|
@ -41,10 +43,7 @@ namespace OpenTelemetry.Metrics
|
|||
/// <inheritdoc />
|
||||
public override MeterProviderBuilder AddInstrumentation<TInstrumentation>(Func<TInstrumentation> instrumentationFactory)
|
||||
{
|
||||
if (instrumentationFactory == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(instrumentationFactory));
|
||||
}
|
||||
Guard.Null(instrumentationFactory, nameof(instrumentationFactory));
|
||||
|
||||
this.instrumentationFactories.Add(
|
||||
new InstrumentationFactory(
|
||||
|
|
@ -58,17 +57,11 @@ namespace OpenTelemetry.Metrics
|
|||
/// <inheritdoc />
|
||||
public override MeterProviderBuilder AddMeter(params string[] names)
|
||||
{
|
||||
if (names == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(names));
|
||||
}
|
||||
Guard.Null(names, nameof(names));
|
||||
|
||||
foreach (var name in names)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
{
|
||||
throw new ArgumentException($"{nameof(names)} contains null or whitespace string.");
|
||||
}
|
||||
Guard.NullOrWhitespace(name, nameof(name));
|
||||
|
||||
this.meterSources.Add(name);
|
||||
}
|
||||
|
|
@ -114,7 +107,9 @@ namespace OpenTelemetry.Metrics
|
|||
|
||||
internal MeterProviderBuilder SetResourceBuilder(ResourceBuilder resourceBuilder)
|
||||
{
|
||||
this.resourceBuilder = resourceBuilder ?? throw new ArgumentNullException(nameof(resourceBuilder));
|
||||
Debug.Assert(resourceBuilder != null, $"{nameof(resourceBuilder)} must not be null");
|
||||
|
||||
this.resourceBuilder = resourceBuilder;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue