Skip to content

Commit 0fda268

Browse files
committed
test: added test for INSTANA_TRACE_IMMEDIATELY
refs https://jsw.ibm.com/browse/INSTA-43861
1 parent 8c6b0d0 commit 0fda268

File tree

4 files changed

+139
-1
lines changed

4 files changed

+139
-1
lines changed

packages/collector/test/apps/agentStub.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ if (process.env.INSTANA_DEBUG === 'true') {
2929
// NOTE: we can leave the hardcoded port here as this file is not used in the test env!
3030
const port = process.env.AGENT_PORT || 42699;
3131
const uniqueAgentUuids = process.env.AGENT_UNIQUE_UUIDS === 'true';
32+
const slowHostResponse = process.env.SLOW_HOST_RESPONSE === 'true';
3233
const extraHeaders = process.env.EXTRA_HEADERS ? process.env.EXTRA_HEADERS.split(',') : [];
3334
const secretsMatcher = process.env.SECRETS_MATCHER ? process.env.SECRETS_MATCHER : 'contains-ignore-case';
3435
const secretsList = process.env.SECRETS_LIST ? process.env.SECRETS_LIST.split(',') : ['pass', 'secret', 'token'];
@@ -65,10 +66,24 @@ app.use(
6566
})
6667
);
6768

69+
// This endpoint is for the "checkHost" (agentHostLookup) functionality.
6870
app.get('/', (req, res) => {
71+
if (slowHostResponse) {
72+
setTimeout(() => {
73+
res.json({ version: '1.1.999' });
74+
}, 1000 * 3);
75+
76+
return;
77+
}
78+
6979
res.json({ version: '1.1.999' });
7080
});
7181

82+
// This endpoint is for our test env to check if the instance is up.
83+
app.get('/ping', (req, res) => {
84+
res.send('pong');
85+
});
86+
7287
app.put('/com.instana.plugin.nodejs.discovery', (req, res) => {
7388
const pid = req.body.pid;
7489

@@ -127,6 +142,7 @@ app.put('/com.instana.plugin.nodejs.discovery', (req, res) => {
127142
response.tracing.disable = disable;
128143
}
129144
}
145+
130146
res.send(response);
131147
});
132148

packages/collector/test/apps/agentStubControls.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class AgentStubControls {
4242
env.SLOW_METRICS_REPLY = opts.slowMetricsReply.toString();
4343
}
4444

45+
if (opts.slowHostResponse) {
46+
env.SLOW_HOST_RESPONSE = opts.slowHostResponse.toString();
47+
}
48+
4549
if (opts.enableSpanBatching) {
4650
env.ENABLE_SPANBATCHING = 'true';
4751
}
@@ -80,7 +84,7 @@ class AgentStubControls {
8084
}
8185

8286
async waitUntilAgentHasStarted() {
83-
const url = `http://127.0.0.1:${this.agentPort}`;
87+
const url = `http://127.0.0.1:${this.agentPort}/ping`;
8488

8589
// eslint-disable-next-line no-console
8690
console.log(`[AgentStubControls] starting: ${url}`);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
16+
const logPrefix = `Activate Immediately App (${process.pid}):\t`;
17+
const port = require('../../../test_util/app-port')();
18+
const express = require('express');
19+
const app = express();
20+
const agentPort = process.env.INSTANA_AGENT_PORT;
21+
22+
app.get('/', (req, res) => {
23+
res.send();
24+
});
25+
26+
app.get('/trigger', async (req, res) => {
27+
await fetch(`http://localhost:${agentPort}/ping`);
28+
res.send();
29+
});
30+
31+
app.listen(port, () => {
32+
log(`Listening on port: ${port}`);
33+
});
34+
35+
function log() {
36+
/* eslint-disable no-console */
37+
const args = Array.prototype.slice.call(arguments);
38+
args[0] = logPrefix + args[0];
39+
console.log.apply(console, args);
40+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* (c) Copyright IBM Corp. 2025
3+
*/
4+
5+
'use strict';
6+
7+
const expect = require('chai').expect;
8+
const constants = require('@instana/core').tracing.constants;
9+
const supportedVersion = require('@instana/core').tracing.supportedVersion;
10+
const config = require('../../../../../core/test/config');
11+
const testUtils = require('../../../../../core/test/test_util');
12+
const ProcessControls = require('../../../test_util/ProcessControls');
13+
const { AgentStubControls } = require('../../../apps/agentStubControls');
14+
const mochaSuiteFn = supportedVersion(process.versions.node) ? describe : describe.skip;
15+
16+
mochaSuiteFn.only('tracing/activateImmediately', function () {
17+
this.timeout(config.getTestTimeout());
18+
19+
let customAgent;
20+
let appControls;
21+
22+
before(async () => {
23+
customAgent = new AgentStubControls();
24+
25+
await customAgent.startAgent({
26+
slowHostResponse: true
27+
});
28+
29+
appControls = new ProcessControls({
30+
dirname: __dirname,
31+
useGlobalAgent: true,
32+
agentControls: customAgent,
33+
collectorUninitialized: true,
34+
env: {
35+
INSTANA_TRACE_IMMEDIATELY: 'true',
36+
INSTANA_AGENT_REQUEST_TIMEOUT: '6000'
37+
}
38+
});
39+
40+
await appControls.start();
41+
await testUtils.delay(100);
42+
43+
await appControls.sendRequest({
44+
method: 'GET',
45+
path: '/trigger'
46+
});
47+
});
48+
49+
beforeEach(async () => {
50+
await customAgent.clearReceivedTraceData();
51+
});
52+
53+
after(async () => {
54+
await appControls.stop();
55+
});
56+
57+
afterEach(async () => {
58+
await appControls.clearIpcMessages();
59+
});
60+
61+
it('must trace', async () => {
62+
return testUtils.retry(() =>
63+
customAgent.getSpans().then(spans => {
64+
expect(spans.length).to.equal(2);
65+
66+
testUtils.expectExactlyOneMatching(spans, [
67+
span => expect(span.n).to.equal('node.http.server'),
68+
span => expect(span.k).to.equal(constants.ENTRY)
69+
]);
70+
71+
testUtils.expectExactlyOneMatching(spans, [
72+
span => expect(span.n).to.equal('node.http.client'),
73+
span => expect(span.k).to.equal(constants.EXIT)
74+
]);
75+
})
76+
);
77+
});
78+
});

0 commit comments

Comments
 (0)