Skip to content

Commit a381918

Browse files
committed
refactor(collector): removed agent opts file
1 parent acef2c0 commit a381918

File tree

14 files changed

+141
-199
lines changed

14 files changed

+141
-199
lines changed

packages/collector/src/agent/opts.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

packages/collector/src/agentConnection.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
const { util, uninstrumentedHttp, uninstrumentedFs: fs } = require('@instana/core');
99
const pathUtil = require('path');
1010
const circularReferenceRemover = require('./util/removeCircular');
11-
const agentOpts = require('./agent/opts');
1211
const cmdline = require('./cmdline');
1312

1413
/** @typedef {import('@instana/core/src/core').InstanaBaseSpan} InstanaBaseSpan */
@@ -34,14 +33,18 @@ let isConnected = false;
3433
/** @type {string | null} */
3534
let cpuSetFileContent = null;
3635

36+
/** @type {import('./types/collector').CollectorConfig} */
37+
let config;
38+
3739
/**
38-
* @param {import('@instana/core/src/config').InstanaConfig} config
40+
* @param {import('./types/collector').CollectorConfig} _config
3941
* @param {any} _pidStore
4042
*/
41-
exports.init = function init(config, _pidStore) {
42-
logger = config.logger;
43+
exports.init = function init(_config, _pidStore) {
44+
config = _config;
4345
pidStore = _pidStore;
4446

47+
logger = config.logger;
4548
cmdline.init(config);
4649
cpuSetFileContent = getCpuSetFileContent();
4750
};
@@ -133,8 +136,8 @@ exports.announceNodeCollector = function announceNodeCollector(callback) {
133136

134137
const req = http.request(
135138
{
136-
host: agentOpts.host,
137-
port: agentOpts.port,
139+
host: config.agentHost,
140+
port: config.agentPort,
138141
path: '/com.instana.plugin.nodejs.discovery',
139142
method: 'PUT',
140143
agent: http.agent,
@@ -161,7 +164,7 @@ exports.announceNodeCollector = function announceNodeCollector(callback) {
161164
}
162165
);
163166

164-
req.setTimeout(agentOpts.requestTimeout, function onTimeout() {
167+
req.setTimeout(config.requestTimeout, function onTimeout() {
165168
handleCallback(new Error('Announce request to agent failed due to timeout'));
166169
req.destroy();
167170
});
@@ -235,8 +238,8 @@ exports.sendLogToAgent = function sendLogToAgent(logLevel, message, stackTrace)
235238

236239
const req = http.request(
237240
{
238-
host: agentOpts.host,
239-
port: agentOpts.port,
241+
host: config.agentHost,
242+
port: config.agentPort,
240243
path: '/com.instana.agent.logger',
241244
method: 'POST',
242245
agent: http.agent,
@@ -255,7 +258,7 @@ exports.sendLogToAgent = function sendLogToAgent(logLevel, message, stackTrace)
255258
// swallow all errors
256259
}
257260

258-
req.setTimeout(agentOpts.requestTimeout, swallow);
261+
req.setTimeout(config.requestTimeout, swallow);
259262
req.on('error', swallow);
260263

261264
req.write(payload);
@@ -271,8 +274,8 @@ function checkWhetherResponseForPathIsOkay(path, cb) {
271274

272275
const req = http.request(
273276
{
274-
host: agentOpts.host,
275-
port: agentOpts.port,
277+
host: config.agentHost,
278+
port: config.agentPort,
276279
path,
277280
agent: http.agent,
278281
method: 'HEAD'
@@ -284,7 +287,7 @@ function checkWhetherResponseForPathIsOkay(path, cb) {
284287
}
285288
);
286289

287-
req.setTimeout(agentOpts.requestTimeout, function onTimeout() {
290+
req.setTimeout(config.requestTimeout, function onTimeout() {
288291
isConnected = false;
289292
cb(isConnected);
290293
req.destroy();
@@ -440,7 +443,7 @@ function sendData(path, data, cb, ignore404 = false) {
440443
if (typeof logger.trace === 'function') {
441444
logger.trace(`Sending data to ${path}.`);
442445
} else {
443-
logger.debug(`Sending data to ${path}, ${agentOpts}`);
446+
logger.debug(`Sending data to ${path}, ${config.agentHost}:${config.agentPort}`);
444447
}
445448

446449
// Convert payload to a buffer to correctly identify content-length ahead of time.
@@ -452,8 +455,8 @@ function sendData(path, data, cb, ignore404 = false) {
452455

453456
const req = http.request(
454457
{
455-
host: agentOpts.host,
456-
port: agentOpts.port,
458+
host: config.agentHost,
459+
port: config.agentPort,
457460
path,
458461
method: 'POST',
459462
agent: http.agent,
@@ -486,7 +489,7 @@ function sendData(path, data, cb, ignore404 = false) {
486489
}
487490
);
488491

489-
req.setTimeout(agentOpts.requestTimeout, function onTimeout() {
492+
req.setTimeout(config.requestTimeout, function onTimeout() {
490493
cb(new Error(`Failed to send data to agent via POST ${path}. Ran into a timeout.`));
491494
req.destroy();
492495
});

packages/collector/src/announceCycle/agentHostLookup.js

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ const { callbackify } = require('util');
99
const { atMostOnce } = require('@instana/core').util;
1010
const { http } = require('@instana/core').uninstrumentedHttp;
1111

12-
const agentOpts = require('../agent/opts');
1312
const defaultGatewayParser = require('./defaultGatewayParser');
1413
const readDefaultGateway = callbackify(defaultGatewayParser.parseProcSelfNetRouteFile);
1514

1615
/** @type {import('@instana/core/src/core').GenericLogger} */
1716
let logger;
1817

18+
/** @type {import('@instana/collector/src/types/collector').CollectorConfig} */
19+
let config;
1920
/**
20-
* @param {import('@instana/core/src/config').InstanaConfig} config
21+
* @param {import('@instana/collector/src/types/collector').CollectorConfig} _config
2122
*/
22-
const init = config => {
23+
const init = _config => {
24+
config = _config;
25+
2326
logger = config.logger;
2427
defaultGatewayParser.init(config);
2528
};
@@ -52,25 +55,23 @@ const retryTimeoutMillis = process.env.INSTANA_RETRY_AGENT_CONNECTION_IN_MS
5255
* @param {import('./').AnnounceCycleContext} ctx
5356
*/
5457
function enter(ctx) {
55-
const agentHost = agentOpts.host;
56-
57-
checkHost(agentHost, function onCheckHost(localhostCheckErr) {
58+
checkHost(config.agentHost, function onCheckHost(localhostCheckErr) {
5859
if (!localhostCheckErr) {
59-
setAgentHost(agentHost);
6060
ctx.transitionTo('unannounced');
6161
return;
6262
}
6363

6464
logger.debug(
65-
`No Instana host agent is running on ${agentHost}:${agentOpts.port}: ${localhostCheckErr}. Trying the default ` +
66-
'gateway next.'
65+
`No Instana host agent is running on ${config.agentHost}:${config.agentPort}: ${localhostCheckErr}.` +
66+
' Trying the default gateway next.'
6767
);
6868

6969
readDefaultGateway(function onReadDefaultGateway(getDefaultGatewayErr, defaultGateway) {
7070
if (getDefaultGatewayErr) {
7171
logger.warn(
72-
`The Instana host agent cannot be reached via ${agentHost}:${agentOpts.port} and the default gateway ` +
73-
`cannot be determined. Details: Error for the connection attempt: ${safelyExtractErrorMessage(
72+
`The Instana host agent cannot be reached via ${config.agentHost}:${config.agentPort} ` +
73+
'and the default gateway cannot be determined. ' +
74+
`Details: Error for the connection attempt: ${safelyExtractErrorMessage(
7475
localhostCheckErr
7576
)}; error for determining the gateway: ${safelyExtractErrorMessage(
7677
getDefaultGatewayErr
@@ -84,14 +85,15 @@ function enter(ctx) {
8485

8586
checkHost(defaultGateway, function onCheckHostDefaultGateway(defaultGatewayCheckErr) {
8687
if (!defaultGatewayCheckErr) {
87-
setAgentHost(defaultGateway);
88+
config.agentHost = defaultGateway;
8889
ctx.transitionTo('unannounced');
8990
return;
9091
}
9192

9293
logger.warn(
93-
`The Instana host agent can neither be reached via ${agentHost}:${agentOpts.port} nor via the default ` +
94-
`gateway ${defaultGateway}:${agentOpts.port}. Details: Error for the first attempt: ` +
94+
`The Instana host agent can neither be reached via ${config.agentHost}:${config.agentPort} ` +
95+
`nor via the default gateway ${defaultGateway}:${config.agentPort}. ` +
96+
'Details: Error for the first attempt: ' +
9597
`${safelyExtractErrorMessage(localhostCheckErr)}; error for the second attempt: ${safelyExtractErrorMessage(
9698
defaultGatewayCheckErr
9799
)}. The Instana host agent might not be ready yet, scheduling another attempt to establish a connection ` +
@@ -139,7 +141,7 @@ function checkHost(host, cb) {
139141
req = http.request(
140142
{
141143
host,
142-
port: agentOpts.port,
144+
port: config.agentPort,
143145
path: '/',
144146
agent: http.agent,
145147
method: 'GET',
@@ -155,7 +157,7 @@ function checkHost(host, cb) {
155157
res.resume();
156158
handleCallback(
157159
new Error(
158-
`The attempt to connect to the Instana host agent on ${host}:${agentOpts.port} has failed with an ` +
160+
`The attempt to connect to the Instana host agent on ${host}:${config.agentPort} has failed with an ` +
159161
`unexpected status code. Expected HTTP 200 but received: ${res.statusCode}`
160162
)
161163
);
@@ -165,16 +167,16 @@ function checkHost(host, cb) {
165167
} catch (e) {
166168
handleCallback(
167169
new Error(
168-
`The attempt to connect to the Instana host agent on ${host}:${agentOpts.port} has failed with the following ` +
169-
`error: ${e.message}`
170+
`The attempt to connect to the Instana host agent on ${host}:${config.agentPort} ` +
171+
`has failed with the following error: ${e.message}`
170172
)
171173
);
172174
return;
173175
}
174176

175177
req.on('timeout', function onTimeout() {
176178
handleCallback(
177-
new Error(`The attempt to connect to the Instana host agent on ${host}:${agentOpts.port} has timed out`)
179+
new Error(`The attempt to connect to the Instana host agent on ${host}:${config.agentPort} has timed out`)
178180
);
179181
});
180182

@@ -184,8 +186,8 @@ function checkHost(host, cb) {
184186
req.on('error', err => {
185187
handleCallback(
186188
new Error(
187-
`The attempt to connect to the Instana host agent on ${host}:${agentOpts.port} has failed with the following ` +
188-
`error: ${err.message}`
189+
`The attempt to connect to the Instana host agent on ${host}:${config.agentPort} ` +
190+
`has failed with the following error: ${err.message}`
189191
)
190192
);
191193
});
@@ -219,8 +221,8 @@ function handleResponse(host, res, cb) {
219221
} catch (e) {
220222
cb(
221223
new Error(
222-
`The attempt to connect to the Instana host agent on ${host}:${agentOpts.port} has failed, the response ` +
223-
`cannot be parsed as JSON. The party listening on ${host}:${agentOpts.port} does not seem to be an ` +
224+
`The attempt to connect to the Instana host agent on ${host}:${config.agentPort} has failed, the response ` +
225+
`cannot be parsed as JSON. The party listening on ${host}:${config.agentPort} does not seem to be an ` +
224226
`Instana host agent. Full response: ${responsePayload}`
225227
)
226228
);
@@ -231,9 +233,9 @@ function handleResponse(host, res, cb) {
231233
} else {
232234
cb(
233235
new Error(
234-
`The attempt to connect to the Instana host agent on ${host}:${agentOpts.port} has failed, the response did` +
235-
` not have a "version" property. The party listening on ${host}:${agentOpts.port} does not seem to be an ` +
236-
`Instana host agent. Full response: ${responsePayload}`
236+
`The attempt to connect to the Instana host agent on ${host}:${config.agentPort} ` +
237+
' has failed, the response did not have a "version" property. The party listening on ' +
238+
`${host}:${config.agentPort} does not seem to be an Instana host agent. Full response: ${responsePayload}`
237239
)
238240
);
239241
}
@@ -253,14 +255,6 @@ function safelyExtractErrorMessage(error) {
253255
return error;
254256
}
255257

256-
/**
257-
* @param {string} host
258-
*/
259-
function setAgentHost(host) {
260-
logger.info(`Found an agent on ${host}:${agentOpts.port}, proceeding to announce request.`);
261-
agentOpts.host = host;
262-
}
263-
264258
module.exports = {
265259
init,
266260
enter,

packages/collector/src/announceCycle/agentready.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ try {
1414

1515
const { tracing } = require('@instana/core');
1616
const agentConnection = require('../agentConnection');
17-
const agentOpts = require('../agent/opts');
1817
const initializedTooLate = require('../util/initializedTooLate');
1918
const metrics = require('../metrics');
2019
const requestHandler = require('../agent/requestHandler');
@@ -55,24 +54,23 @@ let tracingMetricsTimeout = null;
5554
/** @type {{ pid: number, getEntityId: Function }} */
5655
let pidStore;
5756

58-
/**
59-
* @type {boolean}
60-
*/
61-
let disableEOLEvents;
57+
/** @type {import('@instana/collector/src/types/collector').CollectorConfig} */
58+
let config;
6259

6360
/**
64-
* @param {import('@instana/core/src/config').InstanaConfig} config
61+
* @param {import('@instana/collector/src/types/collector').CollectorConfig} _config
6562
* @param {any} _pidStore
6663
*/
67-
function init(config, _pidStore) {
64+
function init(_config, _pidStore) {
65+
config = _config;
66+
6867
logger = config.logger;
6968
pidStore = _pidStore;
70-
disableEOLEvents = config.tracing?.disableEOLEvents;
69+
7170
initializedTooLate.init(config);
7271
requestHandler.init(config);
7372

74-
// TODO: Why is autoProfile part of agentOpts? O_o Please refactor this away in next major release.
75-
if (agentOpts.autoProfile) {
73+
if (config.autoProfile) {
7674
try {
7775
// @ts-ignore - TS cannot find @instana/profile.
7876
// TODO: @instana/autoprofile is not linted or typed
@@ -122,14 +120,14 @@ function enter(_ctx) {
122120
}
123121
);
124122
scheduleTracingMetrics();
125-
if (!disableEOLEvents) {
123+
if (!config.tracing?.disableEOLEvents) {
126124
detectEOLNodeVersion();
127125
}
128126
}
129127

130-
tracing.activate(agentOpts.config);
128+
tracing.activate(config.agentConfig);
131129

132-
if (agentOpts.autoProfile && autoprofile) {
130+
if (config.autoProfile && autoprofile) {
133131
profiler = autoprofile.start();
134132
/**
135133
* @param {*} profiles
@@ -236,7 +234,7 @@ function sendEOLEvent() {
236234
timestamp: Date.now(),
237235
duration: EOL_EVENT_DURATION,
238236
severity: agentConnection.AgentEventSeverity.WARNING,
239-
path: `${agentOpts.agentUuid}/${pid}/nodejs-eol`
237+
path: `${config.agentUuid}/${pid}/nodejs-eol`
240238
},
241239
err => {
242240
if (err) {

0 commit comments

Comments
 (0)