Skip to content

Commit db431ac

Browse files
committed
chore: fine tuning
1 parent 44a485d commit db431ac

File tree

8 files changed

+65
-46
lines changed

8 files changed

+65
-46
lines changed

packages/collector/src/announceCycle/unannounced.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const maxRetryDelay = 60 * 1000; // one minute
4343
* @typedef {Object} TracingConfig
4444
* @property {Array.<string>} [extra-http-headers]
4545
* @property {KafkaTracingConfig} [kafka]
46-
* @property {IgnoreEndpoints} [ignore-endpoints]
46+
* @property {Object.<string, Array.<string>>} [ignore-endpoints]
4747
* @property {boolean} [span-batching-enabled]
4848
*/
4949

@@ -52,11 +52,6 @@ const maxRetryDelay = 60 * 1000; // one minute
5252
* @property {boolean} [trace-correlation]
5353
*/
5454

55-
/**
56-
* @typedef {Object} IgnoreEndpoints
57-
* @property {Object.<string, Array.<string>>} [endpoints]
58-
*/
59-
6055
module.exports = {
6156
/**
6257
* @param {import('./').AnnounceCycleContext} ctx
@@ -231,16 +226,17 @@ function applySpanBatchingConfiguration(agentResponse) {
231226
* @param {AgentAnnounceResponse} agentResponse
232227
*/
233228
function applyIgnoreEndpointsConfiguration(agentResponse) {
234-
if (agentResponse.tracing && agentResponse.tracing['ignore-endpoints']) {
229+
if (agentResponse?.tracing?.['ignore-endpoints']) {
235230
const endpointTracingConfigFromAgent = agentResponse.tracing['ignore-endpoints'];
236231
const endpointTracingConfig = {};
237232
// eslint-disable-next-line no-restricted-syntax
238233
for (const [service, actions] of Object.entries(endpointTracingConfigFromAgent)) {
239234
// From agent, the commands are separated by a pipe ('|')
240235
// @ts-ignore
241-
endpointTracingConfig[service.toLowerCase()] =
242-
// @ts-ignore
243-
actions != null ? actions?.split('|')?.map(action => action?.trim()?.toLowerCase()) : [];
236+
endpointTracingConfig[service.toLowerCase()] = actions
237+
? // @ts-ignore
238+
actions?.split(/[|,]/).map(action => action?.trim()?.toLowerCase())
239+
: [];
244240
}
245241

246242
ensureNestedObjectExists(agentOpts.config, ['tracing', 'ignoreEndpoints']);

packages/collector/test/announceCycle/unannounced_test.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ describe('unannounced state', () => {
213213
}
214214
});
215215
});
216-
it('should apply the configuration to ignore specified endpoints for a single package', done => {
216+
it('should apply the configuration to ignore specified endpoint for a single package', done => {
217217
prepareAnnounceResponse({
218218
tracing: {
219219
'ignore-endpoints': {
@@ -234,6 +234,27 @@ describe('unannounced state', () => {
234234
}
235235
});
236236
});
237+
it('should apply the configuration to ignore multiple endpoints for a single package', done => {
238+
prepareAnnounceResponse({
239+
tracing: {
240+
'ignore-endpoints': {
241+
redis: 'TYPE|GET'
242+
}
243+
}
244+
});
245+
unannouncedState.enter({
246+
transitionTo: () => {
247+
expect(agentOptsStub.config).to.deep.equal({
248+
tracing: {
249+
ignoreEndpoints: {
250+
redis: ['type', 'get']
251+
}
252+
}
253+
});
254+
done();
255+
}
256+
});
257+
});
237258
it('should apply the tracing configuration to ignore multiple endpoints across different packages', done => {
238259
prepareAnnounceResponse({
239260
tracing: {

packages/core/src/tracing/spanBuffer.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'use strict';
77

88
const tracingMetrics = require('./metrics');
9-
const { filterSpan } = require('../util/filterSpan');
9+
const { applyFilter } = require('../util/spanFilter');
1010
const { transform } = require('./backend_mappers');
1111

1212
/** @type {import('../core').GenericLogger} */
@@ -189,8 +189,8 @@ exports.addSpan = function (span) {
189189
return;
190190
}
191191

192-
// Transform the span
193-
const processedSpan = manageSpan(span);
192+
// Process the span, apply any transformations, and implement filtering if necessary.
193+
const processedSpan = processSpan(span);
194194
if (!processedSpan) {
195195
logger.warn('Span of type %s has no trace ID. Not transmitting this span', span.n);
196196
return;
@@ -505,23 +505,24 @@ function removeSpansIfNecessary() {
505505
* @param {import('../core').InstanaBaseSpan} span
506506
* @returns {import('../core').InstanaBaseSpan} span
507507
*/
508-
function manageSpan(span) {
508+
function processSpan(span) {
509509
if (!span || typeof span.n !== 'string') {
510510
return span;
511511
}
512512

513-
// Currently only filter if ignoreEndpoint is configured
513+
// Currently, filter only if the ignoreEndpoint is configured.
514+
// In the next phase, this check will be removed.
514515
if (ignoreEndpoints) {
515516
// @ts-ignore
516-
span = filterSpan({ span, ignoreEndpoints });
517+
span = applyFilter({ span, ignoreEndpoints });
517518

518519
if (!span) {
519520
return null;
520521
}
521522
}
522523
return transform(span);
523524
}
524-
// export the manageSpan function for use in test.
525+
// export the processSpan function for use in test.
525526
if (process.env.NODE_ENV === 'test') {
526-
module.exports.manageSpan = manageSpan;
527+
module.exports.processSpan = processSpan;
527528
}

packages/core/src/util/normalizeConfig.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const constants = require('../tracing/constants');
2727
* @property {boolean} [disableW3cTraceCorrelation]
2828
* @property {KafkaTracingOptions} [kafka]
2929
* @property {boolean} [allowRootExitSpan]
30-
* @property {Object} [ignoreEndpoints]
30+
* @property {Object.<string, Array.<string>>} [ignoreEndpoints]
3131
*/
3232

3333
/**
@@ -62,11 +62,6 @@ const constants = require('../tracing/constants');
6262
* @property {number} [timeBetweenHealthcheckCalls]
6363
*/
6464

65-
/**
66-
* @typedef {Object} IgnoreEndpoints
67-
* @property {Object.<string, Array.<string>>} [endpoints]
68-
*/
69-
7065
/**
7166
* @typedef {Object} AgentConfig
7267
* @property {AgentTracingConfig} [tracing]
@@ -231,7 +226,7 @@ function normalizeTracingConfig(config) {
231226
normalizeDisableW3cTraceCorrelation(config);
232227
normalizeTracingKafka(config);
233228
normalizeAllowRootExitSpan(config);
234-
normalizeIgnoredEndpoints(config);
229+
normalizeIgnoreEndpoints(config);
235230
}
236231

237232
/**
@@ -691,15 +686,15 @@ function normalizeSingleValue(configValue, defaultValue, configPath, envVarKey)
691686
/**
692687
* @param {InstanaConfig} config
693688
*/
694-
function normalizeIgnoredEndpoints(config) {
695-
if (!config.tracing.ignoreEndpoints) {
689+
function normalizeIgnoreEndpoints(config) {
690+
if (!config.tracing?.ignoreEndpoints) {
696691
config.tracing.ignoreEndpoints = {};
697692
}
698693

699694
for (const [service, methods] of Object.entries(config.tracing.ignoreEndpoints)) {
700695
const normalizedService = service.toLowerCase();
701696
if (!Array.isArray(methods)) {
702-
console.warn(
697+
logger.warn(
703698
`Invalid configuration for ${normalizedService}: ignoredEndpoints.${normalizedService} is not an array, the value will be ignored: ${JSON.stringify(
704699
methods
705700
)}`
@@ -708,7 +703,7 @@ function normalizeIgnoredEndpoints(config) {
708703
config.tracing.ignoreEndpoints[normalizedService] = [];
709704
} else {
710705
// @ts-ignore
711-
config.tracing.ignoreEndpoints[normalizedService] = methods.map(method => method.toLowerCase());
706+
config.tracing.ignoreEndpoints[normalizedService] = methods.map(method => method?.toLowerCase());
712707
}
713708
}
714709
}

packages/core/src/util/filterSpan.js renamed to packages/core/src/util/spanFilter.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
'use strict';
66

77
/** @type {import('../core').GenericLogger} */
8-
let logger;
9-
logger = require('../logger').getLogger('tracing/util/filterSpan', newLogger => {
8+
let logger = require('../logger').getLogger('util/spanFilter', newLogger => {
109
logger = newLogger;
1110
});
1211
/**
@@ -26,15 +25,15 @@ function shouldIgnore(span, ignoreEndpoints) {
2625
* @param {{ span: import('../core').InstanaBaseSpan, ignoreEndpoints: { [key: string]: Array<string> } }} params
2726
* @returns {import('../core').InstanaBaseSpan | null}
2827
*/
29-
function filterSpan({ span, ignoreEndpoints }) {
28+
function applyFilter({ span, ignoreEndpoints }) {
3029
if (ignoreEndpoints && shouldIgnore(span, ignoreEndpoints[span.n])) {
3130
logger.debug('Span ignored due to matching ignore endpoint', {
3231
spanType: span.n,
33-
ignoredOperation: span.data[span.n]?.operation
32+
ignoreOperation: span.data[span.n]?.operation
3433
});
3534
return null;
3635
}
3736
return span;
3837
}
3938

40-
module.exports = { filterSpan };
39+
module.exports = { applyFilter };

packages/core/test/tracing/spanBuffer_test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ describe('tracing/spanBuffer', () => {
566566
verifyNoBatching(span1, span2);
567567
});
568568
});
569-
describe('manageSpan test', () => {
569+
describe('processSpan test', () => {
570570
before(() => {
571571
spanBuffer.init(
572572
{
@@ -597,16 +597,16 @@ describe('tracing/spanBuffer', () => {
597597
}
598598
};
599599
it('should return the span if span is not valid', () => {
600-
expect(spanBuffer.manageSpan(null)).to.equal(null);
600+
expect(spanBuffer.processSpan(null)).to.equal(null);
601601
});
602602

603603
it('should filter out the span when command is listed in ignoreEndpoints config', () => {
604-
expect(spanBuffer.manageSpan(span)).to.equal(null);
604+
expect(spanBuffer.processSpan(span)).to.equal(null);
605605
});
606606

607607
it('should transform and return the span for command not specified in ignoreEndpoints config', () => {
608608
span.data.redis.operation = 'set';
609-
const result = spanBuffer.manageSpan(span);
609+
const result = spanBuffer.processSpan(span);
610610
expect(result.data.redis.command).to.equal('set');
611611
expect(result.data.redis).to.not.have.property('operation');
612612
});

packages/core/test/util/normalizeConfig_test.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,14 @@ describe('util.normalizeConfig', () => {
484484
});
485485
expect(config.tracing.ignoreEndpoints).to.deep.equal({ redis: ['get'] });
486486
});
487-
487+
it('should apply multiple ignore endpoints via config', () => {
488+
const config = normalizeConfig({
489+
tracing: {
490+
ignoreEndpoints: { redis: ['GET', 'TYPE'] }
491+
}
492+
});
493+
expect(config.tracing.ignoreEndpoints).to.deep.equal({ redis: ['get', 'type'] });
494+
});
488495
it('should apply ignore endpoints via config for multiple packages', () => {
489496
const config = normalizeConfig({
490497
tracing: {

packages/core/test/util/filterSpan_test.js renamed to packages/core/test/util/spanFilter_test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
'use strict';
66

77
const expect = require('chai').expect;
8-
const { filterSpan } = require('../../src/util/filterSpan');
8+
const { applyFilter } = require('../../src/util/spanFilter');
99

1010
const span = {
1111
t: '1234567803',
@@ -23,24 +23,24 @@ let ignoreEndpoints = {
2323
redis: ['GET', 'TYPE']
2424
};
2525

26-
describe('util.filterSpan', () => {
26+
describe('util.spanFilter', () => {
2727
it('should return null when the span should be ignored', () => {
2828
span.data.redis.operation = 'GET';
29-
expect(filterSpan({ span, ignoreEndpoints })).equal(null);
29+
expect(applyFilter({ span, ignoreEndpoints })).equal(null);
3030
});
3131

3232
it('should return the span when command is not in the ignore list', () => {
3333
span.data.redis.operation = 'HGET';
34-
expect(filterSpan({ span, ignoreEndpoints })).equal(span);
34+
expect(applyFilter({ span, ignoreEndpoints })).equal(span);
3535
});
3636

3737
it('should return the span when span.n does not match any endpoint in config', () => {
3838
span.n = 'node.http.client';
39-
expect(filterSpan({ span, ignoreEndpoints })).equal(span);
39+
expect(applyFilter({ span, ignoreEndpoints })).equal(span);
4040
});
4141
it('should return span when no ignoreconfiguration', () => {
4242
ignoreEndpoints = {};
4343
span.data.redis.operation = 'GET';
44-
expect(filterSpan({ span, ignoreEndpoints })).equal(span);
44+
expect(applyFilter({ span, ignoreEndpoints })).equal(span);
4545
});
4646
});

0 commit comments

Comments
 (0)