Skip to content

Commit 9f16b7f

Browse files
[Resources.Host] Remove unused code on netfx (#3847)
1 parent 900114d commit 9f16b7f

2 files changed

Lines changed: 26 additions & 22 deletions

File tree

src/OpenTelemetry.Resources.Host/HostDetector.cs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ namespace OpenTelemetry.Resources.Host;
1616
/// </summary>
1717
internal sealed class HostDetector : IResourceDetector
1818
{
19+
#if !NETFRAMEWORK
1920
private const string ETCMACHINEID = "/etc/machine-id";
2021
private const string ETCVARDBUSMACHINEID = "/var/lib/dbus/machine-id";
21-
#if !NETFRAMEWORK
2222
private readonly Func<OSPlatform, bool> isOsPlatform;
23-
#endif
2423
private readonly Func<IEnumerable<string>> getFilePaths;
2524
private readonly Func<string?> getMacOsMachineId;
25+
#endif
2626
private readonly Func<string?> getWindowsMachineId;
2727

2828
/// <summary>
@@ -32,9 +32,9 @@ public HostDetector()
3232
: this(
3333
#if !NETFRAMEWORK
3434
RuntimeInformation.IsOSPlatform,
35-
#endif
3635
GetFilePaths,
3736
GetMachineIdMacOs,
37+
#endif
3838
GetMachineIdWindows)
3939
{
4040
}
@@ -56,30 +56,29 @@ public HostDetector(
5656
internal HostDetector(
5757
#if !NETFRAMEWORK
5858
Func<OSPlatform, bool> isOsPlatform,
59-
#endif
6059
Func<IEnumerable<string>> getFilePaths,
6160
Func<string?> getMacOsMachineId,
61+
#endif
6262
Func<string?> getWindowsMachineId)
6363
{
6464
#if !NETFRAMEWORK
6565
Guard.ThrowIfNull(isOsPlatform);
66-
#endif
6766
Guard.ThrowIfNull(getFilePaths);
6867
Guard.ThrowIfNull(getMacOsMachineId);
68+
#endif
6969
Guard.ThrowIfNull(getWindowsMachineId);
7070

7171
#if !NETFRAMEWORK
7272
this.isOsPlatform = isOsPlatform;
73-
#endif
7473
this.getFilePaths = getFilePaths;
7574
this.getMacOsMachineId = getMacOsMachineId;
75+
#endif
7676
this.getWindowsMachineId = getWindowsMachineId;
7777
}
7878

7979
#if !NETFRAMEWORK
80-
public static string? MapArchitectureToOtel(Architecture arch)
81-
{
82-
return arch switch
80+
public static string? MapArchitectureToOtel(Architecture arch) =>
81+
arch switch
8382
{
8483
Architecture.X86 => "x86",
8584
Architecture.X64 => "amd64",
@@ -90,13 +89,15 @@ internal HostDetector(
9089
Architecture.Armv6 => "arm32",
9190
Architecture.Ppc64le => "ppc64",
9291

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
92+
// The following architectures do not have a mapping in OTel spec: https://github.com/open-telemetry/semantic-conventions/blob/v1.39.0/docs/resource/host.md
9493
Architecture.Wasm => null,
9594
Architecture.LoongArch64 => null,
95+
#if NET10_0_OR_GREATER
96+
Architecture.RiscV64 => null,
97+
#endif
9698
#endif
9799
_ => null,
98100
};
99-
}
100101
#endif
101102

102103
/// <summary>
@@ -134,13 +135,14 @@ public Resource Detect()
134135
}
135136
catch (InvalidOperationException ex)
136137
{
137-
// Handling InvalidOperationException due to https://learn.microsoft.com/en-us/dotnet/api/system.environment.machinename#exceptions
138+
// Handling InvalidOperationException due to https://learn.microsoft.com/dotnet/api/system.environment.machinename#exceptions
138139
HostResourceEventSource.Log.ResourceAttributesExtractException(nameof(HostDetector), ex);
139140
}
140141

141142
return Resource.Empty;
142143
}
143144

145+
#if !NETFRAMEWORK
144146
internal static string? ParseMacOsOutput(string? output)
145147
{
146148
if (output == null || string.IsNullOrEmpty(output))
@@ -175,6 +177,7 @@ private static IEnumerable<string> GetFilePaths()
175177
yield return ETCMACHINEID;
176178
yield return ETCVARDBUSMACHINEID;
177179
}
180+
#endif
178181

179182
private static string? GetMachineIdMacOs()
180183
{
@@ -245,18 +248,16 @@ private static IEnumerable<string> GetFilePaths()
245248
}
246249
#pragma warning restore CA1416
247250

248-
private string? GetMachineId()
249-
{
251+
private string? GetMachineId() =>
250252
#if NETFRAMEWORK
251-
return this.getWindowsMachineId();
253+
this.getWindowsMachineId();
252254
#else
253-
return this.isOsPlatform(OSPlatform.Windows) ? this.getWindowsMachineId() :
254-
this.isOsPlatform(OSPlatform.Linux) ? this.GetMachineIdLinux() :
255-
this.isOsPlatform(OSPlatform.OSX) ? ParseMacOsOutput(this.getMacOsMachineId()) : null;
256-
255+
this.isOsPlatform(OSPlatform.Windows) ? this.getWindowsMachineId() :
256+
this.isOsPlatform(OSPlatform.Linux) ? this.GetMachineIdLinux() :
257+
this.isOsPlatform(OSPlatform.OSX) ? ParseMacOsOutput(this.getMacOsMachineId()) : null;
257258
#endif
258-
}
259259

260+
#if !NETFRAMEWORK
260261
private string? GetMachineIdLinux()
261262
{
262263
var paths = this.getFilePaths();
@@ -278,4 +279,5 @@ private static IEnumerable<string> GetFilePaths()
278279

279280
return null;
280281
}
282+
#endif
281283
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace OpenTelemetry.Resources.Host.Tests;
1010

1111
public class HostDetectorTests
1212
{
13+
#if !NETFRAMEWORK
1314
private const string MacOSMachineIdOutput = @"+-o J293AP <class IOPlatformExpertDevice, id 0x100000227, registered, matched,$
1415
{
1516
""IOPolledInterface"" = ""AppleARMWatchdogTimerHibernateHandler is not seria$
@@ -38,6 +39,7 @@ public class HostDetectorTests
3839
""#size-cells"" = <02000000>
3940
""IOPlatformUUID"" = ""1AB2345C-03E4-57D4-A375-1234D48DE123""
4041
}";
42+
#endif
4143

4244
#if NET
4345
private static readonly IEnumerable<string> ETCMACHINEID = ["Samples/etc_machineid"];
@@ -118,22 +120,22 @@ public void TestHostMachineIdMacOs()
118120
Assert.NotEmpty(resourceAttributes[HostSemanticConventions.AttributeHostId]);
119121
Assert.Equal("1AB2345C-03E4-57D4-A375-1234D48DE123", resourceAttributes[HostSemanticConventions.AttributeHostId]);
120122
}
121-
#endif
122123

123124
[Fact]
124125
public void TestParseMacOsOutput()
125126
{
126127
var id = HostDetector.ParseMacOsOutput(MacOSMachineIdOutput);
127128
Assert.Equal("1AB2345C-03E4-57D4-A375-1234D48DE123", id);
128129
}
130+
#endif
129131

130132
[Fact]
131133
public void TestHostMachineIdWindows()
132134
{
133135
#if NET
134136
var detector = new HostDetector(osPlatform => osPlatform == OSPlatform.Windows, () => [], () => throw new Exception("should not be called"), () => "windows-machine-id");
135137
#else
136-
var detector = new HostDetector(() => [], () => throw new Exception("should not be called"), () => "windows-machine-id");
138+
var detector = new HostDetector(() => "windows-machine-id");
137139
#endif
138140

139141
var resource = ResourceBuilder.CreateEmpty().AddDetector(detector).Build();

0 commit comments

Comments
 (0)