Skip to content

Commit

Permalink
Distinguish different workers in Prometheus PushGateway (#1427)
Browse files Browse the repository at this point in the history
* Distinguish different workers in Prometheus PushGateway

Signed-off-by: CaptainIRS <36656347+CaptainIRS@users.noreply.github.com>

* Address review comments

* Remove redundant labels
* Add roundIndex to groupings
* Update unit tests utilizing workerIndex and roundIndex
* Write new unit test for _sendUpdate

Signed-off-by: CaptainIRS <36656347+CaptainIRS@users.noreply.github.com>
  • Loading branch information
CaptainIRS committed Aug 4, 2022
1 parent 2efba52 commit c648fa9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ class PrometheusPushTxObserver extends TxObserverInterface {

// automatically apply default internal and user supplied labels
this.defaultLabels = (options && options.defaultLabels) ? options.defaultLabels : {};
this.defaultLabels.workerIndex = this.workerIndex;
this.defaultLabels.roundIndex = this.currentRound;
this.defaultLabels.roundLabel = this.roundLabel;
this.registry.setDefaultLabels(this.defaultLabels);

Expand Down Expand Up @@ -111,7 +109,10 @@ class PrometheusPushTxObserver extends TxObserverInterface {
* @private
*/
async _sendUpdate() {
this.prometheusPushGateway.pushAdd({jobName: 'workers'}, function(err, _resp, _body) {
this.prometheusPushGateway.pushAdd({jobName: 'caliper-workers', groupings: {
workerIndex: this.workerIndex,
roundIndex: this.currentRound
}}, function(err, _resp, _body) {
if (err) {
Logger.error(`Error sending update to Prometheus Push Gateway: ${err.stack}`);
}
Expand All @@ -126,9 +127,7 @@ class PrometheusPushTxObserver extends TxObserverInterface {
async activate(roundIndex, roundLabel) {
await super.activate(roundIndex, roundLabel);

// update worker and round metadata
this.defaultLabels.workerIndex = this.workerIndex;
this.defaultLabels.roundIndex = this.currentRound;
// update round metadata
this.defaultLabels.roundLabel = this.roundLabel;
this.registry.setDefaultLabels(this.defaultLabels);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,11 @@ describe('When using a PrometheusPushTxObserver', () => {
// Assert expected default options
prometheusPushTxObserver.pushInterval.should.equal(10000);
prometheusPushTxObserver.defaultLabels.should.deep.equal({
roundIndex: 0,
roundLabel: undefined,
workerIndex: 0
roundLabel: undefined
});
should.not.exist(prometheusPushTxObserver.processMetricCollectInterval);
prometheusPushTxObserver.defaultLabels.should.deep.equal({
roundIndex: 0,
roundLabel: undefined,
workerIndex: 0
roundLabel: undefined
});
});

Expand All @@ -129,9 +125,7 @@ describe('When using a PrometheusPushTxObserver', () => {
prometheusPushTxObserver.pushInterval.should.equal(1234);
prometheusPushTxObserver.processMetricCollectInterval.should.equal(100);
prometheusPushTxObserver.defaultLabels.should.deep.equal({
roundIndex: 0,
roundLabel: undefined,
workerIndex: 0,
anotherLabel: 'anotherLabel'
});
});
Expand All @@ -145,12 +139,30 @@ describe('When using a PrometheusPushTxObserver', () => {
await prometheusPushTxObserver.activate(2, 'myTestRound');

prometheusPushTxObserver.defaultLabels.should.deep.equal({
roundIndex: 2,
roundLabel: 'myTestRound',
workerIndex: 0
});
});

it('should pass on the appropriate groupings to the PushGateway API', async () => {
const options = {
pushUrl: 'http://my.url.com'
};
const prometheusPushTxObserver = PrometheusPushTxObserver.createTxObserver(options, undefined, 0);
await prometheusPushTxObserver.activate(2, 'myTestRound');

prometheusPushTxObserver.prometheusPushGateway.pushAdd = sinon.spy();

prometheusPushTxObserver._sendUpdate();

sinon.assert.calledWith(prometheusPushTxObserver.prometheusPushGateway.pushAdd, sinon.match({
jobName: 'caliper-workers',
groupings: sinon.match({
workerIndex: 0,
roundIndex: 2
})
}), sinon.match.func);
});

it('should update transaction statistics during use', async () => {
const options = {
pushUrl: 'http://my.url.com'
Expand Down Expand Up @@ -216,7 +228,7 @@ describe('When using a PrometheusPushTxObserver', () => {

await prometheusPushTxObserver.deactivate();

// Values should be zero, or empty
// Values should be zero, or empty
// Ref: https://github.com/siimon/prom-client/blob/721829cc593bb7da28ae009985caeeacb4b59e05/test/counterTest.js
const txSubmitted = await prometheusPushTxObserver.counterTxSubmitted.get();
txSubmitted.values[0].value.should.equal(0);
Expand Down

0 comments on commit c648fa9

Please sign in to comment.