Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distinguish different workers in Prometheus PushGateway #1427

Merged
merged 2 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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