Micrometer with Prometheus Pushgateway - Add Basic Auth credentials

540 Views Asked by At

I have a Spring boot application with Prometheus Pushgateway using Micrometer, mainly based on this tutorial: https://luramarchanjo.tech/2020/01/05/spring-boot-2.2-and-prometheus-pushgateway-with-micrometer.html

pom.xml has following related dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_pushgateway</artifactId>
    <version>0.16.0</version>
</dependency>

And application.properties file has:

management.metrics.export.prometheus.pushgateway.enabled=false
management.metrics.export.prometheus.pushgateway.shutdown-operation=PUSH
management.metrics.export.prometheus.pushgateway.baseUrl=localhost:9091

It is working fine locally in Dev environment while connecting to Pushgateway without any authentication. In our CI environment, Prometheus Pushgateway has basic auth enabled. How do I configure Basic auth credentials in this Spring boot application?

1

There are 1 best solutions below

3
Jonatan Ivanov On

It seems neither Spring Boot's auto-configuration not Prometheus' PushGateway supports providing credentials directly.

I think you can do two things:

  1. I'm not sure if it is supported in the HTTP client but some does/did support passing credentials in the url that will be translated to headers: https://serverfault.com/a/371918
  2. You can also create your own PrometheusPushGatewayManager bean and call setConnectionFactory on the PushGateway instance with a factory that has the creds before you pass the PushGateway to the ctor of PrometheusPushGatewayManager.