Skip to content

Commit

Permalink
[Resources.OperatingSystem] Fix issue with detecting MacOS (#1965)
Browse files Browse the repository at this point in the history
  • Loading branch information
ysolomchenko authored Jul 22, 2024
1 parent b4d9029 commit 269a3b8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/Component.BuildTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ jobs:
exclude:
- os: ubuntu-latest
version: net462
- os: macos-latest
version: net462

runs-on: ${{ matrix.os }}

Expand Down Expand Up @@ -73,6 +75,11 @@ jobs:
- name: Setup dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
6.0.x
7.0.x
8.0.x
- name: dotnet restore ${{ steps.resolve-project.outputs.title }}
run: dotnet restore ${{ steps.resolve-project.outputs.project }} -p:EnablePackageValidation=true
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ jobs:
with:
project-name: Component[OpenTelemetry.Resources.OperatingSystem]
code-cov-name: Resources.OperatingSystem
os-list: '[ "windows-latest", "ubuntu-latest", "macos-latest" ]'

build-test-resources-process:
needs: detect-changes
Expand Down
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Resources.OperatingSystem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Fix detection of macOS which was wrongly identified as Linux.
([#1965](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1965))

## 0.1.0-alpha.1

Released 2024-Jul-11
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#if !NETFRAMEWORK
using System.Runtime.InteropServices;
#endif
using static OpenTelemetry.Resources.OperatingSystem.OperatingSystemSemanticConventions;

namespace OpenTelemetry.Resources.OperatingSystem;
Expand Down Expand Up @@ -31,22 +34,25 @@ public Resource Detect()

private static string? GetOSType()
{
var platform = Environment.OSVersion.Platform;
if (platform == PlatformID.Win32NT)
#if NETFRAMEWORK
return OperatingSystemsValues.Windows;
#else
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return OperatingSystemsValues.Windows;
}

if (platform == PlatformID.MacOSX)
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return OperatingSystemsValues.Linux;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return OperatingSystemsValues.Darwin;
}

if (platform == PlatformID.Unix)
else
{
return OperatingSystemsValues.Linux;
return null;
}

return null;
#endif
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Runtime.InteropServices;
using Xunit;

namespace OpenTelemetry.Resources.OperatingSystem.Test;
Expand All @@ -14,17 +15,29 @@ public void TestOperatingSystemAttributes()

var resourceAttributes = resource.Attributes.ToDictionary(x => x.Key, x => (string)x.Value);

var operatingSystems = new[]
string expectedPlatform;

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
expectedPlatform = OperatingSystemSemanticConventions.OperatingSystemsValues.Windows;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
expectedPlatform = OperatingSystemSemanticConventions.OperatingSystemsValues.Linux;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
expectedPlatform = OperatingSystemSemanticConventions.OperatingSystemsValues.Darwin;
}
else
{
OperatingSystemSemanticConventions.OperatingSystemsValues.Windows,
OperatingSystemSemanticConventions.OperatingSystemsValues.Linux,
OperatingSystemSemanticConventions.OperatingSystemsValues.Darwin,
};
throw new PlatformNotSupportedException("Unknown platform");
}

Assert.Single(resourceAttributes);

Assert.True(resourceAttributes.ContainsKey(OperatingSystemSemanticConventions.AttributeOperatingSystemType));

Assert.Contains(resourceAttributes[OperatingSystemSemanticConventions.AttributeOperatingSystemType], operatingSystems);
Assert.Equal(resourceAttributes[OperatingSystemSemanticConventions.AttributeOperatingSystemType], expectedPlatform);
}
}

0 comments on commit 269a3b8

Please sign in to comment.