-
Notifications
You must be signed in to change notification settings - Fork 406
/
Copy pathprometheus-tx-observer.js
189 lines (166 loc) · 7.96 KB
/
prometheus-tx-observer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const CaliperUtils = require('../../common/utils/caliper-utils');
const ConfigUtil = require('../../common/config/config-util');
const TxObserverInterface = require('./tx-observer-interface');
const express = require('express');
const appServer = express();
const prometheusClient = require('prom-client');
const prometheusGcStats = require('prometheus-gc-stats');
const Logger = CaliperUtils.getLogger('prometheus-tx-observer');
/**
* Prometheus TX observer used to maintain Prometheus metrics by acting as a scrape target.
*/
class PrometheusTxObserver extends TxObserverInterface {
/**
* Initializes the instance.
* @param {object} options The observer configuration object.
* @param {MessengerInterface} messenger The worker messenger instance. Not used.
* @param {number} workerIndex The 0-based index of the worker node.
*/
constructor(options, messenger, workerIndex) {
super(messenger, workerIndex);
this.metricPath = (options && options.metricPath) ? options.metricPath : '/metrics';
this.scrapePort = (options && options.scrapePort) ? Number(options.scrapePort) : ConfigUtil.get(ConfigUtil.keys.Observer.Prometheus.ScrapePort);
if (CaliperUtils.isForkedProcess()) {
this.scrapePort += workerIndex;
}
this.processMetricCollectInterval = (options && options.processMetricCollectInterval) ? options.processMetricCollectInterval : undefined;
this.defaultLabels = (options && options.defaultLabels) ? options.defaultLabels : {};
Logger.debug(`Configuring Prometheus scrape server for worker ${workerIndex} on port ${this.scrapePort}, with metrics exposed on ${this.metricPath} endpoint`);
// do not use global registry to avoid conflicts with other potential prom-based observers
this.registry = new prometheusClient.Registry();
// automatically apply default internal and user supplied labels
this.defaultLabels.workerIndex = this.workerIndex;
this.defaultLabels.roundIndex = this.currentRound;
this.defaultLabels.roundLabel = this.roundLabel;
this.registry.setDefaultLabels(this.defaultLabels);
// Exposed metrics
this.counterTxSubmitted = new prometheusClient.Counter({
name: 'caliper_tx_submitted',
help: 'The total number of submitted transactions.',
registers: [this.registry]
});
this.counterTxFinished = new prometheusClient.Counter({
name: 'caliper_tx_finished',
help: 'The total number of finished transactions.',
labelNames: ['final_status'],
registers: [this.registry]
});
// configure buckets
let buckets = prometheusClient.linearBuckets(0.1, 0.5, 10); // default
if (options && options.histogramBuckets) {
if (options.histogramBuckets.explicit) {
buckets = options.histogramBuckets.explicit;
} else if (options.histogramBuckets.linear) {
let linear = options.histogramBuckets.linear;
buckets = prometheusClient.linearBuckets(linear.start, linear.width, linear.count);
} else if (options.histogramBuckets.exponential) {
let exponential = options.histogramBuckets.exponential;
buckets = prometheusClient.exponentialBuckets(exponential.start, exponential.factor, exponential.count);
}
}
this.histogramLatency = new prometheusClient.Histogram({
name: 'caliper_tx_e2e_latency',
help: 'The histogram of end-to-end transaction latencies in seconds.',
labelNames: ['final_status'],
buckets,
registers: [this.registry]
});
// setting an interval enables the default metric collection
if (this.processMetricCollectInterval) {
this.processMetricHandle = prometheusClient.collectDefaultMetrics({
register: this.registry,
timestamps: false,
timeout: this.processMetricCollectInterval
});
const startGcStats = prometheusGcStats(this.registry);
startGcStats();
}
// configure server for Prometheus scrapes:
appServer.get(`${this.metricPath}`, async (req, res) => {
try {
res.set('Content-Type', this.registry.contentType);
res.end(await this.registry.metrics());
} catch (err) {
Logger.error(`Error in metrics provision within worker ${this.workerIndex}`, err.stack);
res.status(500).end(`Error collecting metrics from Hyperledger Caliper worker ${this.workerIndex}`);
}
});
}
/**
* Activates the TX observer instance, and in turn, the new TX statistics collector.
* @param {number} roundIndex The 0-based index of the current round.
* @param {string} roundLabel The roundLabel name.
*/
async activate(roundIndex, roundLabel) {
await super.activate(roundIndex, roundLabel);
// update worker and round metadata
this.defaultLabels.workerIndex = this.workerIndex;
this.defaultLabels.roundIndex = this.currentRound;
this.defaultLabels.roundLabel = this.roundLabel;
this.registry.setDefaultLabels(this.defaultLabels);
// Enable server
this.server = appServer.listen(this.scrapePort);
Logger.debug(`Enabled Prometheus scrape server on ${this.scrapePort}, with metrics exposed on ${this.metricPath} endpoint`);
}
/**
* Deactivates the TX observer interface, and resets all metric collectors
*/
async deactivate() {
await super.deactivate();
this.counterTxSubmitted.reset();
this.counterTxFinished.reset();
this.histogramLatency.reset();
this.registry.resetMetrics();
// Disable server
this.server.close();
}
/**
* Called when TXs are submitted.
* @param {number} count The number of submitted TXs. Can be greater than one for a batch of TXs.
*/
txSubmitted(count) {
this.counterTxSubmitted.inc(count);
}
/**
* Called when TXs are finished.
* @param {TxStatus | TxStatus[]} results The result information of the finished TXs. Can be a collection of results for a batch of TXs.
*/
txFinished(results) {
if (Array.isArray(results)) {
for (const result of results) {
// pass/fail status from result.GetStatus()
this.counterTxFinished.labels(result.GetStatus()).inc();
this.histogramLatency.labels(result.GetStatus()).observe(result.GetTimeFinal() - result.GetTimeCreate());
}
} else {
// pass/fail status from result.GetStatus()
this.counterTxFinished.labels(results.GetStatus()).inc();
this.histogramLatency.labels(results.GetStatus()).observe((results.GetTimeFinal() - results.GetTimeCreate())/1000);
}
}
}
/**
* Factory function for creating a PrometheusTxObserver instance.
* @param {object} options The observer configuration object.
* @param {MessengerInterface} messenger The worker messenger instance.
* @param {number} workerIndex The 0-based index of the worker node.
* @return {TxObserverInterface} The observer instance.
*/
function createTxObserver(options, messenger, workerIndex) {
return new PrometheusTxObserver(options, messenger, workerIndex);
}
module.exports.createTxObserver = createTxObserver;