Skip to content

Commit cd92380

Browse files
clarakosiPchelolo
authored andcommitted
Update _handleStaticLabels
1 parent bfd5fd2 commit cd92380

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

lib/metrics/prometheus.js

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ class PrometheusMetric {
3434
}
3535

3636
_handleStaticLabels(labels) {
37+
const updatedLabels = [...labels];
3738
if (this.staticLabels !== undefined) {
3839
Object.keys(this.staticLabels).forEach((name) => {
39-
if (labels.indexOf(this.staticLabels[name]) === -1) {
40-
labels.unshift(this.staticLabels[name]);
40+
if (updatedLabels.indexOf(this.staticLabels[name]) === -1) {
41+
updatedLabels.unshift(this.staticLabels[name]);
4142
}
4243
});
4344
}
45+
return updatedLabels;
4446
}
4547

4648
_normalize(str) {
@@ -50,30 +52,22 @@ class PrometheusMetric {
5052
}
5153

5254
increment(amount, labels) {
53-
let updatedLabels = [...labels];
54-
this._handleStaticLabels(updatedLabels);
55-
updatedLabels = updatedLabels.map(this._normalize);
55+
const updatedLabels = this._handleStaticLabels(labels).map(this._normalize);
5656
this.metric.labels.apply(this.metric, updatedLabels).inc(amount);
5757
}
5858

5959
decrement(amount, labels) {
60-
let updatedLabels = [...labels];
61-
this._handleStaticLabels(updatedLabels);
62-
updatedLabels = updatedLabels.map(this._normalize);
60+
const updatedLabels = this._handleStaticLabels(labels).map(this._normalize);
6361
this.metric.labels.apply(this.metric, updatedLabels).dec(amount);
6462
}
6563

6664
observe(value, labels) {
67-
let updatedLabels = [...labels];
68-
this._handleStaticLabels(updatedLabels);
69-
updatedLabels = updatedLabels.map(this._normalize);
65+
const updatedLabels = this._handleStaticLabels(labels).map(this._normalize);
7066
this.metric.labels.apply(this.metric, updatedLabels).observe(value);
7167
}
7268

7369
gauge(amount, labels) {
74-
let updatedLabels = [...labels];
75-
this._handleStaticLabels(updatedLabels);
76-
updatedLabels = updatedLabels.map(this._normalize);
70+
const updatedLabels = this._handleStaticLabels(labels).map(this._normalize);
7771
if (amount < 0) {
7872
this.metric.labels.apply(this.metric, updatedLabels).dec(Math.abs(amount));
7973
} else {
@@ -82,23 +76,17 @@ class PrometheusMetric {
8276
}
8377

8478
set(value, labels) {
85-
let updatedLabels = [...labels];
86-
this._handleStaticLabels(updatedLabels);
87-
updatedLabels = updatedLabels.map(this._normalize);
79+
const updatedLabels = this._handleStaticLabels(labels).map(this._normalize);
8880
this.metric.labels.apply(this.metric, updatedLabels).set(value);
8981
}
9082

9183
timing(value, labels) {
92-
let updatedLabels = [...labels];
93-
this._handleStaticLabels(updatedLabels);
94-
updatedLabels = updatedLabels.map(this._normalize);
84+
const updatedLabels = this._handleStaticLabels(labels).map(this._normalize);
9585
this.observe(value, updatedLabels);
9686
}
9787

9888
endTiming(startTime, labels) {
99-
let updatedLabels = [...labels];
100-
this._handleStaticLabels(updatedLabels);
101-
updatedLabels = updatedLabels.map(this._normalize);
89+
const updatedLabels = this._handleStaticLabels(labels).map(this._normalize);
10290
this.timing(Date.now() - startTime, updatedLabels);
10391
}
10492
}

0 commit comments

Comments
 (0)