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