【问题标题】:Micrometer Timer and Spring Boot 2.0.4千分尺计时器和 Spring Boot 2.0.4
【发布时间】:2018-10-09 19:27:53
【问题描述】:

我正在尝试使用 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>
...

使用 Counter,正如我所说,一切正常,请参阅:

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

但是 Timer 不起作用,我尝试了 @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。

这是我配置 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

编辑 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);
    }
}

计数器工作正常,但计时器不能。

【问题讨论】:

    标签: spring-boot influxdb spring-boot-actuator micrometer spring-micrometer


    【解决方案1】:

    我怀疑这是 this question 的副本,但对于 Timer.Sample。请参阅我对问题的回答,以了解仪表的步进版本的工作原理。

    我还添加了a test for Timer.Sample 以确认它正在工作。


    更新:

    根据以下评论中的要求,我更新了the sample 以使用自动配置的。

    我确认它有效(至少在 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" : [ ]
    }
    


    更新 2:

    FTR 我确认它正在按如下方式发布到 InfluxDB:

    > 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,在您的测试中,您正在手动创建 InfluxRegistry,但我需要将其注入 Spring,因为我所有的 Influx 配置都位于 my.properties 文件中。
    • @RonaldoLanhellas 感谢您的反馈!如果测试适合您,我只是希望它只适用于自动配置的测试。它适用于您的 InfluxDB 吗?
    • 我会测试你的情况并返回,但是我在这个测试中配置了influx DB URL
    • @RonaldoLanhellas 我按要求更新了我的答案。请检查更新的答案和示例。
    • @RonaldoLanhellas 对于测试问题,您可以使用 InfluxDB URI 覆盖 InfluxConfig.uri()
    【解决方案2】:

    我遇到了同样的问题,我解决了这个问题,只是在初始化中添加了一行:

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

    看起来很奇怪,但是在 InfluxDB 中成功创建了计时器测量之后。

    【讨论】:

      猜你喜欢
      • 2021-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 2020-03-15
      • 2019-01-27
      • 1970-01-01
      • 2022-12-22
      相关资源
      最近更新 更多