Skip to content

Commit

Permalink
add test for more tests for prometheus transaction observer
Browse files Browse the repository at this point in the history
Signed-off-by: Babatunde Sanusi <swisskid95@gmail.com>
  • Loading branch information
tunedev committed Sep 11, 2024
1 parent 3c6b739 commit 8da3d0a
Showing 1 changed file with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const prometheusClient = require('prom-client');
chai.use(chaiAsPromised);
const should = chai.should();
const mockery = require('mockery');
Expand Down Expand Up @@ -61,8 +62,14 @@ class Utils {
};
}

/**
* Stubs the 'listen' method of the appServer and returns an object with a stubbed 'close' method.
*
* @param {sinon.SinonSandbox} sandbox - The Sinon sandbox used to stub methods.
* @param {Object} appServer - The app server instance whose 'listen' method will be stubbed.
*/
static stubAppServer(sandbox, appServer) {
sandbox.stub(appServer, 'listen').returns({close: sinon.stub()})
sandbox.stub(appServer, 'listen').returns({close: sinon.stub()});
}
}

Expand All @@ -81,7 +88,7 @@ describe('When using a PrometheusTxObserver', () => {

before(() => {
sandbox = sinon.createSandbox();
})
});

after(()=> {
mockery.deregisterAll();
Expand Down Expand Up @@ -205,4 +212,68 @@ describe('When using a PrometheusTxObserver', () => {
txFinished.values.should.deep.equal([]);
});

it('should adjust scrapePort if process is forked', () => {
sinon.stub(Utils, 'isForkedProcess').returns(true);
const prometheusTxObserver = PrometheusTxObserver.createTxObserver({ scrapePort: 3000 }, undefined, 1);

// The scrapePort should be adjusted by the workerIndex (1)
prometheusTxObserver.scrapePort.should.equal(3001);

Utils.isForkedProcess.restore();
});

it('should configure explicit histogram buckets', () => {
const options = {
histogramBuckets: {
explicit: [0.1, 0.5, 1, 2, 5]
}
};
const prometheusTxObserver = PrometheusTxObserver.createTxObserver(options, undefined, 0);
prometheusTxObserver.histogramLatency.upperBounds.should.deep.equal([0.1, 0.5, 1, 2, 5]);
});

it('should configure linear histogram buckets', () => {
const options = {
histogramBuckets: {
linear: {
start: 0.5,
width: 0.5,
count: 5
}
}
};
const prometheusTxObserver = PrometheusTxObserver.createTxObserver(options, undefined, 0);
prometheusTxObserver.histogramLatency.upperBounds.should.deep.equal([0.5, 1, 1.5, 2, 2.5]);
});

it('should configure exponential histogram buckets', () => {
const options = {
histogramBuckets: {
exponential: {
start: 0.5,
factor: 2,
count: 5
}
}
};
const prometheusTxObserver = PrometheusTxObserver.createTxObserver(options, undefined, 0);
prometheusTxObserver.histogramLatency.upperBounds.should.deep.equal([0.5, 1, 2, 4, 8]);
});

it('should enable process metric collection if processMetricCollectInterval is set', () => {
sinon.spy(prometheusClient, 'collectDefaultMetrics');
const options = {
processMetricCollectInterval: 1000
};
const prometheusTxObserver = PrometheusTxObserver.createTxObserver(options, undefined, 0);

// Ensure process metric collection is enabled
prometheusClient.collectDefaultMetrics.calledOnceWith({
register: prometheusTxObserver.registry,
timestamps: false,
timeout: 1000
}).should.be.true;

prometheusClient.collectDefaultMetrics.restore();
});
});

0 comments on commit 8da3d0a

Please sign in to comment.