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

Send domain name when detected #2067

Merged
merged 5 commits into from
Jul 12, 2023
Merged
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
76 changes: 61 additions & 15 deletions src/Elastic.Apm/Helpers/SystemInfoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
// See the LICENSE file in the project root for more information

using System;
using System.Data.Common;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;
using Elastic.Apm.Api;
using Elastic.Apm.Api.Kubernetes;
Expand Down Expand Up @@ -97,32 +100,75 @@ internal Api.System GetSystemInfo(string hostName)

internal string GetHostName()
{
var fqdn = string.Empty;

try
{
return Environment.MachineName;
fqdn = Dns.GetHostEntry(string.Empty).HostName;
}
catch (Exception e)
{
_logger.Warning()?.LogException(e, "Failed to get hostname via Dns.GetHostName - revert to environment variables");
_logger.Warning()?.LogException(e, "Failed to get hostname via Dns.GetHostEntry(string.Empty).HostName.");
}

try
{
// try environment variables
var host = (Environment.GetEnvironmentVariable("COMPUTERNAME")
?? Environment.GetEnvironmentVariable("HOSTNAME"))
?? Environment.GetEnvironmentVariable("HOST");

if (host == null)
_logger.Error()?.Log("Failed to get hostname via environment variables.");
return host;
}
catch (Exception exception)
if (!string.IsNullOrEmpty(fqdn))
return NormalizeHostName(fqdn);

try
{
var hostName = IPGlobalProperties.GetIPGlobalProperties().HostName;
var domainName = IPGlobalProperties.GetIPGlobalProperties().DomainName;

if (!string.IsNullOrEmpty(domainName))
{
_logger.Error()?.LogException(exception, "Failed to get hostname.");
hostName = $"{hostName}.{domainName}";
}

fqdn = hostName;

}
catch (Exception e)
{
_logger.Warning()?.LogException(e, "Failed to get hostname via IPGlobalProperties.GetIPGlobalProperties().");
}

if (!string.IsNullOrEmpty(fqdn))
return NormalizeHostName(fqdn);

try
{
fqdn = Environment.MachineName;
}
catch (Exception e)
{
_logger.Warning()?.LogException(e, "Failed to get hostname via Environment.MachineName.");
}

if (!string.IsNullOrEmpty(fqdn))
return NormalizeHostName(fqdn);

_logger.Debug()?.Log("Falling back to environment variables to get hostname.");

try
{
fqdn = (Environment.GetEnvironmentVariable("COMPUTERNAME")
?? Environment.GetEnvironmentVariable("HOSTNAME"))
?? Environment.GetEnvironmentVariable("HOST");

if (string.IsNullOrEmpty(fqdn))
_logger.Error()?.Log("Failed to get hostname via environment variables.");

return NormalizeHostName(fqdn);
}
catch (Exception e)
{
_logger.Error()?.LogException(e, "Failed to get hostname.");
}

return null;

static string NormalizeHostName(string hostName) =>
string.IsNullOrEmpty(hostName) ? null : hostName.Trim().ToLower();
}

private void ParseContainerInfo(Api.System system, string reportedHostName)
Expand Down
Loading