Skip to content

Commit 12129e6

Browse files
committed
feat: added INSTANA_DISABLE_METRICS env
refs https://jsw.ibm.com/browse/INSTA-66149
1 parent 3c2c753 commit 12129e6

File tree

5 files changed

+122
-3
lines changed

5 files changed

+122
-3
lines changed

packages/collector/src/announceCycle/agentready.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ function enter(_ctx) {
109109

110110
if (isMainThread) {
111111
uncaught.activate();
112-
metrics.activate();
112+
113+
if (process.env.INSTANA_DISABLE_METRICS !== 'true') {
114+
metrics.activate();
115+
}
116+
113117
requestHandler.activate();
114118
transmissionCycle.activate(
115119
metrics,

packages/collector/src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ function init(userConfig = {}) {
142142

143143
if (isMainThread) {
144144
uncaught.init(config, agentConnection, pidStore);
145-
metrics.init(config, pidStore);
145+
146+
if (process.env.INSTANA_DISABLE_METRICS !== 'true') {
147+
metrics.init(config, pidStore);
148+
} else {
149+
logger.info('Metrics collection is disabled via INSTANA_DISABLE_METRICS.');
150+
}
146151
}
147152

148153
logger.info(`@instana/collector module version: ${require(path.join(__dirname, '..', 'package.json')).version}`);

packages/collector/src/metrics/transmissionCycle.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ exports.activate = function activate(_metrics, _downstreamConnection, _onSuccess
8181
isActive = true;
8282

8383
transmissionsSinceLastFullDataEmit = 0;
84-
sendMetrics();
84+
85+
if (process.env.INSTANA_DISABLE_METRICS !== 'true') {
86+
sendMetrics();
87+
}
8588
};
8689

8790
function sendMetrics() {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* (c) Copyright IBM Corp. 2021
3+
* (c) Copyright Instana Inc. and contributors 2019
4+
*/
5+
6+
'use strict';
7+
8+
// NOTE: c8 bug https://github.com/bcoe/c8/issues/166
9+
process.on('SIGTERM', () => {
10+
process.disconnect();
11+
process.exit(0);
12+
});
13+
14+
require('../../../..')();
15+
const express = require('express');
16+
const morgan = require('morgan');
17+
const bodyParser = require('body-parser');
18+
const port = require('../../../test_util/app-port')();
19+
const app = express();
20+
const logPrefix = `Metrics activation is off (${process.pid}):\t`;
21+
const agentPort = process.env.INSTANA_AGENT_PORT;
22+
23+
if (process.env.WITH_STDOUT) {
24+
app.use(morgan(`${logPrefix}:method :url :status`));
25+
}
26+
27+
app.use(bodyParser.json());
28+
29+
app.get('/', (req, res) => {
30+
res.sendStatus(200);
31+
});
32+
33+
app.post('/create-spans', async (req, res) => {
34+
await fetch(`http://localhost:${agentPort}/ping`);
35+
res.json({ message: 'OK' });
36+
});
37+
38+
app.listen(port, () => {
39+
log(`Listening on port: ${port}`);
40+
});
41+
42+
function log() {
43+
/* eslint-disable no-console */
44+
const args = Array.prototype.slice.call(arguments);
45+
args[0] = logPrefix + args[0];
46+
console.log.apply(console, args);
47+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* (c) Copyright IBM Corp. 2025
3+
*/
4+
5+
'use strict';
6+
7+
const expect = require('chai').expect;
8+
const supportedVersion = require('@instana/core').tracing.supportedVersion;
9+
10+
const config = require('../../../../../core/test/config');
11+
const testUtils = require('../../../../../core/test/test_util');
12+
const ProcessControls = require('../../../test_util/ProcessControls');
13+
const globalAgent = require('../../../globalAgent');
14+
15+
const mochaSuiteFn = supportedVersion(process.versions.node) ? describe : describe.skip;
16+
17+
mochaSuiteFn('tracing/metrics_deactivated', function () {
18+
this.timeout(config.getTestTimeout() * 2);
19+
20+
globalAgent.setUpCleanUpHooks();
21+
const agentControls = globalAgent.instance;
22+
23+
describe('when tracing is enabled', function () {
24+
let controls;
25+
26+
before(async () => {
27+
controls = new ProcessControls({
28+
dirname: __dirname,
29+
useGlobalAgent: true,
30+
env: {
31+
INSTANA_DISABLE_METRICS: 'true',
32+
INSTANA_DEBUG: 'true'
33+
}
34+
});
35+
36+
await controls.startAndWaitForAgentConnection();
37+
});
38+
39+
beforeEach(async () => {
40+
await agentControls.clearReceivedData();
41+
});
42+
43+
after(async () => {
44+
await controls.stop();
45+
});
46+
47+
it('should still trace spans', async () => {
48+
const response = await controls.sendRequest({
49+
method: 'POST',
50+
path: '/create-spans'
51+
});
52+
53+
expect(response.message).to.equal('OK');
54+
await testUtils.retry(async () => {
55+
const spans = await agentControls.getSpans();
56+
expect(spans.length).to.equal(2);
57+
});
58+
});
59+
});
60+
});

0 commit comments

Comments
 (0)