How to push untyped metrics with multiple labels(key-value) using JAVA Prometheus Pushgateway client?

2.3k Views Asked by At

I want to push metrics using java PushGateway client.

Sample PushGateway URL pushgateway.com:9091/metrics/job/job_name

Sample Metrics metrics_name{instance="i1", label1="l1", label2="l2", label3="l3", label4="l4"} value

Can any one please provide me a code snippet to push the above metrics into Pushgateway using Prometheus Java Simpleclient Pushgateway?

Note We have 20K ephemeral instances running of the application hence decided to use Pushgateway.

1

There are 1 best solutions below

0
On

From the official documentation of PushGateway:

  • To use the grouping key job="directory_cleaner",path="/var/tmp", the following path will not work:
/metrics/job/directory_cleaner/path//var/tmp

Instead, use the base64 URL-safe encoding for the label value and mark it by suffixing the label name with @base64:

/metrics/job/directory_cleaner/path@base64/L3Zhci90bXA

Here is an example of generating url from multiple key-value pair stored in Map<String, String> groupingKey:

    String url = gatewayBaseURL;
    if (job.contains("/")) {
      url += "job@base64/" + base64url(job);
    } else {
      url += "job/" + URLEncoder.encode(job, "UTF-8");
    }

    if (groupingKey != null) {
      for (Map.Entry<String, String> entry: groupingKey.entrySet()) {
        if (entry.getValue().contains("/")) {
          url += "/" + entry.getKey() + "@base64/" + base64url(entry.getValue());
        } else {
          url += "/" + entry.getKey() + "/" + URLEncoder.encode(entry.getValue(), "UTF-8");
        }
      }
    }
    HttpURLConnection connection = connectionFactory.create(url);

Github link