Skip to content

Commit

Permalink
internal constructor and delegates for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-hensley committed Mar 29, 2024
1 parent e4d00a5 commit 32a6842
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 34 deletions.
97 changes: 63 additions & 34 deletions src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;

Check warning on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / lint-dotnet-format / run-dotnet-format

Using directive is unnecessary.

Check warning on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / lint-dotnet-format / run-dotnet-format

Using directive is unnecessary.

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (windows-latest, net462)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (windows-latest, net462)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (windows-latest, net462)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (windows-latest, net462)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (windows-latest, net6.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (windows-latest, net6.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (windows-latest, net6.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (windows-latest, net6.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (windows-latest, net7.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (windows-latest, net7.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (windows-latest, net7.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (windows-latest, net7.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (windows-latest, net8.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (windows-latest, net8.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (windows-latest, net8.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (windows-latest, net8.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (ubuntu-latest, net6.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (ubuntu-latest, net6.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (ubuntu-latest, net7.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (ubuntu-latest, net7.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (ubuntu-latest, net8.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 8 in src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs

View workflow job for this annotation

GitHub Actions / build-test-host / build-test (ubuntu-latest, net8.0)

Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)
using System.Text;
using Microsoft.Win32;
using OpenTelemetry.Resources;
Expand All @@ -16,6 +17,34 @@ namespace OpenTelemetry.ResourceDetectors.Host;
/// </summary>
public sealed class HostDetector : IResourceDetector
{
private readonly PlatformID platformId;
private readonly Func<string> getMacOsMachineId;
private readonly Func<string> getWindowsMachineId;

/// <summary>
/// Initializes a new instance of the <see cref="HostDetector"/> class.
/// </summary>
public HostDetector()
: this(
Environment.OSVersion.Platform,
GetMachineIdMacOs,
GetMachineIdWindows)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="HostDetector"/> class for testing.
/// </summary>
/// <param name="platformId">Target platform ID.</param>
/// <param name="getMacOsMachineId">Function to get MacOS machine ID.</param>
/// <param name="getWindowsMachineId">Function to get Windows machine ID.</param>
internal HostDetector(PlatformID platformId, Func<string> getMacOsMachineId, Func<string> getWindowsMachineId)
{
this.platformId = platformId;
this.getMacOsMachineId = getMacOsMachineId ?? throw new ArgumentNullException(nameof(getMacOsMachineId));
this.getWindowsMachineId = getWindowsMachineId ?? throw new ArgumentNullException(nameof(getWindowsMachineId));
}

/// <summary>
/// Detects the resource attributes from host.
/// </summary>
Expand All @@ -27,7 +56,7 @@ public Resource Detect()
return new Resource(new List<KeyValuePair<string, object>>(1)
{
new(HostSemanticConventions.AttributeHostName, Environment.MachineName),
new(HostSemanticConventions.AttributeHostId, GetMachineId()),
new(HostSemanticConventions.AttributeHostId, this.GetMachineId()),
});
}
catch (InvalidOperationException ex)
Expand All @@ -39,39 +68,6 @@ public Resource Detect()
return Resource.Empty;
}

private static string GetMachineId()
{
return Environment.OSVersion.Platform switch
{
PlatformID.Unix => GetMachineIdLinux(),
PlatformID.MacOSX => GetMachineIdMacOs(),
PlatformID.Win32NT => GetMachineIdWindows(),
_ => string.Empty,
};
}

private static string GetMachineIdLinux()
{
var paths = new[] { "/etc/machine-id", "/var/lib/dbus/machine-id" };

foreach (var path in paths)
{
if (File.Exists(path))
{
try
{
return File.ReadAllText(path).Trim();
}
catch (Exception ex)
{
HostResourceEventSource.Log.ResourceAttributesExtractException(nameof(HostDetector), ex);
}
}
}

return string.Empty;
}

private static string GetMachineIdMacOs()
{
var startInfo = new ProcessStartInfo
Expand All @@ -98,4 +94,37 @@ private static string GetMachineIdWindows()
return Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Cryptography", false)?.GetValue("MachineGuid") as string ?? string.Empty;
}
#pragma warning restore CA1416

private string GetMachineId()
{
return this.platformId switch
{
PlatformID.Unix => this.GetMachineIdLinux(),
PlatformID.MacOSX => this.getMacOsMachineId(),
PlatformID.Win32NT => this.getWindowsMachineId(),
_ => string.Empty,
};
}

private string GetMachineIdLinux()
{
var paths = new[] { "/etc/machine-id", "/var/lib/dbus/machine-id" };

foreach (var path in paths)
{
if (File.Exists(path))
{
try
{
return File.ReadAllText(path).Trim();
}
catch (Exception ex)
{
HostResourceEventSource.Log.ResourceAttributesExtractException(nameof(HostDetector), ex);
}
}
}

return string.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System;
using System.IO;
using System.Linq;
using System.Text;
using OpenTelemetry.Resources;
using Xunit;

Expand All @@ -21,4 +24,42 @@ public void TestHostAttributes()
Assert.NotEmpty(resourceAttributes[HostSemanticConventions.AttributeHostName]);
Assert.NotEmpty(resourceAttributes[HostSemanticConventions.AttributeHostId]);
}

[Fact]
public void TestHostMachineId()
{
var etcMachineIdStream = (string path) =>
{
return path == "/etc/machine-id"
? new MemoryStream(Encoding.UTF8.GetBytes("etc-machine-id"))
: null;
};
var varLibDbusMachineIdStream = new MemoryStream(Encoding.UTF8.GetBytes("var-lib-dbus-machine-id"));
}

[Fact]
public void TestHostMachineIdMacOs()
{
var detector = new HostDetector(
PlatformID.MacOSX,
() => "macos-machine-id",
() => throw new Exception("should not be called"));
var resource = ResourceBuilder.CreateEmpty().AddDetector(detector).Build();
var resourceAttributes = resource.Attributes.ToDictionary(x => x.Key, x => (string)x.Value);
Assert.NotEmpty(resourceAttributes[HostSemanticConventions.AttributeHostId]);
Assert.Equal("macos-machine-id", resourceAttributes[HostSemanticConventions.AttributeHostId]);
}

[Fact]
public void TestHostMachineIdWindows()
{
var detector = new HostDetector(
PlatformID.Win32NT,
() => throw new Exception("should not be called"),
() => "windows-machine-id");
var resource = ResourceBuilder.CreateEmpty().AddDetector(detector).Build();
var resourceAttributes = resource.Attributes.ToDictionary(x => x.Key, x => (string)x.Value);
Assert.NotEmpty(resourceAttributes[HostSemanticConventions.AttributeHostId]);
Assert.Equal("windows-machine-id", resourceAttributes[HostSemanticConventions.AttributeHostId]);
}
}

0 comments on commit 32a6842

Please sign in to comment.