Skip to content

Commit 54fc8f3

Browse files
AguirreMoyKielek
andauthored
Add host.arch attribute to HostDetector and update tests (#3147)
Co-authored-by: Piotr Kiełkowicz <[email protected]>
1 parent 1b1f8b3 commit 54fc8f3

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

src/OpenTelemetry.Resources.Host/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
* Updated OpenTelemetry core component version(s) to `1.13.0`.
66
([#3158](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3158))
77

8+
* Added support for the `host.arch` resource attribute in `HostDetector`
9+
for .NET only.
10+
([#3147](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3147))
11+
812
## 1.12.0-beta.1
913

1014
Released 2025-May-06

src/OpenTelemetry.Resources.Host/HostDetector.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,29 @@ internal HostDetector(
7676
this.getWindowsMachineId = getWindowsMachineId;
7777
}
7878

79+
#if !NETFRAMEWORK
80+
public static string? MapArchitectureToOtel(Architecture arch)
81+
{
82+
return arch switch
83+
{
84+
Architecture.X86 => "x86",
85+
Architecture.X64 => "x64",
86+
Architecture.Arm => "arm32",
87+
Architecture.Arm64 => "arm64",
88+
#if NET
89+
Architecture.S390x => "s390x",
90+
Architecture.Armv6 => "arm32",
91+
Architecture.Ppc64le => "ppc64",
92+
93+
// following architectures do not have a mapping in OTEL spec https://github.com/open-telemetry/semantic-conventions/blob/v1.37.0/docs/resource/host.md
94+
Architecture.Wasm => null,
95+
Architecture.LoongArch64 => null,
96+
#endif
97+
_ => null,
98+
};
99+
}
100+
#endif
101+
79102
/// <summary>
80103
/// Detects the resource attributes from host.
81104
/// </summary>
@@ -84,17 +107,29 @@ public Resource Detect()
84107
{
85108
try
86109
{
87-
var attributes = new List<KeyValuePair<string, object>>(2)
110+
var attributes = new List<KeyValuePair<string, object>>(3)
88111
{
89112
new(HostSemanticConventions.AttributeHostName, Environment.MachineName),
90113
};
114+
91115
var machineId = this.GetMachineId();
92116

93117
if (machineId != null && !string.IsNullOrEmpty(machineId))
94118
{
95119
attributes.Add(new(HostSemanticConventions.AttributeHostId, machineId));
96120
}
97121

122+
#if !NETFRAMEWORK
123+
var arch = MapArchitectureToOtel(RuntimeInformation.OSArchitecture);
124+
if (arch != null)
125+
{
126+
attributes.Add(new(HostSemanticConventions.AttributeHostArch, arch));
127+
}
128+
#endif
129+
#if NET471_OR_GREATER
130+
#error Architecture is available in .NET Framework 4.7.1+, enable it when we move to that as minimum supported version
131+
#endif
132+
98133
return new Resource(attributes);
99134
}
100135
catch (InvalidOperationException ex)

src/OpenTelemetry.Resources.Host/HostSemanticConventions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ internal static class HostSemanticConventions
77
{
88
public const string AttributeHostName = "host.name";
99
public const string AttributeHostId = "host.id";
10+
public const string AttributeHostArch = "host.arch";
1011
}

src/OpenTelemetry.Resources.Host/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ using var loggerFactory = LoggerFactory.Create(builder =>
5454
The resource detectors will record the following metadata based on where
5555
your application is running:
5656

57-
- **HostDetector**: `host.id` (when running on non-containerized systems), `host.name`.
57+
- **HostDetector**:
58+
- `host.arch` (supported only on .NET),
59+
- `host.id` (when running on non-containerized systems),
60+
- `host.name`.
5861

5962
## References
6063

test/OpenTelemetry.Resources.Host.Tests/HostDetectorTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,20 @@ public void TestHostAttributes()
5151

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

54+
#if NET
55+
Assert.Equal(3, resourceAttributes.Count);
56+
#else
5457
Assert.Equal(2, resourceAttributes.Count);
58+
#endif
5559

5660
Assert.NotEmpty(resourceAttributes[HostSemanticConventions.AttributeHostName]);
5761
Assert.NotEmpty(resourceAttributes[HostSemanticConventions.AttributeHostId]);
62+
#if NET
63+
Assert.NotEmpty(resourceAttributes["host.arch"]);
64+
#pragma warning disable CA1308 // Normalize strings to uppercase
65+
Assert.Equal(RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant(), resourceAttributes["host.arch"]);
66+
#pragma warning restore CA1308 // Normalize strings to uppercase
67+
#endif
5868
}
5969

6070
#if NET

0 commit comments

Comments
 (0)