-
Notifications
You must be signed in to change notification settings - Fork 825
/
PrometheusSerializer.ts
347 lines (312 loc) · 9.59 KB
/
PrometheusSerializer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { diag, Attributes, AttributeValue } from '@opentelemetry/api';
import {
ResourceMetrics,
InstrumentType,
DataPointType,
ScopeMetrics,
MetricData,
DataPoint,
Histogram,
} from '@opentelemetry/sdk-metrics';
import { hrTimeToMilliseconds } from '@opentelemetry/core';
import { IResource } from '@opentelemetry/resources';
type PrometheusDataTypeLiteral =
| 'counter'
| 'gauge'
| 'histogram'
| 'summary'
| 'untyped';
function escapeString(str: string) {
return str.replace(/\\/g, '\\\\').replace(/\n/g, '\\n');
}
/**
* String Attribute values are converted directly to Prometheus attribute values.
* Non-string values are represented as JSON-encoded strings.
*
* `undefined` is converted to an empty string.
*/
function escapeAttributeValue(str: AttributeValue = '') {
if (typeof str !== 'string') {
str = JSON.stringify(str);
}
return escapeString(str).replace(/"/g, '\\"');
}
const invalidCharacterRegex = /[^a-z0-9_]/gi;
const multipleUnderscoreRegex = /_{2,}/g;
/**
* Ensures metric names are valid Prometheus metric names by removing
* characters allowed by OpenTelemetry but disallowed by Prometheus.
*
* https://prometheus.io/docs/concepts/data_model/#metric-names-and-attributes
*
* 1. Names must match `[a-zA-Z_:][a-zA-Z0-9_:]*`
*
* 2. Colons are reserved for user defined recording rules.
* They should not be used by exporters or direct instrumentation.
*
* OpenTelemetry metric names are already validated in the Meter when they are created,
* and they match the format `[a-zA-Z][a-zA-Z0-9_.\-]*` which is very close to a valid
* prometheus metric name, so we only need to strip characters valid in OpenTelemetry
* but not valid in prometheus and replace them with '_'.
*
* @param name name to be sanitized
*/
function sanitizePrometheusMetricName(name: string): string {
// replace all invalid characters with '_'
return name
.replace(invalidCharacterRegex, '_')
.replace(multipleUnderscoreRegex, '_');
}
/**
* @private
*
* Helper method which assists in enforcing the naming conventions for metric
* names in Prometheus
* @param name the name of the metric
* @param type the kind of metric
* @returns string
*/
function enforcePrometheusNamingConvention(
name: string,
type: InstrumentType
): string {
// Prometheus requires that metrics of the Counter kind have "_total" suffix
if (!name.endsWith('_total') && type === InstrumentType.COUNTER) {
name = name + '_total';
}
return name;
}
function valueString(value: number) {
if (Number.isNaN(value)) {
return 'NaN';
} else if (!Number.isFinite(value)) {
if (value < 0) {
return '-Inf';
} else {
return '+Inf';
}
} else {
return `${value}`;
}
}
function toPrometheusType(metricData: MetricData): PrometheusDataTypeLiteral {
switch (metricData.dataPointType) {
case DataPointType.SUM:
if (metricData.isMonotonic) {
return 'counter';
}
return 'gauge';
case DataPointType.GAUGE:
return 'gauge';
case DataPointType.HISTOGRAM:
return 'histogram';
default:
return 'untyped';
}
}
function stringify(
metricName: string,
attributes: Attributes,
value: number,
timestamp?: number,
additionalAttributes?: Attributes
) {
let hasAttribute = false;
let attributesStr = '';
for (const [key, val] of Object.entries(attributes)) {
const sanitizedAttributeName = sanitizePrometheusMetricName(key);
hasAttribute = true;
attributesStr += `${
attributesStr.length > 0 ? ',' : ''
}${sanitizedAttributeName}="${escapeAttributeValue(val)}"`;
}
if (additionalAttributes) {
for (const [key, val] of Object.entries(additionalAttributes)) {
const sanitizedAttributeName = sanitizePrometheusMetricName(key);
hasAttribute = true;
attributesStr += `${
attributesStr.length > 0 ? ',' : ''
}${sanitizedAttributeName}="${escapeAttributeValue(val)}"`;
}
}
if (hasAttribute) {
metricName += `{${attributesStr}}`;
}
return `${metricName} ${valueString(value)}${
timestamp !== undefined ? ' ' + String(timestamp) : ''
}\n`;
}
const NO_REGISTERED_METRICS = '# no registered metrics';
export class PrometheusSerializer {
private _prefix: string | undefined;
private _appendTimestamp: boolean;
constructor(prefix?: string, appendTimestamp = false) {
if (prefix) {
this._prefix = prefix + '_';
}
this._appendTimestamp = appendTimestamp;
}
serialize(resourceMetrics: ResourceMetrics): string {
let str = '';
for (const scopeMetrics of resourceMetrics.scopeMetrics) {
str += this._serializeScopeMetrics(scopeMetrics);
}
if (str === '') {
str += NO_REGISTERED_METRICS;
}
return this._serializeResource(resourceMetrics.resource) + str;
}
private _serializeScopeMetrics(scopeMetrics: ScopeMetrics) {
let str = '';
for (const metric of scopeMetrics.metrics) {
str += this._serializeMetricData(metric) + '\n';
}
return str;
}
private _serializeMetricData(metricData: MetricData) {
let name = sanitizePrometheusMetricName(
escapeString(metricData.descriptor.name)
);
if (this._prefix) {
name = `${this._prefix}${name}`;
}
const dataPointType = metricData.dataPointType;
name = enforcePrometheusNamingConvention(name, metricData.descriptor.type);
const help = `# HELP ${name} ${escapeString(
metricData.descriptor.description || 'description missing'
)}`;
const unit = metricData.descriptor.unit
? `\n# UNIT ${name} ${escapeString(metricData.descriptor.unit)}`
: '';
const type = `# TYPE ${name} ${toPrometheusType(metricData)}`;
let results = '';
switch (dataPointType) {
case DataPointType.SUM:
case DataPointType.GAUGE: {
results = metricData.dataPoints
.map(it =>
this._serializeSingularDataPoint(
name,
metricData.descriptor.type,
it
)
)
.join('');
break;
}
case DataPointType.HISTOGRAM: {
results = metricData.dataPoints
.map(it =>
this._serializeHistogramDataPoint(
name,
metricData.descriptor.type,
it
)
)
.join('');
break;
}
default: {
diag.error(
`Unrecognizable DataPointType: ${dataPointType} for metric "${name}"`
);
}
}
return `${help}${unit}\n${type}\n${results}`.trim();
}
private _serializeSingularDataPoint(
name: string,
type: InstrumentType,
dataPoint: DataPoint<number>
): string {
let results = '';
name = enforcePrometheusNamingConvention(name, type);
const { value, attributes } = dataPoint;
const timestamp = hrTimeToMilliseconds(dataPoint.endTime);
results += stringify(
name,
attributes,
value,
this._appendTimestamp ? timestamp : undefined,
undefined
);
return results;
}
private _serializeHistogramDataPoint(
name: string,
type: InstrumentType,
dataPoint: DataPoint<Histogram>
): string {
let results = '';
name = enforcePrometheusNamingConvention(name, type);
const attributes = dataPoint.attributes;
const histogram = dataPoint.value;
const timestamp = hrTimeToMilliseconds(dataPoint.endTime);
/** Histogram["bucket"] is not typed with `number` */
for (const key of ['count', 'sum'] as ('count' | 'sum')[]) {
const value = histogram[key];
if (value != null)
results += stringify(
name + '_' + key,
attributes,
value,
this._appendTimestamp ? timestamp : undefined,
undefined
);
}
let cumulativeSum = 0;
const countEntries = histogram.buckets.counts.entries();
let infiniteBoundaryDefined = false;
for (const [idx, val] of countEntries) {
cumulativeSum += val;
const upperBound = histogram.buckets.boundaries[idx];
/** HistogramAggregator is producing different boundary output -
* in one case not including infinity values, in other -
* full, e.g. [0, 100] and [0, 100, Infinity]
* we should consider that in export, if Infinity is defined, use it
* as boundary
*/
if (upperBound === undefined && infiniteBoundaryDefined) {
break;
}
if (upperBound === Infinity) {
infiniteBoundaryDefined = true;
}
results += stringify(
name + '_bucket',
attributes,
cumulativeSum,
this._appendTimestamp ? timestamp : undefined,
{
le:
upperBound === undefined || upperBound === Infinity
? '+Inf'
: String(upperBound),
}
);
}
return results;
}
protected _serializeResource(resource: IResource): string {
const name = 'target_info';
const help = `# HELP ${name} Target metadata`;
const type = `# TYPE ${name} gauge`;
const results = stringify(name, resource.attributes, 1).trim();
return `${help}\n${type}\n${results}\n`;
}
}