Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client migration from 0.15.0 to 1.2.1 #1207

Open
a-dudko opened this issue Nov 18, 2024 · 1 comment
Open

Client migration from 0.15.0 to 1.2.1 #1207

a-dudko opened this issue Nov 18, 2024 · 1 comment

Comments

@a-dudko
Copy link

a-dudko commented Nov 18, 2024

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
Copy link
Member

fstab commented Nov 19, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants