public static void main(String[] args) {
DropwizardMetricRegistry registry = new DropwizardMetricRegistry();
PrometheusInstanceSampleSpecModsProvider instanceSampleSpecModsProvider = new PrometheusInstanceSampleSpecModsProvider();
PrometheusInstanceSamplesProvider instanceSamplesProvider = new PrometheusInstanceSamplesProvider(
instanceSampleSpecModsProvider,
registry
);
MetricName name = MetricName.of("counter_1", "suffix");
System.out.println(name + " part 0 is 'counter_1': " + name.part(0).equals("counter_1"));
Counter counter = registry.counter(name);
counter.inc();
instanceSampleSpecModsProvider.addMod(
forMetricInstancesMatching(
nameMask("counter_1.suffix"),
mi -> mi.name().part(0).equals("counter_1")
),
(metric, instance) -> prometheusInstanceSampleSpec()
.name(MetricName.name("new_name_for_counter_1"))
);
MetricName name2 = MetricName.of("counter_2", "type_1", "suffix");
System.out.println(name2 + " part 0 is 'counter_2': " + name2.part(0).equals("counter_2"));
Counter counter2 = registry.counter(name2);
counter2.inc();
instanceSampleSpecModsProvider.addMod(
forMetricInstancesMatching(
nameMask("counter_2.**.suffix"),
mi -> mi.name().part(0).equals("counter_2")
),
(metric, instance) -> prometheusInstanceSampleSpec()
.name(MetricName.name("new_name_for_counter_2"))
);
PrometheusMetricsExporter prometheusMetricsExporter = new PrometheusMetricsExporter(instanceSamplesProvider);
System.out.println(prometheusMetricsExporter.exportMetrics());
}
counter_1.suffix part 0 is 'counter_1': true
counter_2.type_1.suffix part 0 is 'counter_2': true
# HELP counter_2_type_1_suffix Generated from metric instances with name counter_2.type_1.suffix
# TYPE counter_2_type_1_suffix gauge
counter_2_type_1_suffix 1.0
# HELP new_name_for_counter_1 Generated from metric instances with name counter_1.suffix
# TYPE new_name_for_counter_1 gauge
new_name_for_counter_1 1.0
counter_1.suffix part 0 is 'counter_1': true
counter_2.type_1.suffix part 0 is 'counter_2': true
# HELP new_name_for_counter_1 Generated from metric instances with name counter_1.suffix
# TYPE new_name_for_counter_1 gauge
new_name_for_counter_1 1.0
# HELP new_name_for_counter_2 Generated from metric instances with name counter_2.type_1.suffix
# TYPE new_name_for_counter_2 gauge
new_name_for_counter_2 1.0
It's expected that names for both counters will be replaced.
But currently only counter_1's name is replaced.
Please, take a look.