Skip to content

Commit

Permalink
[repo] Simplify preprocessor directives (#1924)
Browse files Browse the repository at this point in the history
  • Loading branch information
joegoldman2 authored Jun 25, 2024
1 parent ea06116 commit e3a90d7
Show file tree
Hide file tree
Showing 56 changed files with 149 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ConnectionStringBuilder(string connectionString)
continue;
}

#if NET6_0_OR_GREATER
#if NET
var index = token.IndexOf(EqualSign, StringComparison.Ordinal);
#else
var index = token.IndexOf(EqualSign);
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 System.Diagnostics.Tracing;
Expand All @@ -26,7 +26,7 @@ private MetricEtwDataTransport()
}

[NonEvent]
#if NET6_0_OR_GREATER
#if NET
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "WriteEventCore is safe when eventData object is a primitive type, which it is in this case.")]
#endif
public unsafe void Send(MetricEventType eventType, byte[] data, int size)
Expand All @@ -41,7 +41,7 @@ public unsafe void Send(MetricEventType eventType, byte[] data, int size)
}

[NonEvent]
#if NET6_0_OR_GREATER
#if NET
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "WriteEventCore is safe when eventData object is a primitive type, which it is in this case.")]
#endif
public unsafe void SendOtlpProtobufEvent(byte[] data, int size)
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.Buffers.Binary;
#endif
using System.Globalization;
Expand Down Expand Up @@ -50,7 +50,7 @@ internal static class MessagePackSerializer
private const int LIMIT_MAX_FIX_ARRAY_LENGTH = 15;
private const int STRING_SIZE_LIMIT_CHAR_COUNT = (1 << 14) - 1; // 16 * 1024 - 1 = 16383

#if NET6_0_OR_GREATER
#if NET
private const int MAX_STACK_ALLOC_SIZE_IN_BYTES = 256;
#endif

Expand Down Expand Up @@ -187,7 +187,7 @@ public static int SerializeUInt64(byte[] buffer, int cursor, ulong value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int WriteInt16(byte[] buffer, int cursor, short value)
{
#if NET6_0_OR_GREATER
#if NET
BinaryPrimitives.WriteInt16BigEndian(buffer.AsSpan(cursor), value);
return cursor + sizeof(short);
#else
Expand All @@ -204,7 +204,7 @@ public static int WriteInt16(byte[] buffer, int cursor, short value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int WriteInt32(byte[] buffer, int cursor, int value)
{
#if NET6_0_OR_GREATER
#if NET
BinaryPrimitives.WriteInt32BigEndian(buffer.AsSpan(cursor), value);
return cursor + sizeof(int);
#else
Expand All @@ -223,7 +223,7 @@ public static int WriteInt32(byte[] buffer, int cursor, int value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int WriteInt64(byte[] buffer, int cursor, long value)
{
#if NET6_0_OR_GREATER
#if NET
BinaryPrimitives.WriteInt64BigEndian(buffer.AsSpan(cursor), value);
return cursor + sizeof(long);
#else
Expand All @@ -246,7 +246,7 @@ public static int WriteInt64(byte[] buffer, int cursor, long value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int WriteUInt16(byte[] buffer, int cursor, ushort value)
{
#if NET6_0_OR_GREATER
#if NET
BinaryPrimitives.WriteUInt16BigEndian(buffer.AsSpan(cursor), value);
return cursor + sizeof(ushort);
#else
Expand All @@ -263,7 +263,7 @@ public static int WriteUInt16(byte[] buffer, int cursor, ushort value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int WriteUInt32(byte[] buffer, int cursor, uint value)
{
#if NET6_0_OR_GREATER
#if NET
BinaryPrimitives.WriteUInt32BigEndian(buffer.AsSpan(cursor), value);
return cursor + sizeof(uint);
#else
Expand All @@ -282,7 +282,7 @@ public static int WriteUInt32(byte[] buffer, int cursor, uint value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int WriteUInt64(byte[] buffer, int cursor, ulong value)
{
#if NET6_0_OR_GREATER
#if NET
BinaryPrimitives.WriteUInt64BigEndian(buffer.AsSpan(cursor), value);
return cursor + sizeof(ulong);
#else
Expand Down Expand Up @@ -410,7 +410,7 @@ public static int SerializeAsciiString(byte[] buffer, int cursor, string value)
return cursor;
}

#if NET6_0_OR_GREATER
#if NET

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int SerializeUnicodeString(byte[] buffer, int cursor, ReadOnlySpan<char> value)
Expand Down Expand Up @@ -636,7 +636,7 @@ public static int Serialize(byte[] buffer, int cursor, object obj)
case DateTimeOffset v:
return SerializeUtcDateTime(buffer, cursor, v.UtcDateTime);

#if NET6_0_OR_GREATER
#if NET
case ISpanFormattable v:
Span<char> tmp = stackalloc char[MAX_STACK_ALLOC_SIZE_IN_BYTES / sizeof(char)];
if (v.TryFormat(tmp, out int charsWritten, string.Empty, CultureInfo.InvariantCulture))
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 System.Diagnostics.Tracing;
Expand All @@ -26,7 +26,7 @@ public void InformationalEvent()
}

[NonEvent]
#if NET6_0_OR_GREATER
#if NET
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "WriteEventCore is safe when eventData object is a primitive type, which it is in this case.")]
#endif
public unsafe void SendEvent(int eventId, byte[] data, int size)
Expand Down
10 changes: 5 additions & 5 deletions src/OpenTelemetry.Exporter.Geneva/TLDExporter/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal static class JsonSerializer
private const byte ASCII_CARRIAGE_RETURN = 0x0D;
private const byte ASCII_HORIZONTAL_TAB = 0x09;

#if NET6_0_OR_GREATER
#if NET
private const int MAX_STACK_ALLOC_SIZE_IN_BYTES = 256;
#endif

Expand Down Expand Up @@ -88,7 +88,7 @@ public static int SerializeString(byte[] buffer, int cursor, string value)
return cursor;
}

#if NET6_0_OR_GREATER
#if NET
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int SerializeString(byte[] buffer, int cursor, ReadOnlySpan<char> value)
{
Expand Down Expand Up @@ -250,7 +250,7 @@ public static int Serialize(byte[] buffer, int cursor, object obj)
{
case bool v:
return WriteString(buffer, cursor, v ? "true" : "false");
#if NET6_0_OR_GREATER
#if NET
case byte:
case sbyte:
case short:
Expand Down Expand Up @@ -317,7 +317,7 @@ public static int Serialize(byte[] buffer, int cursor, object obj)
case object[] v:
return SerializeArray(buffer, cursor, v);

#if NET6_0_OR_GREATER
#if NET
case ISpanFormattable v:
tmp = stackalloc char[MAX_STACK_ALLOC_SIZE_IN_BYTES / sizeof(char)];
if (v.TryFormat(tmp, out charsWritten, default, CultureInfo.InvariantCulture))
Expand Down Expand Up @@ -410,7 +410,7 @@ private static int WriteString(byte[] buffer, int cursor, string value)
return cursor;
}

#if NET6_0_OR_GREATER
#if NET
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int WriteString(byte[] buffer, int cursor, ReadOnlySpan<char> value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ConnectionStringParser(string connectionString)
continue;
}

#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#if NET || NETSTANDARD2_1_OR_GREATER
var index = token.IndexOf(EqualSign, StringComparison.Ordinal);
#else
var index = token.IndexOf(EqualSign);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public EventNameManager(string defaultEventNamespace, string defaultEventName)

this.defaultEventFullName = BuildEventFullName(defaultEventNamespace, defaultEventName)!;

#if NET6_0_OR_GREATER
#if NET
Debug.Assert(this.defaultEventFullName != null, "this.defaultFullyQualifiedEventName was null");
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System.Collections;
using System.Diagnostics;
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
#if NETSTANDARD2_1_OR_GREATER || NET
using System.Diagnostics.CodeAnalysis;
#endif
using System.Text.Json;
Expand All @@ -21,7 +21,7 @@ internal sealed class ExtensionFieldInformationManager

public bool TryResolveExtensionFieldInformation(
string fullFieldName,
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
#if NETSTANDARD2_1_OR_GREATER || NET
[NotNullWhen(true)]
#endif
out ExtensionFieldInformation? resolvedFieldInformation)
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 System.Diagnostics.Tracing;
Expand Down Expand Up @@ -79,7 +79,7 @@ public void ExportExceptionThrown(string itemType, string exception)
}

[Event(2, Message = "Sent '{0}' batch of {1} item(s) to '{2}' transport.", Level = EventLevel.Informational)]
#if NET6_0_OR_GREATER
#if NET
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Parameters passed to WriteEvent are all primitive values.")]
#endif
public void TransportDataSent(string itemType, int numberOfRecords, string transportDescription)
Expand All @@ -88,7 +88,7 @@ public void TransportDataSent(string itemType, int numberOfRecords, string trans
}

[Event(3, Message = "Wrote '{0}' batch of {1} item(s) to '{2}' sink.", Level = EventLevel.Informational)]
#if NET6_0_OR_GREATER
#if NET
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Parameters passed to WriteEvent are all primitive values.")]
#endif
public void SinkDataWritten(string itemType, int numberOfRecords, string sinkDescription)
Expand All @@ -109,7 +109,7 @@ public void TransportExceptionThrown(string transportType, string exception)
}

[Event(6, Message = "Error response received by '{0}' transport. StatusCode: {1}, ErrorMessage: '{2}', ErrorDetails: '{3}'", Level = EventLevel.Error)]
#if NET6_0_OR_GREATER
#if NET
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Parameters passed to WriteEvent are all primitive values.")]
#endif
public void HttpTransportErrorResponseReceived(string transportType, int statusCode, string errorMessage, string errorDetails)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace OpenTelemetry.Exporter.OneCollector;

internal static class CommonSchemaJsonSerializationHelper
{
#if NET6_0_OR_GREATER
#if NET
public const int MaximumStackAllocSizeInBytes = 256;
#endif

Expand Down Expand Up @@ -209,7 +209,7 @@ private static void SerializeMapValueToJson(IEnumerable<KeyValuePair<string, obj

private static void SerializeObjectValueToJson(object value, Utf8JsonWriter writer)
{
#if NET6_0_OR_GREATER
#if NET
if (value is ISpanFormattable spanFormattable)
{
Span<char> destination = stackalloc char[MaximumStackAllocSizeInBytes / 2];
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.Runtime.InteropServices;
#endif
using System.Text.Json;
Expand Down Expand Up @@ -46,7 +46,7 @@ public void AddExtensionAttribute(KeyValuePair<string, object?> attribute)
Debug.Assert(fieldInformation?.FieldName != null, "fieldInformation.FieldName was null");
Debug.Assert(fieldInformation?.EncodedFieldName.EncodedUtf8Bytes.Length > 0, "fieldInformation.EncodedFieldName was empty");

#if NET6_0_OR_GREATER
#if NET
ref var lookupIndex = ref CollectionsMarshal.GetValueRefOrAddDefault(this.keys, fieldInformation.ExtensionName, out var existed);
if (!existed)
{
Expand Down Expand Up @@ -92,7 +92,7 @@ public void SerializeExtensionPropertiesToJson(bool writeExtensionObjectEnvelope
writer.WriteStartObject(CommonSchemaJsonSerializationHelper.ExtensionsProperty);
}

#if NET6_0_OR_GREATER
#if NET
var allValues = CollectionsMarshal.AsSpan(this.allValues);
#else
var allValues = this.allValues;
Expand All @@ -108,7 +108,7 @@ public void SerializeExtensionPropertiesToJson(bool writeExtensionObjectEnvelope
{
unsafe
{
#if NET6_0_OR_GREATER
#if NET
ref var attribute = ref allValues[keyLookup.ValueIndicies[i]];
#else
var attribute = allValues[keyLookup.ValueIndicies[i]];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ protected override bool TryComputeLength(out long length)
return true;
}

#if NET6_0_OR_GREATER
#if NET
protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellationToken)
{
this.stream.CopyTo(stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public HttpClientWrapper(HttpClient httpClient)

public HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
{
#if NET6_0_OR_GREATER
#if NET
return this.httpClient.Send(request, CancellationToken.None);
#else
return this.httpClient.SendAsync(request, CancellationToken.None).GetAwaiter().GetResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal virtual void Validate()
throw new OneCollectorExporterValidationException("Instrumentation key was not specified on connection string.");
}

#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#if NET || NETSTANDARD2_1_OR_GREATER
var positionOfFirstDash = this.InstrumentationKey.IndexOf('-', StringComparison.OrdinalIgnoreCase);
#else
var positionOfFirstDash = this.InstrumentationKey!.IndexOf('-');
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry.Extensions.AWS/AWSXRayIdGenerator.cs
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;
using System.Globalization;
using OpenTelemetry.Internal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static TracerProviderBuilder AddXRayTraceId(this TracerProviderBuilder bu
return builder;
}

#if NET6_0_OR_GREATER
#if NET
/// <summary>
/// Replace the trace id of root activity.
/// </summary>
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 @@ -25,7 +25,7 @@ public static class TraceEnrichmentProviderBuilderExtensions
/// <remarks>
/// Add this enricher *before* exporter related Activity processors.
/// </remarks>
#if NET6_0_OR_GREATER
#if NET
public static TracerProviderBuilder AddTraceEnricher<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(this TracerProviderBuilder builder)
#else
public static TracerProviderBuilder AddTraceEnricher<T>(this TracerProviderBuilder builder)
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.Extensions;
Expand All @@ -26,7 +26,7 @@ public static class TraceEnrichmentServiceCollectionExtensions
/// <remarks>
/// Add this enricher *before* exporter related Activity processors.
/// </remarks>
#if NET6_0_OR_GREATER
#if NET
public static IServiceCollection AddTraceEnricher<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(this IServiceCollection services)
#else
public static IServiceCollection AddTraceEnricher<T>(this IServiceCollection services)
Expand Down
Loading

0 comments on commit e3a90d7

Please sign in to comment.