PrometheusReporter implements MetricReporter { @VisibleForTesting static String replaceInvalidChars(final String input) { return UNALLOWED_CHAR_PATTERN.matcher(input).replaceAll("_"); }  @Override void open(MetricConfig config); @Override void close(); @Override void notifyOfAddedMetric(final Metric metric, final String metricName, final MetricGroup group); @Override void notifyOfRemovedMetric(final Metric metric, final String metricName, final MetricGroup group);  }
@Test public void invalidCharactersAreReplacedWithUnderscore() { assertThat(PrometheusReporter.replaceInvalidChars(""), equalTo("")); assertThat(PrometheusReporter.replaceInvalidChars("abc"), equalTo("abc")); assertThat(PrometheusReporter.replaceInvalidChars("abc\""), equalTo("abc_")); assertThat(PrometheusReporter.replaceInvalidChars("\"abc"), equalTo("_abc")); assertThat(PrometheusReporter.replaceInvalidChars("\"abc\""), equalTo("_abc_")); assertThat(PrometheusReporter.replaceInvalidChars("\"a\"b\"c\""), equalTo("_a_b_c_")); assertThat(PrometheusReporter.replaceInvalidChars("\"\"\"\""), equalTo("____")); assertThat(PrometheusReporter.replaceInvalidChars(" "), equalTo("____")); assertThat(PrometheusReporter.replaceInvalidChars("\"ab ;(c)'"), equalTo("_ab___c__")); assertThat(PrometheusReporter.replaceInvalidChars("a b c"), equalTo("a_b_c")); assertThat(PrometheusReporter.replaceInvalidChars("a b c "), equalTo("a_b_c_")); assertThat(PrometheusReporter.replaceInvalidChars("a;b'c*"), equalTo("a_b_c_")); assertThat(PrometheusReporter.replaceInvalidChars("a,=;:?'b,=;:?'c"), equalTo("a___:__b___:__c")); }