springboot2.0 + prometheus + grafana

maven 配置项

        
            org.springframework.boot
            spring-boot-starter-actuator
        
            
            io.micrometer
            micrometer-registry-prometheus
        

application.yml

server:
  port: 8080
management:
  metrics:
    tags:
      application: monitor1App
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      base-path: /
      exposure:
        include: env, health, info, metrics, prometheus, threaddump
        exclude:

metrics 用法

	@Autowired
	private MeterRegistry registry;
	private Counter counter;
	private DistributionSummary summary;
	
	//private Gauge gauge;
	private AtomicDouble gaugeDouble;
	
	
	@PostConstruct
	private void init(){
		counter = registery.counter("cnt_test","tagName","tagValue");
		summary = registery.summary("sum_test","tagName","tagValue");
		gaugeDouble = register.gauge("gauge_test",initTag("tagName","tagValue"),new AtomicDouble(0));
	}
	
	public void test(){
		count.increment();
		sumary.record(100);
		gaugeDouble.set(10.1);
	}
	
	private List initTag(String tagName, String tagValue) {
		 ArrayList list = new ArrayList() {};
		 list.add(new ImmutableTag(tagName, tagValue));
		 return list;
	}

prometheus 配置

配置文件名 prometheus.yml ,
增加如下配置项:

- job_name: 'springboot'
	scrape_interval: 5s
	metrics_path: /prometheus
	static_configs:
	- targets: ['127.0.0.1:8080']

grafana 的配置

增加变量
label_values(cnt_test,tagName) 抽取所有tagValue

你可能感兴趣的:(java)