@@ -51,6 +51,7 @@ public abstract class SimpleCollector<Child> extends Collector {
51
51
protected final String help ;
52
52
protected final String unit ;
53
53
protected final List <String > labelNames ;
54
+ protected final LabelValueSanitizer labelValueSanitizer ;
54
55
55
56
protected final ConcurrentMap <List <String >, Child > children = new ConcurrentHashMap <List <String >, Child >();
56
57
protected Child noLabelsChild ;
@@ -64,6 +65,7 @@ public Child labels(String... labelValues) {
64
65
if (labelValues .length != labelNames .size ()) {
65
66
throw new IllegalArgumentException ("Incorrect number of labels." );
66
67
}
68
+ labelValues = labelValueSanitizer .sanitize (labelValues );
67
69
for (String label : labelValues ) {
68
70
if (label == null ) {
69
71
throw new IllegalArgumentException ("Label cannot be null." );
@@ -174,12 +176,42 @@ protected SimpleCollector(Builder b) {
174
176
for (String n : labelNames ) {
175
177
checkMetricLabelName (n );
176
178
}
179
+ labelValueSanitizer = b .labelValueSanitizer ;
177
180
178
181
if (!b .dontInitializeNoLabelsChild ) {
179
182
initializeNoLabelsChild ();
180
183
}
181
184
}
182
185
186
+ /**
187
+ * Default Sanitizer - Will not perform any manipulation on labels
188
+ */
189
+ static LabelValueSanitizer NoOpLabelValueSanitizer () {
190
+ return new LabelValueSanitizer () {
191
+ @ Override
192
+ public String [] sanitize (String ... labelValue ) {
193
+ return labelValue ;
194
+ }
195
+ };
196
+ }
197
+
198
+ /**
199
+ * Transforms null labels to an empty string to guard against IllegalArgument runtime exceptions
200
+ */
201
+ static LabelValueSanitizer TransformNullLabelsToEmptyString () {
202
+ return new LabelValueSanitizer () {
203
+ @ Override
204
+ public String [] sanitize (String ... labelValue ) {
205
+ for (int i = 0 ; i < labelValue .length ; i ++) {
206
+ if (labelValue [i ] == null ) {
207
+ labelValue [i ] = "" ;
208
+ }
209
+ }
210
+ return labelValue ;
211
+ }
212
+ };
213
+ }
214
+
183
215
/**
184
216
* Builders let you configure and then create collectors.
185
217
*/
@@ -191,6 +223,7 @@ public abstract static class Builder<B extends Builder<B, C>, C extends SimpleCo
191
223
String unit = "" ;
192
224
String help = "" ;
193
225
String [] labelNames = new String []{};
226
+ LabelValueSanitizer labelValueSanitizer = NoOpLabelValueSanitizer ();
194
227
// Some metrics require additional setup before the initialization can be done.
195
228
boolean dontInitializeNoLabelsChild ;
196
229
@@ -239,6 +272,18 @@ public B labelNames(String... labelNames) {
239
272
return (B )this ;
240
273
}
241
274
275
+ /**
276
+ * Sanitize labels. Optional, defaults to no manipulation of labels.
277
+ * Useful to scrub credentials from labels
278
+ * or avoid Null values causing runtime exceptions
279
+ * @param handler
280
+ * @return new array of labels to use
281
+ */
282
+ public B labelValueSanitizer (LabelValueSanitizer handler ) {
283
+ this .labelValueSanitizer = handler ;
284
+ return (B )this ;
285
+ }
286
+
242
287
/**
243
288
* Return the constructed collector.
244
289
* <p>
0 commit comments