我正在尝试使用 Spring Boot 2.0.4 + Micrometer 将指标发送到 InfluxDB,但只有 Counter 有效,Timer 没有。
所以,这是我的依赖:
...
<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>
...
正如我所说,使用计数器,一切正常,请参阅:
private final Counter counter = Metrics.counter("my.counter", "my.extra.tag", this.getClass().getCanonicalName());
counter.increment();
但是计时器不起作用,我尝试了@Timed 和 Timer.sample,两者都没有向 influxDB 发送任何指标。我用 @Service 类中的方法注释了它:
@Timed(value = "my.timer", extraTags = { "my.extra.tag", "TimerSomething" })
所以,我尝试像这样更改 Timer.sample:https ://micrometer.io/docs/concepts#_storing_start_state_in_code_timer_sample_code ,但没有任何东西发送到 influxDB。
这是我配置流入的属性:
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
编辑1:
我尝试创建一个简单的测试,请参阅:
@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);
}
}
计数器工作正常,但计时器不行。