Skip to content

Commit

Permalink
Send domain name when detected (#2067)
Browse files Browse the repository at this point in the history
* Initial reference implementation

* Expand log message with name of env var

* Ensure host name is returned

* Update GetHostName

* Remove special handing for linux local suffix(es)
  • Loading branch information
stevejgordon authored Jul 12, 2023
1 parent f4dfb45 commit 43479ee
Show file tree
Hide file tree
Showing 2 changed files with 269 additions and 223 deletions.
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

0 comments on commit 43479ee

Please sign in to comment.