forked from smallrye/smallrye-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrometheusExporter.java
390 lines (324 loc) · 15.5 KB
/
PrometheusExporter.java
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* Copyright 2017 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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
*
* http://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.
*/
package io.smallrye.metrics.exporters;
import io.smallrye.metrics.MetricRegistries;
import io.smallrye.metrics.Tag;
import io.smallrye.metrics.app.HistogramImpl;
import io.smallrye.metrics.app.MeterImpl;
import io.smallrye.metrics.app.TimerImpl;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.metrics.Counter;
import org.eclipse.microprofile.metrics.Gauge;
import org.eclipse.microprofile.metrics.Metadata;
import org.eclipse.microprofile.metrics.Metered;
import org.eclipse.microprofile.metrics.Metric;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.MetricType;
import org.eclipse.microprofile.metrics.MetricUnits;
import org.eclipse.microprofile.metrics.Snapshot;
import org.jboss.logging.Logger;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Export data in Prometheus text format
*
* @author Heiko W. Rupp
*/
public class PrometheusExporter implements Exporter {
private static final Logger log = Logger.getLogger("io.smallrye.metrics");
// This allows to suppress the (noisy) # HELP line
private static final String MICROPROFILE_METRICS_OMIT_HELP_LINE = "microprofile.metrics.omitHelpLine";
private static final Pattern SNAKE_CASE_PATTERN = Pattern.compile("(?<=[a-z])[A-Z]");
private static final String LF = "\n";
private static final String GAUGE = "gauge";
private static final String SPACE = " ";
private static final String SUMMARY = "summary";
private static final String USCORE = "_";
private static final String COUNTER = "counter";
private static final String QUANTILE = "quantile";
private boolean writeHelpLine;
public PrometheusExporter() {
Config config = ConfigProvider.getConfig();
Optional<Boolean> tmp = config.getOptionalValue(MICROPROFILE_METRICS_OMIT_HELP_LINE, Boolean.class);
writeHelpLine = !tmp.isPresent() || !tmp.get();
}
public StringBuffer exportOneScope(MetricRegistry.Type scope) {
StringBuffer sb = new StringBuffer();
getEntriesForScope(scope, sb);
return sb;
}
@Override
public StringBuffer exportAllScopes() {
StringBuffer sb = new StringBuffer();
for (MetricRegistry.Type scope : MetricRegistry.Type.values()) {
getEntriesForScope(scope, sb);
}
return sb;
}
@Override
public StringBuffer exportOneMetric(MetricRegistry.Type scope, String metricName) {
MetricRegistry registry = MetricRegistries.get(scope);
Map<String, Metric> metricMap = registry.getMetrics();
Metric m = metricMap.get(metricName);
Map<String, Metric> outMap = new HashMap<>(1);
outMap.put(metricName, m);
StringBuffer sb = new StringBuffer();
exposeEntries(scope, sb, registry, outMap);
return sb;
}
@Override
public String getContentType() {
return "text/plain";
}
private void getEntriesForScope(MetricRegistry.Type scope, StringBuffer sb) {
MetricRegistry registry = MetricRegistries.get(scope);
Map<String, Metric> metricMap = registry.getMetrics();
exposeEntries(scope, sb, registry, metricMap);
}
private void exposeEntries(MetricRegistry.Type scope, StringBuffer sb, MetricRegistry registry, Map<String, Metric> metricMap) {
for (Map.Entry<String, Metric> entry : metricMap.entrySet()) {
String key = entry.getKey();
Metadata md = registry.getMetadata().get(key);
if (md == null) {
throw new IllegalStateException("No entry for " + key + " found");
}
Metric metric = entry.getValue();
StringBuffer metricBuf = new StringBuffer();
try {
switch (md.getTypeRaw()) {
case GAUGE:
case COUNTER:
key = getPrometheusMetricName(key);
String suffix = null;
if (!md.getUnit().equals(MetricUnits.NONE)) {
suffix = USCORE + PrometheusUnit.getBaseUnitAsPrometheusString(md.getUnit());
}
writeHelpLine(metricBuf, scope, key, md, suffix);
writeTypeLine(metricBuf, scope, key, md, suffix, null);
createSimpleValueLine(metricBuf, scope, key, md, metric);
break;
case METERED:
MeterImpl meter = (MeterImpl) metric;
writeMeterValues(metricBuf, scope, meter, md);
break;
case TIMER:
TimerImpl timer = (TimerImpl) metric;
writeTimerValues(metricBuf, scope, timer, md);
break;
case HISTOGRAM:
HistogramImpl histogram = (HistogramImpl) metric;
writeHistogramValues(metricBuf, scope, histogram, md);
break;
default:
throw new IllegalArgumentException("Not supported: " + key);
}
sb.append(metricBuf);
} catch (Exception e) {
log.warn("Unable to export metric " + key, e);
}
}
}
private void writeTimerValues(StringBuffer sb, MetricRegistry.Type scope, TimerImpl timer, Metadata md) {
String unit = md.getUnit();
unit = PrometheusUnit.getBaseUnitAsPrometheusString(unit);
String theUnit = unit.equals("none") ? "" : USCORE + unit;
writeMeterRateValues(sb, scope, timer.getMeter(), md);
Snapshot snapshot = timer.getSnapshot();
writeSnapshotBasics(sb, scope, md, snapshot, theUnit, true);
String suffix = USCORE + PrometheusUnit.getBaseUnitAsPrometheusString(md.getUnit());
writeHelpLine(sb, scope, md.getName(), md, suffix);
writeTypeLine(sb,scope,md.getName(),md, suffix,SUMMARY);
writeValueLine(sb,scope,suffix + "_count",timer.getCount(),md, null, false);
writeSnapshotQuantiles(sb, scope, md, snapshot, theUnit, true);
}
private void writeHistogramValues(StringBuffer sb, MetricRegistry.Type scope, HistogramImpl histogram, Metadata md) {
Snapshot snapshot = histogram.getSnapshot();
String unit = md.getUnit();
unit = PrometheusUnit.getBaseUnitAsPrometheusString(unit);
String theUnit = unit.equals("none") ? "" : USCORE + unit;
writeHelpLine(sb, scope, md.getName(), md, SUMMARY);
writeSnapshotBasics(sb, scope, md, snapshot, theUnit, true);
writeTypeLine(sb,scope,md.getName(),md, theUnit,SUMMARY);
writeValueLine(sb,scope,theUnit + "_count",histogram.getCount(),md, null, false);
writeSnapshotQuantiles(sb, scope, md, snapshot, theUnit, true);
}
private void writeSnapshotBasics(StringBuffer sb, MetricRegistry.Type scope, Metadata md, Snapshot snapshot, String unit, boolean performScaling) {
writeTypeAndValue(sb, scope, "_min" + unit, snapshot.getMin(), GAUGE, md, performScaling);
writeTypeAndValue(sb, scope, "_max" + unit, snapshot.getMax(), GAUGE, md, performScaling);
writeTypeAndValue(sb, scope, "_mean" + unit, snapshot.getMean(), GAUGE, md, performScaling);
writeTypeAndValue(sb, scope, "_stddev" + unit, snapshot.getStdDev(), GAUGE, md, performScaling);
}
private void writeSnapshotQuantiles(StringBuffer sb, MetricRegistry.Type scope, Metadata md, Snapshot snapshot, String unit, boolean performScaling) {
writeValueLine(sb, scope, unit, snapshot.getMedian(), md, new Tag(QUANTILE, "0.5"), performScaling);
writeValueLine(sb, scope, unit, snapshot.get75thPercentile(), md, new Tag(QUANTILE, "0.75"), performScaling);
writeValueLine(sb, scope, unit, snapshot.get95thPercentile(), md, new Tag(QUANTILE, "0.95"), performScaling);
writeValueLine(sb, scope, unit, snapshot.get98thPercentile(), md, new Tag(QUANTILE, "0.98"), performScaling);
writeValueLine(sb, scope, unit, snapshot.get99thPercentile(), md, new Tag(QUANTILE, "0.99"), performScaling);
writeValueLine(sb, scope, unit, snapshot.get999thPercentile(), md, new Tag(QUANTILE, "0.999"), performScaling);
}
private void writeMeterValues(StringBuffer sb, MetricRegistry.Type scope, Metered metric, Metadata md) {
writeHelpLine(sb, scope, md.getName(), md, "_total");
writeTypeAndValue(sb, scope, "_total", metric.getCount(), COUNTER, md, false);
writeMeterRateValues(sb, scope, metric, md);
}
private void writeMeterRateValues(StringBuffer sb, MetricRegistry.Type scope, Metered metric, Metadata md) {
writeTypeAndValue(sb, scope, "_rate_per_second", metric.getMeanRate(), GAUGE, md, false);
writeTypeAndValue(sb, scope, "_one_min_rate_per_second", metric.getOneMinuteRate(), GAUGE, md, false);
writeTypeAndValue(sb, scope, "_five_min_rate_per_second", metric.getFiveMinuteRate(), GAUGE, md, false);
writeTypeAndValue(sb, scope, "_fifteen_min_rate_per_second", metric.getFifteenMinuteRate(), GAUGE, md, false);
}
private void writeTypeAndValue(StringBuffer sb, MetricRegistry.Type scope, String suffix, double valueRaw, String type, Metadata md, boolean performScaling) {
String key = md.getName();
writeTypeLine(sb, scope, key, md, suffix, type);
writeValueLine(sb, scope, suffix, valueRaw, md, null, performScaling);
}
private void writeValueLine(StringBuffer sb, MetricRegistry.Type scope, String suffix, double valueRaw, Metadata md) {
writeValueLine(sb, scope, suffix, valueRaw, md, null);
}
private void writeValueLine(StringBuffer sb, MetricRegistry.Type scope, String suffix, double valueRaw, Metadata md, Tag extraTag) {
writeValueLine(sb, scope, suffix, valueRaw, md, extraTag, true);
}
private void writeValueLine(StringBuffer sb,
MetricRegistry.Type scope,
String suffix,
double valueRaw,
Metadata md,
Tag extraTag,
boolean performScaling) {
String name = md.getName();
name = getPrometheusMetricName(name);
fillBaseName(sb, scope, name);
if (suffix != null) {
sb.append(suffix);
}
// add tags
Map<String, String> tags = new HashMap<>(md.getTags());
if (extraTag != null) {
tags.put(extraTag.getKey(), extraTag.getValue());
}
if (!tags.isEmpty()) {
addTags(sb, tags);
}
sb.append(SPACE);
Double value;
if(performScaling) {
String scaleFrom = "nanoseconds";
if(md.getTypeRaw() == MetricType.HISTOGRAM)
// for histograms, internally the data is stored using the metric's unit
scaleFrom = md.getUnit();
value = PrometheusUnit.scaleToBase(scaleFrom, valueRaw);
} else {
value = valueRaw;
}
sb.append(value).append(LF);
}
private void addTags(StringBuffer sb, Map<String, String> tags) {
Iterator<Map.Entry<String, String>> iter = tags.entrySet().iterator();
sb.append("{");
while (iter.hasNext()) {
Map.Entry<String, String> tag = iter.next();
sb.append(tag.getKey()).append("=\"").append(tag.getValue()).append("\"");
if (iter.hasNext()) {
sb.append(",");
}
}
sb.append("}");
}
private void fillBaseName(StringBuffer sb, MetricRegistry.Type scope, String key) {
sb.append(scope.getName().toLowerCase()).append(":").append(key);
}
private void writeHelpLine(StringBuffer sb, MetricRegistry.Type scope, String key, Metadata md, String suffix) {
// Only write this line if we actually have a description in metadata
if (writeHelpLine && md.getDescription() != null) {
sb.append("# HELP ");
sb.append(scope.getName().toLowerCase());
sb.append(':').append(getPrometheusMetricName(key));
if (suffix != null) {
sb.append(suffix);
}
sb.append(SPACE);
sb.append(md.getDescription());
sb.append(LF);
}
}
private void writeTypeLine(StringBuffer sb, MetricRegistry.Type scope, String key, Metadata md, String suffix, String typeOverride) {
sb.append("# TYPE ");
sb.append(scope.getName().toLowerCase());
sb.append(':').append(getPrometheusMetricName(key));
if (suffix != null) {
sb.append(suffix);
}
sb.append(SPACE);
if (typeOverride != null) {
sb.append(typeOverride);
} else if (md.getTypeRaw().equals(MetricType.TIMER)) {
sb.append(SUMMARY);
} else if (md.getTypeRaw().equals(MetricType.METERED)) {
sb.append(COUNTER);
} else {
sb.append(md.getType());
}
sb.append(LF);
}
private void createSimpleValueLine(StringBuffer sb, MetricRegistry.Type scope, String key, Metadata md, Metric metric) {
// value line
fillBaseName(sb, scope, key);
if (!md.getUnit().equals(MetricUnits.NONE)) {
String unit = PrometheusUnit.getBaseUnitAsPrometheusString(md.getUnit());
sb.append("_").append(unit);
}
String tags = md.getTagsAsString();
if (tags != null && !tags.isEmpty()) {
sb.append('{').append(tags).append('}');
}
Double valIn;
if (md.getTypeRaw().equals(MetricType.GAUGE)) {
Number value1 = (Number) ((Gauge) metric).getValue();
if (value1 != null) {
valIn = value1.doubleValue();
} else {
log.warn("Value is null for " + key);
throw new IllegalStateException("Value must not be null for " + key);
}
} else {
valIn = (double) ((Counter) metric).getCount();
}
Double value = PrometheusUnit.scaleToBase(md.getUnit(), valIn);
sb.append(SPACE).append(value).append(LF);
}
static String getPrometheusMetricName(String name) {
String out = name.replaceAll("[^\\w]+",USCORE);
out = decamelize(out);
out = out.replace("__", USCORE);
out = out.replace(":_", ":");
return out;
}
private static String decamelize(String in) {
Matcher m = SNAKE_CASE_PATTERN.matcher(in);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, USCORE + m.group().toLowerCase());
}
m.appendTail(sb);
return sb.toString().toLowerCase();
}
}