0

I'm trying to send metrics to InfluxDB using Spring Boot 2.0.4 + Micrometer, but only Counter works, the Timer didn't.

So, this is my dependencies:

...
<dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-influx</artifactId>
        </dependency
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
<dependency>
            <groupId>org.influxdb</groupId>
            <artifactId>influxdb-java</artifactId>
            <version>2.8</version>
        </dependency>
...

Using Counter, as i said, everything works, see:

private final Counter counter = Metrics.counter("my.counter", "my.extra.tag", this.getClass().getCanonicalName());
counter.increment();

But Timer don't work, i tried @Timed and Timer.sample, both don't send any metrics to influxDB. I annoted by method inside a @Service class with it:

@Timed(value = "my.timer", extraTags = { "my.extra.tag", "TimerSomething" })

So, i tried change to Timer.sample like this: https://micrometer.io/docs/concepts#_storing_start_state_in_code_timer_sample_code, but nothing is sent to influxDB.

This is my properties to configure influx:

management.endpoints.web.exposure.include: info, health, metrics
management.metrics.export.influx.enabled=true
management.metrics.export.influx.auto-create-db=false
management.metrics.export.influx.batch-size=10000
management.metrics.export.influx.db=my.metrics.db
management.metrics.export.influx.uri=http://xxxxx:8086

Edit 1:

I tried create a simple test, see:

@RunWith(MockitoJUnitRunner.class)
public class MicrometerTest {

    private final InfluxConfig config = new InfluxConfig() {

        @Override
        public String get(String s) {
            return null;
        }

        @Override
        public Duration step() {
            return Duration.ofSeconds(5);
        }

        @Override
        public boolean autoCreateDb() {
            return false;
        }

        @Override
        public String db() {
            return "mydb";
        }

        @Override
        public String uri() {
            return "http://xxxx:8086";
        }

    };

    private final InfluxMeterRegistry registry = new InfluxMeterRegistry(this.config, Clock.SYSTEM);


    @Test
    public void counter() throws InterruptedException {
        Counter counter = this.registry.counter("my.counter", Tags.of("key", "value"));

        counter.increment();
        TimeUnit.SECONDS.sleep(5);
        TimeUnit.SECONDS.sleep(5);
    }

    @Test
    public void verifica_se_timer_funciona() throws InterruptedException {
        Timer.Sample sample = Timer.start(this.registry);

        TimeUnit.SECONDS.sleep(1);

        Timer timer = this.registry.timer("my.timer", "response", "200");
        sample.stop(timer);
        TimeUnit.SECONDS.sleep(5);
    }
}

The counter works fine but timer don't.

Ronaldo Lanhellas
  • 2,975
  • 5
  • 46
  • 92

2 Answers2

0

I suspect this is a duplicate of this question but for Timer.Sample. Please see my answer on the question to understand how step version of meters work.

I also added a test for Timer.Sample to confirm it's working.


UPDATE:

As requested in the below comment, I updated the sample to use the auto-configured one.

And I confirmed it works (at least in Spring Boot Actuator): http://localhost:8080/actuator/metrics/hello.timer

{
  "name" : "hello.timer",
  "description" : null,
  "baseUnit" : "milliseconds",
  "measurements" : [ {
    "statistic" : "COUNT",
    "value" : 1.0
  }, {
    "statistic" : "TOTAL_TIME",
    "value" : 0.225828
  }, {
    "statistic" : "MAX",
    "value" : 0.225828
  } ],
  "availableTags" : [ ]
}


UPDATE 2:

FTR I confirmed it's publishing to InfluxDB as follows:

> delete from hello_timer 
> select * from hello_timer 
> select * from hello_timer 
name: hello_timer 
time count mean metric_type sum upper 
---- ----- ---- ----------- --- ----- 
1539266391883000000 1 0.54792 histogram 0.54792 0.54792 
>
Johnny Lim
  • 5,623
  • 8
  • 38
  • 53
0

I had the same issue and I fixed this just added single line into initialization:

timer = Metrics.timer("report.execution.time");
timer.record(0, TimeUnit.NANOSECONDS);

Looks quite strange but after that timer measurement successfully creats in InfluxDB.

Kirill
  • 1
  • 1