Skip to content

Commit c648fa9

Browse files
authored
Distinguish different workers in Prometheus PushGateway (#1427)
* 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>
1 parent 2efba52 commit c648fa9

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

packages/caliper-core/lib/worker/tx-observers/prometheus-push-tx-observer.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ class PrometheusPushTxObserver extends TxObserverInterface {
4545

4646
// automatically apply default internal and user supplied labels
4747
this.defaultLabels = (options && options.defaultLabels) ? options.defaultLabels : {};
48-
this.defaultLabels.workerIndex = this.workerIndex;
49-
this.defaultLabels.roundIndex = this.currentRound;
5048
this.defaultLabels.roundLabel = this.roundLabel;
5149
this.registry.setDefaultLabels(this.defaultLabels);
5250

@@ -111,7 +109,10 @@ class PrometheusPushTxObserver extends TxObserverInterface {
111109
* @private
112110
*/
113111
async _sendUpdate() {
114-
this.prometheusPushGateway.pushAdd({jobName: 'workers'}, function(err, _resp, _body) {
112+
this.prometheusPushGateway.pushAdd({jobName: 'caliper-workers', groupings: {
113+
workerIndex: this.workerIndex,
114+
roundIndex: this.currentRound
115+
}}, function(err, _resp, _body) {
115116
if (err) {
116117
Logger.error(`Error sending update to Prometheus Push Gateway: ${err.stack}`);
117118
}
@@ -126,9 +127,7 @@ class PrometheusPushTxObserver extends TxObserverInterface {
126127
async activate(roundIndex, roundLabel) {
127128
await super.activate(roundIndex, roundLabel);
128129

129-
// update worker and round metadata
130-
this.defaultLabels.workerIndex = this.workerIndex;
131-
this.defaultLabels.roundIndex = this.currentRound;
130+
// update round metadata
132131
this.defaultLabels.roundLabel = this.roundLabel;
133132
this.registry.setDefaultLabels(this.defaultLabels);
134133

packages/caliper-core/test/worker/tx-observers/prometheus-push-tx-observer.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,11 @@ describe('When using a PrometheusPushTxObserver', () => {
102102
// Assert expected default options
103103
prometheusPushTxObserver.pushInterval.should.equal(10000);
104104
prometheusPushTxObserver.defaultLabels.should.deep.equal({
105-
roundIndex: 0,
106-
roundLabel: undefined,
107-
workerIndex: 0
105+
roundLabel: undefined
108106
});
109107
should.not.exist(prometheusPushTxObserver.processMetricCollectInterval);
110108
prometheusPushTxObserver.defaultLabels.should.deep.equal({
111-
roundIndex: 0,
112-
roundLabel: undefined,
113-
workerIndex: 0
109+
roundLabel: undefined
114110
});
115111
});
116112

@@ -129,9 +125,7 @@ describe('When using a PrometheusPushTxObserver', () => {
129125
prometheusPushTxObserver.pushInterval.should.equal(1234);
130126
prometheusPushTxObserver.processMetricCollectInterval.should.equal(100);
131127
prometheusPushTxObserver.defaultLabels.should.deep.equal({
132-
roundIndex: 0,
133128
roundLabel: undefined,
134-
workerIndex: 0,
135129
anotherLabel: 'anotherLabel'
136130
});
137131
});
@@ -145,12 +139,30 @@ describe('When using a PrometheusPushTxObserver', () => {
145139
await prometheusPushTxObserver.activate(2, 'myTestRound');
146140

147141
prometheusPushTxObserver.defaultLabels.should.deep.equal({
148-
roundIndex: 2,
149142
roundLabel: 'myTestRound',
150-
workerIndex: 0
151143
});
152144
});
153145

146+
it('should pass on the appropriate groupings to the PushGateway API', async () => {
147+
const options = {
148+
pushUrl: 'http://my.url.com'
149+
};
150+
const prometheusPushTxObserver = PrometheusPushTxObserver.createTxObserver(options, undefined, 0);
151+
await prometheusPushTxObserver.activate(2, 'myTestRound');
152+
153+
prometheusPushTxObserver.prometheusPushGateway.pushAdd = sinon.spy();
154+
155+
prometheusPushTxObserver._sendUpdate();
156+
157+
sinon.assert.calledWith(prometheusPushTxObserver.prometheusPushGateway.pushAdd, sinon.match({
158+
jobName: 'caliper-workers',
159+
groupings: sinon.match({
160+
workerIndex: 0,
161+
roundIndex: 2
162+
})
163+
}), sinon.match.func);
164+
});
165+
154166
it('should update transaction statistics during use', async () => {
155167
const options = {
156168
pushUrl: 'http://my.url.com'
@@ -216,7 +228,7 @@ describe('When using a PrometheusPushTxObserver', () => {
216228

217229
await prometheusPushTxObserver.deactivate();
218230

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

0 commit comments

Comments
 (0)