当前位置: 首页 > 工具软件 > Micrometer > 使用案例 >

java metrics 简书_Micrometer 收集Metrics

路伟
2023-12-01

添加依赖

# Spring Boot 收集Metrics

org.springframework.boot

spring-boot-starter-actuator

# 提供Prometheus格式的Metrics

io.micrometer

micrometer-registry-prometheus

配置文件

#metrics

management.endpoints.web.exposure.include=* # 或者设置为promethues,只开放promethues

management.metrics.enable.jvm=true

management.endpoint.health.show-details= always

info.app.name=certificate-manager

代码埋点

// 创建 Registry

private MeterRegistry registry = new SimpleMeterRegistry();

// 或者使用局的 Registry

Metrics.counter("test.name","type","type1","desc","desc1").increment();

添加通用Tag

@Bean

MeterRegistryCustomizer 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']

 类似资料: