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

Simplify platform check. #4853

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public static IServiceCollection AddResourceMonitoring(
return services.AddResourceMonitoringInternal(configure);
}

// can't easily test the exception throwing case
[ExcludeFromCodeCoverage]
private static IServiceCollection AddResourceMonitoringInternal(
this IServiceCollection services,
Action<IResourceMonitorBuilder> configure)
Expand All @@ -62,35 +64,25 @@ private static IServiceCollection AddResourceMonitoringInternal(
#if NETFRAMEWORK
_ = builder.AddWindowsProvider();
#else
if (GetPlatform() == PlatformID.Win32NT)
if (OperatingSystem.IsWindows())
Copy link
Contributor

@xakep139 xakep139 Jan 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case we need to check whether else clause falls under IsLinux() - and throw an exception otherwise

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

{
_ = builder.AddWindowsProvider();
}
else
else if (OperatingSystem.IsLinux())
{
_ = builder.AddLinuxProvider();
}
else
{
throw new PlatformNotSupportedException();
}
#endif

configure.Invoke(builder);

return services;
}

#if !NETFRAMEWORK
[ExcludeFromCodeCoverage]
private static PlatformID GetPlatform()
{
var os = Environment.OSVersion;
if (os.Platform != PlatformID.Win32NT && os.Platform != PlatformID.Unix)
{
throw new NotSupportedException("Resource monitoring is not supported on this operating system.");
}

return os.Platform;
}
#endif

private static ResourceMonitorBuilder AddWindowsProvider(this ResourceMonitorBuilder builder)
{
builder.PickWindowsSnapshotProvider();
Expand Down