Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[resource host+process] Allow host name and process owner attributes to be excluded #2084

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
OpenTelemetry.Resources.HostResourceBuilderExtensions
static OpenTelemetry.Resources.HostResourceBuilderExtensions.AddHostDetector(this OpenTelemetry.Resources.ResourceBuilder! builder) -> OpenTelemetry.Resources.ResourceBuilder!
static OpenTelemetry.Resources.HostResourceBuilderExtensions.AddHostDetector(this OpenTelemetry.Resources.ResourceBuilder! builder, bool includeHostName) -> OpenTelemetry.Resources.ResourceBuilder!
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Resources.Host/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Making the inclusion of `host.name` to the `Resource` configurable.
Use the `AddHostDetector(bool includeHostName)` overload to control this behavior.
([#2084](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2084))

## 0.1.0-beta.3

Released 2024-Aug-30
Expand Down
26 changes: 18 additions & 8 deletions src/OpenTelemetry.Resources.Host/HostDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,36 @@ internal sealed class HostDetector : IResourceDetector
private readonly Func<IEnumerable<string>> getFilePaths;
private readonly Func<string?> getMacOsMachineId;
private readonly Func<string?> getWindowsMachineId;
private readonly bool includeHostName;

/// <summary>
/// Initializes a new instance of the <see cref="HostDetector"/> class.
/// </summary>
public HostDetector()
/// <param name="includeHostName">Determines whether to include host name in the resource attributes.</param>
public HostDetector(bool includeHostName = true)
: this(
#if NET
RuntimeInformation.IsOSPlatform,
#endif
GetFilePaths,
GetMachineIdMacOs,
GetMachineIdWindows)
GetMachineIdWindows,
includeHostName)
{
}

#if NET
public HostDetector(
Func<IEnumerable<string>> getFilePaths,
Func<string?> getMacOsMachineId,
Func<string?> getWindowsMachineId)
Func<string?> getWindowsMachineId,
bool includeHostName = true)
: this(
RuntimeInformation.IsOSPlatform,
getFilePaths,
getMacOsMachineId,
getWindowsMachineId)
getWindowsMachineId,
includeHostName)
{
}
#endif
Expand All @@ -59,7 +64,8 @@ internal HostDetector(
#endif
Func<IEnumerable<string>> getFilePaths,
Func<string?> getMacOsMachineId,
Func<string?> getWindowsMachineId)
Func<string?> getWindowsMachineId,
bool includeHostName = true)
{
#if NET
Guard.ThrowIfNull(isOsPlatform);
Expand All @@ -74,6 +80,7 @@ internal HostDetector(
this.getFilePaths = getFilePaths;
this.getMacOsMachineId = getMacOsMachineId;
this.getWindowsMachineId = getWindowsMachineId;
this.includeHostName = includeHostName;
}

/// <summary>
Expand All @@ -84,10 +91,13 @@ public Resource Detect()
{
try
{
var attributes = new List<KeyValuePair<string, object>>(2)
var attributes = new List<KeyValuePair<string, object>>(2);

if (this.includeHostName)
{
new(HostSemanticConventions.AttributeHostName, Environment.MachineName),
};
attributes.Add(new(HostSemanticConventions.AttributeHostName, Environment.MachineName));
}

var machineId = this.GetMachineId();

if (machineId != null && !string.IsNullOrEmpty(machineId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,19 @@ public static class HostResourceBuilderExtensions
/// <param name="builder">The <see cref="ResourceBuilder"/> being configured.</param>
/// <returns>The instance of <see cref="ResourceBuilder"/> being configured.</returns>
public static ResourceBuilder AddHostDetector(this ResourceBuilder builder)
{
return AddHostDetector(builder, includeHostName: true);
}

/// <summary>
/// Enables host resource detector.
/// </summary>
/// <param name="builder">The <see cref="ResourceBuilder"/> being configured.</param>
/// <param name="includeHostName">Determines whether to include host name in the resource attributes.</param>
/// <returns>The instance of <see cref="ResourceBuilder"/> being configured.</returns>
public static ResourceBuilder AddHostDetector(this ResourceBuilder builder, bool includeHostName)
{
Guard.ThrowIfNull(builder);
return builder.AddDetector(new HostDetector());
return builder.AddDetector(new HostDetector(includeHostName));
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
OpenTelemetry.Resources.ProcessResourceBuilderExtensions
static OpenTelemetry.Resources.ProcessResourceBuilderExtensions.AddProcessDetector(this OpenTelemetry.Resources.ResourceBuilder! builder) -> OpenTelemetry.Resources.ResourceBuilder!
static OpenTelemetry.Resources.ProcessResourceBuilderExtensions.AddProcessDetector(this OpenTelemetry.Resources.ResourceBuilder! builder, bool includeProcessOwner) -> OpenTelemetry.Resources.ResourceBuilder!
5 changes: 5 additions & 0 deletions src/OpenTelemetry.Resources.Process/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

* Making the inclusion of `process.owner` to the `Resource` configurable.
Use the `AddProcessDetector(bool includeProcessOwner)` overload to control
this behavior.
([#2084](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2084))

## 0.1.0-beta.2

Released 2024-Jun-18
Expand Down
26 changes: 21 additions & 5 deletions src/OpenTelemetry.Resources.Process/ProcessDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,36 @@ namespace OpenTelemetry.Resources.Process;
/// </summary>
internal sealed class ProcessDetector : IResourceDetector
{
private readonly bool includeProcessOwner;

/// <summary>
/// Initializes a new instance of the <see cref="ProcessDetector"/> class.
/// </summary>
/// <param name="includeProcessOwner">Determines whether to include process owner in the resource attributes.</param>
public ProcessDetector(bool includeProcessOwner = true)
{
this.includeProcessOwner = includeProcessOwner;
}

/// <summary>
/// Detects the resource attributes for process.
/// </summary>
/// <returns>Resource with key-value pairs of resource attributes.</returns>
public Resource Detect()
{
return new Resource(new List<KeyValuePair<string, object>>(2)
var attributes = new List<KeyValuePair<string, object>>(2);

if (this.includeProcessOwner)
{
new(ProcessSemanticConventions.AttributeProcessOwner, Environment.UserName),
attributes.Add(new(ProcessSemanticConventions.AttributeProcessOwner, Environment.UserName));
}

#if NET
new(ProcessSemanticConventions.AttributeProcessPid, Environment.ProcessId),
attributes.Add(new(ProcessSemanticConventions.AttributeProcessPid, Environment.ProcessId));
#else
new(ProcessSemanticConventions.AttributeProcessPid, System.Diagnostics.Process.GetCurrentProcess().Id),
attributes.Add(new(ProcessSemanticConventions.AttributeProcessPid, System.Diagnostics.Process.GetCurrentProcess().Id));
#endif
});

return new Resource(attributes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,19 @@ public static class ProcessResourceBuilderExtensions
/// <param name="builder">The <see cref="ResourceBuilder"/> being configured.</param>
/// <returns>The instance of <see cref="ResourceBuilder"/> being configured.</returns>
public static ResourceBuilder AddProcessDetector(this ResourceBuilder builder)
{
return AddProcessDetector(builder, includeProcessOwner: true);
}

/// <summary>
/// Enables process resource detector.
/// </summary>
/// <param name="builder">The <see cref="ResourceBuilder"/> being configured.</param>
/// <param name="includeProcessOwner">A flag indicating whether or not to include the process owner in the resource.</param>
/// <returns>The instance of <see cref="ResourceBuilder"/> being configured.</returns>
public static ResourceBuilder AddProcessDetector(this ResourceBuilder builder, bool includeProcessOwner)
{
Guard.ThrowIfNull(builder);
return builder.AddDetector(new ProcessDetector());
return builder.AddDetector(new ProcessDetector(includeProcessOwner));
}
}
12 changes: 12 additions & 0 deletions test/OpenTelemetry.Resources.Host.Tests/HostDetectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ public void TestHostAttributes()
Assert.NotEmpty(resourceAttributes[HostSemanticConventions.AttributeHostId]);
}

[Fact]
public void TestHostAttributesWithoutHostName()
{
var resource = ResourceBuilder.CreateEmpty().AddHostDetector(includeHostName: false).Build();

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

Assert.Single(resourceAttributes);

Assert.NotEmpty(resourceAttributes[HostSemanticConventions.AttributeHostId]);
}

#if NET
[Fact]
public void TestHostMachineIdLinux()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,17 @@ public void TestProcessAttributes()
Assert.IsType<string>(resourceAttributes[ProcessSemanticConventions.AttributeProcessOwner]);
Assert.IsType<long>(resourceAttributes[ProcessSemanticConventions.AttributeProcessPid]);
}

[Fact]
public void TestProcessAttributes_ExcludeProcessOwner()
{
var resource = ResourceBuilder.CreateEmpty().AddProcessDetector(includeProcessOwner: false).Build();

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

Assert.Single(resourceAttributes);

Assert.False(resourceAttributes.ContainsKey(ProcessSemanticConventions.AttributeProcessOwner));
Assert.IsType<long>(resourceAttributes[ProcessSemanticConventions.AttributeProcessPid]);
}
}
Loading