I am looking for a way to export spring boot metrics to file in Spring Boot 2.
In Spring Boot 1.5.10, we used a Custom MetricsExporter class which implements MetricWriter and overrides set and increment methods to write the metrics using loggers. We used a log file because we have a different mechanism to process the log file later for metrics analysis.
We also used MetricsConfig class, which uses the bean MetricsEndpointMetricReader to read metrics from the metrics endpoint reader in a custom config class.
But, when we upgraded to Spring Boot 2.0.1 those are not working as there was a breaking change in the existing metrics classes.
Can someone help us with how we can export the metrics and write them using loggers when using Spring Boot 2.0?
@ExportMetricWriter public class MetricsExporter implements MetricWriter { private static Logger LOGGER = LoggerFactory.getLogger("metrics"); @Override public void set(Metric<?> value) { // Write the Gauge metrics to log file LOGGER.info("timestamp={}, name={}, value={}", value.getTimestamp(), value.getName(),value.getValue()); } @Override public void increment(Delta<?> delta) { //Write the Counter metrics to log file LOGGER.info("timestamp={}, name={}, value={}", delta.getTimestamp(), delta.getName(),delta.getValue()); } @Override public void reset(String metricName) { } } The MetricsConfig Class is as below:
@Configuration public class MetricsConfig { //Define the MetricsExporter bean to export metrics at regular interval to a log file @Bean public MetricsExporter metricsExporter() { return new MetricsExporter(); } //Define the MetricsEndpointMetricReader bean to export both push(counters and gauges) and pull(public) metrics @Bean public MetricsEndpointMetricReader metricsEndpointMetricReader(MetricsEndpoint metricsEndpoint) { return new MetricsEndpointMetricReader(metricsEndpoint); } }
LoggingMeterRegistryis not available with v1.5. How did you achieve this?