Skip to content

Commit

Permalink
address PR comments siimon#1
Browse files Browse the repository at this point in the history
  • Loading branch information
lassic committed Sep 12, 2023
1 parent 0ad0b0a commit fe4a561
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

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

const ESCAPE_STRING_REPLACE_MAP = {
'\\': '\\\\',
'\n': '\\n',
};

const ESCAPE_LABEL_VALUE_REPLACE_MAP = {
...ESCAPE_STRING_REPLACE_MAP,
'"': '\\\\"',
};

const ESCAPE_REPLACE_REGEXP = /\\|\n|"/g;

function REPLACE_FUNC(dict) {
return char => dict[char] || '';
}
Expand All @@ -15,23 +27,6 @@ class Registry {
return 'application/openmetrics-text; version=1.0.0; charset=utf-8';
}

static get ESCAPE_STRING_REPLACE_MAP() {
return {
'\\': '\\\\',
'\n': '\\n',
};
}

static get ESCAPE_LABEL_VALUE_REPLACE_MAP() {
return Object.assign({}, Registry.ESCAPE_STRING_REPLACE_MAP, {
'"': '\\\\"',
});
}

static get ESCAPE_REPLACE_REGEXP() {
return /\\|\n|"/g;
}

constructor(regContentType = Registry.PROMETHEUS_CONTENT_TYPE) {
this._metrics = {};
this._collectors = [];
Expand Down Expand Up @@ -256,14 +251,14 @@ function escapeLabelValue(str) {
return str;
}

return escapeString(str, Registry.ESCAPE_LABEL_VALUE_REPLACE_MAP);
return escapeString(str, ESCAPE_LABEL_VALUE_REPLACE_MAP);
}
function escapeString(str, extraReplaceDict) {
const fullDict = extraReplaceDict
? extraReplaceDict
: Registry.ESCAPE_STRING_REPLACE_MAP;
: ESCAPE_STRING_REPLACE_MAP;

return str.replace(Registry.ESCAPE_REPLACE_REGEXP, REPLACE_FUNC(fullDict));
return str.replace(ESCAPE_REPLACE_REGEXP, REPLACE_FUNC(fullDict));
}
function standardizeCounterName(name) {
return name.replace(/_total$/, '');
Expand Down

0 comments on commit fe4a561

Please sign in to comment.