001package io.prometheus.metrics.expositionformats; 002 003import io.prometheus.metrics.config.ExporterProperties; 004import io.prometheus.metrics.config.OpenMetrics2Properties; 005import io.prometheus.metrics.config.PrometheusProperties; 006import javax.annotation.Nullable; 007 008public class ExpositionFormats { 009 010 private final PrometheusProtobufWriter prometheusProtobufWriter; 011 private final PrometheusTextFormatWriter prometheusTextFormatWriter; 012 private final OpenMetricsTextFormatWriter openMetricsTextFormatWriter; 013 private final OpenMetrics2TextFormatWriter openMetrics2TextFormatWriter; 014 015 private ExpositionFormats( 016 PrometheusProtobufWriter prometheusProtobufWriter, 017 PrometheusTextFormatWriter prometheusTextFormatWriter, 018 OpenMetricsTextFormatWriter openMetricsTextFormatWriter, 019 OpenMetrics2TextFormatWriter openMetrics2TextFormatWriter) { 020 this.prometheusProtobufWriter = prometheusProtobufWriter; 021 this.prometheusTextFormatWriter = prometheusTextFormatWriter; 022 this.openMetricsTextFormatWriter = openMetricsTextFormatWriter; 023 this.openMetrics2TextFormatWriter = openMetrics2TextFormatWriter; 024 } 025 026 public static ExpositionFormats init() { 027 return init(PrometheusProperties.get()); 028 } 029 030 @SuppressWarnings("deprecation") 031 public static ExpositionFormats init(PrometheusProperties properties) { 032 ExporterProperties exporterProps = properties.getExporterProperties(); 033 OpenMetrics2Properties om2Props = properties.getOpenMetrics2Properties(); 034 035 return new ExpositionFormats( 036 new PrometheusProtobufWriter(), 037 PrometheusTextFormatWriter.builder() 038 .setIncludeCreatedTimestamps(exporterProps.getIncludeCreatedTimestamps()) 039 .setTimestampsInMs(exporterProps.getPrometheusTimestampsInMs()) 040 .build(), 041 OpenMetricsTextFormatWriter.builder() 042 .setCreatedTimestampsEnabled(exporterProps.getIncludeCreatedTimestamps()) 043 .setExemplarsOnAllMetricTypesEnabled(exporterProps.getExemplarsOnAllMetricTypes()) 044 .build(), 045 OpenMetrics2TextFormatWriter.builder() 046 .setOpenMetrics2Properties(om2Props) 047 .setCreatedTimestampsEnabled(exporterProps.getIncludeCreatedTimestamps()) 048 .setExemplarsOnAllMetricTypesEnabled(exporterProps.getExemplarsOnAllMetricTypes()) 049 .build()); 050 } 051 052 /** 053 * @deprecated Use {@link #init(PrometheusProperties)} instead. 054 */ 055 @Deprecated 056 @SuppressWarnings({"InlineMeSuggester"}) 057 public static ExpositionFormats init(ExporterProperties properties) { 058 return init(PrometheusProperties.builder().exporterProperties(properties).build()); 059 } 060 061 public ExpositionFormatWriter findWriter(@Nullable String acceptHeader) { 062 if (prometheusProtobufWriter.accepts(acceptHeader)) { 063 return prometheusProtobufWriter; 064 } 065 066 // Prefer OM2 over OM1 when any OM2 feature is enabled 067 if (isOpenMetrics2Enabled() && openMetrics2TextFormatWriter.accepts(acceptHeader)) { 068 return openMetrics2TextFormatWriter; 069 } 070 071 if (openMetricsTextFormatWriter.accepts(acceptHeader)) { 072 return openMetricsTextFormatWriter; 073 } 074 075 return prometheusTextFormatWriter; 076 } 077 078 private boolean isOpenMetrics2Enabled() { 079 return openMetrics2TextFormatWriter.getOpenMetrics2Properties().getEnabled(); 080 } 081 082 public PrometheusProtobufWriter getPrometheusProtobufWriter() { 083 return prometheusProtobufWriter; 084 } 085 086 public PrometheusTextFormatWriter getPrometheusTextFormatWriter() { 087 return prometheusTextFormatWriter; 088 } 089 090 public OpenMetricsTextFormatWriter getOpenMetricsTextFormatWriter() { 091 return openMetricsTextFormatWriter; 092 } 093 094 public OpenMetrics2TextFormatWriter getOpenMetrics2TextFormatWriter() { 095 return openMetrics2TextFormatWriter; 096 } 097}