首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >千分尺计时器和Spring Boot 2.0.4

千分尺计时器和Spring Boot 2.0.4
EN

Stack Overflow用户
提问于 2018-10-10 03:27:53
回答 2查看 2.7K关注 0票数 0

我尝试使用Spring Boot2.0.4+微米将指标发送到InfluxDB,但只有计数器可以工作,计时器不能。

所以,这是我的依赖项:

...
<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发送任何内容。

以下是我用来配置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);
    }
}

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

EN

回答 2

Stack Overflow用户

发布于 2018-10-10 07:47:01

我怀疑这是除了Timer.Sample之外的this question的复制品。请参阅我对问题的回答,以了解step版本的仪表是如何工作的。

我还添加了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 I确认将发布到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 
>
票数 0
EN

Stack Overflow用户

发布于 2019-09-12 16:29:51

我也遇到了同样的问题,我修复了这个问题,只需在初始化中添加一行:

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52728081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档