in my Spring Boot application, I am using a metrics library which is a wrapper over: io.dropwizard.metrics. So I am getting all the API and system metrics from MetricsRegistry.
As a result, while I am hitting the /metrics actuator endpoint, I am able to see all these metrics.
Now, the problem is: all the metric names in the format a.b.c, which I would like to rename as: a_b_c.
But I could not find any way to achieve the same.
Could anyone please help with a few pointers? Thanks in advance.
Looking at the source code de
MetricRegistry.java, in theory you could iterate over the existing metrics in theMetricsRegistryand, for each metric, remove the metric from the registry and re-add it with the new name.However, note that directly manipulating the
MetricsRegistrylike this might introduce issues or limitations, especially with metrics that are automatically managed or updated by other components within your application.Before renaming, consider keeping a backup of original metric names and their references, for making sure you can revert changes if needed. If your application updates metrics in a highly concurrent environment, consider the implications of modifying the
MetricsRegistryat runtime. You may need to implement additional synchronization or use aMetricRegistryListenerto manage changes dynamically.Again, directly manipulating the
MetricsRegistrymight not be suitable for all applications, especially those that rely on automatic metric registration and updates.You could try indeed to extend
MetricRegistryand override the method responsible for registering metrics. That custom registry can then replace the default one in your Spring Boot application context.To use your custom registry, you need to make sure it is the one picked up by Spring Boot's autoconfiguration. That often involves defining it as a bean before the default configuration kicks in:
If you are dealing with a more complex scenario where metrics are being registered through a
MetricSetor via aMetricBuilder, consider wrapping these constructs to modify metric names at the point of creation.Then, when you register your
MetricSet:Yes, registering your custom metric set on the
ApplicationReadyEventshould work in a Spring Boot application: it makes sure your custom metric set is registered after all the autoconfiguration is complete but before the application starts serving traffic: your metrics are in place and ready to be used as soon as your application is up.CustomMetricSetis a wrapper around your originalMetricSetthat you have designed to modify the metric names according to your naming convention (e.g., replacing dots with underscores). When the application is fully ready, this listener triggers, wrapping and registering your metric set with the modified names.If you have extended
HandlerInterceptorAdapterto register API metrics and they are not appearing in yourCustomMetricRegistry, check if:the API metrics registration is happening before your custom metric registry is fully set up or recognized by the application context.
Make sure your custom metric registry is declared and initialized early. Using a
@Beanmethod in a@Configurationclass, as shown in your setup, is usually sufficient. Consider marking the configuration class with@Priorityor using@DependsOnif there are specific beans that must be initialized first (see "@Orderin Spring").your interceptor is referencing a different instance of
MetricRegistrythan the one you have customized and registered as a bean. That can happen in a Spring Boot application due to the way beans are auto-configured and instantiated.In your
HandlerInterceptorAdapterimplementation, explicitly autowire your custom metric registry to make sure it is using the correct instance. For example:If your custom registry extends
MetricRegistry, make sure you are injecting the right type. If necessary, qualify the autowiring with@Qualifierto make sure the correct bean is used.your custom metric registry is getting overridden or not used where you expect (if there are multiple beans of type
MetricRegistryor if auto-configuration creates a default one).Use Spring Boot's actuator endpoint
/beansor the Spring Boot Admin interface to inspect the beans in your application context. Look for multiple instances ofMetricRegistryand see which one is being injected into your interceptor. You might need to use@Primaryon your custom metric registry bean to make sure it is the one being picked up by default.If API metrics are still not showing up, consider registering them explicitly after your application is fully started, similar to how you handled system metrics with the
ApplicationReadyEvent. That makes sure the registration happens after all configurations and auto-configurations are complete.That way, your API metrics are registered with the correct
MetricRegistryinstance and at the right time in the application lifecycle.