Skip to content

Commit

Permalink
[Resources.OperatingSystem] initial implementation (#1943)
Browse files Browse the repository at this point in the history
Co-authored-by: Piotr Kiełkowicz <[email protected]>
  • Loading branch information
ysolomchenko and Kielek authored Jul 11, 2024
1 parent 7d3c03f commit 8e66920
Show file tree
Hide file tree
Showing 19 changed files with 267 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ body:
- OpenTelemetry.Resources.Container
- OpenTelemetry.Resources.Gcp
- OpenTelemetry.Resources.Host
- OpenTelemetry.Resources.OperatingSystem
- OpenTelemetry.Resources.Process
- OpenTelemetry.Resources.ProcessRuntime
- OpenTelemetry.Sampler.AWS
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/feature_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ body:
- OpenTelemetry.Resources.Container
- OpenTelemetry.Resources.Gcp
- OpenTelemetry.Resources.Host
- OpenTelemetry.Resources.OperatingSystem
- OpenTelemetry.Resources.Process
- OpenTelemetry.Resources.ProcessRuntime
- OpenTelemetry.Sampler.AWS
Expand Down
5 changes: 5 additions & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ flags:
paths:
- src/OpenTelemetry.Resources.Host

unittests-Resources.OperatingSystem:
carryforward: true
paths:
- src/OpenTelemetry.Resources.OperatingSystem

unittests-Resources.Process:
carryforward: true
paths:
Expand Down
3 changes: 3 additions & 0 deletions .github/component_owners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ components:
src/OpenTelemetry.Resources.Host/:
- Kielek
- lachmatt
src/OpenTelemetry.Resources.OperatingSystem/:
- Kielek
- lachmatt
src/OpenTelemetry.Resources.Process/:
- Kielek
- lachmatt
Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jobs:
resources-container: ['*/OpenTelemetry.Resources.Container*/**', '!**/*.md']
resources-gcp: ['*/OpenTelemetry.Resources.Gcp*/**', '!**/*.md']
resources-host: ['*/OpenTelemetry.Resources.Host*/**', '!**/*.md']
resources-operatingsystem: ['*/OpenTelemetry.Resources.OperatingSystem/**', '*/OpenTelemetry.Resources.OperatingSystem.Tests/**', '!**/*.md']
resources-process: ['*/OpenTelemetry.Resources.Process/**', '*/OpenTelemetry.Resources.Process.Tests/**', '!**/*.md']
resources-processruntime: ['*/OpenTelemetry.Resources.ProcessRuntime/**', '*/OpenTelemetry.Resources.ProcessRuntime.Tests/**', '!**/*.md']
sampler-aws: ['*/OpenTelemetry.Sampler.AWS**/**', '!**/*.md']
Expand Down Expand Up @@ -485,6 +486,17 @@ jobs:
project-name: Component[OpenTelemetry.Resources.Host]
code-cov-name: Resources.Host

build-test-resources-operatingsystem:
needs: detect-changes
if: |
contains(needs.detect-changes.outputs.changes, 'resources-operatingsystem')
|| contains(needs.detect-changes.outputs.changes, 'build')
|| contains(needs.detect-changes.outputs.changes, 'shared')
uses: ./.github/workflows/Component.BuildTest.yml
with:
project-name: Component[OpenTelemetry.Resources.OperatingSystem]
code-cov-name: Resources.OperatingSystem

build-test-resources-process:
needs: detect-changes
if: |
Expand Down Expand Up @@ -561,6 +573,7 @@ jobs:
|| contains(needs.detect-changes.outputs.changes, 'resources-azure')
|| contains(needs.detect-changes.outputs.changes, 'resources-container')
|| contains(needs.detect-changes.outputs.changes, 'resources-host')
|| contains(needs.detect-changes.outputs.changes, 'resources-operatingsystem')
|| contains(needs.detect-changes.outputs.changes, 'resources-process')
|| contains(needs.detect-changes.outputs.changes, 'resources-processruntime')
|| contains(needs.detect-changes.outputs.changes, 'sampler-aws')
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/prepare-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ on:
- OpenTelemetry.Resources.Container
- OpenTelemetry.Resources.Gcp
- OpenTelemetry.Resources.Host
- OpenTelemetry.Resources.OperatingSystem
- OpenTelemetry.Resources.Process
- OpenTelemetry.Resources.ProcessRuntime
- OpenTelemetry.Sampler.AWS
Expand Down
14 changes: 14 additions & 0 deletions opentelemetry-dotnet-contrib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Resources.Pro
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Resources.Process.Tests", "test\OpenTelemetry.Resources.Process.Tests\OpenTelemetry.Resources.Process.Tests.csproj", "{A5EF701C-439E-407F-8BB4-394166000C6D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Resources.OperatingSystem", "src\OpenTelemetry.Resources.OperatingSystem\OpenTelemetry.Resources.OperatingSystem.csproj", "{F8E9EC7E-85A1-4C97-9C05-7EA82C1513A1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Resources.OperatingSystem.Tests", "test\OpenTelemetry.Resources.OperatingSystem.Tests\OpenTelemetry.Resources.OperatingSystem.Tests.csproj", "{62B7060A-C35B-4D49-A0C2-3BF02880A200}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Resources.Host", "src\OpenTelemetry.Resources.Host\OpenTelemetry.Resources.Host.csproj", "{033CA8D4-1529-413A-B244-07958D5F9A48}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Resources.Host.Tests", "test\OpenTelemetry.Resources.Host.Tests\OpenTelemetry.Resources.Host.Tests.csproj", "{36271347-2055-438E-9659-B71542A17A73}"
Expand Down Expand Up @@ -803,6 +807,14 @@ Global
{9B994669-E839-4C42-A0F1-DF9DD058C1DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9B994669-E839-4C42-A0F1-DF9DD058C1DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9B994669-E839-4C42-A0F1-DF9DD058C1DC}.Release|Any CPU.Build.0 = Release|Any CPU
{F8E9EC7E-85A1-4C97-9C05-7EA82C1513A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F8E9EC7E-85A1-4C97-9C05-7EA82C1513A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F8E9EC7E-85A1-4C97-9C05-7EA82C1513A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F8E9EC7E-85A1-4C97-9C05-7EA82C1513A1}.Release|Any CPU.Build.0 = Release|Any CPU
{62B7060A-C35B-4D49-A0C2-3BF02880A200}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{62B7060A-C35B-4D49-A0C2-3BF02880A200}.Debug|Any CPU.Build.0 = Debug|Any CPU
{62B7060A-C35B-4D49-A0C2-3BF02880A200}.Release|Any CPU.ActiveCfg = Release|Any CPU
{62B7060A-C35B-4D49-A0C2-3BF02880A200}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -927,6 +939,8 @@ Global
{BE40900A-2859-471D-8802-21DFD73DDAA7} = {2097345F-4DD3-477D-BC54-A922F9B2B402}
{3A464E7A-42F3-44B0-B8D7-80521A7704A6} = {B75EE478-97F7-4E9F-9A5A-DB3D0988EDEA}
{9B994669-E839-4C42-A0F1-DF9DD058C1DC} = {3A464E7A-42F3-44B0-B8D7-80521A7704A6}
{F8E9EC7E-85A1-4C97-9C05-7EA82C1513A1} = {22DF5DC0-1290-4E83-A9D8-6BB7DE3B3E63}
{62B7060A-C35B-4D49-A0C2-3BF02880A200} = {2097345F-4DD3-477D-BC54-A922F9B2B402}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B0816796-CDB3-47D7-8C3C-946434DE3B66}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
OpenTelemetry.Resources.OperatingSystemResourceBuilderExtensions
static OpenTelemetry.Resources.OperatingSystemResourceBuilderExtensions.AddOperatingSystemDetector(this OpenTelemetry.Resources.ResourceBuilder! builder) -> OpenTelemetry.Resources.ResourceBuilder!
10 changes: 10 additions & 0 deletions src/OpenTelemetry.Resources.OperatingSystem/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Runtime.CompilerServices;

#if SIGNED
[assembly: InternalsVisibleTo("OpenTelemetry.Resources.OperatingSystem.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010051c1562a090fb0c9f391012a32198b5e5d9a60e9b80fa2d7b434c9e5ccb7259bd606e66f9660676afc6692b8cdc6793d190904551d2103b7b22fa636dcbb8208839785ba402ea08fc00c8f1500ccef28bbf599aa64ffb1e1d5dc1bf3420a3777badfe697856e9d52070a50c3ea5821c80bef17ca3acffa28f89dd413f096f898")]
#else
[assembly: InternalsVisibleTo("OpenTelemetry.Resources.OperatingSystem.Tests")]
#endif
8 changes: 8 additions & 0 deletions src/OpenTelemetry.Resources.OperatingSystem/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changelog

## Unreleased

* Initial release of
`OpenTelemetry.ResourceDetectors.OperatingSystem`
project.
([#1943](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1943))
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!-- OmniSharp/VS Code requires TargetFrameworks to be in descending order for IntelliSense and analysis. -->
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);$(NetFrameworkMinimumSupportedVersion)</TargetFrameworks>
<Description>OpenTelemetry Extensions - Operating System Resource Detector for .NET</Description>
<MinVerTagPrefix>Resources.OperatingSystem-</MinVerTagPrefix>
</PropertyGroup>

<!--Do not run Package Baseline Validation as this package has never released a stable version.
Remove this property once we have released a stable version and add PackageValidationBaselineVersion property.-->
<PropertyGroup>
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="$(OpenTelemetryCoreLatestVersion)" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)\src\Shared\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using static OpenTelemetry.Resources.OperatingSystem.OperatingSystemSemanticConventions;

namespace OpenTelemetry.Resources.OperatingSystem;

/// <summary>
/// Operating system detector.
/// </summary>
internal sealed class OperatingSystemDetector : IResourceDetector
{
/// <summary>
/// Detects the resource attributes from the operating system.
/// </summary>
/// <returns>Resource with key-value pairs of resource attributes.</returns>
public Resource Detect()
{
var osType = GetOSType();

if (osType == null)
{
return Resource.Empty;
}

return new Resource(
[
new(AttributeOperatingSystemType, osType),
]);
}

private static string? GetOSType()
{
var platform = Environment.OSVersion.Platform;
if (platform == PlatformID.Win32NT)
{
return OperatingSystemsValues.Windows;
}

if (platform == PlatformID.MacOSX)
{
return OperatingSystemsValues.Darwin;
}

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

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

using OpenTelemetry.Internal;
using OpenTelemetry.Resources.OperatingSystem;

namespace OpenTelemetry.Resources;

/// <summary>
/// Extension methods to simplify registering of operating system detectors.
/// </summary>
public static class OperatingSystemResourceBuilderExtensions
{
/// <summary>
/// Enables operating system detector.
/// </summary>
/// <param name="builder"><see cref="ResourceBuilder" /> being configured.</param>
/// <returns>The instance of <see cref="ResourceBuilder" /> being configured.</returns>
public static ResourceBuilder AddOperatingSystemDetector(this ResourceBuilder builder)
{
Guard.ThrowIfNull(builder);
return builder.AddDetector(new OperatingSystemDetector());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

namespace OpenTelemetry.Resources.OperatingSystem;

internal static class OperatingSystemSemanticConventions
{
public const string AttributeOperatingSystemType = "os.type";

public static class OperatingSystemsValues
{
public const string Windows = "windows";

public const string Linux = "linux";

public const string Darwin = "darwin";
}
}
44 changes: 44 additions & 0 deletions src/OpenTelemetry.Resources.OperatingSystem/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Operating System Detectors

[![NuGet version badge](https://img.shields.io/nuget/v/OpenTelemetry.Resources.OperatingSyatem)](https://www.nuget.org/packages/OpenTelemetry.Resources.OperatingSyatem)
[![NuGet download count badge](https://img.shields.io/nuget/dt/OpenTelemetry.Resources.OperatingSyatem)](https://www.nuget.org/packages/OpenTelemetry.Resources.OperatingSyatem)
[![codecov.io](https://codecov.io/gh/open-telemetry/opentelemetry-dotnet-contrib/branch/main/graphs/badge.svg?flag=unittests-Resources.OperatingSyatem)](https://app.codecov.io/gh/open-telemetry/opentelemetry-dotnet-contrib?flags[0]=unittests-Resources.OperatingSyatem)

> [!IMPORTANT]
> Resources detected by this packages are defined by [experimental semantic convention](https://github.com/open-telemetry/semantic-conventions/blob/v1.26.0/docs/resource/os.md).
> These resources can be changed without prior notification.
## Getting Started

You need to install the
`OpenTelemetry.Resources.OperatingSystem` package to be able to use the
Operating System Resource Detectors.

```shell
dotnet add package OpenTelemetry.Resources.OperatingSystem --prerelease
```

## Usage

You can configure Operating System resource detector to
the `TracerProvider` with the following example below.

```csharp
using OpenTelemetry;
using OpenTelemetry.Resources;

var tracerProvider = Sdk.CreateTracerProviderBuilder()
// other configurations
.ConfigureResource(resource => resource
.AddOperatingSystemDetector())
.Build();
```

The resource detectors will record the following metadata based on where
your application is running:

- **OperatingSystemDetector**: `os.type`.

## References

- [OpenTelemetry Project](https://opentelemetry.io/)
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<TrimmerRootAssembly Include="OpenTelemetry.Resources.Azure" />
<TrimmerRootAssembly Include="OpenTelemetry.Resources.Container" />
<TrimmerRootAssembly Include="OpenTelemetry.Resources.Host" />
<TrimmerRootAssembly Include="OpenTelemetry.Resources.OperatingSystem" />
<TrimmerRootAssembly Include="OpenTelemetry.Resources.Process" />
<TrimmerRootAssembly Include="OpenTelemetry.Resources.ProcessRuntime" />
<TrimmerRootAssembly Include="OpenTelemetry.Sampler.AWS" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>Unit test project for Operating System Detector for OpenTelemetry</Description>
<!-- OmniSharp/VS Code requires TargetFrameworks to be in descending order for IntelliSense and analysis. -->
<TargetFrameworks>$(SupportedNetTargets)</TargetFrameworks>
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);$(NetFrameworkMinimumSupportedVersion)</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Resources.OperatingSystem\OpenTelemetry.Resources.OperatingSystem.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using Xunit;

namespace OpenTelemetry.Resources.OperatingSystem.Test;

public class OperatingSystemDetectorTests
{
[Fact]
public void TestOperatingSystemAttributes()
{
var resource = ResourceBuilder.CreateEmpty().AddOperatingSystemDetector().Build();

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

var operatingSystems = new[]
{
OperatingSystemSemanticConventions.OperatingSystemsValues.Windows,
OperatingSystemSemanticConventions.OperatingSystemsValues.Linux,
OperatingSystemSemanticConventions.OperatingSystemsValues.Darwin,
};

Assert.Single(resourceAttributes);

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

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

0 comments on commit 8e66920

Please sign in to comment.