美文网首页
Micrometer 收集Metrics

Micrometer 收集Metrics

作者: 冯延龙 | 来源:发表于2019-07-09 16:44 被阅读0次

添加依赖

# Spring Boot 收集Metrics
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
# 提供Prometheus格式的Metrics
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>

配置文件

#metrics
management.endpoints.web.exposure.include=* # 或者设置为promethues,只开放promethues
management.metrics.enable.jvm=true
management.endpoint.health.show-details= always
info.app.name=certificate-manager

以上配置完成后可以输出基本的metrics,可以访问http://ip:port/actuator/prometheus 查看

代码埋点

// 创建 Registry
private MeterRegistry registry = new SimpleMeterRegistry();
// 或者使用局的 Registry
Metrics.counter("test.name","type","type1","desc","desc1").increment();

添加通用Tag

  @Bean
    MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
        return registry -> registry.config().commonTags("application", "certificate-manager");
    }

或者通过配置文件(properties文件yml修改格式即可)

management.metrics.tags.region=us-east-1
management.metrics.tags.stack=prod

测试Controller

package com.ericsson.automotive.cm.certificatemanager.v1.controller;

import io.micrometer.core.instrument.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/monitor")

public class MonitorController {

    private MyObj myObj = new MyObj();
    private Counter testCounter;
    private MeterRegistry registry = Metrics.globalRegistry;
    @Autowired
    public MonitorController() {
        Gauge.builder("people-gauge", myObj, MyObj::getVal)
                .register(registry);
        testCounter = Metrics.counter("test.counter.total", "services", "demo");

    }

    @RequestMapping(value = "/counter", method = RequestMethod.GET)
    public String counter() {
        testCounter.increment();
        return "counter + 1";
    }

    @RequestMapping(value = "/time", method = RequestMethod.GET)
    public String time() {
        Timer timer = Metrics.timer("timer", "timer-method", "cost");
        timer.record(() -> timer());
        return "timer ok";
    }
    @RequestMapping(value = "/time2", method = RequestMethod.GET)
    public void time2(){
        Timer.Sample sample = Timer.start(registry);
        timer();
        sample.stop(registry.timer("timer-method2", "timer-method", "ca"));
    }

    @RequestMapping(value = "/gauge", method = RequestMethod.GET)
    public String gauge() {
        myObj.setVal(LocalDateTime.now().getSecond());
        return "gauge is " + LocalDateTime.now().getSecond();
    }

    private void timer() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public class MyObj {
        private int val;

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }
    }

}

prometheus配置Target

在promethues目录下修改配置文件prometheus.yml加入自己的应用提供的metrics接口。如 http://192.168.197.4:8080/actuator/prometheus

  - job_name: 'spring-actuator'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 15s
    static_configs:
    - targets: ['192.168.197.4:8081','192.168.197.4:8080']

相关文章

网友评论

      本文标题:Micrometer 收集Metrics

      本文链接:https://www.haomeiwen.com/subject/imounqtx.html