Skip to content

Commit 6a90a82

Browse files
committed
feat(UMetrics): export default nodejs metrics
1 parent 27b03ef commit 6a90a82

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/UMetrics.js

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import promClient from 'prom-client';
12
import MetricRegistry from './MetricRegistry';
23
import GaugeMetric from './metric/GaugeMetric';
34
import Transport from './transport/Transport';
@@ -56,9 +57,22 @@ class UMetrics {
5657
* @return UMetrics
5758
*/
5859
start() {
60+
if (this.nodejsMetricsEnabled) {
61+
this.enableExportingDefaultMetrics();
62+
}
5963
this.transport.start();
6064
return this;
6165
}
66+
67+
/**
68+
* @private
69+
*/
70+
enableExportingDefaultMetrics() {
71+
promClient.collectDefaultMetrics({
72+
prefix: this.prefix,
73+
timeout: this.nodejsMetricsInterval,
74+
});
75+
}
6276
}
6377

6478
// Конструкторы метрик, как внешний интерфейс

src/UMetrics.server.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,34 @@ describe('UMetrics facade', () => {
5656
});
5757
expect(uMetrics.nodejsMetricsInterval).to.be.equals(7000);
5858
});
59+
60+
it('Should enable default metrics exports if option provided', () => {
61+
let calledCount = 0;
62+
const uMetrics = new UMetrics(new Transport(), {
63+
port: 1111,
64+
nodejsMetricsEnabled: true,
65+
});
66+
67+
uMetrics.enableExportingDefaultMetrics = () => {
68+
calledCount += 1;
69+
};
70+
71+
uMetrics.start();
72+
expect(calledCount).to.be.equals(1);
73+
});
74+
75+
it('Should not enable default metrics exports if option is not provided', () => {
76+
let calledCount = 0;
77+
const uMetrics = new UMetrics(new Transport(), {
78+
port: 1111,
79+
nodejsMetricsEnabled: false,
80+
});
81+
82+
uMetrics.enableExportingDefaultMetrics = () => {
83+
calledCount += 1;
84+
};
85+
86+
uMetrics.start();
87+
expect(calledCount).to.be.equals(0);
88+
});
5989
});

0 commit comments

Comments
 (0)