prometheus/client_java

Client migration from 0.15.0 to 1.2.1

a-dudko opened this issue · 1 comments

Hi! We are migrating our project using io.prometheus:simpleclient:jar:0.15.0 to io.prometheus:prometheus-metrics-core:jar:1.2.1

Unfortunately there is a scenario which is not covered in the migration guides I was able to find.

Here is our scrapping controller

@WebEndpoint(id = "customPrometheus")
public class CustomPrometheusScrapeEndpoint {
    private final CollectorRegistry collectorRegistry;

    public BusinessPrometheusScrapeEndpoint(CollectorRegistry collectorRegistry) {
        this.collectorRegistry = collectorRegistry;
    }

    @ReadOperation(produces = {"text/plain; version=0.0.4; charset=utf-8"})
    public String scrape() throws IOException {
        Writer writer = new StringWriter();
        TextFormat.write004(writer, this.collectorRegistry.metricFamilySamples());
        return writer.toString();
    }
}

I see that both TextFormat and CollectorRegistry are already removed and there is no indication what the code above should be replaced with. Could you please give a hint here?

fstab commented

Hi @a-dudko, you could try something like this:

PrometheusRegistry registry = PrometheusRegistry.defaultRegistry;

ByteArrayOutputStream out = new ByteArrayOutputStream();
ExpositionFormatWriter textFormatWriter = new PrometheusTextFormatWriter(false);
textFormatWriter.write(out, registry.scrape());
return out.toString("UTF-8");

Does that work for you?