// Path: elasticsearch-plugin/src/main/java/pl/suchenia/elasticsearchPrometheusMetrics/writer/PrometheusFormatWriter.java
// public class PrometheusFormatWriter {
//     private final StringWriter writer = new StringWriter();
//     private final Map<String, String> globalLabels;
// 
//     public PrometheusFormatWriter(Map<String, String> globalLabels) {
//         this.globalLabels = globalLabels;
//     }
// 
//     public SingleMetricsDefinitionBuilder addGauge(String name) {
//         return new SingleMetricsDefinitionBuilder(MetricType.GAUGE, writer, name, globalLabels);
//     }
// 
//     public SingleMetricsDefinitionBuilder addCounter(String name) {
//         return new SingleMetricsDefinitionBuilder(MetricType.COUNTER, writer, name, globalLabels);
//     }
// 
//     public SummaryMetricsDefinitionBuilder addSummary(String name) {
//         return new SummaryMetricsDefinitionBuilder(writer, name, globalLabels);
//     }
// 
//     public String toString() {
//         return writer.toString();
//     }
// }


import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.rest.RestRequest;
import pl.suchenia.elasticsearchPrometheusMetrics.writer.PrometheusFormatWriter;
import java.util.concurrent.CompletableFuture;
import static org.elasticsearch.rest.RestStatus.INTERNAL_SERVER_ERROR;
import static org.elasticsearch.rest.RestStatus.OK;
package pl.suchenia.elasticsearchPrometheusMetrics;




@FunctionalInterface
public interface StringBufferedRestHandler extends RestHandler {
    @Override
    default void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
        generateResponse(channel, client)
                .thenAccept((writer) ->channel.sendResponse(new BytesRestResponse(OK,
                        "text/plain; version=0.0.4; charset=utf-8",
                        writer.toString())))
                .exceptionally((e) -> {
                        channel.sendResponse(new BytesRestResponse(INTERNAL_SERVER_ERROR,
                            "text/plain; charset=utf-8",
                            e.toString()));
                        return null;
                });
    }

// Path: elasticsearch-plugin/src/main/java/pl/suchenia/elasticsearchPrometheusMetrics/writer/PrometheusFormatWriter.java
// public class PrometheusFormatWriter {
//     private final StringWriter writer = new StringWriter();
//     private final Map<String, String> globalLabels;
// 
//     public PrometheusFormatWriter(Map<String, String> globalLabels) {
//         this.globalLabels = globalLabels;
//     }
// 
//     public SingleMetricsDefinitionBuilder addGauge(String name) {
//         return new SingleMetricsDefinitionBuilder(MetricType.GAUGE, writer, name, globalLabels);
//     }
// 
//     public SingleMetricsDefinitionBuilder addCounter(String name) {
//         return new SingleMetricsDefinitionBuilder(MetricType.COUNTER, writer, name, globalLabels);
//     }
// 
//     public SummaryMetricsDefinitionBuilder addSummary(String name) {
//         return new SummaryMetricsDefinitionBuilder(writer, name, globalLabels);
//     }
// 
//     public String toString() {
//         return writer.toString();
//     }
// }


// Path: elasticsearch-plugin/src/main/java/pl/suchenia/elasticsearchPrometheusMetrics/StringBufferedRestHandler.java
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.rest.RestRequest;
import pl.suchenia.elasticsearchPrometheusMetrics.writer.PrometheusFormatWriter;
import java.util.concurrent.CompletableFuture;
import static org.elasticsearch.rest.RestStatus.INTERNAL_SERVER_ERROR;
import static org.elasticsearch.rest.RestStatus.OK;

package pl.suchenia.elasticsearchPrometheusMetrics;




@FunctionalInterface
public interface StringBufferedRestHandler extends RestHandler {
    @Override
    default void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
        generateResponse(channel, client)
                .thenAccept((writer) ->channel.sendResponse(new BytesRestResponse(OK,
                        "text/plain; version=0.0.4; charset=utf-8",
                        writer.toString())))
                .exceptionally((e) -> {
                        channel.sendResponse(new BytesRestResponse(INTERNAL_SERVER_ERROR,
                            "text/plain; charset=utf-8",
                            e.toString()));
                        return null;
                });
    }

    CompletableFuture<PrometheusFormatWriter> generateResponse(RestChannel channel, NodeClient client);