@@ -24,6 +24,14 @@ public class TextFormat {
2424 */
2525 public final static String CONTENT_TYPE_OPENMETRICS_100 = "application/openmetrics-text; version=1.0.0; charset=utf-8" ;
2626
27+ /**
28+ * A property to be passed via the command line (-D...="true") to allow old-style pre OpenMetrics counter names,
29+ * in other words invalid counter names (not ending with the suffix "_total")
30+ *
31+ * @since 0.10.1
32+ */
33+ private final static String ALLOW_INVALID_COUNTER_NAMES = "io.prometheus.allowInvalidCounterNames" ;
34+
2735 /**
2836 * Return the content type that should be used for a given Accept HTTP header.
2937 *
@@ -64,6 +72,10 @@ public static void writeFormat(String contentType, Writer writer, Enumeration<Co
6472 * Write out the text version 0.0.4 of the given MetricFamilySamples.
6573 */
6674 public static void write004 (Writer writer , Enumeration <Collector .MetricFamilySamples > mfs ) throws IOException {
75+ // true in case ALLOW_INVALID_COUNTER_NAMES property is present and is true (ignoring case),
76+ // false in all other cases (including missing value)
77+ boolean allowInvalidCounterNames
78+ = Boolean .parseBoolean (System .getProperties ().getProperty (ALLOW_INVALID_COUNTER_NAMES ));
6779 Map <String , Collector .MetricFamilySamples > omFamilies = new TreeMap <String , Collector .MetricFamilySamples >();
6880 /* See http://prometheus.io/docs/instrumenting/exposition_formats/
6981 * for the output format specification. */
@@ -72,7 +84,7 @@ public static void write004(Writer writer, Enumeration<Collector.MetricFamilySam
7284 String name = metricFamilySamples .name ;
7385 writer .write ("# HELP " );
7486 writer .write (name );
75- if (metricFamilySamples .type == Collector .Type .COUNTER ) {
87+ if (metricFamilySamples .type == Collector .Type .COUNTER && ! allowInvalidCounterNames ) {
7688 writer .write ("_total" );
7789 }
7890 if (metricFamilySamples .type == Collector .Type .INFO ) {
@@ -84,7 +96,7 @@ public static void write004(Writer writer, Enumeration<Collector.MetricFamilySam
8496
8597 writer .write ("# TYPE " );
8698 writer .write (name );
87- if (metricFamilySamples .type == Collector .Type .COUNTER ) {
99+ if (metricFamilySamples .type == Collector .Type .COUNTER && ! allowInvalidCounterNames ) {
88100 writer .write ("_total" );
89101 }
90102 if (metricFamilySamples .type == Collector .Type .INFO ) {
0 commit comments