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 (#200) This also adds testing of node v20 and testing on windows (just one node version). Refs: elastic/apm-agent-nodejs#3310
- Loading branch information
Showing
12 changed files
with
247 additions
and
60 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,77 @@ | ||
'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/dotnet/api/system.net.dns.gethostentry | ||
out = spawnSync( | ||
'powershell.exe', | ||
['[System.Net.Dns]::GetHostEntry($env:computerName).HostName'], | ||
{ encoding: 'utf8', shell: true, timeout: 2000 } | ||
) | ||
if (!out.error) { | ||
hostname = out.stdout.trim() | ||
break | ||
} | ||
|
||
// https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/hostname | ||
out = spawnSync( | ||
'hostname.exe', | ||
{ encoding: 'utf8', shell: true, timeout: 2000 } | ||
) | ||
if (!out.error) { | ||
hostname = out.stdout.trim() | ||
break | ||
} | ||
|
||
if ('COMPUTERNAME' in process.env) { | ||
hostname = process.env['COMPUTERNAME'].trim() // eslint-disable-line dot-notation | ||
} | ||
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 | ||
} | ||
|
||
// ---- main | ||
|
||
if (require.main === module) { | ||
console.log(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and other contributors where applicable. | ||
* Licensed under the BSD 2-Clause License; you may not use this file except in | ||
* compliance with the BSD 2-Clause License. | ||
*/ | ||
|
||
'use strict' | ||
|
||
// Run all "test/**/*.test.js" files, each in a separate process. | ||
|
||
var spawn = require('child_process').spawn | ||
|
||
var glob = require('glob') | ||
|
||
// ---- support functions | ||
|
||
// Run a single test file. | ||
function runTestFile (testFile, cb) { | ||
console.log(`# running test: node ${testFile}`) | ||
var ps = spawn('node', [testFile], { stdio: 'inherit' }) | ||
ps.on('error', cb) | ||
ps.on('close', function (code) { | ||
if (code !== 0) { | ||
const err = new Error('non-zero error code') | ||
err.code = 'ENONZERO' | ||
err.exitCode = code | ||
return cb(err) | ||
} | ||
cb() | ||
}) | ||
} | ||
|
||
function series (tasks, cb) { | ||
var results = [] | ||
var pos = 0 | ||
|
||
function done (err, result) { | ||
if (err) return cb(err) | ||
results.push(result) | ||
|
||
if (++pos === tasks.length) { | ||
cb(null, results) | ||
} else { | ||
tasks[pos](done) | ||
} | ||
} | ||
|
||
setImmediate(tasks[pos], done) | ||
} | ||
|
||
function handlerBind (handler) { | ||
return function (task) { | ||
return handler.bind(null, task) | ||
} | ||
} | ||
|
||
function mapSeries (tasks, handler, cb) { | ||
series(tasks.map(handlerBind(handler)), cb) | ||
} | ||
|
||
// ---- mainline | ||
|
||
function main () { | ||
var testFiles = glob.sync( | ||
// Find all ".test.js" files, except those in "fixtures" dirs and in | ||
// "node_modules" dirs created for test packages. | ||
'test/**/*.test.js', | ||
{ ignore: ['**/node_modules/**', '**/fixtures/**'] } | ||
) | ||
|
||
mapSeries(testFiles, runTestFile, function (err) { | ||
if (err) throw err | ||
}) | ||
} | ||
|
||
main() |
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
Oops, something went wrong.