-
Notifications
You must be signed in to change notification settings - Fork 797
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#342 - Implemented Label Value Sanitizer
Signed-off-by: Daniel Hoelbling-Inzko <[email protected]>
- Loading branch information
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
simpleclient/src/main/java/io/prometheus/client/LabelValueSanitizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.prometheus.client; | ||
|
||
public interface LabelValueSanitizer { | ||
String[] sanitize(String... labelValue); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
simpleclient/src/test/java/io/prometheus/client/SimpleCollectorWithLabelSanitizerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.prometheus.client; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.junit.rules.ExpectedException.none; | ||
|
||
|
||
public class SimpleCollectorWithLabelSanitizerTest { | ||
|
||
CollectorRegistry registry; | ||
Gauge metric; | ||
|
||
@Rule | ||
public final ExpectedException thrown = none(); | ||
|
||
@Before | ||
public void setUp() { | ||
registry = new CollectorRegistry(); | ||
metric = Gauge.build().name("nonulllabels").help("help").labelNames("l").labelValueSanitizer(Gauge.TransformNullLabelsToEmptyString()).register(registry); | ||
} | ||
|
||
private Double getValue(String labelValue) { | ||
return registry.getSampleValue("nonulllabels", new String[]{"l"}, new String[]{labelValue}); | ||
} | ||
|
||
@Test | ||
public void testNullLabelDoesntThrowWithLabelSanitizer() { | ||
metric.labels(new String[]{null}).inc(); | ||
assertEquals(1.0, getValue("").doubleValue(), .001); | ||
} | ||
} |