Skip to content

Commit 00601d7

Browse files
committed
refactor: removed agent/log.js and move the fn to agent connection
1 parent ae461ff commit 00601d7

File tree

5 files changed

+70
-58
lines changed

5 files changed

+70
-58
lines changed

packages/collector/src/agent/log.js

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

packages/collector/src/agent/loggerToAgentStream.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55

66
'use strict';
77

8-
const log = require('./log');
9-
108
const pidStore = require('../pidStore/internalPidStore');
119

12-
/** @type {NodeJS.WritableStream} */
10+
/** @type {import('../agentConnection')} */
11+
let downstreamConnection;
12+
13+
/** @type {NodeJS.WritableStream & {
14+
* setDownstreamConnection: (downstreamConnection: import('../agentConnection')) => void
15+
* }} */
1316
module.exports = {
1417
/**
1518
* @param {*} record
@@ -30,7 +33,14 @@ module.exports = {
3033
stack = record.err.stack;
3134
}
3235

33-
log(logLevel, message, stack);
36+
downstreamConnection.sendLogToAgent(logLevel, message, stack);
37+
},
38+
39+
/**
40+
* @param {import('../agentConnection')} _downstreamConnection
41+
*/
42+
setDownstreamConnection: _downstreamConnection => {
43+
downstreamConnection = _downstreamConnection;
3444
}
3545
};
3646

packages/collector/src/agentConnection.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,52 @@ exports.checkWhetherAgentIsReadyToAcceptData = function checkWhetherAgentIsReady
216216
checkWhetherResponseForPathIsOkay(`/com.instana.plugin.nodejs.${pidStore.pid}`, cb);
217217
};
218218

219+
/**
220+
* @param {*} logLevel
221+
* @param {*} message
222+
* @param {*} stackTrace
223+
*/
224+
exports.sendLogToAgent = function sendLogToAgent(logLevel, message, stackTrace) {
225+
/** @type {{m: string, st?: string}} */
226+
const payloadObject = {
227+
m: message.trim()
228+
};
229+
230+
if (stackTrace) {
231+
payloadObject.st = stackTrace.trim();
232+
}
233+
234+
const payload = Buffer.from(JSON.stringify(payloadObject), 'utf8');
235+
236+
const req = http.request(
237+
{
238+
host: agentOpts.host,
239+
port: agentOpts.port,
240+
path: '/com.instana.agent.logger',
241+
method: 'POST',
242+
agent: http.agent,
243+
headers: {
244+
'Content-Type': 'application/json; charset=UTF-8',
245+
'Content-Length': payload.length,
246+
'x-log-level': logLevel
247+
}
248+
},
249+
res => {
250+
res.resume();
251+
}
252+
);
253+
254+
function swallow() {
255+
// swallow all errors
256+
}
257+
258+
req.setTimeout(agentOpts.requestTimeout, swallow);
259+
req.on('error', swallow);
260+
261+
req.write(payload);
262+
req.end();
263+
};
264+
219265
/**
220266
* @param {string} path
221267
* @param {(...args: *) => *} cb

packages/collector/src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ function init(userConfig = {}) {
113113
const announceCycle = require('./announceCycle');
114114
const metrics = require('./metrics');
115115

116+
log.setDownstreamConnection(agentConnection);
117+
116118
pidStore.init(config);
117119
agentOpts.init(config);
118120
announceCycle.init(config, pidStore);

packages/collector/src/logger.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ exports.init = function init(userConfig = {}) {
158158

159159
exports.getLogger = () => instanaLogger;
160160

161+
/**
162+
*
163+
* @param {import('./agentConnection')} downstreamConnection
164+
*/
165+
exports.setDownstreamConnection = downstreamConnection => {
166+
loggerToAgentStream.setDownstreamConnection(downstreamConnection);
167+
};
168+
161169
/**
162170
* @param {import('@instana/core/src/core').GenericLogger | *} _logger
163171
* @returns {boolean}

0 commit comments

Comments
 (0)