Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: IsOsPlatform() fails on older .NET Framework Versions #1552

Merged
merged 5 commits into from Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -17,7 +17,6 @@
using NewRelic.SystemInterfaces;
using Newtonsoft.Json;
using NewRelic.Agent.Configuration;
using System.Runtime.InteropServices;

namespace NewRelic.Agent.Core
{
Expand Down Expand Up @@ -46,14 +45,16 @@ public Environment(ISystemInfo systemInfo, IProcessStatic processStatic, IConfig

AddVariable("OS", () => System.Environment.OSVersion?.VersionString);

#if NETSTANDARD2_0
// report linux distro name and version when appropriate
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
// This API is only supported on .net FX 4.7 + so limiting it
// to .net core since that is the one affected.
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
{
AddVariable("Linux Distro Name", () => RuntimeEnvironmentInfo.OperatingSystem);
AddVariable("Linux Distro Version", () => RuntimeEnvironmentInfo.OperatingSystemVersion);
}

#if NETSTANDARD2_0
// This API is only supported on .net FX 4.7 + so limiting it
// to .net core since that is the one affected.
AddVariable(".NET Version", () => System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.ToString());
Expand Down
Expand Up @@ -3,7 +3,6 @@

using System;
using System.IO;
using System.Runtime.InteropServices;

namespace NewRelic.Agent.Core
{
Expand Down Expand Up @@ -75,20 +74,22 @@ private static string GetOSVersion()

private static string GetFreeBSDVersion()
{
#if NETSTANDARD2_0
// This is same as sysctl kern.version
// FreeBSD 11.0-RELEASE-p1 FreeBSD 11.0-RELEASE-p1 #0 r306420: Thu Sep 29 01:43:23 UTC 2016 root@releng2.nyi.freebsd.org:/usr/obj/usr/src/sys/GENERIC
// What we want is major release as minor releases should be compatible.
String version = RuntimeInformation.OSDescription;
String version = System.Runtime.InteropServices.RuntimeInformation.OSDescription;
try
{
// second token up to first dot
return RuntimeInformation.OSDescription.Split()[1].Split('.')[0];
return System.Runtime.InteropServices.RuntimeInformation.OSDescription.Split()[1].Split('.')[0];
}
catch (Exception ex)
{
log4net.ILog logger = log4net.LogManager.GetLogger(typeof(RuntimeEnvironmentInfo));
logger.Debug($"Unable to report Operating System: Unexpected exception in GetFreeBSDVersion: {ex}");
}
#endif
return string.Empty;
}

Expand Down Expand Up @@ -185,24 +186,28 @@ private static DistroInfo NormalizeDistroInfo(DistroInfo distroInfo)

private static Platform DetermineOSPlatform()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
#if NETSTANDARD2_0
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
{
return Platform.Windows;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
{
return Platform.Linux;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX))
{
return Platform.Darwin;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD")))
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Create("FREEBSD")))
{
return Platform.FreeBSD;
}

return Platform.Unknown;
#else
return Platform.Windows;
#endif
}
}
}