-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathnormalizeConfig.js
More file actions
110 lines (96 loc) · 3.36 KB
/
normalizeConfig.js
File metadata and controls
110 lines (96 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* (c) Copyright IBM Corp. 2021
* (c) Copyright Instana Inc. and contributors 2019
*/
'use strict';
const util = require('@instana/core/src/config/util');
const defaults = {
agentHost: '127.0.0.1',
agentPort: 42699,
agentRequestTimeout: 5000,
autoProfile: false
};
/**
* Merges the config that was passed to the init function with environment variables and default values.
* @param {import('../types/collector').CollectorConfig} userConfig
* @returns {import('../types/collector').CollectorConfig}
*/
module.exports = function normalizeConfig(userConfig = {}) {
const finalConfig = {};
// 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);
finalConfig.agentPort = normalizeAgentPort(userConfig, defaults);
finalConfig.agentRequestTimeout = normalizeAgentRequestTimeout(userConfig, defaults);
finalConfig.autoProfile = normalizeAutoProfile(userConfig, defaults);
finalConfig.reportUnhandledPromiseRejections = normalizeUnhandledRejections(userConfig);
finalConfig.tracing = userConfig.tracing || {};
return finalConfig;
};
/**
* @param {import('../types/collector').CollectorConfig} userConfig
* @param {{ agentHost: string }} defaultConfig
* @returns {string}
*/
function normalizeAgentHost(userConfig, defaultConfig) {
return resolveConfig(process.env.INSTANA_AGENT_HOST, userConfig.agentHost, defaultConfig.agentHost);
}
/**
* @param {import('../types/collector').CollectorConfig} userConfig
* @param {{ agentPort: number }} defaultConfig
* @returns {number}
*/
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;
}
if (envValue != null) {
return envValue;
}
return defaultValue;
}