代码之家  ›  专栏  ›  技术社区  ›  Ronaldo Lanhellas

千分尺计时器和弹簧套2.0.4

  •  0
  • Ronaldo Lanhellas  · 技术社区  · 6 年前

    我试图使用SpringBoot2.0.4+测微计将度量值发送给InfloxDB,但是只有计数器工作,计时器没有工作。

    所以,这是我的依赖:

    ...
    <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,两者都没有向infloxdb发送任何指标。我用它在@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 ,但没有发送到infloxdb。

    这是要配置流入的我的属性:

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

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

    1 回复  |  直到 6 年前
        1
  •  0
  •   Johnny Lim    6 年前

    我怀疑这是 this question 但是为了 Timer.Sample . 请看我对这个问题的回答,以了解步进式电表是如何工作的。

    我还补充说 a test for Timer.Sample 以确认它在工作。


    更新:

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

    我确认了它的工作原理(至少在弹簧靴执行器中): 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确认正在向InfloxDB发布以下内容:

    > 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 
    >