Skip to content

Commit bd4c9bb

Browse files
abhilash-sivankirrg001aryamohanan
authored
fix: resolved more logging objects structure (#1510)
* fix: resolved more logging object structure and added optional chaining for safety --------- Co-authored-by: kirrg001 <[email protected]> Co-authored-by: Arya <[email protected]>
1 parent 770ca1f commit bd4c9bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+127
-158
lines changed

packages/aws-fargate/src/activate.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ function init() {
3131
metrics.init(config, function onReady(err, ecsContainerPayload) {
3232
if (err) {
3333
logger.error(
34-
`Initializing @instana/aws-fargate failed. This fargate task will not be monitored. ${err.message} ${err.stack}`
34+
`Initializing @instana/aws-fargate failed. This fargate task will not be monitored.
35+
${err?.message} ${err?.stack}`
3536
);
3637
metrics.deactivate();
3738
return;
@@ -71,7 +72,7 @@ function init() {
7172
process.send && process.send('instana.aws-fargate.initialized');
7273
} catch (e) {
7374
logger.error(
74-
`Initializing @instana/aws-fargate failed. This fargate task will not be monitored. ${e.message} ${e.stack}`
75+
`Initializing @instana/aws-fargate failed. This fargate task will not be monitored. ${e?.message} ${e?.stack}`
7576
);
7677
}
7778
});

packages/aws-fargate/src/metrics/transmissionCycle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function onMetricsHaveBeenSent(transmittedPayloadPerProcessor, error) {
7171
transmissionTimeoutHandle.unref();
7272

7373
if (error) {
74-
logger.error(`Error received while trying to send snapshot data and metrics: ${error.message}`);
74+
logger.error(`Error received while trying to send snapshot data and metrics: ${error?.message}`);
7575
return;
7676
}
7777

packages/aws-lambda/src/metrics/npmPackageDescription.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exports.currentPayload = undefined;
1616
exports.activate = function activate() {
1717
coreUtil.applicationUnderMonitoring.getMainPackageJsonStartingAtDirectory(rootDir.root, (err, pckg) => {
1818
if (err) {
19-
logger.warn(`Failed to determine main package json. Reason: ${err.message} ${err.stack}`);
19+
logger.warn(`Failed to determine main package json. Reason: ${err?.message} ${err?.stack}`);
2020
}
2121

2222
if (!err && pckg) {

packages/aws-lambda/src/metrics/npmPackageName.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exports.currentPayload = undefined;
1616
exports.activate = function activate() {
1717
coreUtil.applicationUnderMonitoring.getMainPackageJsonStartingAtDirectory(rootDir.root, (err, pckg) => {
1818
if (err) {
19-
logger.warn(`Failed to determine main package json. Reason: ${err.message} ${err.stack}`);
19+
logger.warn(`Failed to determine main package json. Reason: ${err?.message} ${err?.stack}`);
2020
}
2121

2222
if (!err && pckg) {

packages/aws-lambda/src/metrics/npmPackageVersion.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exports.currentPayload = undefined;
1616
exports.activate = function activate() {
1717
coreUtil.applicationUnderMonitoring.getMainPackageJsonStartingAtDirectory(rootDir.root, (err, pckg) => {
1818
if (err) {
19-
logger.warn(`Failed to determine main package json. Reason: ${err.message} ${err.stack}`);
19+
logger.warn(`Failed to determine main package json. Reason: ${err?.message} ${err?.stack}`);
2020
}
2121

2222
if (!err && pckg) {

packages/aws-lambda/src/ssm.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ module.exports.init = ({ logger }) => {
8888
logger.warn('AWS SDK is not available.');
8989
errorFromAWS =
9090
`Unable to fetch the Instana key from the SSM Parameter Store using "${process.env.INSTANA_SSM_PARAM_NAME}",` +
91-
` as the AWS SDK is unavailable. Reason: ${err.message}`;
91+
` as the AWS SDK is unavailable. Reason: ${err?.message}`;
9292
}
9393
};
9494

packages/azure-container-services/src/activate.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ function init() {
5353
process.send && process.send('instana.azure-app-service.initialized');
5454
} catch (e) {
5555
logger.error(
56-
'Initializing @instana/azure-container-services failed. This azure container service will not be monitored.',
57-
e
56+
'Initializing @instana/azure-container-services failed. This azure container service will not be monitored.' +
57+
`${e?.message} ${e?.stack}`
5858
);
5959
}
6060
}

packages/collector/src/actions/source.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function readFile(request, multiCb) {
3838
fs.readFile(request.args.file, { encoding: 'utf8' }, (error, content) => {
3939
if (error) {
4040
logger.debug(
41-
`Failed to retrieve source file for user request: ${request.args.file}. ${error.message} ${error.stack}`
41+
`Failed to retrieve source file for user request: ${request.args.file}. ${error?.message} ${error?.stack}`
4242
);
4343
multiCb({
4444
error: `Could not load file. Error: ${error.message}`

packages/collector/src/agent/requestHandler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function sendResponse(request, response) {
6363
if (error) {
6464
logger.warn(
6565
`Failed to send agent response for action ${request.action} and message ID ${request.messageId}.
66-
Error: ${error.message} ${error.stack}`
66+
Error: ${error?.message} ${error?.stack}`
6767
);
6868
}
6969
});

packages/collector/src/agentConnection.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ exports.announceNodeCollector = function announceNodeCollector(cb) {
169169
payload.inode = payload.inode.substring(linkPathPrefix.length);
170170
}
171171
} catch (e) {
172-
logger.debug('Failed to retrieve inode for file descriptor %s: %s', payload.fd, e.message);
172+
logger.debug(`Failed to retrieve inode for file descriptor ${payload.fd}: ${e?.message}`);
173173
}
174174
}
175175

@@ -449,7 +449,7 @@ function getCpuSetFileContent() {
449449
return content;
450450
} catch (err) {
451451
if (err.code !== 'ENOENT') {
452-
logger.warn('cpuset file could not be read. Reason: %s', err.message);
452+
logger.warn(`cpuset file could not be read. Reason: ${err?.message}`);
453453
}
454454
return null;
455455
}

packages/collector/src/announceCycle/agentHostLookup.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,6 @@ function safelyExtractErrorMessage(error) {
231231
* @param {string} host
232232
*/
233233
function setAgentHost(host) {
234-
logger.info('Found an agent on %s:%s, proceeding to announce request.', host, agentOpts.port);
234+
logger.info(`Found an agent on ${host}:${agentOpts.port}, proceeding to announce request.`);
235235
agentOpts.host = host;
236236
}

packages/collector/src/announceCycle/agentready.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ function sendTracingMetrics() {
157157
agentConnection.sendTracingMetricsToAgent(payload, error => {
158158
if (error) {
159159
logger.info(
160-
'Error received while trying to send tracing metrics to agent: %s. This will not affect monitoring or tracing.',
161-
error.message
160+
`Error received while trying to send tracing metrics to agent: ${error?.message}.` +
161+
' This will not affect monitoring or tracing.'
162162
);
163163
if (typeof error.message === 'string' && error.message.indexOf('Got status code 404')) {
164164
logger.info(
@@ -188,8 +188,7 @@ function fireMonitoringEvent() {
188188
agentConnection.sendAgentMonitoringEvent('nodejs_collector_native_addon_autoprofile_missing', 'PROFILER', error => {
189189
if (error) {
190190
logger.error(
191-
'Error received while trying to send a monitoring event to the Instana host agent: %s',
192-
error.message
191+
`Error received while trying to send a monitoring event to the Instana host agent: ${error?.message}`
193192
);
194193
}
195194
});
@@ -215,7 +214,8 @@ function sendEOLEvent() {
215214
err => {
216215
if (err) {
217216
logger.debug(
218-
`Sending a monitoring event for the Node.js version end-of-life check has failed. ${err.message} ${err.stack}`
217+
`Sending a monitoring event for the Node.js version end-of-life check has failed.
218+
${err?.message} ${err?.stack}`
219219
);
220220
}
221221
}

packages/collector/src/announceCycle/announced.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ function checkWhetherAgentIsReadyToAccept(totalNumberOfAttempts, ctx) {
3737
ctx.transitionTo('agentready');
3838
} else if (totalNumberOfAttempts > MAX_RETRIES) {
3939
logger.warn(
40-
'The Instana host agent is not yet ready to accept data after %s attempts. Restarting the cycle to establish ' +
41-
'a connection.',
42-
totalNumberOfAttempts
40+
`The Instana host agent is not yet ready to accept data after ${totalNumberOfAttempts} attempts.
41+
Restarting the cycle to establish a connection.`
4342
);
4443
ctx.transitionTo('unannounced');
4544
} else {

packages/collector/src/announceCycle/defaultGatewayParser.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ exports._parseFile = async function _parseFile(filename) {
4242
fileContent = await fs.readFile(filename, { encoding: 'utf8' });
4343
} catch (e) {
4444
cachedResult = null;
45-
logger.debug(`Could not open ${filename} when trying to retrieve the default gateway IP.`, e);
45+
logger.debug(
46+
`Could not open ${filename} when trying to retrieve the default gateway IP. ${e?.message} ${e?.stack}`
47+
);
4648
if (e.code === 'ENOENT') {
4749
throw new Error(`Failed to determine the default gateway: The file ${filename} does not exist`);
4850
} else {

packages/collector/src/announceCycle/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const ctx = {
3939
* @param {string} newStateName
4040
*/
4141
transitionTo: function (newStateName) {
42-
logger.info('Transitioning from %s to %s', currentState || '<init>', newStateName);
42+
logger.info(`Transitioning from ${currentState || '<init>'} to ${newStateName}`);
4343

4444
if (currentState) {
4545
states[currentState].leave(ctx);

packages/collector/src/announceCycle/unannounced.js

+12-16
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,9 @@ function tryToAnnounce(ctx, retryDelay = initialRetryDelay) {
7878
agentConnection.announceNodeCollector((err, rawResponse) => {
7979
if (err) {
8080
logger.info(
81-
'Establishing the connection to the Instana host agent has failed: %s. This usually means that the Instana ' +
82-
'host agent is not yet ready to accept connections. This is not an error. Establishing the connection will ' +
83-
'be retried in %s ms.',
84-
err.message,
85-
retryDelay
81+
`Establishing the connection to the Instana host agent has failed: ${err?.message}. ` +
82+
'This usually means that the Instana host agent is not yet ready to accept connections.' +
83+
`This is not an error. Establishing the connection will be retried in ${retryDelay} ms.`
8684
);
8785
setTimeout(tryToAnnounce, retryDelay, ctx, nextRetryDelay).unref();
8886
return;
@@ -95,18 +93,16 @@ function tryToAnnounce(ctx, retryDelay = initialRetryDelay) {
9593
logger.error(
9694
"Failed to parse the JSON payload from the Instana host agent's response. Establishing the connection" +
9795
`to the Instana host agent will be retried in ${retryDelay} ms. The response payload was ${rawResponse}.` +
98-
`${e.message} ${e.stack}`
96+
`${e?.message} ${e?.stack}`
9997
);
10098
setTimeout(tryToAnnounce, retryDelay, ctx, nextRetryDelay).unref();
10199
return;
102100
}
103101

104102
if (pidStore.pid !== agentResponse.pid) {
105103
logger.info(
106-
'Reporting data to the Instana host agent with the PID from the root namespace (%s) instead of the ' +
107-
'in-container PID (%s).',
108-
agentResponse.pid,
109-
pidStore.pid
104+
`Reporting data to the Instana host agent with the PID from the root namespace (${agentResponse.pid}) ` +
105+
`instead of the in-container PID (${pidStore.pid}).`
110106
);
111107
pidStore.pid = agentResponse.pid;
112108
}
@@ -135,18 +131,18 @@ function applySecretsConfiguration(agentResponse) {
135131
if (agentResponse.secrets) {
136132
if (!(typeof agentResponse.secrets.matcher === 'string')) {
137133
logger.warn(
138-
'Received an invalid secrets configuration from the Instana host agent, attribute matcher is not a string: $s',
139-
agentResponse.secrets.matcher
134+
`Received an invalid secrets configuration from the Instana host agent, attribute matcher is not a string:
135+
${agentResponse.secrets.matcher}`
140136
);
141137
} else if (Object.keys(secrets.matchers).indexOf(agentResponse.secrets.matcher) < 0) {
142138
logger.warn(
143-
'Received an invalid secrets configuration from the Intana agent, matcher is not supported: $s',
144-
agentResponse.secrets.matcher
139+
`Received an invalid secrets configuration from the Intana agent, matcher is not supported:
140+
${agentResponse.secrets.matcher}`
145141
);
146142
} else if (!Array.isArray(agentResponse.secrets.list)) {
147143
logger.warn(
148-
'Received an invalid secrets configuration from the Instana host agent, attribute list is not an array: $s',
149-
agentResponse.secrets.list
144+
`Received an invalid secrets configuration from the Instana host agent, attribute list is not an array:
145+
${agentResponse.secrets.list}`
150146
);
151147
} else {
152148
secrets.setMatcher(agentResponse.secrets.matcher, agentResponse.secrets.list);

packages/collector/src/cmdline.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ exports.getCmdline = function getCmdline() {
3535
}
3636
} catch (err) {
3737
if (err.code !== 'ENOENT') {
38-
logger.warn('cmdline could not be retrieved via proc file. Reason: %s', err.message);
38+
logger.warn(`cmdline could not be retrieved via proc file. Reason: ${err?.message}`);
3939
}
4040
}
4141

packages/collector/src/metrics/transmissionCycle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function sendMetrics() {
114114
*/
115115
function onMetricsHaveBeenSent(isFullTransmission, transmittedValue, error, responsePayload) {
116116
if (error) {
117-
logger.error('Error received while trying to send snapshot data and metrics: %s', error.message);
117+
logger.error(`Error received while trying to send snapshot data and metrics: ${error?.message} ${error?.stack}`);
118118
if (onError) {
119119
onError();
120120
}

packages/collector/src/pidStore/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ const eventName = 'pidChanged';
2121
const eventEmitter = new EventEmitter();
2222
exports.onPidChange = eventEmitter.on.bind(eventEmitter, eventName);
2323

24-
logger.info('PID Store starting with pid %s', internalPidStore.pid);
24+
logger.info(`PID Store starting with pid ${internalPidStore.pid}`);
2525

2626
Object.defineProperty(exports, 'pid', {
2727
get: function getPid() {
2828
return internalPidStore.pid;
2929
},
3030
set: function setPid(newPid) {
3131
if (internalPidStore.pid !== newPid) {
32-
logger.info('Changing pid to %s', newPid);
32+
logger.info(`Changing pid to ${newPid}`);
3333
internalPidStore.pid = newPid;
3434
eventEmitter.emit(eventName, internalPidStore.pid);
3535
}
@@ -52,7 +52,7 @@ if (!process.env.CONTINUOUS_INTEGRATION) {
5252
const pidInParentNamespace = getPidFromParentNamespace();
5353
if (pidInParentNamespace) {
5454
internalPidStore.pid = pidInParentNamespace;
55-
logger.info('Changing pid to %s due to successful identification of PID in parent namespace', pidInParentNamespace);
55+
logger.info(`Changing pid to ${pidInParentNamespace} due to successful identification of PID in parent namespace`);
5656
}
5757
}
5858

@@ -75,7 +75,7 @@ function getPidFromParentNamespace() {
7575
return pidInSchedFile;
7676
} catch (err) {
7777
if (err.code !== 'ENOENT') {
78-
logger.warn('PID could not be read from sched file. Reason: %s', err.message);
78+
logger.warn(`PID could not be read from sched file. Reason: ${err?.message}`);
7979
}
8080
}
8181
}

packages/collector/src/uncaught/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function onUnhandledRejection(reason) {
101101

102102
downstreamConnection.sendEvent(createEventForUnhandledRejection(reason), error => {
103103
if (error) {
104-
logger.error('Error received while trying to send event to agent: %s', error.message);
104+
logger.error(`Error received while trying to send event to agent: ${error?.message}`);
105105
}
106106
});
107107
}

packages/collector/src/util/initializedTooLate.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ exports.check = function check() {
4343
function fireMonitoringEvent() {
4444
agentConnection.sendAgentMonitoringEvent('nodejs_collector_initialized_too_late', 'TRACER', error => {
4545
if (error) {
46-
logger.error('Error received while trying to send Agent Monitoring Event to agent: %s', error.message);
46+
logger.error(`Error received while trying to send Agent Monitoring Event to agent: ${error?.message}`);
4747
}
4848
});
4949
}

packages/core/src/secrets.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ exports.matchers = {
123123
regexes.push(new RegExp(regexString));
124124
} catch (e) {
125125
logger.warn(
126-
'Received invalid regex from agent: %s - this regex will not be used for removing secrets',
127-
regexString
126+
`Received invalid regex from agent: ${regexString} - this regex will not be used for removing secrets`
128127
);
129128
}
130129
});
@@ -166,7 +165,7 @@ function checkSecrets(configuredSecrets) {
166165
if (typeof s === 'string') {
167166
secrets.push(s);
168167
} else {
169-
logger.warn('Received invalid secret key from agent: %s - this key will not be used for removing secrets', s);
168+
logger.warn(`Received invalid secret key from agent: ${s} - this key will not be used for removing secrets`);
170169
}
171170
});
172171
return secrets;
@@ -185,7 +184,7 @@ function toLowerCase(configuredSecrets) {
185184
if (typeof s === 'string') {
186185
secrets.push(s.toLowerCase());
187186
} else {
188-
logger.warn('Received invalid secret key from agent: %s - this key will not be used for removing secrets', s);
187+
logger.warn(`Received invalid secret key from agent: ${s} - this key will not be used for removing secrets`);
189188
}
190189
});
191190
return secrets;
@@ -224,11 +223,11 @@ exports.init = function init(config) {
224223
*/
225224
exports.setMatcher = function setMatcher(matcherId, secretsList) {
226225
if (!(typeof matcherId === 'string')) {
227-
logger.warn('Received invalid secrets configuration, attribute matcher is not a string: $s', matcherId);
226+
logger.warn(`Received invalid secrets configuration, attribute matcher is not a string: ${matcherId}`);
228227
} else if (Object.keys(exports.matchers).indexOf(matcherId) < 0) {
229-
logger.warn('Received invalid secrets configuration, matcher is not supported: $s', matcherId);
228+
logger.warn(`Received invalid secrets configuration, matcher is not supported: ${matcherId}`);
230229
} else if (!Array.isArray(secretsList)) {
231-
logger.warn('Received invalid secrets configuration, attribute list is not an array: $s', secretsList);
230+
logger.warn(`Received invalid secrets configuration, attribute list is not an array: ${secretsList}`);
232231
} else {
233232
isSecretInternal = exports.matchers[matcherId](secretsList);
234233
}

packages/core/src/tracing/cls.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class InstanaPseudoSpan extends InstanaSpan {
231231
function startSpan(spanName, kind, traceId, parentSpanId, w3cTraceContext) {
232232
tracingMetrics.incrementOpened();
233233
if (!kind || (kind !== ENTRY && kind !== EXIT && kind !== INTERMEDIATE)) {
234-
logger.warn('Invalid span (%s) without kind/with invalid kind: %s, assuming EXIT.', spanName, kind);
234+
logger.warn(`Invalid span (${spanName}) without kind/with invalid kind: ${kind}, assuming EXIT.`);
235235
kind = EXIT;
236236
}
237237
const span = new InstanaSpan(spanName);
@@ -303,7 +303,7 @@ function startSpan(spanName, kind, traceId, parentSpanId, w3cTraceContext) {
303303
*/
304304
function putPseudoSpan(spanName, kind, traceId, spanId) {
305305
if (!kind || (kind !== ENTRY && kind !== EXIT && kind !== INTERMEDIATE)) {
306-
logger.warn('Invalid pseudo span (%s) without kind/with invalid kind: %s, assuming EXIT.', spanName, kind);
306+
logger.warn(`Invalid pseudo span (${spanName}) without kind/with invalid kind: ${kind}, assuming EXIT.`);
307307
kind = EXIT;
308308
}
309309
const span = new InstanaPseudoSpan(spanName);

packages/core/src/tracing/shimmer.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ exports.wrap = (origObject, origMethod, instrumentationWrapperMethod) => {
8585
throw err;
8686
}
8787

88-
logger.warn(`An internal error happend in the Instana Node.js collector. Please contact support. ${err.stack}`);
88+
logger.warn(
89+
`An internal error happend in the Instana Node.js collector. Please contact support. ${err?.stack}`
90+
);
8991

9092
if (originalCalled) return;
9193
originalCalled = true;

0 commit comments

Comments
 (0)