Skip to content

Commit

Permalink
[repo] Simplify preprocessor directives (#5712)
Browse files Browse the repository at this point in the history
Co-authored-by: joegoldman2 <147369450+joegoldman@users.noreply.github.com>
Co-authored-by: Mikel Blanchard <mblanchard@macrosssoftware.com>
  • Loading branch information
3 people authored Jun 24, 2024
1 parent 2f14c24 commit 6d467d7
Show file tree
Hide file tree
Showing 42 changed files with 86 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#if NET6_0_OR_GREATER
#if NET
using System.Diagnostics.CodeAnalysis;
#endif
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -26,7 +26,7 @@ public static class OpenTelemetryDependencyInjectionLoggerProviderBuilderExtensi
/// <param name="loggerProviderBuilder"><see cref="LoggerProviderBuilder"/>.</param>
/// <returns>The supplied <see cref="LoggerProviderBuilder"/> for chaining.</returns>
public static LoggerProviderBuilder AddInstrumentation<
#if NET6_0_OR_GREATER
#if NET
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
T>(this LoggerProviderBuilder loggerProviderBuilder)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#if NET6_0_OR_GREATER
#if NET
using System.Diagnostics.CodeAnalysis;
#endif
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -26,7 +26,7 @@ public static class OpenTelemetryDependencyInjectionMeterProviderBuilderExtensio
/// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
/// <returns>The supplied <see cref="MeterProviderBuilder"/> for chaining.</returns>
public static MeterProviderBuilder AddInstrumentation<
#if NET6_0_OR_GREATER
#if NET
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
T>(this MeterProviderBuilder meterProviderBuilder)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#if NET6_0_OR_GREATER
#if NET
using System.Diagnostics.CodeAnalysis;
#endif
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -26,7 +26,7 @@ public static class OpenTelemetryDependencyInjectionTracerProviderBuilderExtensi
/// <param name="tracerProviderBuilder"><see cref="TracerProviderBuilder"/>.</param>
/// <returns>The supplied <see cref="TracerProviderBuilder"/> for chaining.</returns>
public static TracerProviderBuilder AddInstrumentation<
#if NET6_0_OR_GREATER
#if NET
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
T>(this TracerProviderBuilder tracerProviderBuilder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public override void Inject<T>(PropagationContext context, T carrier, Action<T,
return;
}

#if NET6_0_OR_GREATER
#if NET
var traceparent = string.Create(55, context.ActivityContext, WriteTraceParentIntoSpan);
#else
var traceparent = string.Concat("00-", context.ActivityContext.TraceId.ToHexString(), "-", context.ActivityContext.SpanId.ToHexString());
Expand Down Expand Up @@ -430,7 +430,7 @@ private static bool IsLowerAlphaDigit(char c)
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z');
}

#if NET6_0_OR_GREATER
#if NET
private static void WriteTraceParentIntoSpan(Span<char> destination, ActivityContext context)
{
"00-".CopyTo(destination);
Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry.Api/Logs/LoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#nullable enable

#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
#if NETSTANDARD2_1_OR_GREATER || NET
using System.Diagnostics.CodeAnalysis;
#endif
#if EXPOSE_EXPERIMENTAL_FEATURES && NET8_0_OR_GREATER
Expand Down Expand Up @@ -103,7 +103,7 @@ Logger GetLogger(string? name, string? version)
#endif
virtual bool TryCreateLogger(
string? name,
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
#if NETSTANDARD2_1_OR_GREATER || NET
[NotNullWhen(true)]
#endif
out Logger? logger)
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry.Api/Trace/Tracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static TelemetrySpan CurrentSpan
/// </summary>
/// <param name="span">The span to be made current.</param>
/// <returns>The supplied span for call chaining.</returns>
#if NET6_0_OR_GREATER
#if NET
[return: NotNullIfNotNull(nameof(span))]
#endif
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry.Api/Trace/TracerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#nullable enable

using System.Collections.Concurrent;
#if NET6_0_OR_GREATER
#if NET
using System.Diagnostics.CodeAnalysis;
#endif

Expand Down Expand Up @@ -36,7 +36,7 @@ protected TracerProvider()
/// <param name="version">Version of the instrumentation library.</param>
/// <returns>Tracer instance.</returns>
public Tracer GetTracer(
#if NET6_0_OR_GREATER
#if NET
[AllowNull]
#endif
string name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using Grpc.Core;
using OpenTelemetry.Internal;
#if NETSTANDARD2_1 || NET6_0_OR_GREATER
#if NETSTANDARD2_1 || NET
using Grpc.Net.Client;
#endif

Expand All @@ -25,7 +25,7 @@ protected BaseOtlpGrpcExportClient(OtlpExporterOptions options)
this.TimeoutMilliseconds = options.TimeoutMilliseconds;
}

#if NETSTANDARD2_1 || NET6_0_OR_GREATER
#if NETSTANDARD2_1 || NET
internal GrpcChannel Channel { get; set; }
#else
internal Channel Channel { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected HttpRequestMessage CreateHttpRequest(TRequest exportRequest)

protected HttpResponseMessage SendHttpRequest(HttpRequestMessage request, CancellationToken cancellationToken)
{
#if NET6_0_OR_GREATER
#if NET
return this.HttpClient.Send(request, cancellationToken);
#else
return this.HttpClient.SendAsync(request, cancellationToken).GetAwaiter().GetResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ExportRequestContent(OtlpCollector.ExportLogsServiceRequest exportRequest
this.Headers.ContentType = ProtobufMediaTypeHeader;
}

#if NET6_0_OR_GREATER
#if NET
protected override void SerializeToStream(Stream stream, TransportContext context, CancellationToken cancellationToken)
{
this.SerializeToStreamInternal(stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ExportRequestContent(OtlpCollector.ExportMetricsServiceRequest exportRequ
this.Headers.ContentType = ProtobufMediaTypeHeader;
}

#if NET6_0_OR_GREATER
#if NET
protected override void SerializeToStream(Stream stream, TransportContext context, CancellationToken cancellationToken)
{
this.SerializeToStreamInternal(stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ExportRequestContent(OtlpCollector.ExportTraceServiceRequest exportReques
this.Headers.ContentType = ProtobufMediaTypeHeader;
}

#if NET6_0_OR_GREATER
#if NET
protected override void SerializeToStream(Stream stream, TransportContext context, CancellationToken cancellationToken)
{
this.SerializeToStreamInternal(stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal static class OtlpRetry
private const int MaxBackoffMilliseconds = 5000;
private const double BackoffMultiplier = 1.5;

#if !NET6_0_OR_GREATER
#if !NET
private static readonly Random Random = new Random();
#endif

Expand Down Expand Up @@ -191,7 +191,7 @@ private static int CalculateNextRetryDelay(int nextRetryDelayMilliseconds)

private static TimeSpan? TryGetHttpRetryDelay(HttpStatusCode statusCode, HttpResponseHeaders? responseHeaders)
{
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
#if NETSTANDARD2_1_OR_GREATER || NET
return statusCode == HttpStatusCode.TooManyRequests || statusCode == HttpStatusCode.ServiceUnavailable
#else
return statusCode == (HttpStatusCode)429 || statusCode == HttpStatusCode.ServiceUnavailable
Expand Down Expand Up @@ -222,7 +222,7 @@ private static bool IsHttpStatusCodeRetryable(HttpStatusCode statusCode, bool ha
{
switch (statusCode)
{
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
#if NETSTANDARD2_1_OR_GREATER || NET
case HttpStatusCode.TooManyRequests:
#else
case (HttpStatusCode)429:
Expand All @@ -238,7 +238,7 @@ private static bool IsHttpStatusCodeRetryable(HttpStatusCode statusCode, bool ha

private static int GetRandomNumber(int min, int max)
{
#if NET6_0_OR_GREATER
#if NET
return Random.Shared.Next(min, max);
#else
// TODO: Implement this better to minimize lock contention.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Grpc.Core;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
#if NETSTANDARD2_1 || NET6_0_OR_GREATER
#if NETSTANDARD2_1 || NET
using Grpc.Net.Client;
#endif
using System.Diagnostics;
Expand All @@ -22,7 +22,7 @@ namespace OpenTelemetry.Exporter;

internal static class OtlpExporterOptionsExtensions
{
#if NETSTANDARD2_1 || NET6_0_OR_GREATER
#if NETSTANDARD2_1 || NET
public static GrpcChannel CreateChannel(this OtlpExporterOptions options)
#else
public static Channel CreateChannel(this OtlpExporterOptions options)
Expand All @@ -33,7 +33,7 @@ public static Channel CreateChannel(this OtlpExporterOptions options)
throw new NotSupportedException($"Endpoint URI scheme ({options.Endpoint.Scheme}) is not supported. Currently only \"http\" and \"https\" are supported.");
}

#if NETSTANDARD2_1 || NET6_0_OR_GREATER
#if NETSTANDARD2_1 || NET
return GrpcChannel.ForAddress(options.Endpoint);
#else
ChannelCredentials channelCredentials;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public PrometheusCollectionManager(PrometheusExporter exporter)
this.scopes = new HashSet<string>();
}

#if NET6_0_OR_GREATER
#if NET
public ValueTask<CollectionResponse> EnterCollect(bool openMetricsRequested)
#else
public Task<CollectionResponse> EnterCollect(bool openMetricsRequested)
Expand All @@ -57,7 +57,7 @@ public Task<CollectionResponse> EnterCollect(bool openMetricsRequested)
{
Interlocked.Increment(ref this.readerCount);
this.ExitGlobalLock();
#if NET6_0_OR_GREATER
#if NET
return new ValueTask<CollectionResponse>(new CollectionResponse(this.previousOpenMetricsDataView, this.previousPlainTextDataView, previousDataViewGeneratedAtUtc.Value, fromCache: true));
#else
return Task.FromResult(new CollectionResponse(this.previousOpenMetricsDataView, this.previousPlainTextDataView, previousDataViewGeneratedAtUtc.Value, fromCache: true));
Expand All @@ -74,7 +74,7 @@ public Task<CollectionResponse> EnterCollect(bool openMetricsRequested)

Interlocked.Increment(ref this.readerCount);
this.ExitGlobalLock();
#if NET6_0_OR_GREATER
#if NET
return new ValueTask<CollectionResponse>(this.collectionTcs.Task);
#else
return this.collectionTcs.Task;
Expand Down Expand Up @@ -134,7 +134,7 @@ public Task<CollectionResponse> EnterCollect(bool openMetricsRequested)

this.ExitGlobalLock();

#if NET6_0_OR_GREATER
#if NET
return new ValueTask<CollectionResponse>(response);
#else
return Task.FromResult(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static int WriteDouble(byte[] buffer, int cursor, double value)
{
if (MathHelper.IsFinite(value))
{
#if NET6_0_OR_GREATER
#if NET
Span<char> span = stackalloc char[128];

var result = value.TryFormat(span, out var cchWritten, "G", CultureInfo.InvariantCulture);
Expand Down Expand Up @@ -62,7 +62,7 @@ public static int WriteDouble(byte[] buffer, int cursor, double value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int WriteLong(byte[] buffer, int cursor, long value)
{
#if NET6_0_OR_GREATER
#if NET
Span<char> span = stackalloc char[20];

var result = value.TryFormat(span, out var cchWritten, "G", CultureInfo.InvariantCulture);
Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override ExportResult Export(in Batch<Activity> batch)
Content = new JsonContent(this, batch),
};

#if NET6_0_OR_GREATER
#if NET
using var response = this.httpClient.Send(request, CancellationToken.None);
#else
using var response = this.httpClient.SendAsync(request, CancellationToken.None).GetAwaiter().GetResult();
Expand Down Expand Up @@ -190,7 +190,7 @@ public JsonContent(ZipkinExporter exporter, in Batch<Activity> batch)
this.Headers.ContentType = JsonHeader;
}

#if NET6_0_OR_GREATER
#if NET
protected override void SerializeToStream(Stream stream, TransportContext context, CancellationToken cancellationToken)
{
this.SerializeToStreamInternal(stream);
Expand Down
12 changes: 6 additions & 6 deletions src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics;
#if NET6_0_OR_GREATER
#if NET
using System.Diagnostics.CodeAnalysis;
#endif
using System.Diagnostics.Tracing;
Expand Down Expand Up @@ -215,7 +215,7 @@ public void NoDroppedExportProcessorItems(string exportProcessorName, string exp
this.WriteEvent(31, exportProcessorName, exporterName);
}

#if NET6_0_OR_GREATER
#if NET
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Parameters to this method are primitive and are trimmer safe.")]
#endif
[Event(32, Message = "'{0}' exporting to '{1}' dropped '{2}' item(s) due to buffer full.", Level = EventLevel.Warning)]
Expand All @@ -224,7 +224,7 @@ public void ExistsDroppedExportProcessorItems(string exportProcessorName, string
this.WriteEvent(32, exportProcessorName, exporterName, droppedCount);
}

#if NET6_0_OR_GREATER
#if NET
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Parameters to this method are primitive and are trimmer safe.")]
#endif
[Event(33, Message = "Measurements from Instrument '{0}', Meter '{1}' will be ignored. Reason: '{2}'. Suggested action: '{3}'", Level = EventLevel.Warning)]
Expand Down Expand Up @@ -257,7 +257,7 @@ public void ProviderDisposed(string providerName)
this.WriteEvent(37, providerName);
}

#if NET6_0_OR_GREATER
#if NET
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Parameters to this method are primitive and are trimmer safe.")]
#endif
[Event(38, Message = "Duplicate Instrument '{0}', Meter '{1}' encountered. Reason: '{2}'. Suggested action: '{3}'", Level = EventLevel.Warning)]
Expand All @@ -278,7 +278,7 @@ public void MetricReaderEvent(string message)
this.WriteEvent(40, message);
}

#if NET6_0_OR_GREATER
#if NET
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Parameters to this method are primitive and are trimmer safe.")]
#endif
[Event(41, Message = "View Configuration ignored for Instrument '{0}', Meter '{1}'. Reason: '{2}'. Measurements from the instrument will use default configuration for Aggregation. Suggested action: '{3}'", Level = EventLevel.Warning)]
Expand All @@ -287,7 +287,7 @@ public void MetricViewIgnored(string instrumentName, string meterName, string re
this.WriteEvent(41, instrumentName, meterName, reason, fix);
}

#if NET6_0_OR_GREATER
#if NET
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Parameters to this method are primitive and are trimmer safe.")]
#endif
[Event(43, Message = "ForceFlush invoked for processor type '{0}' returned result '{1}'.", Level = EventLevel.Verbose)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#if NET6_0_OR_GREATER
#if NET
using System.Diagnostics.CodeAnalysis;
#endif
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -96,7 +96,7 @@ public static LoggerProviderBuilder AddProcessor(this LoggerProviderBuilder logg
/// <param name="loggerProviderBuilder"><see cref="LoggerProviderBuilder"/>.</param>
/// <returns>The supplied <see cref="LoggerProviderBuilder"/> for chaining.</returns>
public static LoggerProviderBuilder AddProcessor<
#if NET6_0_OR_GREATER
#if NET
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
T>(this LoggerProviderBuilder loggerProviderBuilder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics;
#if NET6_0_OR_GREATER
#if NET
using System.Diagnostics.CodeAnalysis;
#endif
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -242,7 +242,7 @@ private static ILoggingBuilder AddOpenTelemetryInternal(
// and then there should be a way to do this without any warnings.
// The correctness of these suppressions is verified by a test which validates that all properties of OpenTelemetryLoggerOptions
// are of a primitive type.
#if NET6_0_OR_GREATER
#if NET
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "OpenTelemetryLoggerOptions contains only primitive properties.")]
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "OpenTelemetryLoggerOptions contains only primitive properties.")]
#endif
Expand Down
Loading

0 comments on commit 6d467d7

Please sign in to comment.