Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/collector/test/apps/agentStub.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ if (process.env.INSTANA_DEBUG === 'true') {
// NOTE: we can leave the hardcoded port here as this file is not used in the test env!
const port = process.env.AGENT_PORT || 42699;
const uniqueAgentUuids = process.env.AGENT_UNIQUE_UUIDS === 'true';
const slowHostResponse = process.env.SLOW_HOST_RESPONSE === 'true';
const extraHeaders = process.env.EXTRA_HEADERS ? process.env.EXTRA_HEADERS.split(',') : [];
const secretsMatcher = process.env.SECRETS_MATCHER ? process.env.SECRETS_MATCHER : 'contains-ignore-case';
const secretsList = process.env.SECRETS_LIST ? process.env.SECRETS_LIST.split(',') : ['pass', 'secret', 'token'];
Expand Down Expand Up @@ -65,10 +66,24 @@ app.use(
})
);

// This endpoint is for the "checkHost" (agentHostLookup) functionality.
app.get('/', (req, res) => {
if (slowHostResponse) {
setTimeout(() => {
res.json({ version: '1.1.999' });
}, 1000 * 3);

return;
}

res.json({ version: '1.1.999' });
});

// This endpoint is for our test env to check if the instance is up.
app.get('/ping', (req, res) => {
res.send('pong');
});

app.put('/com.instana.plugin.nodejs.discovery', (req, res) => {
const pid = req.body.pid;

Expand Down Expand Up @@ -127,6 +142,7 @@ app.put('/com.instana.plugin.nodejs.discovery', (req, res) => {
response.tracing.disable = disable;
}
}

res.send(response);
});

Expand Down
6 changes: 5 additions & 1 deletion packages/collector/test/apps/agentStubControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class AgentStubControls {
env.SLOW_METRICS_REPLY = opts.slowMetricsReply.toString();
}

if (opts.slowHostResponse) {
env.SLOW_HOST_RESPONSE = opts.slowHostResponse.toString();
}

if (opts.enableSpanBatching) {
env.ENABLE_SPANBATCHING = 'true';
}
Expand Down Expand Up @@ -80,7 +84,7 @@ class AgentStubControls {
}

async waitUntilAgentHasStarted() {
const url = `http://127.0.0.1:${this.agentPort}`;
const url = `http://127.0.0.1:${this.agentPort}/ping`;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will outsource to main separately.


// eslint-disable-next-line no-console
console.log(`[AgentStubControls] starting: ${url}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* (c) Copyright IBM Corp. 2025
*/

'use strict';

// NOTE: c8 bug https://github.com/bcoe/c8/issues/166
process.on('SIGTERM', () => {
process.disconnect();
process.exit(0);
});

require('../../../..')();

const logPrefix = `Activate Immediately App (${process.pid}):\t`;
const port = require('../../../test_util/app-port')();
const express = require('express');
const app = express();
const agentPort = process.env.INSTANA_AGENT_PORT;

app.get('/', (req, res) => {
res.send();
});

app.get('/trigger', async (req, res) => {
await fetch(`http://localhost:${agentPort}/ping`);
res.send();
});

app.listen(port, () => {
log(`Listening on port: ${port}`);
});

function log() {
/* eslint-disable no-console */
const args = Array.prototype.slice.call(arguments);
args[0] = logPrefix + args[0];
console.log.apply(console, args);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* (c) Copyright IBM Corp. 2025
*/

'use strict';

const expect = require('chai').expect;
const constants = require('@instana/core').tracing.constants;
const supportedVersion = require('@instana/core').tracing.supportedVersion;
const config = require('../../../../../core/test/config');
const testUtils = require('../../../../../core/test/test_util');
const ProcessControls = require('../../../test_util/ProcessControls');
const { AgentStubControls } = require('../../../apps/agentStubControls');
const mochaSuiteFn = supportedVersion(process.versions.node) ? describe : describe.skip;

mochaSuiteFn('tracing/activateImmediately', function () {
this.timeout(config.getTestTimeout());

let customAgent;
let appControls;

before(async () => {
customAgent = new AgentStubControls();

await customAgent.startAgent({
slowHostResponse: true
});

appControls = new ProcessControls({
dirname: __dirname,
useGlobalAgent: true,
agentControls: customAgent,
collectorUninitialized: true,
env: {
INSTANA_TRACE_IMMEDIATELY: 'true',
INSTANA_AGENT_REQUEST_TIMEOUT: '6000'
}
});

await appControls.start();
await testUtils.delay(100);

await appControls.sendRequest({
method: 'GET',
path: '/trigger'
});
});

beforeEach(async () => {
await customAgent.clearReceivedTraceData();
});

after(async () => {
await appControls.stop();
});

afterEach(async () => {
await appControls.clearIpcMessages();
});

it('must trace', async () => {
return testUtils.retry(() =>
customAgent.getSpans().then(spans => {
expect(spans.length).to.equal(2);

testUtils.expectExactlyOneMatching(spans, [
span => expect(span.n).to.equal('node.http.server'),
span => expect(span.k).to.equal(constants.ENTRY)
]);

testUtils.expectExactlyOneMatching(spans, [
span => expect(span.n).to.equal('node.http.client'),
span => expect(span.k).to.equal(constants.EXIT)
]);
})
);
});
});