diff --git a/packages/collector/test/apps/agentStub.js b/packages/collector/test/apps/agentStub.js index 05799b9f06..8923789ac3 100644 --- a/packages/collector/test/apps/agentStub.js +++ b/packages/collector/test/apps/agentStub.js @@ -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']; @@ -67,6 +68,14 @@ app.use( // Use this endpoint 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' }); }); @@ -133,6 +142,7 @@ app.put('/com.instana.plugin.nodejs.discovery', (req, res) => { response.tracing.disable = disable; } } + res.send(response); }); diff --git a/packages/collector/test/apps/agentStubControls.js b/packages/collector/test/apps/agentStubControls.js index 6088dbd2b6..8f5d16ae97 100644 --- a/packages/collector/test/apps/agentStubControls.js +++ b/packages/collector/test/apps/agentStubControls.js @@ -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'; } diff --git a/packages/collector/test/tracing/misc/activate_immediately/app.js b/packages/collector/test/tracing/misc/activate_immediately/app.js new file mode 100644 index 0000000000..6d6e06c009 --- /dev/null +++ b/packages/collector/test/tracing/misc/activate_immediately/app.js @@ -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); +} diff --git a/packages/collector/test/tracing/misc/activate_immediately/test.js b/packages/collector/test/tracing/misc/activate_immediately/test.js new file mode 100644 index 0000000000..767a1defca --- /dev/null +++ b/packages/collector/test/tracing/misc/activate_immediately/test.js @@ -0,0 +1,79 @@ +/* + * (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(); + await customAgent.stopAgent(); + }); + + 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) + ]); + }) + ); + }); +});