Skip to content

Commit

Permalink
Merge branch 'main' into alanwest/goofiness
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas authored Feb 16, 2022
2 parents 5c30327 + 360957d commit b006ed8
Show file tree
Hide file tree
Showing 87 changed files with 323 additions and 195 deletions.
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.ThrowIfNullOrEmpty(name, nameof(name));
Guard.ThrowIfNullOrEmpty(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.ThrowIfNull(propagators, nameof(propagators));
Guard.ThrowIfNull(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.ThrowIfNullOrEmpty(slotName, nameof(slotName));
Guard.ThrowIfNullOrEmpty(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.ThrowIfNullOrEmpty(slotName, nameof(slotName));
Guard.ThrowIfNullOrEmpty(slotName);
var slot = GuardNotFound(slotName);
var contextSlot = Guard.ThrowIfNotOfType<RuntimeContextSlot<T>>(slot, nameof(slot));
var contextSlot = Guard.ThrowIfNotOfType<RuntimeContextSlot<T>>(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.ThrowIfNullOrEmpty(slotName, nameof(slotName));
Guard.ThrowIfNullOrEmpty(slotName);
var slot = GuardNotFound(slotName);
var runtimeContextSlotValueAccessor = Guard.ThrowIfNotOfType<IRuntimeContextSlotValueAccessor>(slot, nameof(slot));
var runtimeContextSlotValueAccessor = Guard.ThrowIfNotOfType<IRuntimeContextSlotValueAccessor>(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.ThrowIfNullOrEmpty(slotName, nameof(slotName));
Guard.ThrowIfNullOrEmpty(slotName);
var slot = GuardNotFound(slotName);
var runtimeContextSlotValueAccessor = Guard.ThrowIfNotOfType<IRuntimeContextSlotValueAccessor>(slot, nameof(slot));
var runtimeContextSlotValueAccessor = Guard.ThrowIfNotOfType<IRuntimeContextSlotValueAccessor>(slot);
return runtimeContextSlotValueAccessor.Value;
}

Expand Down
50 changes: 40 additions & 10 deletions src/OpenTelemetry.Api/Internal/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,53 @@
using System.Runtime.CompilerServices;
using System.Threading;

#if !NETCOREAPP3_0_OR_GREATER
namespace System.Runtime.CompilerServices
{
/// <summary>
/// Allows capturing of the expressions passed to a method.
/// </summary>
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
#pragma warning disable SA1402 // File may only contain a single type
#pragma warning disable SA1649 // File name should match first type name
internal sealed class CallerArgumentExpressionAttribute : Attribute
#pragma warning restore SA1649 // File name should match first type name
#pragma warning restore SA1402 // File may only contain a single type
{
/// <summary>
/// Initializes a new instance of the <see cref="CallerArgumentExpressionAttribute"/> class.
/// </summary>
/// <param name="parameterName">The name of the targeted parameter.</param>
public CallerArgumentExpressionAttribute(string parameterName)
{
this.ParameterName = parameterName;
}

/// <summary>
/// Gets the target parameter name of the CallerArgumentExpression.
/// </summary>
public string ParameterName { get; }
}
}
#endif

#pragma warning disable SA1403 // File may only contain a single namespace
namespace OpenTelemetry.Internal
#pragma warning restore SA1403 // File may only contain a single namespace
{
/// <summary>
/// Methods for guarding against exception throwing values.
/// </summary>
internal static class Guard
{
private const string DefaultParamName = "N/A";

/// <summary>
/// Throw an exception if the value is null.
/// </summary>
/// <param name="value">The value to check.</param>
/// <param name="paramName">The parameter name to use in the thrown exception.</param>
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfNull(object value, string paramName = DefaultParamName)
public static void ThrowIfNull(object value, [CallerArgumentExpression("value")] string paramName = null)
{
if (value is null)
{
Expand All @@ -50,7 +80,7 @@ public static void ThrowIfNull(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 ThrowIfNullOrEmpty(string value, string paramName = DefaultParamName)
public static void ThrowIfNullOrEmpty(string value, [CallerArgumentExpression("value")] string paramName = null)
{
if (string.IsNullOrEmpty(value))
{
Expand All @@ -65,7 +95,7 @@ public static void ThrowIfNullOrEmpty(string value, string paramName = DefaultPa
/// <param name="paramName">The parameter name to use in the thrown exception.</param>
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfNullOrWhitespace(string value, string paramName = DefaultParamName)
public static void ThrowIfNullOrWhitespace(string value, [CallerArgumentExpression("value")] string paramName = null)
{
if (string.IsNullOrWhiteSpace(value))
{
Expand All @@ -81,7 +111,7 @@ public static void ThrowIfNullOrWhitespace(string value, string paramName = Defa
/// <param name="paramName">The parameter name to use in the thrown exception.</param>
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfZero(int value, string message = "Must not be zero", string paramName = DefaultParamName)
public static void ThrowIfZero(int value, string message = "Must not be zero", [CallerArgumentExpression("value")] string paramName = null)
{
if (value == 0)
{
Expand All @@ -96,7 +126,7 @@ public static void ThrowIfZero(int value, string message = "Must not be zero", s
/// <param name="paramName">The parameter name to use in the thrown exception.</param>
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfInvalidTimeout(int value, string paramName = DefaultParamName)
public static void ThrowIfInvalidTimeout(int value, [CallerArgumentExpression("value")] string paramName = null)
{
ThrowIfOutOfRange(value, paramName, min: Timeout.Infinite, message: $"Must be non-negative or '{nameof(Timeout)}.{nameof(Timeout.Infinite)}'");
}
Expand All @@ -113,7 +143,7 @@ public static void ThrowIfInvalidTimeout(int value, string paramName = DefaultPa
/// <param name="message">An optional custom message to use in the thrown exception.</param>
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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)
public static void ThrowIfOutOfRange(int value, [CallerArgumentExpression("value")] string paramName = null, 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);
}
Expand All @@ -130,7 +160,7 @@ public static void ThrowIfOutOfRange(int value, string paramName = DefaultParamN
/// <param name="message">An optional custom message to use in the thrown exception.</param>
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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)
public static void ThrowIfOutOfRange(double value, [CallerArgumentExpression("value")] string paramName = null, 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);
}
Expand All @@ -144,7 +174,7 @@ public static void ThrowIfOutOfRange(double value, string paramName = DefaultPar
/// <returns>The value casted to the specified type.</returns>
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T ThrowIfNotOfType<T>(object value, string paramName = DefaultParamName)
public static T ThrowIfNotOfType<T>(object value, [CallerArgumentExpression("value")] string paramName = null)
{
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.ThrowIfNull(attributes, nameof(attributes));
Guard.ThrowIfNull(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.ThrowIfNull(key, nameof(key));
Guard.ThrowIfNull(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.ThrowIfNull(builder, nameof(builder));
Guard.ThrowIfNull(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.ThrowIfNull(loggerOptions, nameof(loggerOptions));
Guard.ThrowIfNull(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.ThrowIfNull(builder, nameof(builder));
Guard.ThrowIfNull(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.ThrowIfNull(builder, nameof(builder));
Guard.ThrowIfNull(exportedItems, nameof(exportedItems));
Guard.ThrowIfNull(builder);
Guard.ThrowIfNull(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.ThrowIfNull(loggerOptions, nameof(loggerOptions));
Guard.ThrowIfNull(exportedItems, nameof(exportedItems));
Guard.ThrowIfNull(loggerOptions);
Guard.ThrowIfNull(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.ThrowIfNull(builder, nameof(builder));
Guard.ThrowIfNull(exportedItems, nameof(exportedItems));
Guard.ThrowIfNull(builder);
Guard.ThrowIfNull(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.ThrowIfNull(options, nameof(options));
Guard.ThrowIfNull(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.ThrowIfNull(resource, nameof(resource));
Guard.ThrowIfNull(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.ThrowIfNull(builder, nameof(builder));
Guard.ThrowIfNull(builder);

if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Compat issues with assembly OpenTelemetry.Exporter.OpenTelemetryProtocol:
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.CompilerGeneratedAttribute' exists on 'OpenTelemetry.Exporter.OtlpExporterOptions.Endpoint.get()' in the contract but not the implementation.
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.CompilerGeneratedAttribute' exists on 'OpenTelemetry.Exporter.OtlpExporterOptions.Endpoint.set(System.Uri)' in the contract but not the implementation.
Total Issues: 2
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
## Unreleased

* LogExporter bug fix to handle null EventName.
([#2870](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2871))
([#2871](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2871))

* Fixed the default endpoint for OTLP exporter over HTTP/Protobuf.
The default value is `http://localhost:4318`.
([#2868](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2868))

## 1.2.0-rc2

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.ThrowIfNull(options, nameof(options));
Guard.ThrowIfInvalidTimeout(options.TimeoutMilliseconds, nameof(options.TimeoutMilliseconds));
Guard.ThrowIfNull(options);
Guard.ThrowIfInvalidTimeout(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.ThrowIfNull(options, nameof(options));
Guard.ThrowIfNull(httpClient, nameof(httpClient));
Guard.ThrowIfInvalidTimeout(options.TimeoutMilliseconds, $"{nameof(options)}.{nameof(options.TimeoutMilliseconds)}");
Guard.ThrowIfNull(options);
Guard.ThrowIfNull(httpClient);
Guard.ThrowIfInvalidTimeout(options.TimeoutMilliseconds);

this.Options = options;
this.Headers = options.GetHeaders<Dictionary<string, string>>((d, k, v) => d.Add(k, v));
Expand Down
Loading

0 comments on commit b006ed8

Please sign in to comment.