Skip to content

Commit d25bbf4

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

File tree

5 files changed

+121
-3
lines changed

5 files changed

+121
-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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* (c) Copyright IBM Corp. 2025
3+
*/
4+
5+
'use strict';
6+
7+
// NOTE: c8 bug https://github.com/bcoe/c8/issues/166
8+
process.on('SIGTERM', () => {
9+
process.disconnect();
10+
process.exit(0);
11+
});
12+
13+
require('../../../..')();
14+
const express = require('express');
15+
const morgan = require('morgan');
16+
const bodyParser = require('body-parser');
17+
const port = require('../../../test_util/app-port')();
18+
const app = express();
19+
const logPrefix = `Metrics activation is off (${process.pid}):\t`;
20+
const agentPort = process.env.INSTANA_AGENT_PORT;
21+
22+
if (process.env.WITH_STDOUT) {
23+
app.use(morgan(`${logPrefix}:method :url :status`));
24+
}
25+
26+
app.use(bodyParser.json());
27+
28+
app.get('/', (req, res) => {
29+
res.sendStatus(200);
30+
});
31+
32+
app.post('/create-spans', async (req, res) => {
33+
await fetch(`http://localhost:${agentPort}/ping`);
34+
res.json({ message: 'OK' });
35+
});
36+
37+
app.listen(port, () => {
38+
log(`Listening on port: ${port}`);
39+
});
40+
41+
function log() {
42+
/* eslint-disable no-console */
43+
const args = Array.prototype.slice.call(arguments);
44+
args[0] = logPrefix + args[0];
45+
console.log.apply(console, args);
46+
}
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)