Skip to content

Commit 2c90b9a

Browse files
committed
chore: added test
1 parent a1f2918 commit 2c90b9a

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

packages/collector/test/apps/agentStub.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const ignoreEndpoints = process.env.IGNORE_ENDPOINTS && JSON.parse(process.env.I
4646
const disable = process.env.AGENT_DISABLE_TRACING && JSON.parse(process.env.AGENT_DISABLE_TRACING);
4747

4848
const uuids = {};
49+
const agentLogs = [];
4950
let discoveries = {};
5051
let rejectAnnounceAttempts = 0;
5152
let requests = {};
@@ -136,6 +137,16 @@ app.head(
136137
})
137138
);
138139

140+
app.post('/com.instana.agent.logger', (req, res) => {
141+
logger.info('Received log message from agent: %j', req.body);
142+
agentLogs.push(req.body);
143+
res.sendStatus(200);
144+
});
145+
146+
app.get('/agent/logs', (req, res) => {
147+
res.json(agentLogs);
148+
});
149+
139150
app.post(
140151
'/com.instana.plugin.nodejs.:pid',
141152
checkExistenceOfKnownPid(function handleEntityData(req, res) {

packages/collector/test/apps/agentStubControls.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ class AgentStubControls {
175175
});
176176
}
177177

178+
getAgentLogs() {
179+
return fetch(`http://127.0.0.1:${this.agentPort}/agent/logs`, {
180+
method: 'GET',
181+
headers: {
182+
'Content-Type': 'application/json'
183+
}
184+
}).then(response => response.json());
185+
}
186+
178187
async reset() {
179188
// eslint-disable-next-line no-console
180189
console.log(`[AgentStubControls] reset ${this.agentPort}`);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* (c) Copyright IBM Corp. 20215
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+
const agentPort = process.env.INSTANA_AGENT_PORT;
14+
15+
require('../../../..')({
16+
// NOTE: The test env mutes all logs by default. No logs, no agent logs.
17+
level: 'info'
18+
});
19+
20+
const express = require('express');
21+
const port = require('../../../test_util/app-port')();
22+
const app = express();
23+
24+
const logPrefix = `Agent Logs App (${process.pid}):\t`;
25+
26+
app.get('/', (req, res) => {
27+
res.send();
28+
});
29+
30+
app.get('/trace', async (req, res) => {
31+
await fetch(`http://127.0.0.1:${agentPort}`);
32+
res.send();
33+
});
34+
35+
app.listen(port, () => {
36+
log(`Listening on port: ${port}`);
37+
});
38+
39+
function log() {
40+
/* eslint-disable no-console */
41+
const args = Array.prototype.slice.call(arguments);
42+
args[0] = logPrefix + args[0];
43+
console.log.apply(console, args);
44+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* (c) Copyright IBM Corp. 2025
3+
*/
4+
5+
'use strict';
6+
7+
const { expect } = require('chai');
8+
const supportedVersion = require('@instana/core').tracing.supportedVersion;
9+
const config = require('../../../../../core/test/config');
10+
const testUtils = require('../../../../../core/test/test_util');
11+
const ProcessControls = require('../../../test_util/ProcessControls');
12+
const globalAgent = require('../../../globalAgent');
13+
const agentControls = globalAgent.instance;
14+
15+
const mochaSuiteFn = supportedVersion(process.versions.node) ? describe : describe.skip;
16+
17+
mochaSuiteFn('tracing/agent logs', function () {
18+
this.timeout(config.getTestTimeout());
19+
globalAgent.setUpCleanUpHooks();
20+
21+
describe('Ensure agent logs are transmitted', function () {
22+
let controls;
23+
24+
before(async () => {
25+
controls = new ProcessControls({
26+
dirname: __dirname,
27+
useGlobalAgent: true
28+
});
29+
30+
await controls.startAndWaitForAgentConnection();
31+
});
32+
33+
beforeEach(async () => {
34+
await agentControls.clearReceivedTraceData();
35+
});
36+
37+
after(async () => {
38+
await controls.stop();
39+
});
40+
41+
it('expect agent logs data', async () => {
42+
await controls.sendRequest({
43+
path: '/trace'
44+
});
45+
46+
await testUtils.retry(async () => {
47+
const spans = await agentControls.getSpans();
48+
const agentLogs = await agentControls.getAgentLogs();
49+
50+
expect(spans.length).to.equal(2);
51+
expect(agentLogs.length).to.equal(9);
52+
});
53+
});
54+
});
55+
});

0 commit comments

Comments
 (0)