17
17
*/
18
18
package org .apache .hadoop .metrics2 .sink ;
19
19
20
+ import java .util .ArrayList ;
21
+ import java .util .List ;
22
+ import java .util .regex .Matcher ;
20
23
import org .apache .commons .configuration2 .SubsetConfiguration ;
21
24
import org .apache .hadoop .metrics2 .AbstractMetric ;
22
25
import org .apache .hadoop .metrics2 .MetricType ;
@@ -52,6 +55,13 @@ public class PrometheusMetricsSink implements MetricsSink {
52
55
Pattern .compile ("(?<!(^|[A-Z_]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])" );
53
56
private static final Pattern DELIMITERS = Pattern .compile ("[^a-zA-Z0-9]+" );
54
57
58
+ private static final Pattern NN_TOPMETRICS_PATTERN =
59
+ Pattern .compile (
60
+ "^(nn_top_user_op_counts_window_ms_\\ d+)_op_.*?(total_count|count)$" );
61
+ private static final Pattern NN_TOPMETRICS_TAGS_PATTERN =
62
+ Pattern
63
+ .compile ("^op=(?<op>\\ w+)(.user=(?<user>.*)|)\\ .(TotalCount|count)$" );
64
+
55
65
public PrometheusMetricsSink () {
56
66
}
57
67
@@ -95,25 +105,28 @@ public void init(SubsetConfiguration conf) {
95
105
}
96
106
97
107
public void writeMetrics (Writer writer ) throws IOException {
108
+ List <String > extendMetricsTags = new ArrayList <>();
98
109
for (Map .Entry <String , Map <Collection <MetricsTag >, AbstractMetric >> promMetric :
99
110
promMetrics .entrySet ()) {
100
111
AbstractMetric firstMetric = promMetric .getValue ().values ().iterator ().next ();
112
+ String metricKey = getMetricKey (promMetric .getKey (), firstMetric ,
113
+ extendMetricsTags );
101
114
102
115
StringBuilder builder = new StringBuilder ();
103
116
builder .append ("# HELP " )
104
- .append (promMetric . getKey () )
117
+ .append (metricKey )
105
118
.append (" " )
106
119
.append (firstMetric .description ())
107
120
.append ("\n " )
108
121
.append ("# TYPE " )
109
- .append (promMetric . getKey () )
122
+ .append (metricKey )
110
123
.append (" " )
111
124
.append (firstMetric .type ().toString ().toLowerCase ())
112
125
.append ("\n " );
113
126
114
127
for (Map .Entry <Collection <MetricsTag >, AbstractMetric > metric :
115
128
promMetric .getValue ().entrySet ()) {
116
- builder .append (promMetric . getKey () )
129
+ builder .append (metricKey )
117
130
.append ("{" );
118
131
119
132
String sep = "" ;
@@ -129,6 +142,13 @@ public void writeMetrics(Writer writer) throws IOException {
129
142
sep = "," ;
130
143
}
131
144
}
145
+ if (!extendMetricsTags .isEmpty ()) {
146
+ //add extend tags
147
+ for (String tagStr : extendMetricsTags ) {
148
+ builder .append (sep ).append (tagStr );
149
+ }
150
+ extendMetricsTags .clear ();
151
+ }
132
152
builder .append ("} " );
133
153
builder .append (metric .getValue ().value ());
134
154
builder .append ("\n " );
@@ -137,4 +157,39 @@ public void writeMetrics(Writer writer) throws IOException {
137
157
writer .write (builder .toString ());
138
158
}
139
159
}
160
+
161
+ private String getMetricKey (String promMetricKey , AbstractMetric metric ,
162
+ List <String > extendTags ) {
163
+ Matcher matcher = NN_TOPMETRICS_PATTERN .matcher (promMetricKey );
164
+ if (matcher .find () && matcher .groupCount () == 2 ) {
165
+ extendTags .addAll (parseTopMetricsTags (metric .name ()));
166
+ return String .format ("%s_%s" ,
167
+ matcher .group (1 ), matcher .group (2 ));
168
+ }
169
+ return promMetricKey ;
170
+ }
171
+
172
+ /**
173
+ * Parse Custom tags for TopMetrics.
174
+ *
175
+ * @param metricName metricName
176
+ * @return Tags for TopMetrics
177
+ */
178
+ private List <String > parseTopMetricsTags (String metricName ) {
179
+ List <String > topMetricsTags = new ArrayList <>();
180
+ Matcher matcher = NN_TOPMETRICS_TAGS_PATTERN .matcher (metricName );
181
+ if (matcher .find ()) {
182
+ String op = matcher .group ("op" );
183
+ String user = matcher .group ("user" );
184
+ // add tag op = "$op"
185
+ topMetricsTags .add (String
186
+ .format ("op=\" %s\" " , op ));
187
+ if (StringUtils .isNoneEmpty (user )) {
188
+ // add tag user = "$user"
189
+ topMetricsTags .add (String
190
+ .format ("user=\" %s\" " , user ));
191
+ }
192
+ }
193
+ return topMetricsTags ;
194
+ }
140
195
}
0 commit comments