tgr
tgr

Reputation: 3608

How to persist spring-boot metrics as log?

I am running a spring-boot application, that measures the standard and custom metrics. Now I want those metrics to be persisted in elasticsearch for further examination. I have a functioning ELK-stack in place, so the obvious solution to me was persisting the measures in a log file and let filebeat collect it.

I have found an example, that could have achieved this, but it was using MetricRepository, which is no longer supported in java 8.

The official documentation is not helpful. All shown examples use some proprietary format or are writing into a database.

Can anyone please provide a way to persist metrics as log files?

Upvotes: 2

Views: 2630

Answers (1)

Strelok
Strelok

Reputation: 51441

Sounds like you just have to write a MetricWriter implementation and mark the bean with @ExportMetricWriter Should be a piece of cake. Implement the three methods by just writing to the log and that's it.

Something like:

@Component
@ExportMetricWriter
public class MyMetricWriter implements MetricWriter, Closeable {

  private static final Log logger = LogFactory.getLog(MyMetricWriter.class);


@Override
public void increment(Delta<?> delta) {
    // record increment to log
}

@Override
public void set(Metric<?> value) {
    // record set metric to log
}

@Override
public void reset(String name) {
    // Not implemented
}

@Override
public void close() {
    /// ...
     }
}

The docs mentioned a few others you can refer to their implementations for inspiration.

Upvotes: 2

Related Questions