11package io .prometheus .metrics .expositionformats ;
22
33import io .prometheus .metrics .config .ExporterProperties ;
4+ import io .prometheus .metrics .config .OpenMetrics2Properties ;
45import io .prometheus .metrics .config .PrometheusProperties ;
56import javax .annotation .Nullable ;
67
@@ -9,44 +10,79 @@ public class ExpositionFormats {
910 private final PrometheusProtobufWriter prometheusProtobufWriter ;
1011 private final PrometheusTextFormatWriter prometheusTextFormatWriter ;
1112 private final OpenMetricsTextFormatWriter openMetricsTextFormatWriter ;
13+ private final OpenMetrics2TextFormatWriter openMetrics2TextFormatWriter ;
1214
1315 private ExpositionFormats (
1416 PrometheusProtobufWriter prometheusProtobufWriter ,
1517 PrometheusTextFormatWriter prometheusTextFormatWriter ,
16- OpenMetricsTextFormatWriter openMetricsTextFormatWriter ) {
18+ OpenMetricsTextFormatWriter openMetricsTextFormatWriter ,
19+ OpenMetrics2TextFormatWriter openMetrics2TextFormatWriter ) {
1720 this .prometheusProtobufWriter = prometheusProtobufWriter ;
1821 this .prometheusTextFormatWriter = prometheusTextFormatWriter ;
1922 this .openMetricsTextFormatWriter = openMetricsTextFormatWriter ;
23+ this .openMetrics2TextFormatWriter = openMetrics2TextFormatWriter ;
2024 }
2125
2226 public static ExpositionFormats init () {
23- return init (PrometheusProperties .get (). getExporterProperties () );
27+ return init (PrometheusProperties .get ());
2428 }
2529
2630 @ SuppressWarnings ("deprecation" )
27- public static ExpositionFormats init (ExporterProperties properties ) {
31+ public static ExpositionFormats init (PrometheusProperties properties ) {
32+ ExporterProperties exporterProps = properties .getExporterProperties ();
33+ OpenMetrics2Properties om2Props = properties .getOpenMetrics2Properties ();
34+
2835 return new ExpositionFormats (
2936 new PrometheusProtobufWriter (),
3037 PrometheusTextFormatWriter .builder ()
31- .setIncludeCreatedTimestamps (properties .getIncludeCreatedTimestamps ())
32- .setTimestampsInMs (properties .getPrometheusTimestampsInMs ())
38+ .setIncludeCreatedTimestamps (exporterProps .getIncludeCreatedTimestamps ())
39+ .setTimestampsInMs (exporterProps .getPrometheusTimestampsInMs ())
3340 .build (),
3441 OpenMetricsTextFormatWriter .builder ()
35- .setCreatedTimestampsEnabled (properties .getIncludeCreatedTimestamps ())
36- .setExemplarsOnAllMetricTypesEnabled (properties .getExemplarsOnAllMetricTypes ())
42+ .setCreatedTimestampsEnabled (exporterProps .getIncludeCreatedTimestamps ())
43+ .setExemplarsOnAllMetricTypesEnabled (exporterProps .getExemplarsOnAllMetricTypes ())
44+ .build (),
45+ OpenMetrics2TextFormatWriter .builder ()
46+ .setOpenMetrics2Properties (om2Props )
47+ .setCreatedTimestampsEnabled (exporterProps .getIncludeCreatedTimestamps ())
48+ .setExemplarsOnAllMetricTypesEnabled (exporterProps .getExemplarsOnAllMetricTypes ())
3749 .build ());
3850 }
3951
52+ /**
53+ * @deprecated Use {@link #init(PrometheusProperties)} instead.
54+ */
55+ @ Deprecated
56+ @ SuppressWarnings ({"InlineMeSuggester" })
57+ public static ExpositionFormats init (ExporterProperties properties ) {
58+ return init (PrometheusProperties .builder ().exporterProperties (properties ).build ());
59+ }
60+
4061 public ExpositionFormatWriter findWriter (@ Nullable String acceptHeader ) {
4162 if (prometheusProtobufWriter .accepts (acceptHeader )) {
4263 return prometheusProtobufWriter ;
4364 }
65+
66+ // Prefer OM2 over OM1 when any OM2 feature is enabled
67+ if (isOpenMetrics2Enabled () && openMetrics2TextFormatWriter .accepts (acceptHeader )) {
68+ return openMetrics2TextFormatWriter ;
69+ }
70+
4471 if (openMetricsTextFormatWriter .accepts (acceptHeader )) {
4572 return openMetricsTextFormatWriter ;
4673 }
74+
4775 return prometheusTextFormatWriter ;
4876 }
4977
78+ private boolean isOpenMetrics2Enabled () {
79+ OpenMetrics2Properties props = openMetrics2TextFormatWriter .getOpenMetrics2Properties ();
80+ return props .getContentNegotiation ()
81+ || props .getCompositeValues ()
82+ || props .getExemplarCompliance ()
83+ || props .getNativeHistograms ();
84+ }
85+
5086 public PrometheusProtobufWriter getPrometheusProtobufWriter () {
5187 return prometheusProtobufWriter ;
5288 }
@@ -58,4 +94,8 @@ public PrometheusTextFormatWriter getPrometheusTextFormatWriter() {
5894 public OpenMetricsTextFormatWriter getOpenMetricsTextFormatWriter () {
5995 return openMetricsTextFormatWriter ;
6096 }
97+
98+ public OpenMetrics2TextFormatWriter getOpenMetrics2TextFormatWriter () {
99+ return openMetrics2TextFormatWriter ;
100+ }
61101}
0 commit comments