spring-cloud / spring-cloud/spring-cloud-commons

Enhancement: RefreshScope should rebind MeterRegistry metrics

Open
#465 13 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

waiting for votes
Dominant language
Java
Stars
751
Forks
744
Avg merge
1d 14h
Merged PRs (30d)
9

Description

When using the spring cloud config server and changing a property value registered as Metric in a MeterRegistry, the value is not refreshed automatically.

As a first use case, suppose you want to change the value of a Hikari connections pool size:
from spring.datasource.hikari.maximumPoolSize=5 to spring.datasource.hikari.maximumPoolSize=10
(and adding spring.cloud.refresh.extra-refreshable=javax.sql.DataSource in my personal case),
the new created datasource won't appear as a new metric in the MeterRegistry who keeps the old one.
To circumvent this issue, I'm currently using a BeanPostProcessor
to remove the old datasource metrics and to rebind the new datasource to the registry.

/*
When changing a datasource value at runtime, the Prometheus Metrics registry does not pick up the changes automatically.
We have to remove the old datasource values and rebind the new datasource with the registry.
*/
public class DataSourceMetricsRefresher implements BeanPostProcessor {

  private final MeterRegistry registry;

  public DataSourceMetricsRefresher(MeterRegistry registry) {
    this.registry = registry;
  }

  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (DataSource.class.isAssignableFrom(bean.getClass())) {
      HikariDataSource hikariDataSource = DataSourceUnwrapper.unwrap((DataSource) bean, HikariDataSource.class);
      List<Meter> meters = this.registry.getMeters();
      meters.stream().filter(meter -> meter.getId().getName().startsWith("hikaricp")).forEach(registry::remove);
      hikariDataSource.setMetricsTrackerFactory(new MicrometerMetricsTrackerFactory(this.registry));
    }
    return bean;
  }
}

As a second use case, suppose you want to enable/disable hibernate stats on the fly:
spring.jpa.properties.hibernate.generate_statistics=false
Here again, you have to do the same dance to rebind the HibernateMetrics to the registry.

This is not ideal and should be provided out of the box like it's done for logging in the LoggingRebinder.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the requested behavior with spring-cloud-context/src/main/java/org/springframework/cloud/logging/LoggingRebinder.java. Review the MeterRegistry usage in the issue, the referenced Micrometer HibernateMetrics entry point, and the BeanPostProcessor example. Done means refreshed datasource and Hibernate metrics are rebound automatically when configuration changes.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, prometheus, spring, spring-boot
Domain
backend, observability
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.