MCPcopy
hub / github.com/prometheus/client_js / getMetricsAsString

Method getMetricsAsString

lib/registry.js:31–81  ·  view source on GitHub ↗
(metrics)

Source from the content-addressed store, hash-verified

29 }
30
31 async getMetricsAsString(metrics) {
32 const metric =
33 typeof metrics.getForPromString === 'function'
34 ? await metrics.getForPromString()
35 : await metrics.get();
36
37 const name = escapeString(metric.name);
38 const help = `# HELP ${name} ${escapeString(metric.help)}`;
39 const type = `# TYPE ${name} ${metric.type}`;
40 const values = [help, type];
41
42 const defaultLabels =
43 Object.keys(this._defaultLabels).length > 0 ? this._defaultLabels : null;
44
45 const isOpenMetrics =
46 this.contentType === Registry.OPENMETRICS_CONTENT_TYPE;
47
48 for (const val of metric.values || []) {
49 let { metricName = name, labels = {} } = val;
50 const { sharedLabels = {} } = val;
51 if (isOpenMetrics && metric.type === 'counter') {
52 metricName = `${metricName}_total`;
53 }
54
55 if (defaultLabels) {
56 labels = { ...labels, ...defaultLabels, ...labels };
57 }
58
59 // We have to flatten these separately to avoid duplicate labels appearing
60 // between the base labels and the shared labels
61 const formattedLabels = formatLabels(labels, sharedLabels);
62
63 const flattenedShared = flattenSharedLabels(sharedLabels);
64 const labelParts = [...formattedLabels, flattenedShared].filter(Boolean);
65 const labelsString = labelParts.length ? `{${labelParts.join(',')}}` : '';
66 let fullMetricLine = `${metricName}${labelsString} ${getValueAsString(
67 val.value,
68 )}`;
69
70 const { exemplar } = val;
71 if (exemplar && isOpenMetrics) {
72 const formattedExemplars = formatLabels(exemplar.labelSet);
73 fullMetricLine += ` # {${formattedExemplars.join(
74 ',',
75 )}} ${getValueAsString(exemplar.value)} ${exemplar.timestamp}`;
76 }
77 values.push(fullMetricLine);
78 }
79
80 return values.join('\n');
81 }
82
83 async metrics() {
84 const isOpenMetrics =

Callers 2

metricsMethod · 0.95

Calls 6

escapeStringFunction · 0.85
formatLabelsFunction · 0.85
flattenSharedLabelsFunction · 0.85
getForPromStringMethod · 0.80
getMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected