【Prometheus】【07】SpringBoot2.x使用io.prometheus统计Counter、Summary、Histogram

1.概述

Counter可以统计事件发生次数
Summary可以统计每个标签的发生次数,例如可以用于统计每个用户的调用次数
Histogram分桶统计,可以每个桶的调用时间耗时
demo

# HELP requests_latency_seconds Request latency in seconds.
# TYPE requests_latency_seconds summary
requests_latency_seconds_count{aLabel="aLabelValue",} 4.0
requests_latency_seconds_sum{aLabel="aLabelValue",} 0.14344206399999998
# HELP redisRabbitmqConsumerReceiveCounter redisRabbitmqConsumerReceiveCounter
# TYPE redisRabbitmqConsumerReceiveCounter counter
redisRabbitmqConsumerReceiveCounter 4.0
# HELP requests_histogram_latency_seconds Request latency in seconds.
# TYPE requests_histogram_latency_seconds histogram
requests_histogram_latency_seconds_bucket{le="0.005",} 3.0
requests_histogram_latency_seconds_bucket{le="0.01",} 3.0
requests_histogram_latency_seconds_bucket{le="0.025",} 3.0
requests_histogram_latency_seconds_bucket{le="0.05",} 3.0
requests_histogram_latency_seconds_bucket{le="0.075",} 3.0
requests_histogram_latency_seconds_bucket{le="0.1",} 3.0
requests_histogram_latency_seconds_bucket{le="0.25",} 4.0
requests_histogram_latency_seconds_bucket{le="0.5",} 4.0
requests_histogram_latency_seconds_bucket{le="0.75",} 4.0
requests_histogram_latency_seconds_bucket{le="1.0",} 4.0
requests_histogram_latency_seconds_bucket{le="2.5",} 4.0
requests_histogram_latency_seconds_bucket{le="5.0",} 4.0
requests_histogram_latency_seconds_bucket{le="7.5",} 4.0
requests_histogram_latency_seconds_bucket{le="10.0",} 4.0
requests_histogram_latency_seconds_bucket{le="+Inf",} 4.0
requests_histogram_latency_seconds_count 4.0
requests_histogram_latency_seconds_sum 0.14249414600000004

2.使用

2.1引入jar包

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

		<!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient -->
		<dependency>
			<groupId>io.prometheus</groupId>
			<artifactId>simpleclient</artifactId>
			<version>0.9.0</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_common -->
		<dependency>
			<groupId>io.prometheus</groupId>
			<artifactId>simpleclient_common</artifactId>
			<version>0.9.0</version>
		</dependency>

2.2actuator暴漏一个端点

package com.zhenzhen.demo.springboot.config.endpoint;

import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.common.TextFormat;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collections;
import java.util.Set;

/**
 * @author zhen.yin
 * @title:
 * @description:
 * @date 2020/11/14 14:12
 */
@Endpoint(id="my-prometheus-endpoint")
@Configuration
public class MyPrometheusEndpoint {

    @ReadOperation
    public String endpoint() {
        return this.writeRegistry(Collections.emptySet());
    }

    public String writeRegistry(Set<String> metricsToInclude) {
        try {
            Writer writer = new StringWriter();
            TextFormat.write004(writer, CollectorRegistry.defaultRegistry.filteredMetricFamilySamples(metricsToInclude));
            return writer.toString();
        } catch (IOException var3) {
            throw new RuntimeException("Writing metrics failed", var3);
        }
    }
}

2.3配置打开端点

management:
    endpoints:
        web:
            exposure:
                include: my-prometheus-endpoint
    health:
        defaults:
            enabled: false

2.4使用Counter、Summary、Histogram统计

package com.zhenzhen.demo.springboot.controller;

import java.util.Date;

import io.prometheus.client.Counter;
import io.prometheus.client.Histogram;
import io.prometheus.client.SimpleTimer;
import io.prometheus.client.Summary;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zhenzhen.demo.springboot.common.utils.BeanValidatorUtil;
import com.zhenzhen.demo.springboot.condition.HelloCondition;
import com.zhenzhen.demo.springboot.dto.HelloDto;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;

@RestController
@Api(description = "hello的控制器")
@Log
public class HelloController {

	private static final Counter Hello_Counter = Counter.build()
			.name("hello_counter").help("hello_counter").register();

	static final Summary requestLatency = Summary.build()
			.name("requests_latency_seconds")
			.help("Request latency in seconds.")
			.labelNames("aLabel")
			.register();

	static final Histogram requestHistogramLatency = Histogram.build()
			.name("requests_histogram_latency_seconds").help("Request latency in seconds.").register();

	@GetMapping("/hello")
	@ApiOperation(value="hello 方法")
	public HelloDto hello(HelloCondition helloCondition) {
		SimpleTimer requestTimer = new SimpleTimer();
		Histogram.Timer requestHistogramTimer = requestHistogramLatency.startTimer();

		try {
			Hello_Counter.inc();

			BeanValidatorUtil.check(helloCondition);
			log.info("输入参数"+helloCondition);
			return new HelloDto("真哥", new Date());
		} finally {
			requestHistogramTimer.observeDuration();
			//这里可以根据参数输入lable
			requestLatency.labels("aLabelValue").observe(requestTimer.elapsedSeconds());
		}
	}
}

2.5访问

http://localhost:9300/actuator/my-prometheus-endpoint

2.6查看统计的指标

http://localhost:9300/actuator/my-prometheus-endpoint

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值