java metrics 简书_Micrometer 收集Metrics

java metrics 简书_Micrometer 收集Metrics添加依赖#SpringBoot收集Metricsorg.springframework.bootspring-boot-starter-actuator#提供Prometheus格式的Metricsio.micrometermicrometer-registry-prometheus配置文件#metricsmanagement.endpoints.web.exposure.include=…

大家好,欢迎来到IT知识分享网。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’]

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/22037.html

(0)
上一篇 2024-01-12 13:00
下一篇 2024-01-12 16:00

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信