5
5
import io .prometheus .client .Gauge ;
6
6
import io .prometheus .client .Summary ;
7
7
import io .prometheus .client .exporter .common .TextFormat ;
8
- import org .elasticsearch . common . logging .ESLogger ;
8
+ import org .apache . logging .log4j . Logger ;
9
9
import org .elasticsearch .common .logging .ESLoggerFactory ;
10
10
import org .elasticsearch .rest .prometheus .RestPrometheusMetricsAction ;
11
11
14
14
import java .io .Writer ;
15
15
import java .util .HashMap ;
16
16
17
- public class PrometheusMetricsCatalog {
18
- private final static ESLogger logger = ESLoggerFactory .getLogger (RestPrometheusMetricsAction .class .getSimpleName ());
17
+ public class PrometheusMetricsCatalog
18
+ {
19
+ private final static Logger logger = ESLoggerFactory .getLogger (RestPrometheusMetricsAction .class .getSimpleName ());
19
20
20
21
private String cluster ;
21
22
private String metric_prefix ;
22
23
private HashMap metrics ;
23
24
private CollectorRegistry registry ;
24
25
25
- public PrometheusMetricsCatalog (String cluster , String metric_prefix ) {
26
+ public PrometheusMetricsCatalog (String cluster , String metric_prefix )
27
+ {
26
28
this .cluster = cluster ;
27
29
this .metric_prefix = metric_prefix ;
28
30
metrics = new HashMap ();
29
31
registry = new CollectorRegistry ();
30
32
}
31
33
32
- private String [] getExtendedLabelNames (String ... label_names ) {
34
+ private String [] getExtendedLabelNames (String ... label_names )
35
+ {
33
36
String [] extended = new String [label_names .length + 1 ];
34
37
extended [0 ] = "cluster" ;
35
38
@@ -38,7 +41,8 @@ private String[] getExtendedLabelNames(String... label_names) {
38
41
return extended ;
39
42
}
40
43
41
- private String [] getExtendedLabelValues (String ... label_values ) {
44
+ private String [] getExtendedLabelValues (String ... label_values )
45
+ {
42
46
String [] extended = new String [label_values .length + 1 ];
43
47
extended [0 ] = cluster ;
44
48
@@ -47,7 +51,8 @@ private String[] getExtendedLabelValues(String... label_values) {
47
51
return extended ;
48
52
}
49
53
50
- public void registerGauge (String metric , String help , String ... labels ) {
54
+ public void registerGauge (String metric , String help , String ... labels )
55
+ {
51
56
Gauge gauge = Gauge .build ().
52
57
name (metric_prefix + metric ).
53
58
help (help ).
@@ -59,12 +64,14 @@ public void registerGauge(String metric, String help, String... labels) {
59
64
logger .debug (String .format ("Registered new gauge %s" , metric ));
60
65
}
61
66
62
- public void setGauge (String metric , double value , String ... label_values ) {
67
+ public void setGauge (String metric , double value , String ... label_values )
68
+ {
63
69
Gauge gauge = (Gauge ) metrics .get (metric );
64
70
gauge .labels (getExtendedLabelValues (label_values )).set (value );
65
71
}
66
72
67
- public void registerCounter (String metric , String help , String ... labels ) {
73
+ public void registerCounter (String metric , String help , String ... labels )
74
+ {
68
75
Counter counter = Counter .build ().
69
76
name (metric_prefix + metric ).
70
77
help (help ).
@@ -76,20 +83,25 @@ public void registerCounter(String metric, String help, String... labels) {
76
83
logger .debug (String .format ("Registered new counter %s" , metric ));
77
84
}
78
85
79
- public void setCounter (String metric , double value , String ... label_values ) {
86
+ public void setCounter (String metric , double value , String ... label_values )
87
+ {
80
88
String [] extended_label_values = getExtendedLabelValues (label_values );
81
89
Counter counter = (Counter ) metrics .get (metric );
82
90
83
91
double increment = value - counter .labels (extended_label_values ).get ();
84
92
85
- if (increment >= 0 ) {
93
+ if (increment >= 0 )
94
+ {
86
95
counter .labels (extended_label_values ).inc (increment );
87
- } else {
96
+ }
97
+ else
98
+ {
88
99
logger .error (String .format ("Can not increment metric %s with value %f, skipping" , metric , increment ));
89
100
}
90
101
}
91
102
92
- public void registerSummaryTimer (String metric , String help , String ... labels ) {
103
+ public void registerSummaryTimer (String metric , String help , String ... labels )
104
+ {
93
105
Summary summary = Summary .build ().
94
106
name (metric_prefix + metric ).
95
107
help (help ).
@@ -101,17 +113,22 @@ public void registerSummaryTimer(String metric, String help, String... labels) {
101
113
logger .debug (String .format ("Registered new summary %s" , metric ));
102
114
}
103
115
104
- public Summary .Timer startSummaryTimer (String metric , String ... label_values ) {
116
+ public Summary .Timer startSummaryTimer (String metric , String ... label_values )
117
+ {
105
118
Summary summary = (Summary ) metrics .get (metric );
106
119
return summary .labels (getExtendedLabelValues (label_values )).startTimer ();
107
120
}
108
121
109
- public String toTextFormat () throws IOException {
110
- try {
122
+ public String toTextFormat () throws IOException
123
+ {
124
+ try
125
+ {
111
126
Writer writer = new StringWriter ();
112
127
TextFormat .write004 (writer , registry .metricFamilySamples ());
113
128
return writer .toString ();
114
- } catch (IOException e ) {
129
+ }
130
+ catch (IOException e )
131
+ {
115
132
throw e ;
116
133
}
117
134
}
0 commit comments