prometheus / client_java

Prometheus instrumentation library for JVM applications

Home Page:http://prometheus.github.io/client_java/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Accidental breaking API change

fstab opened this issue · comments

With #873 we accidentally introduced a breaking API change. Before the change, you could pass a metric name filter as a lambda expression:

registry.scrape(name -> name.equals("my_metric"));

Now, this fails:

reference to scrape is ambiguous
[ERROR]   both method scrape(io.prometheus.metrics.model.registry.PrometheusScrapeRequest) in io.prometheus.metrics.model.registry.PrometheusRegistry and method scrape(java.util.function.Predicate<java.lang.String>) in io.prometheus.metrics.model.registry.PrometheusRegistry match

Same for (Multi)Collector.collect(name -> name.equals("...")).

@ganzuoni what do you think is a good way to fix this? I know your initial PR had a method getRequestPath(), which we removed because it wasn't needed. I guess we could add it again to the PrometheusScrapeRequest interface? What do you think?

I guess, it will not solve the issue that is caused by method overloading resolution with the nastiest (IMHO) "innovation" ever introduced in Java ecosystem: lambda expression..
You will get the same error if you have a statement like
registry.scrape(null)

Compiler cannot select the right method in this case because null is null and there's no way to distinguish a null Predicate from a null PrometheusScrapeRequest
If you give a hint to the compiler it works

registry.scrape((Predicate<String>) null)

in the same way this statement works as expected

registry.scrape((Predicate<String>) name -> name.equals("my_metric"));

Other solutions require different method names e.g.,
filteredScrape(Predicate<String>)
or
advancedScrape(PrometheusScrapeRequest)