-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathagentready.js
More file actions
269 lines (231 loc) · 8.06 KB
/
agentready.js
File metadata and controls
269 lines (231 loc) · 8.06 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* (c) Copyright IBM Corp. 2021
* (c) Copyright Instana Inc. and contributors 2015
*/
'use strict';
let isMainThread = true;
try {
isMainThread = require('worker_threads').isMainThread;
} catch (err) {
// Worker threads are not available, so we know that this is the main thread.
}
const { tracing } = require('@instana/core');
const agentConnection = require('../agentConnection');
const initializedTooLate = require('../util/initializedTooLate');
const metrics = require('../metrics');
const requestHandler = require('../agent/requestHandler');
const transmissionCycle = require('../metrics/transmissionCycle');
const uncaught = require('../uncaught');
const { isNodeVersionEOL } = require('../util/eol');
const ONE_MINUTE = 60 * 1000;
const EOL_EVENT_REFRESH_INTERVAL = 6 * 60 * ONE_MINUTE; // 6 hours
const EOL_EVENT_DURATION = 6 * 60 * ONE_MINUTE + ONE_MINUTE; // 6 hours + 1 minute
const FIRE_MONITORING_EVENT_DURATION_IN_MS = process.env.INSTANA_FIRE_MONITORING_EVENT_DURATION_IN_MS
? Number(process.env.INSTANA_FIRE_MONITORING_EVENT_DURATION_IN_MS)
: 600000;
/** @type {*} */
let autoprofile;
/** @type {*} */
let profiler;
/** @type {import('@instana/core/src/core').GenericLogger} */
let logger;
/** @type {import('./').AnnounceCycleContext} */
let ctx;
let tracingMetricsDelay = 1000;
if (typeof process.env.INSTANA_TRACER_METRICS_INTERVAL === 'string') {
tracingMetricsDelay = parseInt(process.env.INSTANA_TRACER_METRICS_INTERVAL, 10);
if (isNaN(tracingMetricsDelay) || tracingMetricsDelay <= 0) {
tracingMetricsDelay = 1000;
}
}
/** @type {NodeJS.Timeout} */
let tracingMetricsTimeout = null;
/** @type {{ pid: number, getEntityId: Function }} */
let pidStore;
/** @type {import('@instana/collector/src/types/collector').CollectorConfig} */
let config;
/**
* @param {import('@instana/collector/src/types/collector').CollectorConfig} _config
* @param {any} _pidStore
*/
function init(_config, _pidStore) {
config = _config;
logger = config.logger;
pidStore = _pidStore;
initializedTooLate.init(config);
requestHandler.init(config);
if (config.autoProfile) {
try {
// @ts-ignore - TS cannot find @instana/profile.
// TODO: @instana/autoprofile is not linted or typed
// eslint-disable-next-line import/no-extraneous-dependencies
autoprofile = require('@instana/autoprofile');
} catch (e) {
logger.info(
'Could not load @instana/autoprofile. You will not get profiling information for this Node.js app in Instana,' +
'although autoprofiling has been enabled. This typically occurs when native addons could not be built ' +
'during module installation (npm install/yarn) or when npm install --no-optional or yarn --ignore-optional ' +
'have been used to install dependencies. See the instructions to learn more about the requirements of the ' +
'collector: ' +
'https://www.ibm.com/docs/en/instana-observability/current?topic=nodejs-collector-installation#native-add-ons'
);
fireMonitoringEvent();
setInterval(fireMonitoringEvent, FIRE_MONITORING_EVENT_DURATION_IN_MS).unref();
}
}
}
/**
* @param {import('./').AnnounceCycleContext} _ctx
*/
function enter(_ctx) {
ctx = _ctx;
initializedTooLate.check();
logger.debug(`isMainThread: ${isMainThread}`);
if (isMainThread) {
uncaught.activate();
metrics.activate();
requestHandler.activate();
transmissionCycle.activate(
metrics,
agentConnection,
/**
* @param {Array.<import('../agent/requestHandler').AnnounceRequest>} requests
*/
function onSuccess(requests) {
requestHandler.handleRequests(requests);
},
function onError() {
ctx.transitionTo('unannounced');
}
);
scheduleTracingMetrics();
if (!config.tracing?.disableEOLEvents) {
detectEOLNodeVersion();
}
}
tracing.activate(config.agentConfig);
if (config.autoProfile && autoprofile) {
profiler = autoprofile.start();
/**
* @param {*} profiles
* @param {(...args: *) => *} callback
*/
profiler.sendProfiles = (profiles, callback) => {
agentConnection.sendProfiles(profiles, callback);
};
profiler.getExternalPid = () => pidStore.pid;
profiler.getLogger = () => logger;
profiler.start();
}
logger.info('The Instana Node.js collector is now fully initialized and connected to the Instana host agent.');
// CASE: This is an IPC message only for a parent process.
// TODO: Add an EventEmitter functionality for the current process
// such as `instana.on('instana.collector.initialized')`.
// eslint-disable-next-line no-unused-expressions
process.send && process.send('instana.collector.initialized');
if (!isMainThread) {
const { parentPort } = require('worker_threads');
if (parentPort) {
// CASE: This is for the worker thread if available.
parentPort.postMessage('instana.collector.initialized');
}
}
}
function leave() {
if (isMainThread) {
uncaught.deactivate();
metrics.deactivate();
requestHandler.deactivate();
transmissionCycle.deactivate();
deScheduleTracingMetrics();
}
tracing.deactivate();
if (profiler) {
profiler.destroy();
profiler = null;
}
}
function sendTracingMetrics() {
const payload = tracing._getAndResetTracingMetrics();
agentConnection.sendTracingMetricsToAgent(payload, error => {
if (error) {
logger.info(
`Error received while trying to send tracing metrics to agent: ${error?.message}.` +
' This will not affect monitoring or tracing.'
);
if (typeof error.message === 'string' && error.message.indexOf('Got status code 404')) {
logger.info(
'Apparently the version of the Instana host agent on this host does not support the POST /tracermetrics ' +
'endpoint, will stop sending tracing metrics.'
);
return;
}
}
scheduleTracingMetrics();
});
}
function scheduleTracingMetrics() {
tracingMetricsTimeout = setTimeout(sendTracingMetrics, tracingMetricsDelay);
tracingMetricsTimeout.unref();
}
function deScheduleTracingMetrics() {
if (tracingMetricsTimeout) {
clearTimeout(tracingMetricsTimeout);
tracingMetricsTimeout = null;
}
}
function fireMonitoringEvent() {
agentConnection.sendAgentMonitoringEvent('nodejs_collector_native_addon_autoprofile_missing', 'PROFILER', error => {
if (error) {
logger.error(
`Error received while trying to send a monitoring event to the Instana host agent: ${error?.message}`
);
}
});
}
function sendEOLEvent() {
const pid = pidStore.getEntityId();
agentConnection.sendEvent(
{
title: `Node.js version ${process.versions.node} reached its end of life`,
text:
'This version no longer receives updates or security fixes and might contain unfixed vulnerabilities.\n\n' +
'Please consider upgrading Node.js to an active version.\n\n' +
'For a list of active versions visit ' +
'[https://nodejs.org/en/about/releases/](https://nodejs.org/en/about/releases/)',
plugin: 'com.instana.forge.infrastructure.runtime.nodejs.NodeJsRuntimePlatform',
id: pid,
timestamp: Date.now(),
duration: EOL_EVENT_DURATION,
severity: agentConnection.AgentEventSeverity.WARNING,
path: `${config.agentUuid}/${pid}/nodejs-eol`
},
err => {
if (err) {
logger.debug(
`Sending a monitoring event for the Node.js version end-of-life check has failed.
${err?.message} ${err?.stack}`
);
}
}
);
}
/**
* Sends an issue event to the agent when the Node.js version has reached end of life.
* It will work for non-serverless environments where an agent is present.
* (At the time of writing, the backend service that our serverless in-process collectors send data to does not have
* support for events.)
*/
function detectEOLNodeVersion() {
if (isNodeVersionEOL()) {
setTimeout(() => {
sendEOLEvent();
setInterval(sendEOLEvent, EOL_EVENT_REFRESH_INTERVAL).unref();
}, 2000);
}
}
module.exports = exports = {
init,
enter,
leave
};