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 default String toDebugString(MetricSnapshots metricSnapshots, EscapingScheme escapingScheme) { 024 ByteArrayOutputStream out = new ByteArrayOutputStream(); 025 try { 026 write(out, metricSnapshots, escapingScheme); 027 return out.toString("UTF-8"); 028 } catch (IOException e) { 029 throw new RuntimeException(e); 030 } 031 } 032 033 /** Converts the metric snapshots to a debug string using the default escaping scheme. */ 034 default String toDebugString(MetricSnapshots metricSnapshots) { 035 return toDebugString(metricSnapshots, EscapingScheme.DEFAULT); 036 } 037 038 String getContentType(); 039 040 /** 041 * Returns true if the writer is available. If false, the writer will throw an exception if used. 042 */ 043 default boolean isAvailable() { 044 return true; 045 } 046}