-
Notifications
You must be signed in to change notification settings - Fork 341
upgrade(tracer): Support libdatadog's library_config module #5126
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2bcf68f
upgrade(tracer): Support libdatadog's library_config module
BaptisteFoy 2a75923
address review
BaptisteFoy 10eafd6
temp
BaptisteFoy 15712e0
improve perf
BaptisteFoy 4d11fb3
fix: properly define paths
BaptisteFoy 2531a7a
Merge branch 'master' into baptiste.foy/FA/library-config-libdatadog
BaptisteFoy 8cdfe11
right macos managed path
BaptisteFoy a5082de
Merge branch 'baptiste.foy/FA/library-config-libdatadog' of github.co…
BaptisteFoy 4e83b16
address review
BaptisteFoy d683435
split into two files
BaptisteFoy fb84a38
add tests
BaptisteFoy 71d478e
fix typo
BaptisteFoy 7de0d2a
address review
BaptisteFoy 5beaa76
Merge branch 'master' into baptiste.foy/FA/library-config-libdatadog
BaptisteFoy c17602a
address review
BaptisteFoy 1cbf8a5
don't load config_stable.js on serverless
BaptisteFoy daed725
fix lint
BaptisteFoy a2afd18
Merge branch 'master' into baptiste.foy/FA/library-config-libdatadog
BaptisteFoy e30985d
Merge branch 'master' into baptiste.foy/FA/library-config-libdatadog
BaptisteFoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,100 @@ | ||
const os = require('os') | ||
const fs = require('fs') | ||
|
||
class StableConfig { | ||
constructor () { | ||
this.warnings = [] // Logger hasn't been initialized yet, so we can't use log.warn | ||
this.localEntries = {} | ||
this.fleetEntries = {} | ||
this.wasm_loaded = false | ||
|
||
const { localConfigPath, fleetConfigPath } = this._getStableConfigPaths() | ||
if (!fs.existsSync(localConfigPath) && !fs.existsSync(fleetConfigPath)) { | ||
// Bail out early if files don't exist to avoid unnecessary library loading | ||
return | ||
} | ||
|
||
const localConfig = this._readConfigFromPath(localConfigPath) | ||
const fleetConfig = this._readConfigFromPath(fleetConfigPath) | ||
if (!localConfig && !fleetConfig) { | ||
// Bail out early if files are empty or we can't read them to avoid unnecessary library loading | ||
return | ||
} | ||
|
||
// Note: we don't enforce loading because there may be cases where the library is not available and we | ||
// want to avoid breaking the application. In those cases, we will not have the file-based configuration. | ||
let libdatadog | ||
try { | ||
libdatadog = require('@datadog/libdatadog') | ||
this.wasm_loaded = true | ||
} catch (e) { | ||
this.warnings.push('Can\'t load libdatadog library') | ||
return | ||
} | ||
|
||
const libconfig = libdatadog.maybeLoad('library_config') | ||
if (libconfig === undefined) { | ||
this.warnings.push('Can\'t load library_config library') | ||
return | ||
} | ||
|
||
try { | ||
const configurator = new libconfig.JsConfigurator() | ||
configurator.set_envp(Object.entries(process.env).map(([key, value]) => `${key}=${value}`)) | ||
configurator.set_args(process.argv) | ||
configurator.get_configuration(localConfig.toString(), fleetConfig.toString()).forEach((entry) => { | ||
if (entry.source === 'local_stable_config') { | ||
this.localEntries[entry.name] = entry.value | ||
} else if (entry.source === 'fleet_stable_config') { | ||
this.fleetEntries[entry.name] = entry.value | ||
} | ||
}) | ||
} catch (e) { | ||
this.warnings.push(`Error parsing configuration from file: ${e.message}`) | ||
} | ||
} | ||
|
||
_readConfigFromPath (path) { | ||
try { | ||
return fs.readFileSync(path, 'utf8') | ||
} catch (err) { | ||
if (err.code !== 'ENOENT') { | ||
this.warnings.push(`Error reading config file at ${path}. ${err.code}: ${err.message}`) | ||
} | ||
return '' // Always return a string to avoid undefined.toString() errors | ||
} | ||
} | ||
|
||
_getStableConfigPaths () { | ||
let localConfigPath = '' | ||
let fleetConfigPath = '' | ||
switch (os.type().toLowerCase()) { | ||
case 'linux': | ||
localConfigPath = '/etc/datadog-agent/application_monitoring.yaml' | ||
fleetConfigPath = '/etc/datadog-agent/managed/datadog-agent/stable/application_monitoring.yaml' | ||
break | ||
case 'darwin': | ||
localConfigPath = '/opt/datadog-agent/etc/application_monitoring.yaml' | ||
fleetConfigPath = '/opt/datadog-agent/etc/managed/datadog-agent/stable/application_monitoring.yaml' | ||
break | ||
case 'win32': | ||
localConfigPath = 'C:\\ProgramData\\Datadog\\application_monitoring.yaml' | ||
fleetConfigPath = 'C:\\ProgramData\\Datadog\\managed\\datadog-agent\\stable\\application_monitoring.yaml' | ||
szegedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
break | ||
default: | ||
break | ||
} | ||
|
||
// Allow overriding the paths for testing | ||
if (process.env.DD_TEST_LOCAL_CONFIG_PATH !== undefined) { | ||
localConfigPath = process.env.DD_TEST_LOCAL_CONFIG_PATH | ||
} | ||
if (process.env.DD_TEST_FLEET_CONFIG_PATH !== undefined) { | ||
fleetConfigPath = process.env.DD_TEST_FLEET_CONFIG_PATH | ||
} | ||
|
||
return { localConfigPath, fleetConfigPath } | ||
} | ||
} | ||
|
||
module.exports = StableConfig |
This file contains hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.