Skip to content

Commit 542e088

Browse files
authored
refactor(core): replaced agent config with updated config in instrumentations (#2509)
1 parent e28bcbc commit 542e088

9 files changed

Lines changed: 304 additions & 52 deletions

File tree

packages/collector/test/integration/currencies/messaging/kafkajs/test_base.js

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,152 @@ module.exports = function (name, version, isLatest, mode) {
411411
);
412412
});
413413

414+
describe('traceCorrelation configuration precedence', function () {
415+
describe('when agent enables traceCorrelation (overriding default)', function () {
416+
const customAgentControls = new AgentStubControls();
417+
418+
let consumerControls;
419+
let producerControls;
420+
421+
before(async () => {
422+
await customAgentControls.startAgent({
423+
kafkaConfig: { traceCorrelation: true }
424+
});
425+
426+
consumerControls = new ProcessControls({
427+
dirname: __dirname,
428+
appName: 'consumer',
429+
agentControls: customAgentControls,
430+
env: { ...libraryEnv }
431+
});
432+
producerControls = new ProcessControls({
433+
dirname: __dirname,
434+
appName: 'producer',
435+
agentControls: customAgentControls,
436+
env: { ...libraryEnv }
437+
});
438+
439+
await consumerControls.startAndWaitForAgentConnection(retryTime, retryTimeUntil());
440+
await producerControls.startAndWaitForAgentConnection(retryTime, retryTimeUntil());
441+
});
442+
443+
beforeEach(async () => {
444+
await customAgentControls.clearReceivedTraceData();
445+
await resetMessages(consumerControls);
446+
});
447+
448+
afterEach(async () => {
449+
await resetMessages(consumerControls);
450+
});
451+
452+
after(async () => {
453+
await customAgentControls.stopAgent();
454+
await producerControls.stop();
455+
await consumerControls.stop();
456+
});
457+
458+
it('must maintain trace continuity when agent config enables traceCorrelation', async () => {
459+
await producerControls.sendRequest({
460+
method: 'POST',
461+
path: '/send-messages',
462+
simple: true,
463+
body: JSON.stringify({
464+
key: 'someKey',
465+
value: 'someMessage'
466+
}),
467+
headers: {
468+
'Content-Type': 'application/json'
469+
}
470+
});
471+
472+
await retry(async () => {
473+
const messages = await getMessages(consumerControls);
474+
checkMessages(messages, {});
475+
const spans = await customAgentControls.getSpans();
476+
const httpEntry = verifyHttpEntry(spans);
477+
verifyKafkaExits(spans, httpEntry, {});
478+
verifyFollowUpHttpExit(spans, httpEntry);
479+
});
480+
});
481+
});
482+
483+
describe('when both agent and environment variable configuration are provided', function () {
484+
const customAgentControls = new AgentStubControls();
485+
486+
let consumerControls;
487+
let producerControls;
488+
489+
before(async () => {
490+
await customAgentControls.startAgent({
491+
kafkaConfig: { traceCorrelation: true }
492+
});
493+
494+
consumerControls = new ProcessControls({
495+
dirname: __dirname,
496+
appName: 'consumer',
497+
agentControls: customAgentControls,
498+
env: {
499+
...libraryEnv,
500+
INSTANA_KAFKA_TRACE_CORRELATION: 'false'
501+
}
502+
});
503+
producerControls = new ProcessControls({
504+
dirname: __dirname,
505+
appName: 'producer',
506+
agentControls: customAgentControls,
507+
env: {
508+
...libraryEnv,
509+
INSTANA_KAFKA_TRACE_CORRELATION: 'false'
510+
}
511+
});
512+
513+
await consumerControls.startAndWaitForAgentConnection(retryTime, retryTimeUntil());
514+
await producerControls.startAndWaitForAgentConnection(retryTime, retryTimeUntil());
515+
});
516+
517+
beforeEach(async () => {
518+
await customAgentControls.clearReceivedTraceData();
519+
await resetMessages(consumerControls);
520+
});
521+
522+
afterEach(async () => {
523+
await resetMessages(consumerControls);
524+
});
525+
526+
after(async () => {
527+
await customAgentControls.stopAgent();
528+
await producerControls.stop();
529+
await consumerControls.stop();
530+
});
531+
532+
const kafkaCorrelation = 'correlation-disabled';
533+
534+
it('must not maintain trace continuity when env var disables traceCorrelation (taking precedence over agent config)', async () => {
535+
await producerControls.sendRequest({
536+
method: 'POST',
537+
path: '/send-messages',
538+
simple: true,
539+
body: JSON.stringify({
540+
key: 'someKey',
541+
value: 'someMessage'
542+
}),
543+
headers: {
544+
'Content-Type': 'application/json'
545+
}
546+
});
547+
548+
await retry(async () => {
549+
const messages = await getMessages(consumerControls);
550+
checkMessages(messages, { kafkaCorrelation });
551+
const spans = await customAgentControls.getSpans();
552+
const httpEntry = verifyHttpEntry(spans);
553+
verifyKafkaExits(spans, httpEntry, { kafkaCorrelation });
554+
verifyFollowUpHttpExit(spans, httpEntry);
555+
});
556+
});
557+
});
558+
});
559+
414560
describe('tracing disabled', () => {
415561
let consumerControls;
416562
let producerControls;

packages/collector/test/integration/currencies/protocols/http/client/clientApp.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ app.get('/request-options-only', (req, res) => {
8989
],
9090
'x-exit-options-not-captured-header': 'whatever'
9191
};
92+
} else if (req.query.withHeader === 'config-test') {
93+
downstreamRequest.headers = {
94+
'X-Agent-Header-1': 'agent-value-1',
95+
'X-Agent-Header-2': 'agent-value-2',
96+
'X-Incode-Header-1': 'incode-value-1',
97+
'X-Incode-Header-2': 'incode-value-2',
98+
'X-Not-Configured-Header': 'should-not-be-captured'
99+
};
92100
}
93101
const requestObject = httpModule.request(downstreamRequest, () => res.sendStatus(200));
94102
if (req.query.withHeader === 'set-on-request') {

packages/collector/test/integration/currencies/protocols/http/client/test_base.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,137 @@ module.exports = function (name, version, isLatest) {
3838
registerConnectionRefusalTest.call(this, false);
3939
registerConnectionRefusalTest.call(this, true);
4040

41+
describe('extraHttpHeadersToCapture configuration precedence', function () {
42+
describe('when only agent configuration is provided', function () {
43+
let serverControls;
44+
let clientControls;
45+
const customAgentControls = new AgentStubControls();
46+
47+
before(async () => {
48+
await customAgentControls.startAgent({
49+
extraHeaders: ['x-agent-header-1', 'x-agent-header-2']
50+
});
51+
52+
serverControls = new ProcessControls({
53+
agentControls: customAgentControls,
54+
dirname: __dirname,
55+
appName: 'serverApp',
56+
appUsesHttps: false
57+
});
58+
59+
clientControls = new ProcessControls({
60+
dirname: __dirname,
61+
appName: 'clientApp',
62+
agentControls: customAgentControls,
63+
appUsesHttps: false,
64+
env: {
65+
...commonEnv,
66+
SERVER_PORT: serverControls.getPort()
67+
}
68+
});
69+
70+
await serverControls.startAndWaitForAgentConnection();
71+
await clientControls.startAndWaitForAgentConnection();
72+
});
73+
74+
after(() => Promise.all([serverControls.stop(), clientControls.stop(), customAgentControls.stopAgent()]));
75+
76+
beforeEach(() => customAgentControls.clearReceivedTraceData());
77+
78+
afterEach(() => Promise.all([serverControls.clearIpcMessages(), clientControls.clearIpcMessages()]));
79+
80+
it('should capture headers configured by agent (overriding defaults)', async () => {
81+
await clientControls.sendRequest({
82+
method: 'GET',
83+
path: '/request-options-only?withHeader=config-test'
84+
});
85+
86+
await retry(async () => {
87+
const spans = await customAgentControls.getSpans();
88+
expectExactlyOneMatching(spans, [
89+
span => expect(span.n).to.equal('node.http.client'),
90+
span => expect(span.k).to.equal(constants.EXIT),
91+
span => {
92+
expect(span.data.http.header).to.exist;
93+
94+
expect(span.data.http.header['x-agent-header-1']).to.equal('agent-value-1');
95+
expect(span.data.http.header['x-agent-header-2']).to.equal('agent-value-2');
96+
97+
expect(span.data.http.header['x-incode-header-1']).to.be.undefined;
98+
expect(span.data.http.header['x-incode-header-2']).to.be.undefined;
99+
expect(span.data.http.header['x-not-configured-header']).to.be.undefined;
100+
}
101+
]);
102+
});
103+
});
104+
});
105+
106+
describe('when both agent and environment variable configuration are provided', function () {
107+
let serverControls;
108+
let clientControls;
109+
const customAgentControls = new AgentStubControls();
110+
111+
before(async () => {
112+
await customAgentControls.startAgent({
113+
extraHeaders: ['x-agent-header-1', 'x-agent-header-2']
114+
});
115+
116+
serverControls = new ProcessControls({
117+
agentControls: customAgentControls,
118+
dirname: __dirname,
119+
appName: 'serverApp',
120+
appUsesHttps: false
121+
});
122+
123+
clientControls = new ProcessControls({
124+
dirname: __dirname,
125+
appName: 'clientApp',
126+
agentControls: customAgentControls,
127+
appUsesHttps: false,
128+
env: {
129+
...commonEnv,
130+
SERVER_PORT: serverControls.getPort(),
131+
INSTANA_EXTRA_HTTP_HEADERS: 'x-incode-header-1,x-incode-header-2'
132+
}
133+
});
134+
135+
await serverControls.startAndWaitForAgentConnection();
136+
await clientControls.startAndWaitForAgentConnection();
137+
});
138+
139+
after(() => Promise.all([serverControls.stop(), clientControls.stop(), customAgentControls.stopAgent()]));
140+
141+
beforeEach(() => customAgentControls.clearReceivedTraceData());
142+
143+
afterEach(() => Promise.all([serverControls.clearIpcMessages(), clientControls.clearIpcMessages()]));
144+
145+
it('should capture headers from environment variable config (taking precedence over agent config)', async () => {
146+
await clientControls.sendRequest({
147+
method: 'GET',
148+
path: '/request-options-only?withHeader=config-test'
149+
});
150+
151+
await retry(async () => {
152+
const spans = await customAgentControls.getSpans();
153+
expectExactlyOneMatching(spans, [
154+
span => expect(span.n).to.equal('node.http.client'),
155+
span => expect(span.k).to.equal(constants.EXIT),
156+
span => {
157+
expect(span.data.http.header).to.exist;
158+
159+
expect(span.data.http.header['x-incode-header-1']).to.equal('incode-value-1');
160+
expect(span.data.http.header['x-incode-header-2']).to.equal('incode-value-2');
161+
162+
expect(span.data.http.header['x-agent-header-1']).to.be.undefined;
163+
expect(span.data.http.header['x-agent-header-2']).to.be.undefined;
164+
expect(span.data.http.header['x-not-configured-header']).to.be.undefined;
165+
}
166+
]);
167+
});
168+
});
169+
});
170+
});
171+
41172
describe('SDK CASE 1', function () {
42173
let sdkControls;
43174

packages/core/src/tracing/instrumentation/messaging/rdkafka.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,10 @@ exports.init = function init(config) {
2727
exports.updateConfig = function updateConfig(config) {
2828
traceCorrelationEnabled = config.tracing.kafka.traceCorrelation;
2929
};
30-
// The extraConfig is coming from the agent configs. You can set the kafka format in the agent.
31-
exports.activate = function activate(extraConfig) {
32-
if (extraConfig && extraConfig.tracing && extraConfig.tracing.kafka) {
33-
if (extraConfig.tracing.kafka.traceCorrelation != null) {
34-
traceCorrelationEnabled = extraConfig.tracing.kafka.traceCorrelation;
35-
}
36-
}
30+
31+
//
32+
exports.activate = function activate(_config) {
33+
traceCorrelationEnabled = _config.tracing.kafka.traceCorrelation;
3734
isActive = true;
3835
};
3936

packages/core/src/tracing/instrumentation/protocols/http2Client.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,9 @@ exports.updateConfig = config => {
3535
extraHttpHeadersToCapture = config.tracing.http.extraHttpHeadersToCapture;
3636
};
3737

38-
exports.activate = function activate(extraConfig) {
39-
if (
40-
extraConfig &&
41-
extraConfig.tracing &&
42-
extraConfig.tracing.http &&
43-
Array.isArray(extraConfig.tracing.http.extraHttpHeadersToCapture)
44-
) {
45-
extraHttpHeadersToCapture = extraConfig.tracing.http.extraHttpHeadersToCapture;
46-
}
38+
exports.activate = function activate(_config) {
39+
extraHttpHeadersToCapture = _config.tracing.http.extraHttpHeadersToCapture;
40+
4741
isActive = true;
4842
};
4943

packages/core/src/tracing/instrumentation/protocols/http2Server.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,9 @@ exports.updateConfig = function updateConfig(config) {
3838
extraHttpHeadersToCapture = config.tracing.http.extraHttpHeadersToCapture;
3939
};
4040

41-
exports.activate = function activate(extraConfig) {
42-
if (
43-
extraConfig &&
44-
extraConfig.tracing &&
45-
extraConfig.tracing.http &&
46-
Array.isArray(extraConfig.tracing.http.extraHttpHeadersToCapture)
47-
) {
48-
extraHttpHeadersToCapture = extraConfig.tracing.http.extraHttpHeadersToCapture;
49-
}
41+
exports.activate = function activate(_config) {
42+
extraHttpHeadersToCapture = _config.tracing.http.extraHttpHeadersToCapture;
43+
5044
isActive = true;
5145
};
5246

packages/core/src/tracing/instrumentation/protocols/httpClient.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,9 @@ exports.updateConfig = function updateConfig(config) {
4343
extraHttpHeadersToCapture = config.tracing.http.extraHttpHeadersToCapture;
4444
};
4545

46-
exports.activate = function activate(extraConfig) {
47-
if (
48-
extraConfig &&
49-
extraConfig.tracing &&
50-
extraConfig.tracing.http &&
51-
Array.isArray(extraConfig.tracing.http.extraHttpHeadersToCapture)
52-
) {
53-
extraHttpHeadersToCapture = extraConfig.tracing.http.extraHttpHeadersToCapture;
54-
}
46+
exports.activate = function activate(_config) {
47+
extraHttpHeadersToCapture = _config.tracing.http.extraHttpHeadersToCapture;
48+
5549
isActive = true;
5650
};
5751

packages/core/src/tracing/instrumentation/protocols/httpServer.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,9 @@ exports.updateConfig = function updateConfig(config) {
3232
extraHttpHeadersToCapture = config.tracing.http.extraHttpHeadersToCapture;
3333
};
3434

35-
exports.activate = function activate(extraConfig) {
36-
if (
37-
extraConfig &&
38-
extraConfig.tracing &&
39-
extraConfig.tracing.http &&
40-
Array.isArray(extraConfig.tracing.http.extraHttpHeadersToCapture)
41-
) {
42-
extraHttpHeadersToCapture = extraConfig.tracing.http.extraHttpHeadersToCapture;
43-
}
35+
exports.activate = function activate(_config) {
36+
extraHttpHeadersToCapture = _config.tracing.http.extraHttpHeadersToCapture;
37+
4438
isActive = true;
4539
};
4640

0 commit comments

Comments
 (0)