java metrics 简书_Micrometer 收集Metrics

添加依赖

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值