Skip to content

Commit fe4a561

Browse files
author
lassic
committed
address PR comments #1
1 parent 0ad0b0a commit fe4a561

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

lib/registry.js

+15-20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
const { getValueAsString } = require('./util');
44

5+
const ESCAPE_STRING_REPLACE_MAP = {
6+
'\\': '\\\\',
7+
'\n': '\\n',
8+
};
9+
10+
const ESCAPE_LABEL_VALUE_REPLACE_MAP = {
11+
...ESCAPE_STRING_REPLACE_MAP,
12+
'"': '\\\\"',
13+
};
14+
15+
const ESCAPE_REPLACE_REGEXP = /\\|\n|"/g;
16+
517
function REPLACE_FUNC(dict) {
618
return char => dict[char] || '';
719
}
@@ -15,23 +27,6 @@ class Registry {
1527
return 'application/openmetrics-text; version=1.0.0; charset=utf-8';
1628
}
1729

18-
static get ESCAPE_STRING_REPLACE_MAP() {
19-
return {
20-
'\\': '\\\\',
21-
'\n': '\\n',
22-
};
23-
}
24-
25-
static get ESCAPE_LABEL_VALUE_REPLACE_MAP() {
26-
return Object.assign({}, Registry.ESCAPE_STRING_REPLACE_MAP, {
27-
'"': '\\\\"',
28-
});
29-
}
30-
31-
static get ESCAPE_REPLACE_REGEXP() {
32-
return /\\|\n|"/g;
33-
}
34-
3530
constructor(regContentType = Registry.PROMETHEUS_CONTENT_TYPE) {
3631
this._metrics = {};
3732
this._collectors = [];
@@ -256,14 +251,14 @@ function escapeLabelValue(str) {
256251
return str;
257252
}
258253

259-
return escapeString(str, Registry.ESCAPE_LABEL_VALUE_REPLACE_MAP);
254+
return escapeString(str, ESCAPE_LABEL_VALUE_REPLACE_MAP);
260255
}
261256
function escapeString(str, extraReplaceDict) {
262257
const fullDict = extraReplaceDict
263258
? extraReplaceDict
264-
: Registry.ESCAPE_STRING_REPLACE_MAP;
259+
: ESCAPE_STRING_REPLACE_MAP;
265260

266-
return str.replace(Registry.ESCAPE_REPLACE_REGEXP, REPLACE_FUNC(fullDict));
261+
return str.replace(ESCAPE_REPLACE_REGEXP, REPLACE_FUNC(fullDict));
267262
}
268263
function standardizeCounterName(name) {
269264
return name.replace(/_total$/, '');

0 commit comments

Comments
 (0)