001package io.prometheus.metrics.expositionformats; 002 003import io.prometheus.metrics.config.ExporterProperties; 004import io.prometheus.metrics.config.PrometheusProperties; 005import javax.annotation.Nullable; 006 007public class ExpositionFormats { 008 009 private final PrometheusProtobufWriter prometheusProtobufWriter; 010 private final PrometheusTextFormatWriter prometheusTextFormatWriter; 011 private final OpenMetricsTextFormatWriter openMetricsTextFormatWriter; 012 013 private ExpositionFormats( 014 PrometheusProtobufWriter prometheusProtobufWriter, 015 PrometheusTextFormatWriter prometheusTextFormatWriter, 016 OpenMetricsTextFormatWriter openMetricsTextFormatWriter) { 017 this.prometheusProtobufWriter = prometheusProtobufWriter; 018 this.prometheusTextFormatWriter = prometheusTextFormatWriter; 019 this.openMetricsTextFormatWriter = openMetricsTextFormatWriter; 020 } 021 022 public static ExpositionFormats init() { 023 return init(PrometheusProperties.get().getExporterProperties()); 024 } 025 026 @SuppressWarnings("deprecation") 027 public static ExpositionFormats init(ExporterProperties properties) { 028 return new ExpositionFormats( 029 new PrometheusProtobufWriter(), 030 PrometheusTextFormatWriter.builder() 031 .setIncludeCreatedTimestamps(properties.getIncludeCreatedTimestamps()) 032 .setTimestampsInMs(properties.getPrometheusTimestampsInMs()) 033 .build(), 034 OpenMetricsTextFormatWriter.builder() 035 .setCreatedTimestampsEnabled(properties.getIncludeCreatedTimestamps()) 036 .setExemplarsOnAllMetricTypesEnabled(properties.getExemplarsOnAllMetricTypes()) 037 .build()); 038 } 039 040 public ExpositionFormatWriter findWriter(@Nullable String acceptHeader) { 041 if (prometheusProtobufWriter.accepts(acceptHeader)) { 042 return prometheusProtobufWriter; 043 } 044 if (openMetricsTextFormatWriter.accepts(acceptHeader)) { 045 return openMetricsTextFormatWriter; 046 } 047 return prometheusTextFormatWriter; 048 } 049 050 public PrometheusProtobufWriter getPrometheusProtobufWriter() { 051 return prometheusProtobufWriter; 052 } 053 054 public PrometheusTextFormatWriter getPrometheusTextFormatWriter() { 055 return prometheusTextFormatWriter; 056 } 057 058 public OpenMetricsTextFormatWriter getOpenMetricsTextFormatWriter() { 059 return openMetricsTextFormatWriter; 060 } 061}