Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public PrometheusScrapeHandler(PrometheusProperties config) {
}

public PrometheusScrapeHandler(PrometheusProperties config, PrometheusRegistry registry) {
this.expositionFormats = ExpositionFormats.init(config.getExporterProperties());
this.expositionFormats = ExpositionFormats.init(config);
this.preferUncompressedResponse =
config.getExporterHttpServerProperties().isPreferUncompressedResponse();
this.registry = registry;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.prometheus.metrics.expositionformats;

import io.prometheus.metrics.config.ExporterProperties;
import io.prometheus.metrics.config.OpenMetrics2Properties;
import io.prometheus.metrics.config.PrometheusProperties;
import javax.annotation.Nullable;

Expand All @@ -9,44 +10,79 @@ public class ExpositionFormats {
private final PrometheusProtobufWriter prometheusProtobufWriter;
private final PrometheusTextFormatWriter prometheusTextFormatWriter;
private final OpenMetricsTextFormatWriter openMetricsTextFormatWriter;
private final OpenMetrics2TextFormatWriter openMetrics2TextFormatWriter;

private ExpositionFormats(
PrometheusProtobufWriter prometheusProtobufWriter,
PrometheusTextFormatWriter prometheusTextFormatWriter,
OpenMetricsTextFormatWriter openMetricsTextFormatWriter) {
OpenMetricsTextFormatWriter openMetricsTextFormatWriter,
OpenMetrics2TextFormatWriter openMetrics2TextFormatWriter) {
this.prometheusProtobufWriter = prometheusProtobufWriter;
this.prometheusTextFormatWriter = prometheusTextFormatWriter;
this.openMetricsTextFormatWriter = openMetricsTextFormatWriter;
this.openMetrics2TextFormatWriter = openMetrics2TextFormatWriter;
}

public static ExpositionFormats init() {
return init(PrometheusProperties.get().getExporterProperties());
return init(PrometheusProperties.get());
}

@SuppressWarnings("deprecation")
public static ExpositionFormats init(ExporterProperties properties) {
public static ExpositionFormats init(PrometheusProperties properties) {
ExporterProperties exporterProps = properties.getExporterProperties();
OpenMetrics2Properties om2Props = properties.getOpenMetrics2Properties();

return new ExpositionFormats(
new PrometheusProtobufWriter(),
PrometheusTextFormatWriter.builder()
.setIncludeCreatedTimestamps(properties.getIncludeCreatedTimestamps())
.setTimestampsInMs(properties.getPrometheusTimestampsInMs())
.setIncludeCreatedTimestamps(exporterProps.getIncludeCreatedTimestamps())
.setTimestampsInMs(exporterProps.getPrometheusTimestampsInMs())
.build(),
OpenMetricsTextFormatWriter.builder()
.setCreatedTimestampsEnabled(properties.getIncludeCreatedTimestamps())
.setExemplarsOnAllMetricTypesEnabled(properties.getExemplarsOnAllMetricTypes())
.setCreatedTimestampsEnabled(exporterProps.getIncludeCreatedTimestamps())
.setExemplarsOnAllMetricTypesEnabled(exporterProps.getExemplarsOnAllMetricTypes())
.build(),
OpenMetrics2TextFormatWriter.builder()
.setOpenMetrics2Properties(om2Props)
.setCreatedTimestampsEnabled(exporterProps.getIncludeCreatedTimestamps())
.setExemplarsOnAllMetricTypesEnabled(exporterProps.getExemplarsOnAllMetricTypes())
.build());
}

/**
* @deprecated Use {@link #init(PrometheusProperties)} instead.
*/
@Deprecated
@SuppressWarnings({"InlineMeSuggester"})
public static ExpositionFormats init(ExporterProperties properties) {
return init(PrometheusProperties.builder().exporterProperties(properties).build());
}

public ExpositionFormatWriter findWriter(@Nullable String acceptHeader) {
if (prometheusProtobufWriter.accepts(acceptHeader)) {
return prometheusProtobufWriter;
}

// Prefer OM2 over OM1 when any OM2 feature is enabled
if (isOpenMetrics2Enabled() && openMetrics2TextFormatWriter.accepts(acceptHeader)) {
return openMetrics2TextFormatWriter;
}

if (openMetricsTextFormatWriter.accepts(acceptHeader)) {
return openMetricsTextFormatWriter;
}

return prometheusTextFormatWriter;
}

private boolean isOpenMetrics2Enabled() {
OpenMetrics2Properties props = openMetrics2TextFormatWriter.getOpenMetrics2Properties();
return props.getContentNegotiation()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: need to add another one if you just want to have the features from #1942

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #1953 for that

|| props.getCompositeValues()
|| props.getExemplarCompliance()
|| props.getNativeHistograms();
}

public PrometheusProtobufWriter getPrometheusProtobufWriter() {
return prometheusProtobufWriter;
}
Expand All @@ -58,4 +94,8 @@ public PrometheusTextFormatWriter getPrometheusTextFormatWriter() {
public OpenMetricsTextFormatWriter getOpenMetricsTextFormatWriter() {
return openMetricsTextFormatWriter;
}

public OpenMetrics2TextFormatWriter getOpenMetrics2TextFormatWriter() {
return openMetrics2TextFormatWriter;
}
}
Loading
Loading