-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathnormalizeConfig.js
More file actions
121 lines (110 loc) · 3.48 KB
/
normalizeConfig.js
File metadata and controls
121 lines (110 loc) · 3.48 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
111
112
113
114
115
116
117
118
119
120
121
/*
* (c) Copyright IBM Corp. 2021
* (c) Copyright Instana Inc. and contributors 2019
*/
'use strict';
const util = require('@instana/core/src/config/util');
const validate = require('@instana/core/src/config/validator');
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) {
const { value } = util.resolve(
{
envValue: 'INSTANA_AGENT_HOST',
inCodeValue: userConfig.agentHost,
defaultValue: defaultConfig.agentHost
},
[validate.stringValidator]
);
return value;
}
/**
* @param {import('../types/collector').CollectorConfig} userConfig
* @param {{ agentPort: number }} defaultConfig
* @returns {number}
*/
function normalizeAgentPort(userConfig, defaultConfig) {
const { value } = util.resolve(
{
envValue: 'INSTANA_AGENT_PORT',
inCodeValue: userConfig.agentPort,
defaultValue: defaultConfig.agentPort
},
[validate.numberValidator]
);
return value;
}
/**
* @param {import('../types/collector').CollectorConfig} userConfig
* @param {{ agentRequestTimeout: number }} defaultConfig
* @returns {number}
*/
function normalizeAgentRequestTimeout(userConfig, defaultConfig) {
const { value } = util.resolve(
{
envValue: 'INSTANA_AGENT_REQUEST_TIMEOUT',
inCodeValue: userConfig.agentRequestTimeout,
defaultValue: defaultConfig.agentRequestTimeout
},
[validate.numberValidator]
);
return value;
}
/**
* @param {import('../types/collector').CollectorConfig} userConfig
* @param {{ autoProfile: boolean }} defaultConfig
* @returns {boolean}
*/
function normalizeAutoProfile(userConfig, defaultConfig) {
const { value } = util.resolve(
{
envValue: 'INSTANA_AUTO_PROFILE',
inCodeValue: userConfig.autoProfile,
defaultValue: defaultConfig.autoProfile
},
[validate.booleanValidator]
);
return value;
}
/**
* @param {import('../types/collector').CollectorConfig} userConfig
* @returns {boolean}
*/
function normalizeUnhandledRejections(userConfig) {
const { value } = util.resolve(
{
inCodeValue: userConfig.reportUnhandledPromiseRejections,
defaultValue: false
},
[validate.booleanValidator]
);
return value;
}