This repository has been archived by the owner on Aug 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: metadata.system.{configured_hostname,detected_hostname}, prefe…
…r FQDN Refs: elastic/apm-agent-nodejs#3310
- Loading branch information
Showing
8 changed files
with
121 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict' | ||
|
||
// Detect the current hostname, preferring the FQDN if possible. | ||
// Spec: https://github.com/elastic/apm/blob/main/specs/agents/metadata.md#hostname | ||
|
||
const os = require('os') | ||
const { spawnSync } = require('child_process') | ||
|
||
/** | ||
* *Synchronously* detect the current hostname, preferring the FQDN. | ||
* This is sent to APM server as `metadata.system.detected_hostname` | ||
* and is intended to fit the ECS `host.name` value | ||
* (https://www.elastic.co/guide/en/ecs/current/ecs-host.html#field-host-name). | ||
* | ||
* @returns {String} | ||
*/ | ||
function detectHostname () { | ||
let hostname = null | ||
let out | ||
const fallback = os.hostname() | ||
|
||
switch (os.platform()) { | ||
case 'win32': | ||
// https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/hostname | ||
// XXX want windowsHide:true for v8.8.0 and later? | ||
out = spawnSync('hostname', {encoding: 'utf8', shell: true, timeout: 1000}) | ||
if (out.error) { | ||
if ('COMPUTERNAME' in process.env) { | ||
hostname = process.env['COMPUTERNAME'].trim() | ||
} | ||
} else { | ||
hostname = out.stdout.trim() | ||
} | ||
const domain = process.env['USERDNSDOMAIN'] | ||
if (hostname && 'USERDNSDOMAIN' in process.env) { | ||
hostname += '.' + process.env['USERDNSDOMAIN'].trim() | ||
} | ||
break | ||
|
||
default: | ||
out = spawnSync('/bin/hostname', ['-f'], {encoding: 'utf8', shell: false, timeout: 500}) | ||
if (!out.error) { | ||
hostname = out.stdout.trim() | ||
} | ||
// I'm going a little off of the APM spec here by *not* falling back to | ||
// HOSTNAME or HOST envvars. Latest discussion point is here: | ||
// https://github.com/elastic/apm/pull/517#issuecomment-940973458 | ||
// My understanding is HOSTNAME is a *Bash*-set envvar. | ||
break | ||
} | ||
|
||
if (!hostname) { | ||
hostname = fallback | ||
} | ||
hostname = hostname.trim().toLowerCase() | ||
return hostname | ||
} | ||
|
||
module.exports = { | ||
detectHostname | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters