/spring-boot-execution-metric

Measure execution times of critical code blocks and expose statistics as actuator metrics

Primary LanguageJavaMIT LicenseMIT

Coverage Status Build Status Maven Central

spring-boot-execution-metric

Measure execution times of critical code blocks and expose statistics as actuator metrics

Provides a lightweight method to measure runtime of (selected) critical code executions (such as calling external systems) and expose as spring boot actuator metrics.

Howto use

Maven

<dependency>
    <groupId>eu.hinsch</groupId>
    <artifactId>spring-boot-execution-metric</artifactId>
    <version>0.2.2</version>
</dependency>

Gradle

compile 'eu.hinsch:spring-boot-execution-metric:0.2.2'

Use with AOP / Annotations

Expose metric only

@ExecutionMetric("some-action")
public void someAction() {
    // ...
}

With logging

@ExecutionMetric(value = "some-action", LogLevel = LogLevel.DEBUG)
public void someAction() {
    // ...
}

The logger will automatically taken from the (implementation) class of the annotated method.

Programatic invocation

// configuration
@Bean
public ExecutionMetricFactory executionMetricFactory(CounterService gaugeService, GaugeService counterService) {
    return new ExecutionMetricFactory(gaugeService, counterService);
}

// setup metric stores (once during config)

ExecutorMetric executorMetric = executionMetricFactory.executorMetric("test1", logger);
SupplierMetric supplierMetric = executionMetricFactory.supplierMetric("test2", logger);

// use (on every call)
executorMetric.measure(() -> someAction(...));
SomeValue myValue = supplierMetric.measure(() -> getSomeValue());
   

The code above will expose the following spring boot actuator metrics entries:

gauge.<name>.last = <last call duration>
gauge.<name>.average = <average call duration>
gauge.<name>.min = <minimum call duration>
gauge.<name>.max = <maximum call duration>
counter.<name> = <number of invocation>
``