001package io.prometheus.metrics.expositionformats; 002 003import io.prometheus.metrics.config.EscapingScheme; 004import io.prometheus.metrics.model.snapshots.MetricSnapshots; 005import java.io.ByteArrayOutputStream; 006import java.io.IOException; 007import java.io.OutputStream; 008import javax.annotation.Nullable; 009 010public interface ExpositionFormatWriter { 011 boolean accepts(@Nullable String acceptHeader); 012 013 /** Writes the given metric snapshots to the output stream using the specified escaping scheme. */ 014 void write(OutputStream out, MetricSnapshots metricSnapshots, EscapingScheme escapingScheme) 015 throws IOException; 016 017 /** Writes the given metric snapshots to the output stream using the default escaping scheme. */ 018 default void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOException { 019 write(out, metricSnapshots, EscapingScheme.DEFAULT); 020 } 021 022 /** Converts the metric snapshots to a debug string using the specified escaping scheme. */ 023 // toString with Charset is only available in Java 10+, but we want to support Java 8 024 @SuppressWarnings("JdkObsolete") 025 default String toDebugString(MetricSnapshots metricSnapshots, EscapingScheme escapingScheme) { 026 ByteArrayOutputStream out = new ByteArrayOutputStream(); 027 try { 028 write(out, metricSnapshots, escapingScheme); 029 return out.toString("UTF-8"); 030 } catch (IOException e) { 031 throw new RuntimeException(e); 032 } 033 } 034 035 /** Converts the metric snapshots to a debug string using the default escaping scheme. */ 036 default String toDebugString(MetricSnapshots metricSnapshots) { 037 return toDebugString(metricSnapshots, EscapingScheme.DEFAULT); 038 } 039 040 String getContentType(); 041 042 /** 043 * Returns true if the writer is available. If false, the writer will throw an exception if used. 044 */ 045 default boolean isAvailable() { 046 return true; 047 } 048}