client_java icon indicating copy to clipboard operation
client_java copied to clipboard

Equivalent usage of Simpleclient in micrometer-registry-prometheus `1.13.x`

Open siddhant16 opened this issue 11 months ago • 4 comments
trafficstars

Hi Team! We are migrating our project from micrometer-registry-prometheus:1.12.8 to version 1.13.8 . I see that the io.prometheus:simpleclient:jar:0.16.0 has been deprecated.

We have few scenarios which I do not see in the migration guide.

Here are few scenarios,

  1. What is the equivalent of io.prometheus.client.Collector.MetricFamilySamples and the MetricFamilySamples.Sample
   PrometheusMeterRegistry instance = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
   MetricFamilySamples samples =  instance.getPrometheusRegistry().metricFamilySamples()
 if (sample == null || sample.name == null || sample.labelNames == null || sample.labelValues == null) {
      LOGGER.error("The provided Sample object is either null or one of its fields is null: {}", sample);
      throw new IllegalArgumentException("The provided Sample object is either null or one of its fields is null: " + sample);
    }
  1. Is the import io.prometheus.metrics.model.registry.Collector a replacement of io.prometheus.client.Collector, however, I couldn't find the below in the new Collector.
Enumeration<Collector.MetricFamilySamples> metricFamilySamples;
  1. Similarly how we can replace the io.prometheus.client.exporter.common.TextFormat?

Could you please help me with the code changes. Also, can you share any additional docs available that we can follow for migrating from the usage of simpleclient.

siddhant16 avatar Dec 10 '24 00:12 siddhant16

Hi @siddhant16, could you provide more context on what you would like to achieve?

MetricFamilySamples samples =  instance.getPrometheusRegistry().metricFamilySamples()

Sounds like you are manually collecting metrics in code. I'm wondering why you do that rather than just using Micrometer to expose a Prometheus endpoint?

fstab avatar Dec 10 '24 08:12 fstab

Thank you for the response. So, the MetricFamilySamples that are collected in the above code is to be used for custom metric generation and filtering. We filter the provided MetricsFamilySamples with the endpoint processor and also generate additional metrics for provided PrometheusMetrics using provided MetricFamilySample.

Using the MetricFamilySamples to apply filtering,

...
...
   TextFormat.writeFormat(TextFormat.CONTENT_TYPE_004, writer,
       applySourceFiltering(configToProcessor, metricsFamilySamples));

siddhant16 avatar Dec 10 '24 16:12 siddhant16

Hi @fstab In addition to above I also wanted to know how we can get the names based on the type of metric, the MetricFamilySamples use to have getNames() like below,

    public String[] getNames() {
      switch (this.type) {
        case COUNTER:
          return new String[]{this.name + "_total", this.name + "_created", this.name};
        case SUMMARY:
          return new String[]{this.name + "_count", this.name + "_sum", this.name + "_created", this.name};
        case HISTOGRAM:
          return new String[]{this.name + "_count", this.name + "_sum", this.name + "_bucket", this.name + "_created", this.name};
        case GAUGE_HISTOGRAM:
          return new String[]{this.name + "_gcount", this.name + "_gsum", this.name + "_bucket", this.name};
        case INFO:
          return new String[]{this.name + "_info", this.name};
        default:
          return new String[]{this.name};
      }
    }

siddhant16 avatar Dec 12 '24 18:12 siddhant16

@fstab Do you know if there is a plan to migrate from the deprecated simpleclient to 1.X (https://prometheus.github.io/client_java/)? Thank you.

agavrilov76 avatar Jan 02 '25 14:01 agavrilov76