-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Resources.OperatingSystem] initial implementation (#1943)
Co-authored-by: Piotr Kiełkowicz <[email protected]>
- Loading branch information
1 parent
7d3c03f
commit 8e66920
Showing
19 changed files
with
267 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/OpenTelemetry.Resources.OperatingSystem/.publicApi/PublicAPI.Shipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#nullable enable |
2 changes: 2 additions & 0 deletions
2
src/OpenTelemetry.Resources.OperatingSystem/.publicApi/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
src/OpenTelemetry.Resources.OperatingSystem/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
25 changes: 25 additions & 0 deletions
25
src/OpenTelemetry.Resources.OperatingSystem/OpenTelemetry.Resources.OperatingSystem.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
52 changes: 52 additions & 0 deletions
52
src/OpenTelemetry.Resources.OperatingSystem/OperatingSystemDetector.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/OpenTelemetry.Resources.OperatingSystem/OperatingSystemResourceBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/OpenTelemetry.Resources.OperatingSystem/OperatingSystemSemanticConventions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...etry.Resources.OperatingSystem.Tests/OpenTelemetry.Resources.OperatingSystem.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
30 changes: 30 additions & 0 deletions
30
test/OpenTelemetry.Resources.OperatingSystem.Tests/OperatingSystemDetectorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |