Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions packages/collector/src/agent/opts.js

This file was deleted.

38 changes: 20 additions & 18 deletions packages/collector/src/agentConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
const { util, uninstrumentedHttp, uninstrumentedFs: fs } = require('@instana/core');
const pathUtil = require('path');
const circularReferenceRemover = require('./util/removeCircular');
const agentOpts = require('./agent/opts');
const cmdline = require('./cmdline');

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

/** @type {import('./types/collector').CollectorConfig} */
let config;

/**
* @param {import('@instana/core/src/config').InstanaConfig} config
* @param {import('./types/collector').CollectorConfig} _config
* @param {any} _pidStore
*/
exports.init = function init(config, _pidStore) {
logger = config.logger;
exports.init = function init(_config, _pidStore) {
config = _config;
pidStore = _pidStore;

logger = config.logger;
cmdline.init(config);
cpuSetFileContent = getCpuSetFileContent();
};
Expand Down Expand Up @@ -133,8 +136,8 @@ exports.announceNodeCollector = function announceNodeCollector(callback) {

const req = http.request(
{
host: agentOpts.host,
port: agentOpts.port,
host: config.agentHost,
port: config.agentPort,
path: '/com.instana.plugin.nodejs.discovery',
method: 'PUT',
agent: http.agent,
Expand All @@ -161,7 +164,7 @@ exports.announceNodeCollector = function announceNodeCollector(callback) {
}
);

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

const req = http.request(
{
host: agentOpts.host,
port: agentOpts.port,
host: config.agentHost,
port: config.agentPort,
path: '/com.instana.agent.logger',
method: 'POST',
agent: http.agent,
Expand All @@ -255,7 +258,7 @@ exports.sendLogToAgent = function sendLogToAgent(logLevel, message, stackTrace)
// swallow all errors
}

req.setTimeout(agentOpts.requestTimeout, swallow);
req.setTimeout(config.requestTimeout, swallow);
req.on('error', swallow);

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

const req = http.request(
{
host: agentOpts.host,
port: agentOpts.port,
host: config.agentHost,
port: config.agentPort,
path,
agent: http.agent,
method: 'HEAD'
Expand All @@ -284,7 +287,7 @@ function checkWhetherResponseForPathIsOkay(path, cb) {
}
);

req.setTimeout(agentOpts.requestTimeout, function onTimeout() {
req.setTimeout(config.requestTimeout, function onTimeout() {
isConnected = false;
cb(isConnected);
req.destroy();
Expand Down Expand Up @@ -435,12 +438,11 @@ exports.sendTracingMetricsToAgent = function sendTracingMetricsToAgent(tracingMe
*/
function sendData(path, data, cb, ignore404 = false) {
cb = util.atMostOnce(`callback for sendData: ${path}`, cb);

const payloadAsString = JSON.stringify(data, circularReferenceRemover());
if (typeof logger.trace === 'function') {
logger.trace(`Sending data to ${path}.`);
} else {
logger.debug(`Sending data to ${path}, ${agentOpts}`);
logger.debug(`Sending data to ${path}, ${config.agentHost}:${config.agentPort}`);
}

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

const req = http.request(
{
host: agentOpts.host,
port: agentOpts.port,
host: config.agentHost,
port: config.agentPort,
path,
method: 'POST',
agent: http.agent,
Expand Down Expand Up @@ -486,7 +488,7 @@ function sendData(path, data, cb, ignore404 = false) {
}
);

req.setTimeout(agentOpts.requestTimeout, function onTimeout() {
req.setTimeout(config.requestTimeout, function onTimeout() {
cb(new Error(`Failed to send data to agent via POST ${path}. Ran into a timeout.`));
req.destroy();
});
Expand Down
65 changes: 31 additions & 34 deletions packages/collector/src/announceCycle/agentHostLookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ const { callbackify } = require('util');
const { atMostOnce } = require('@instana/core').util;
const { http } = require('@instana/core').uninstrumentedHttp;

const agentOpts = require('../agent/opts');
const defaultGatewayParser = require('./defaultGatewayParser');
const readDefaultGateway = callbackify(defaultGatewayParser.parseProcSelfNetRouteFile);

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

/** @type {import('@instana/collector/src/types/collector').CollectorConfig} */
let config;
/**
* @param {import('@instana/core/src/config').InstanaConfig} config
* @param {import('@instana/collector/src/types/collector').CollectorConfig} _config
*/
const init = config => {
const init = _config => {
config = _config;

logger = config.logger;
defaultGatewayParser.init(config);
};
Expand Down Expand Up @@ -52,25 +55,24 @@ const retryTimeoutMillis = process.env.INSTANA_RETRY_AGENT_CONNECTION_IN_MS
* @param {import('./').AnnounceCycleContext} ctx
*/
function enter(ctx) {
const agentHost = agentOpts.host;

checkHost(agentHost, function onCheckHost(localhostCheckErr) {
checkHost(config.agentHost, function onCheckHost(localhostCheckErr) {
if (!localhostCheckErr) {
setAgentHost(agentHost);
logger.info(`Found an agent on ${config.agentHost}:${config.agentPort}, proceeding to announce request.`);
ctx.transitionTo('unannounced');
return;
}

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

readDefaultGateway(function onReadDefaultGateway(getDefaultGatewayErr, defaultGateway) {
if (getDefaultGatewayErr) {
logger.warn(
`The Instana host agent cannot be reached via ${agentHost}:${agentOpts.port} and the default gateway ` +
`cannot be determined. Details: Error for the connection attempt: ${safelyExtractErrorMessage(
`The Instana host agent cannot be reached via ${config.agentHost}:${config.agentPort} ` +
'and the default gateway cannot be determined. ' +
`Details: Error for the connection attempt: ${safelyExtractErrorMessage(
localhostCheckErr
)}; error for determining the gateway: ${safelyExtractErrorMessage(
getDefaultGatewayErr
Expand All @@ -84,14 +86,16 @@ function enter(ctx) {

checkHost(defaultGateway, function onCheckHostDefaultGateway(defaultGatewayCheckErr) {
if (!defaultGatewayCheckErr) {
setAgentHost(defaultGateway);
config.agentHost = defaultGateway;
logger.info(`Found an agent on ${config.agentHost}:${config.agentPort}, proceeding to announce request.`);
ctx.transitionTo('unannounced');
return;
}

logger.warn(
`The Instana host agent can neither be reached via ${agentHost}:${agentOpts.port} nor via the default ` +
`gateway ${defaultGateway}:${agentOpts.port}. Details: Error for the first attempt: ` +
`The Instana host agent can neither be reached via ${config.agentHost}:${config.agentPort} ` +
`nor via the default gateway ${defaultGateway}:${config.agentPort}. ` +
'Details: Error for the first attempt: ' +
`${safelyExtractErrorMessage(localhostCheckErr)}; error for the second attempt: ${safelyExtractErrorMessage(
defaultGatewayCheckErr
)}. The Instana host agent might not be ready yet, scheduling another attempt to establish a connection ` +
Expand Down Expand Up @@ -139,7 +143,7 @@ function checkHost(host, cb) {
req = http.request(
{
host,
port: agentOpts.port,
port: config.agentPort,
path: '/',
agent: http.agent,
method: 'GET',
Expand All @@ -155,7 +159,7 @@ function checkHost(host, cb) {
res.resume();
handleCallback(
new Error(
`The attempt to connect to the Instana host agent on ${host}:${agentOpts.port} has failed with an ` +
`The attempt to connect to the Instana host agent on ${host}:${config.agentPort} has failed with an ` +
`unexpected status code. Expected HTTP 200 but received: ${res.statusCode}`
)
);
Expand All @@ -165,16 +169,16 @@ function checkHost(host, cb) {
} catch (e) {
handleCallback(
new Error(
`The attempt to connect to the Instana host agent on ${host}:${agentOpts.port} has failed with the following ` +
`error: ${e.message}`
`The attempt to connect to the Instana host agent on ${host}:${config.agentPort} ` +
`has failed with the following error: ${e.message}`
)
);
return;
}

req.on('timeout', function onTimeout() {
handleCallback(
new Error(`The attempt to connect to the Instana host agent on ${host}:${agentOpts.port} has timed out`)
new Error(`The attempt to connect to the Instana host agent on ${host}:${config.agentPort} has timed out`)
);
});

Expand All @@ -184,8 +188,8 @@ function checkHost(host, cb) {
req.on('error', err => {
handleCallback(
new Error(
`The attempt to connect to the Instana host agent on ${host}:${agentOpts.port} has failed with the following ` +
`error: ${err.message}`
`The attempt to connect to the Instana host agent on ${host}:${config.agentPort} ` +
`has failed with the following error: ${err.message}`
)
);
});
Expand Down Expand Up @@ -219,21 +223,22 @@ function handleResponse(host, res, cb) {
} catch (e) {
cb(
new Error(
`The attempt to connect to the Instana host agent on ${host}:${agentOpts.port} has failed, the response ` +
`cannot be parsed as JSON. The party listening on ${host}:${agentOpts.port} does not seem to be an ` +
`The attempt to connect to the Instana host agent on ${host}:${config.agentPort} has failed, the response ` +
`cannot be parsed as JSON. The party listening on ${host}:${config.agentPort} does not seem to be an ` +
`Instana host agent. Full response: ${responsePayload}`
)
);
return;
}

if (responseJson.version !== undefined) {
cb(null);
} else {
cb(
new Error(
`The attempt to connect to the Instana host agent on ${host}:${agentOpts.port} has failed, the response did` +
` not have a "version" property. The party listening on ${host}:${agentOpts.port} does not seem to be an ` +
`Instana host agent. Full response: ${responsePayload}`
`The attempt to connect to the Instana host agent on ${host}:${config.agentPort} ` +
' has failed, the response did not have a "version" property. The party listening on ' +
`${host}:${config.agentPort} does not seem to be an Instana host agent. Full response: ${responsePayload}`
)
);
}
Expand All @@ -253,14 +258,6 @@ function safelyExtractErrorMessage(error) {
return error;
}

/**
* @param {string} host
*/
function setAgentHost(host) {
logger.info(`Found an agent on ${host}:${agentOpts.port}, proceeding to announce request.`);
agentOpts.host = host;
}

module.exports = {
init,
enter,
Expand Down
26 changes: 12 additions & 14 deletions packages/collector/src/announceCycle/agentready.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ try {

const { tracing } = require('@instana/core');
const agentConnection = require('../agentConnection');
const agentOpts = require('../agent/opts');
const initializedTooLate = require('../util/initializedTooLate');
const metrics = require('../metrics');
const requestHandler = require('../agent/requestHandler');
Expand Down Expand Up @@ -55,24 +54,23 @@ let tracingMetricsTimeout = null;
/** @type {{ pid: number, getEntityId: Function }} */
let pidStore;

/**
* @type {boolean}
*/
let disableEOLEvents;
/** @type {import('@instana/collector/src/types/collector').CollectorConfig} */
let config;

/**
* @param {import('@instana/core/src/config').InstanaConfig} config
* @param {import('@instana/collector/src/types/collector').CollectorConfig} _config
* @param {any} _pidStore
*/
function init(config, _pidStore) {
function init(_config, _pidStore) {
config = _config;

logger = config.logger;
pidStore = _pidStore;
disableEOLEvents = config.tracing?.disableEOLEvents;

initializedTooLate.init(config);
requestHandler.init(config);

// TODO: Why is autoProfile part of agentOpts? O_o Please refactor this away in next major release.
if (agentOpts.autoProfile) {
if (config.autoProfile) {
try {
// @ts-ignore - TS cannot find @instana/profile.
// TODO: @instana/autoprofile is not linted or typed
Expand Down Expand Up @@ -122,14 +120,14 @@ function enter(_ctx) {
}
);
scheduleTracingMetrics();
if (!disableEOLEvents) {
if (!config.tracing?.disableEOLEvents) {
detectEOLNodeVersion();
}
}

tracing.activate(agentOpts.config);
tracing.activate(config.agentConfig);

if (agentOpts.autoProfile && autoprofile) {
if (config.autoProfile && autoprofile) {
profiler = autoprofile.start();
/**
* @param {*} profiles
Expand Down Expand Up @@ -236,7 +234,7 @@ function sendEOLEvent() {
timestamp: Date.now(),
duration: EOL_EVENT_DURATION,
severity: agentConnection.AgentEventSeverity.WARNING,
path: `${agentOpts.agentUuid}/${pid}/nodejs-eol`
path: `${config.agentUuid}/${pid}/nodejs-eol`
},
err => {
if (err) {
Expand Down
Loading