Effortlessly disable metrics in Spring Boot 3. Learn simple steps to control and exclude metrics for a streamlined application.
Introduction
Spring boot 3 provides a powerful and flexible way to collect and expose various metrics for your application, such as JVM, CPU, memory, HTTP, cache, etc. However, sometimes you may want to disable some or all of the metrics including the custom metrics that are defined in your application. In this blog post, we will learn how to do that using the application properties.
Decoding Spring Boot Actuator Metrics
In Spring Boot, metrics work as performance indicators, giving crucial insights into your app’s operation. They cover CPU usage, memory, HTTP requests, and more. Spring Boot 3 makes monitoring easy, letting devs track default metrics and make custom ones effortlessly. These metrics are essential, helping find improvements, optimize resources, and keep your app performing at its peak.
Spring boot 3 uses Micrometer, an application metrics facade, to support various monitoring systems such as Prometheus, Datadog, Graphite, etc. Micrometer allows you to create and register custom metrics using its fluent API. For example, you can create a counter to measure the number of requests to a certain endpoint, or a timer to measure the latency of a method call. You can also add tags to your metrics to provide more dimensions and context.
When and Why Exclude Metrics from Actuator Metrics Endpoint
In certain scenarios, excluding specific metrics from the actuator metrics endpoint becomes advantageous. For instance:
- Metrics that are not relevant or useful for your application or monitoring system. For example, you may not need the metrics for the cache system if you are not using any caching in your application.
- Metrics that are redundant or overlapping with other metrics. For example, you may not need the metrics for the HTTP server requests if you are already using a custom metric to measure the same aspect of your application.
- Metrics that are sensitive or confidential and may pose a security or privacy risk. For example, you may not need the metrics for the JPA system if they contain information about your database queries or entities that may expose your data or business logic.
How to Disable All Metrics?
In some rare cases, you may want to disable all metrics, including your custom metrics, from being exposed or exported. For example, you may want to reduce the resource consumption or the network traffic of your application or monitoring system, or you may want to avoid any potential conflicts or errors caused by the metrics. In this case, you have two options:
1. Use the management.metrics.enable.all
property to false
management.metrics.enable.all=false
application.propertiesThis will disable all metrics, including your custom metrics, from being exposed or exported.
After adding this configuration, the metrics endpoint will be accessible but it will return an empty array meaning no metrics information will be available in this case.
2. Exclude MetricsAutoConfiguration.class
by specifying it inside the @SpringBootApplication
annotation.
@SpringBootApplication(exclude = MetricsAutoConfiguration.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Application.javaThis will disable the MetricsAutoConfiguration
class, which is the main auto-configuration class for metrics in Spring Boot 3.
It is important to note that after excluding the MetricsAutoConfiguration class, attempting to access the Actuator metrics endpoint will result in a 404 error, as the metrics will no longer be accessible.
Disable Default and Custom Metrics
How to Disable Default Metrics?
Spring Boot 3 also provides a number of default metrics for common aspects of your application, such as JVM, CPU, memory, HTTP, cache, etc. These metrics are configured via various auto-configuration classes that are enabled by default.
To disable the default metrics, you have two options:
- Disable all metrics and enable only specific metrics
- Disable only selected metrics and keep the rest
Option 1: Disable all metrics and enable only specific metrics
To disable all metrics and enable only specific metrics, you need to set the management.metrics.enable.all
property to false
and the management.metrics.enable.<metric>
property to true
in your application.properties
file. For example:
management.metrics.enable.all=false
management.metrics.enable.jvm=true
application.properties This will disable all the default metrics and only expose metrics that start with jvm
.
Option 2: Disable only selected metrics and keep the rest
To disable only selected metrics and keep the rest, you need to set the management.metrics.enable.<metric>
property to false
for each metric that you want to disable in your application.properties
file. The <metric>
is the name of the metric that you want to disable. For example, to disable the JVM, CPU, and memory metrics, you can use the following properties:
management.metrics.enable.jvm=false
management.metrics.enable.cpu=false
management.metrics.enable.memory=false
application.propertiesThis will disable the JVM, CPU, and memory metrics and keep the rest of the default metrics.
How to Disable Custom Metrics?
Sometimes, you may want to exclude some of your custom metrics from being exposed or exported. For example, you may have some metrics that are only relevant for development or testing purposes or some metrics that are too sensitive or confidential to be exposed. In this case, you can use the management.metrics.enable.<metric>
property to false
for each custom metric that you want to exclude. For example, to exclude the myapp.secret
metric, you can use the following property:
management.metrics.enable.myapp.secret=false
application.propertiesThis will exclude the myapp.secret
metric from being exposed or exported.
Disable Metrics Using Patterns
Sometimes, you may want to disable a group of metrics that share a common prefix in their names. For example, you may want to disable all the metrics that start with jvm
, such as jvm.buffer.count
, jvm.gc.live.data.size
, jvm.threads.daemon
, etc. In this case, you can use the management.metrics.enable.<pattern>
property to false
for the pattern that you want to disable. The <pattern>
is the prefix of the metric names that you want to disable. For example, to disable all the metrics that start with jvm
, you can use the following property:
management.metrics.enable.jvm=false
application.propertiesThis will disable all the metrics that start with jvm
. You can use this property for any pattern that matches the metric names. For example, to disable all the metrics that start with executor.pool
, such as executor.pool.core
, executor.pool.max
, executor.pool.size
, etc., you can use the following property:
management.metrics.enable.executor.pool=false
application.propertiesThis will disable all the metrics that start with executor.pool
.
Things to Consider
Before you disable or exclude any metrics, you should consider the following points:
- Functional Impact: Disabling or excluding metrics may affect the functionality or performance of your application or monitoring system. For example, some metrics may be used by health indicators, alerts, dashboards, or other features that rely on them. Make sure you understand the implications of disabling or excluding any metrics before you do so.
- Retained Metrics Alert: Disabling or excluding metrics may not completely remove them from your application or monitoring system. For example, some metrics may still be registered in the
MeterRegistry
or stored in the monitoring system’s database, even if they are not exposed or exported. This may consume some resources or cause some errors or warnings. To completely remove any metrics, you may need to use other methods, such as customizing theMeterRegistry
or deleting the metrics from the monitoring system. - Data Privacy Caution: Disabling or excluding metrics may not be enough to protect your data or privacy. For example, some metrics may contain sensitive or confidential information, such as user names, passwords, tokens, etc. Even if you disable or exclude these metrics, they may still be accessible or visible to some parties, such as developers, administrators, or hackers. To protect your data or privacy, you may need to use other methods, such as encryption, masking, or anonymization.
FAQs
How can I disable or exclude all metrics from a specific monitoring system?
You can use the management.metrics.export.<system>.enabled property to false for each monitoring system that you want to disable or exclude. The <system> is the name of the monitoring system, such as Prometheus, Datadog, Graphite, etc.
For example, to disable or exclude all metrics from Prometheus, you can use the following property: management.metrics.export.prometheus.enabled=false
How can I exclude the auto-configuration classes that are responsible for the default metrics in spring boot 3?
You can exclude the auto-configuration classes that are responsible for the default metrics in Spring Boot 3 by using the exclude attribute on the @SpringBootApplication annotation. For example, to disable all the metrics you can specify the MetricsAutoConfiguration
class inside the exclude attribute of SpringBootAnnotation annotation. For example: @SpringBootApplication(exclude = MetricsAutoConfiguration.class)
Conclusion
In summary, selectively excluding certain metrics from Spring Boot’s actuator endpoint is a smart move. It helps streamline monitoring by removing unnecessary, duplicate, or sensitive metrics. By doing this, you ensure that the displayed metrics perfectly match your monitoring requirements, making your system more efficient and secure.
Add a Comment