Skip to content

Commit

Permalink
Updater pre-processor directives
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejgordon committed Nov 25, 2024
1 parent 116b88c commit 53ec26a
Show file tree
Hide file tree
Showing 16 changed files with 24 additions and 27 deletions.
2 changes: 1 addition & 1 deletion sample/ApiSamples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public static void SampleSpanWithCustomContextFillAll()
// ReSharper restore ArrangeMethodOrOperatorBody
#pragma warning restore IDE0022

#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
/// <summary>
/// Test for https://github.com/elastic/apm-agent-dotnet/issues/884
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Elastic.Apm/Filters/CookieHeaderRedactionFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Elastic.Apm.Config;
using Elastic.Apm.Helpers;
using Elastic.Apm.Model;
#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
using System.Buffers;
#endif

Expand Down Expand Up @@ -45,7 +45,7 @@ internal static void HandleCookieHeader(Dictionary<string, string> headers, IRea
// e.g. Cookies | cookies | COOKIES
const int maxKeys = 4;

#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
var matchedKeys = ArrayPool<string>.Shared.Rent(maxKeys);
var matchedValues = ArrayPool<string>.Shared.Rent(maxKeys);
#else
Expand Down Expand Up @@ -81,7 +81,7 @@ internal static void HandleCookieHeader(Dictionary<string, string> headers, IRea
}
}

#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
ArrayPool<string>.Shared.Return(matchedKeys);
ArrayPool<string>.Shared.Return(matchedValues);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static Task<int> ReadAsync(this TextReader reader, char[] buffer, int ind
public static bool IsCompletedSucessfully(this Task task)
{
// IsCompletedSuccessfully is the faster method, but only currently exposed on .NET Core 2.0
#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
return task.IsCompletedSuccessfully;
#else
return task.Status == TaskStatus.RanToCompletion;
Expand Down
2 changes: 1 addition & 1 deletion src/Elastic.Apm/Logging/IApmLoggingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static LogValuesFormatter GetOrAddFormatter(string message, IReadOnlyCol
return formatter;

formatter = new LogValuesFormatter(message, args);
#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
Formatters.AddOrUpdate(message, formatter);
return formatter;
#else
Expand Down
2 changes: 1 addition & 1 deletion src/Elastic.Apm/Logging/ScopedLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal LogValuesFormatter GetOrAddFormatter(string message, IReadOnlyCollectio
return formatter;

formatter = new LogValuesFormatter($"{{{{{{Scope}}}}}} {message}", args, Scope);
#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
Formatters.AddOrUpdate(message, formatter);
return formatter;
#else
Expand Down
6 changes: 3 additions & 3 deletions src/Elastic.Apm/Metrics/Linux/GlobalMemoryStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Runtime.InteropServices;
using Elastic.Apm.Logging;

#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
using System.Buffers;
using System.Buffers.Text;
#endif
Expand All @@ -19,7 +19,7 @@ internal static class GlobalMemoryStatus
{
public const string ProcMemInfo = "/proc/meminfo";

#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
private static readonly FileStreamOptions Options = new() { BufferSize = 0, Mode = FileMode.Open, Access = FileAccess.Read };
private static readonly byte Space = (byte)' ';

Expand Down Expand Up @@ -57,7 +57,7 @@ internal static (long totalMemory, long availableMemory) GetTotalAndAvailableSys
}
try
{
#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
using var fs = new FileStream(memInfoPath, Options);
var buffer = ArrayPool<byte>.Shared.Rent(8192); // Should easily be large enough for max meminfo file.

Expand Down
10 changes: 5 additions & 5 deletions src/Elastic.Apm/Metrics/MetricsProvider/CgroupMetricsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using Elastic.Apm.Helpers;
using Elastic.Apm.Logging;

#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
using System.Buffers.Text;
using System.Runtime.CompilerServices;
using System.Text;
Expand Down Expand Up @@ -46,7 +46,7 @@ internal class CgroupMetricsProvider : IMetricsProvider
internal static readonly Regex Cgroup2MountPoint = new("^\\d+? \\d+? .+? .+? (.*?) .*cgroup2.*cgroup.*");
internal static readonly Regex MemoryCgroup = new("^\\d+:memory:.*");

#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
private static readonly FileStreamOptions Options = new() { BufferSize = 0, Mode = FileMode.Open, Access = FileAccess.Read };
#endif

Expand Down Expand Up @@ -316,7 +316,7 @@ private MetricSample GetMemoryMemLimitBytes()
return new MetricSample(SystemProcessCgroupMemoryMemLimitBytes, totalMemory);
}

#if NET6_0_OR_GREATER // Optimised code for newer runtimes
#if NET8_0_OR_GREATER // Optimised code for newer runtimes
return GetLongValueFromFile(_cGroupFiles.MaxMemoryFile, SystemProcessCgroupMemoryMemLimitBytes);
#else
using var reader = new StreamReader(_cGroupFiles.MaxMemoryFile);
Expand All @@ -339,7 +339,7 @@ private MetricSample GetMemoryMemUsageBytes()
{
try
{
#if NET6_0_OR_GREATER // Optimised code for newer runtimes
#if NET8_0_OR_GREATER // Optimised code for newer runtimes
return GetLongValueFromFile(_cGroupFiles.UsedMemoryFile, SystemProcessCgroupMemoryMemUsageBytes);
#else
using var reader = new StreamReader(_cGroupFiles.UsedMemoryFile);
Expand All @@ -356,7 +356,7 @@ private MetricSample GetMemoryMemUsageBytes()
return null;
}

#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private MetricSample GetLongValueFromFile(string path, string sampleName)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Elastic.Apm/OpenTelemetry/ElasticActivityListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private void CreateSpanForActivity(Activity activity, long timestamp, List<SpanL

// By default we set unknown outcome
transaction.Outcome = Outcome.Unknown;
#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
switch (activity.Status)
{
case ActivityStatusCode.Unset:
Expand Down Expand Up @@ -254,7 +254,7 @@ private static void UpdateSpan(Activity activity, Span span)

// By default we set unknown outcome
span.Outcome = Outcome.Unknown;
#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
switch (activity.Status)
{
case ActivityStatusCode.Unset:
Expand Down
2 changes: 1 addition & 1 deletion src/Elastic.Apm/Report/PayloadSenderV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ private void ProcessQueueItems(object[] queueItems)
var message = "Unknown 400 Bad Request";
if (response?.Content != null)
{
#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
var intakeResponse = _payloadItemSerializer.Deserialize<IntakeResponse>(response.Content.ReadAsStream());
#else
var intakeResponse = _payloadItemSerializer.Deserialize<IntakeResponse>(response.Content.ReadAsStreamAsync().GetAwaiter().GetResult());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ params IDiagnosticsSubscriber[] subscribers
}

private static string GetEnvironmentName(this IServiceProvider serviceProvider) =>
#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
(serviceProvider.GetService(typeof(IWebHostEnvironment)) as IWebHostEnvironment)?.EnvironmentName;
#else
#pragma warning disable CS0246
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static IServiceCollection AddElasticApm(this IServiceCollection services,
}

private static string GetDefaultEnvironmentName(IServiceProvider serviceProvider) =>
#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
(serviceProvider.GetService(typeof(IHostEnvironment)) as IHostEnvironment)?.EnvironmentName; // This is preferred since 3.0
#else
#pragma warning disable CS0246
Expand Down
2 changes: 1 addition & 1 deletion test/Elastic.Apm.Tests/LoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void EnsureFormattersAreShared()
var scopedLogger = consoleLogger.Scoped("MyTestScope");
for (var i = 0; i < 10; i++)
scopedLogger.Warning()?.Log("This is a test log from the test StructuredLogTemplateWith1MissingArgument, args: {arg1}", i);
#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
var cachedFormatterCount = scopedLogger.Formatters.Count();
cachedFormatterCount.Should().Be(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ public async Task HomeSimplePageTransactionTest()

var aspNetCoreVersion = Assembly.Load("Microsoft.AspNetCore").GetName().Version.ToString();
agent.Service.Framework.Version.Should().Be(aspNetCoreVersion);
#if NET6_0
agent.Service.Runtime.Name.Should().Be(Runtime.DotNetName + " 6");
#elif NET8_0
#if NET8_0
agent.Service.Runtime.Name.Should().Be(Runtime.DotNetName + " 8");
#else
agent.Service.Runtime.Name.Should().Be(Runtime.DotNetCoreName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>

#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
using System;
using System.Threading;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void NonPublicStructCopyTest()
Assert.Equal(ValuesDuckType.Third.ToString(), ((IGetValue)copy).GetValueProp);
}

#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
[Fact]
public void NonPublicStructInterfaceProxyTest()
{
Expand Down
1 change: 0 additions & 1 deletion test/startuphook/Elastic.Apm.StartupHook.Sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
This sample application is a default ASP.NET (Core) application
configured to run with

- `net6.0`
- `net8.0`

target frameworks that can be used to try out the [Elastic APM
Expand Down

0 comments on commit 53ec26a

Please sign in to comment.