prom-client
Advanced tools
Comparing version 15.0.0-0 to 15.0.0-1
@@ -100,2 +100,8 @@ /** | ||
async get() { | ||
const data = await this.getForPromString(); | ||
data.values = data.values.map(splayLabels); | ||
return data; | ||
} | ||
async getForPromString() { | ||
if (this.collect) { | ||
@@ -180,5 +186,6 @@ const v = this.collect(); | ||
function setValuePair(labels, value, metricName, exemplar) { | ||
function setValuePair(labels, value, metricName, exemplar, sharedLabels = {}) { | ||
return { | ||
labels, | ||
sharedLabels, | ||
value, | ||
@@ -260,3 +267,2 @@ metricName, | ||
const buckets = []; | ||
const bucketLabelNames = Object.keys(bucketData.labels); | ||
let acc = 0; | ||
@@ -266,5 +272,2 @@ for (const upperBound of histogram.upperBounds) { | ||
const lbls = { le: upperBound }; | ||
for (const labelName of bucketLabelNames) { | ||
lbls[labelName] = bucketData.labels[labelName]; | ||
} | ||
buckets.push( | ||
@@ -276,2 +279,3 @@ setValuePair( | ||
bucketData.bucketExemplars[upperBound], | ||
bucketData.labels, | ||
), | ||
@@ -289,5 +293,2 @@ ); | ||
const infLabel = { le: '+Inf' }; | ||
for (const label of Object.keys(d.data.labels)) { | ||
infLabel[label] = d.data.labels[label]; | ||
} | ||
acc.push( | ||
@@ -299,5 +300,18 @@ setValuePair( | ||
d.data.bucketExemplars['-1'], | ||
d.data.labels, | ||
), | ||
setValuePair(d.data.labels, d.data.sum, `${histogram.name}_sum`), | ||
setValuePair(d.data.labels, d.data.count, `${histogram.name}_count`), | ||
setValuePair( | ||
{}, | ||
d.data.sum, | ||
`${histogram.name}_sum`, | ||
undefined, | ||
d.data.labels, | ||
), | ||
setValuePair( | ||
{}, | ||
d.data.count, | ||
`${histogram.name}_count`, | ||
undefined, | ||
d.data.labels, | ||
), | ||
); | ||
@@ -308,2 +322,11 @@ return acc; | ||
function splayLabels(bucket) { | ||
const { sharedLabels, labels, ...newBucket } = bucket; | ||
for (const label of Object.keys(sharedLabels)) { | ||
labels[label] = sharedLabels[label]; | ||
} | ||
newBucket.labels = labels; | ||
return newBucket; | ||
} | ||
module.exports = Histogram; |
@@ -65,2 +65,5 @@ 'use strict'; | ||
return new Promise((resolve, reject) => { | ||
if (method === 'DELETE' && options.headers) { | ||
delete options.headers['Content-Encoding']; | ||
} | ||
const req = httpModule.request(options, resp => { | ||
@@ -67,0 +70,0 @@ let body = ''; |
@@ -32,3 +32,6 @@ 'use strict'; | ||
async getMetricsAsString(metrics) { | ||
const metric = await metrics.get(); | ||
const metric = | ||
typeof metrics.getForPromString === 'function' | ||
? await metrics.getForPromString() | ||
: await metrics.get(); | ||
@@ -45,2 +48,3 @@ const name = escapeString(metric.name); | ||
let { metricName = name, labels = {} } = val; | ||
const { sharedLabels = {} } = val; | ||
if ( | ||
@@ -57,10 +61,18 @@ this.contentType === Registry.OPENMETRICS_CONTENT_TYPE && | ||
const formattedLabels = formatLabels(labels); | ||
const labelsString = formattedLabels.length | ||
? `{${formattedLabels.join(',')}}` | ||
: ''; | ||
// We have to flatten these separately to avoid duplicate labels appearing | ||
// between the base labels and the shared labels | ||
const formattedLabels = []; | ||
for (const [n, v] of Object.entries(labels)) { | ||
if (Object.prototype.hasOwnProperty.call(sharedLabels, n)) { | ||
continue; | ||
} | ||
formattedLabels.push(`${n}="${escapeLabelValue(v)}"`); | ||
} | ||
values.push( | ||
`${metricName}${labelsString} ${getValueAsString(val.value)}`, | ||
); | ||
const flattenedShared = flattenSharedLabels(sharedLabels); | ||
const labelParts = [...formattedLabels, flattenedShared].filter(Boolean); | ||
const labelsString = labelParts.length ? `{${labelParts.join(',')}}` : ''; | ||
let fullMetricLine = `${metricName}${labelsString} ${getValueAsString( | ||
val.value, | ||
)}`; | ||
@@ -70,8 +82,7 @@ const { exemplar } = val; | ||
const formattedExemplars = formatLabels(exemplar.labelSet); | ||
values.push( | ||
` # {${formattedExemplars.join(',')}} ${getValueAsString( | ||
exemplar.value, | ||
)} ${exemplar.timestamp}`, | ||
); | ||
fullMetricLine += ` # {${formattedExemplars.join( | ||
',', | ||
)}} ${getValueAsString(exemplar.value)} ${exemplar.timestamp}`; | ||
} | ||
values.push(fullMetricLine); | ||
} | ||
@@ -214,2 +225,14 @@ | ||
const sharedLabelCache = new WeakMap(); | ||
function flattenSharedLabels(labels) { | ||
const cached = sharedLabelCache.get(labels); | ||
if (cached) { | ||
return cached; | ||
} | ||
const formattedLabels = formatLabels(labels); | ||
const flattened = formattedLabels.join(','); | ||
sharedLabelCache.set(labels, flattened); | ||
return flattened; | ||
} | ||
function escapeLabelValue(str) { | ||
@@ -216,0 +239,0 @@ if (typeof str !== 'string') { |
{ | ||
"name": "prom-client", | ||
"version": "15.0.0-0", | ||
"version": "15.0.0-1", | ||
"description": "Client for prometheus", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
122386
3065