-
Notifications
You must be signed in to change notification settings - Fork 40
refactor(collector): refcatored config module #2457
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
aryamohanan
merged 8 commits into
fix-config-precedence
from
test-collector-config-refact
Apr 8, 2026
+90
−36
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4abf3fe
refactor(collector): refcatored config module
aryamohanan ce2c3f7
chore: refactor
aryamohanan d03173c
chore: updated
aryamohanan 9e60fbd
chore: updated
aryamohanan 96cecd3
chore: updated
aryamohanan d93823b
chore: fake logger utility
aryamohanan 2ada60c
chore: fake logger utility
aryamohanan a1ae584
chore: fake logger utility
aryamohanan 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,21 @@ | |
| * (c) Copyright Instana Inc. and contributors 2019 | ||
| */ | ||
|
|
||
| /* eslint-disable dot-notation */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const util = require('@instana/core/src/config/util'); | ||
|
|
||
| /** @type {import('@instana/core/src/core').GenericLogger} */ | ||
| let logger; | ||
|
aryamohanan marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * @param {import('@instana/core/src/config').InstanaConfig} config | ||
| */ | ||
| exports.init = function init(config) { | ||
| logger = config.logger; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High: The Prompt for Bob |
||
| util.init(logger); | ||
| }; | ||
|
|
||
| const defaults = { | ||
| agentHost: '127.0.0.1', | ||
| agentPort: 42699, | ||
|
|
@@ -16,38 +27,101 @@ const defaults = { | |
|
|
||
| /** | ||
| * Merges the config that was passed to the init function with environment variables and default values. | ||
| * @param {import('../types/collector').CollectorConfig} config | ||
| * @param {import('../types/collector').CollectorConfig} userConfig | ||
| * @returns {import('../types/collector').CollectorConfig} | ||
| */ | ||
| module.exports = function normalizeConfig(config = {}) { | ||
| config.agentHost = config.agentHost || process.env.INSTANA_AGENT_HOST || defaults.agentHost; | ||
| config.agentPort = config.agentPort || parseToPositiveInteger(process.env.INSTANA_AGENT_PORT, defaults.agentPort); | ||
| config.agentRequestTimeout = | ||
| config.agentRequestTimeout || | ||
| parseToPositiveInteger(process.env.INSTANA_AGENT_REQUEST_TIMEOUT, defaults.agentRequestTimeout); | ||
| function normalizeConfig(userConfig = {}) { | ||
| const finalConfig = {}; | ||
|
|
||
| config.autoProfile = config.autoProfile || process.env.INSTANA_AUTO_PROFILE || defaults.autoProfile; | ||
| config.tracing = config.tracing || {}; | ||
| // NOTE: This function only normalizes collector-specific configuration fields. | ||
| // Other userConfig fields (like serviceName, tracing, etc.) are passed through as-is | ||
| // and will be normalized later by core/config when this collector config is passed | ||
| // as extraFinalConfig to core's normalize function. | ||
| finalConfig.agentHost = normalizeAgentHost(userConfig, defaults); | ||
|
kirrg001 marked this conversation as resolved.
|
||
| finalConfig.agentPort = normalizeAgentPort(userConfig, defaults); | ||
| finalConfig.agentRequestTimeout = normalizeAgentRequestTimeout(userConfig, defaults); | ||
| finalConfig.autoProfile = normalizeAutoProfile(userConfig, defaults); | ||
| finalConfig.reportUnhandledPromiseRejections = normalizeUnhandledRejections(userConfig); | ||
| finalConfig.tracing = userConfig.tracing || {}; | ||
|
|
||
| if (config.reportUnhandledPromiseRejections == null) { | ||
| config.reportUnhandledPromiseRejections = false; | ||
| } | ||
| return finalConfig; | ||
| } | ||
|
|
||
| return config; | ||
| }; | ||
| // Export both init and normalizeConfig | ||
| // For backward compatibility, also make normalizeConfig the default export | ||
| module.exports = normalizeConfig; | ||
| module.exports.init = exports.init; | ||
| module.exports.normalizeConfig = normalizeConfig; | ||
|
aryamohanan marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * @param {import('../types/collector').CollectorConfig} userConfig | ||
| * @param {{ agentHost: string }} defaultConfig | ||
| * @returns {string} | ||
| */ | ||
| function normalizeAgentHost(userConfig, defaultConfig) { | ||
|
kirrg001 marked this conversation as resolved.
|
||
| return resolveConfig(process.env.INSTANA_AGENT_HOST, userConfig.agentHost, defaultConfig.agentHost); | ||
| } | ||
|
|
||
| /** | ||
| * @param {string | number} value | ||
| * @param {number} defaultValue | ||
| * @param {import('../types/collector').CollectorConfig} userConfig | ||
| * @param {{ agentPort: number }} defaultConfig | ||
| * @returns {number} | ||
| */ | ||
| function parseToPositiveInteger(value, defaultValue) { | ||
| if (typeof value !== 'string') { | ||
| return defaultValue; | ||
| function normalizeAgentPort(userConfig, defaultConfig) { | ||
| return util.resolveNumericConfig({ | ||
| envVar: 'INSTANA_AGENT_PORT', | ||
| configValue: userConfig.agentPort, | ||
| defaultValue: defaultConfig.agentPort, | ||
| configPath: 'config.agentPort' | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * @param {import('../types/collector').CollectorConfig} userConfig | ||
| * @param {{ agentRequestTimeout: number }} defaultConfig | ||
| * @returns {number} | ||
| */ | ||
| function normalizeAgentRequestTimeout(userConfig, defaultConfig) { | ||
| return util.resolveNumericConfig({ | ||
| envVar: 'INSTANA_AGENT_REQUEST_TIMEOUT', | ||
| configValue: userConfig.agentRequestTimeout, | ||
| defaultValue: defaultConfig.agentRequestTimeout, | ||
| configPath: 'config.agentRequestTimeout' | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * @param {import('../types/collector').CollectorConfig} userConfig | ||
| * @param {{ autoProfile: string | boolean }} defaultConfig | ||
| * @returns {string | boolean} | ||
| */ | ||
| function normalizeAutoProfile(userConfig, defaultConfig) { | ||
| return resolveConfig(process.env.INSTANA_AUTO_PROFILE, userConfig.autoProfile, defaultConfig.autoProfile); | ||
| } | ||
|
|
||
| /** | ||
| * @param {import('../types/collector').CollectorConfig} userConfig | ||
| * @returns {boolean} | ||
| */ | ||
| function normalizeUnhandledRejections(userConfig) { | ||
| return userConfig.reportUnhandledPromiseRejections ?? false; | ||
| } | ||
|
|
||
| /** | ||
| * @template T | ||
| * @param {T | undefined} envValue | ||
| * @param {T | undefined} configValue | ||
| * @param {T} defaultValue | ||
| * @returns {T} | ||
| */ | ||
| function resolveConfig(envValue, configValue, defaultValue) { | ||
| if (configValue != null) { | ||
| return configValue; | ||
| } | ||
| value = parseInt(value, 10); | ||
| if (!isNaN(value)) { | ||
| return Math.abs(Math.round(value)); | ||
|
|
||
| if (envValue != null) { | ||
| return envValue; | ||
| } | ||
|
|
||
| return defaultValue; | ||
| } | ||
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
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.