Skip to content

Commit

Permalink
chore: agent config avaible in span buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
aryamohanan committed Nov 15, 2024
1 parent 41599d8 commit fe2e0ed
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 51 deletions.
26 changes: 21 additions & 5 deletions packages/core/src/tracing/spanBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ const batchBucketWidth = 18;

/** @type {BatchingBucketMap} */
const batchingBuckets = new Map();
/**
* @type {Object}
*/
let ignoreEndpoints;

/**
* @param {import('../util/normalizeConfig').InstanaConfig} config
Expand All @@ -103,6 +107,7 @@ exports.init = function init(config, _downstreamConnection) {
initialDelayBeforeSendingSpans = Math.max(transmissionDelay, minDelayBeforeSendingSpans);
isFaaS = false;
transmitImmediate = false;
ignoreEndpoints = config.tracing?.ignoreEndpoints;

if (config.tracing.activateImmediately) {
preActivationCleanupIntervalHandle = setInterval(() => {
Expand Down Expand Up @@ -132,6 +137,9 @@ exports.activate = function activate(extraConfig) {
if (extraConfig && extraConfig.tracing && extraConfig.tracing.spanBatchingEnabled) {
batchingEnabled = true;
}
if (extraConfig?.tracing?.ignoreEndpoints) {
ignoreEndpoints = extraConfig.tracing.ignoreEndpoints;
}

isActive = true;
if (activatedAt == null) {
Expand Down Expand Up @@ -489,15 +497,23 @@ function removeSpansIfNecessary() {
spans = spans.slice(-maxBufferedSpans);
}
}
// @ts-ignore
/**
* @param {import('../core').InstanaBaseSpan} span
* @returns {import('../core').InstanaBaseSpan} span
*/
function manageSpan(span) {
if (!span || typeof span.n !== 'string') {
return span;
}
const filteredSpan = filterSpan(span);

if (!filteredSpan) {
return null; // Return null if span was ignored
// Currently only filter if ignoreEndpoint is configured
if (ignoreEndpoints) {
// @ts-ignore
span = filterSpan({ span, ignoreEndpoints });

if (!span) {
return null;
}
}
return transform(filteredSpan);
return transform(span);
}
27 changes: 10 additions & 17 deletions packages/core/src/util/filterSpan.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,29 @@

'use strict';

// TODO: read from config
// @ts-ignore
let config;

/**
* To check whether a span should be ignored based on the command.
*
* @param {import('../core').InstanaBaseSpan} span - The span object to check.
* @param {Array<string>} ignoreEndpoints - An array of endpoints to ignore.
* @returns {boolean} - Returns true if the span should be ignored, false otherwise.
*/
function shouldIgnore(span) {
// @ts-ignore
const ignoreEndpoints = config.tracing.ignoreEndpoints[span.n];

// @ts-ignore
if (span.data?.[span.n]?.command && ignoreEndpoints) {
return ignoreEndpoints.includes(span.data[span.n].command);
function shouldIgnore(span, ignoreEndpoints) {
if (span.data?.[span.n]?.operation && ignoreEndpoints) {
return ignoreEndpoints.includes(span.data[span.n].operation);
}

return false;
}

/**
* @param {Object} span - The span object to check.
* @returns {Object|null} - Returns the span if not ignored, or null if the span should be ignored.
* Filters a span based on ignore criteria.
*
* @param {{ span: import('../core').InstanaBaseSpan, ignoreEndpoints: { [key: string]: Array<string> } }} params
* @returns {import('../core').InstanaBaseSpan | null}
*/
function filterSpan(span, configuration = {}) {
// @ts-ignore
config = configuration || config;
if (config?.tracing?.ignoreEndpoints && shouldIgnore(span)) {
function filterSpan({ span, ignoreEndpoints }) {
if (ignoreEndpoints && shouldIgnore(span, ignoreEndpoints[span.n])) {
return null;
}
return span;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/util/normalizeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const constants = require('../tracing/constants');
* @property {AgentTracingHttpConfig} [http]
* @property {AgentTracingKafkaConfig} [kafka]
* @property {boolean|string} [spanBatchingEnabled]
* @property {Object} [AgentTracingIgnoreEndpoints]
* @property {Object} [ignoreEndpoints]
*/

/**
Expand All @@ -91,7 +91,7 @@ const constants = require('../tracing/constants');
*/

/**
* @typedef {Object} AgentTracingIgnoreEndpoints
* @typedef {Object} ignoreEndpoints
* @property {Object.<string, Array.<string>>} [endpoints]
*/

Expand Down
41 changes: 14 additions & 27 deletions packages/core/test/util/filterSpan_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,37 @@ const span = {
k: 2,
data: {
redis: {
command: ''
operation: ''
}
}
};
let config = {
tracing: {
ignoreEndpoints: {
redis: ['GET', 'SET']
}
}
let ignoreEndpoints = {
redis: ['GET', 'SET']
};

describe('filterSpan', () => {
it('should return null when the span should be ignored', () => {
span.data.redis.command = 'GET';
expect(filterSpan(span, config)).equal(null);
span.data.redis.operation = 'GET';
expect(filterSpan({ span, ignoreEndpoints })).equal(null);
});

it('should return the span when it should not be ignored', () => {
span.data.redis.command = 'DEL';
expect(filterSpan(span, config)).equal(span);
span.data.redis.operation = 'DEL';
expect(filterSpan({ span, ignoreEndpoints })).equal(span);
});

it('should return the span when command is not in the ignore list', () => {
span.data.redis.command = 'HGET';
expect(filterSpan(span, config)).equal(span);
span.data.redis.operation = 'HGET';
expect(filterSpan({ span, ignoreEndpoints })).equal(span);
});

it('should return the span when span.n does not match any endpoint in config', () => {
const otherSpan = {
n: 'node.http.client',
data: {
http: {
command: 'GET'
}
}
};
expect(filterSpan(otherSpan)).equal(otherSpan);
span.n = 'node.http.client';
expect(filterSpan({ span, ignoreEndpoints })).equal(span);
});
it('should return span when no ignoreconfiguration', () => {
config = {
tracing: {}
};
span.data.redis.command = 'GET';
expect(filterSpan(span, config)).equal(span);
ignoreEndpoints = {};
span.data.redis.operation = 'GET';
expect(filterSpan({ span, ignoreEndpoints })).equal(span);
});
});

0 comments on commit fe2e0ed

Please sign in to comment.