file_sd_configs
文件,将部署的服务追加进去本文主要阐述的是对容器中业务指标的监控,对容器的监控以及环境的搭建参照[Prometheus 监控Docker Swarm](Prometheus 监控Docker Swarm.md)java
数据模型web
Prometheus中的数据都是时间序列值。指标名称相同,label相同的记录称为一个样本值,Prometheus中存的就是各个样本值在不一样时间点的数据,称为一个时间序列,每一个时间点对应的值称为一个Sample,包含一个float64数值和一个精确到毫秒的时间值。spring
每一个序列经过指标名称以及相关的label来惟一肯定,指标名称要代表观察的目的(不是强制的)如(http_requests_total
表示系统受到请求总数的时间序列),同一指标中的每一个key/value(称为label)都称做一个维度,如(http_requests_total{code=200}
)表明请求成功的时间序列.docker
外部应用经过PromQL来对时间序列进行相应的查找,在查询时还能够利用Prometheus提供的功能丰富的函数对时间序列中的值进行计算。springboot
指标类型restful
Prometheus提供的客户端jar包中把指标类型分为以下四种app
这个分类只针对客户端使用者有效,在Prometheus server端是不进行区分的。对于Prometheus server端而言,客户端返回的都是时间序列对应的一个Sample,如http_requests_total{code=200} 290
,表示Prometheus server拉取指标的这个时间点,请求成功的总数是290次,是一个纯文本数据,即使咱们不用Prometheus提供的客户端,只要返回的数据知足这种格式,Prometheus server就能正常存储,也能够经过PromQL供外部查询。maven
Counter函数
Counter对应的指标值只能是一个单独的数值,而且除了能在服务启动时重置外,只能对指标值作累加操做,不能作减法操做,能够用来统计请求次数、任务执行次数、关键业务对象操做次数等。spring-boot
Gauge
Gauge对应的指标值只能是一个单独的数值,与Counter不一样的是,能够对Gauge表明的指标值作仁义的加减操做,通常用来表示温度、正在执行的job等指标
Histogram
Histogram 柱状图,再也不是简单对指标的sample值进行加减等操做,对于每个sample值执行下面的三个操做:
例如咱们经过Prometheus提供的客户端经过Histogram.build().name("job_execute_time").help("job执行时间时间分布(分)").buckets(1,5,10) .register();
定义了一个histogram,用来统计job执行时间的分布。对应的buckets是(1,5,10),表明四个区间
Histogram会生成以下6个维度的指标值
job_execute_time_bucket{le="1.0",} job_execute_time_bucket{le="5.0",} job_execute_time_bucket{le="10.0",} job_execute_time_bucket{le="+Inf",} job_execute_time_count job_execute_time_sum
当咱们有一个job执行时间为5.6分钟,则对应的各个维度的值变成
job_execute_time_bucket{le="1.0",} 0.0 job_execute_time_bucket{le="5.0",} 0.0 job_execute_time_bucket{le="10.0",} 1.0 job_execute_time_bucket{le="+Inf",} 1.0 job_execute_time_count 1.0 job_execute_time_sum 5.6
无穷大的确定是和
job_execute_time_count
一致的
能够看到Histogram类型的指标不会保留各个sample的具体数值,每一个bucket中也只是记录样本数的counter。
Summary 采样点分位图统计,相似于histgram,可是采用分位数来将sample分到不一样的bucket中,具体的区别查看HISTOGRAMS AND SUMMARIES,我的数学很差,理解的太痛苦了。
pom.xml中追加Prometheus相关依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-core</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient</artifactId> <version>0.8.1</version> </dependency>
application.yml 文件中追加启用Prometheus相关metric,
spring: application: name: sbprometheus server: port: 8080 management: metrics: export: prometheus: enabled: true endpoint: metrics: enabled: true prometheus: enabled: true endpoints: web: exposure: include: ["prometheus","health"]
定义业务须要的指标
/** * */ package chengf.falcon.sb.prometheus; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import io.micrometer.prometheus.PrometheusMeterRegistry; import io.prometheus.client.Counter; import io.prometheus.client.Gauge; import io.prometheus.client.Histogram; import io.prometheus.client.Summary; /** * @author: 做者: chengaofeng * @date: 建立时间:2020-03-20 12:04:20 * @Description: TODO * @version V1.0 */ @Configuration public class MetricConfig { @Autowired PrometheusMeterRegistry registry; @Value("${spring.application.name}") String appName; @Bean public Counter operatorCount() { return Counter.build().name(appName + "_class_operator_count").help("操做总次数").labelNames("className") .register(registry.getPrometheusRegistry()); } @Bean public Gauge runningJob() { return Gauge.build().name(appName + "_running_job_count").help("正在运行的job数") .register(registry.getPrometheusRegistry()); } @Bean public Histogram executeTime() { return Histogram.build().name(appName + "_job_execute_time").help("job执行时间时间分布(分)").buckets(1,5,10) .register(registry.getPrometheusRegistry()); } @Bean public Summary timeQuantile() { return Summary.build().name(appName + "_job_execute_time_quantile").help("job执行时间时间分布(分)").quantile(0.5, 0.05).quantile(0.9, 0.01) .register(registry.getPrometheusRegistry()); } }
业务代码中更新指标(经过resturl模拟实际的操做)
/** * */ package chengf.falcon.sb.prometheus; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import io.prometheus.client.Counter; import io.prometheus.client.Gauge; import io.prometheus.client.Histogram; import io.prometheus.client.Summary; /** * @author: 做者: chengaofeng * @date: 建立时间:2020-03-03 19:02:43 * @Description: TODO * @version V1.0 */ @RestController public class MetricController { @Autowired Counter operatorCount; @Autowired Gauge runningJob; @Autowired Histogram executeTime; @Autowired Summary timeQuantile; @RequestMapping("/counter/{className}") public String counter(@PathVariable String className) { operatorCount.labels(className).inc(); return "COUNTER"; } @RequestMapping("/guage/{number}") public String guage(@PathVariable int number) { runningJob.set(number); return "guage"; } @RequestMapping("/histogram/{time}") public String histogram(@PathVariable double time) { executeTime.observe(time); return "histogram"; } @RequestMapping("/summary/{time}") public String summary(@PathVariable double time) { timeQuantile.observe(time); return "summary"; } }
启动sprong-boot工程,访问上面的restful接口几回,而后访问/actuator/prometheus
查看指标状况,下面贴出一个样例(去除掉spring 自动给咱们生成的各类指标)
# HELP sbprometheus_job_execute_time_quantile job执行时间时间分布(分) # TYPE sbprometheus_job_execute_time_quantile summary sbprometheus_job_execute_time_quantile{quantile="0.5",} 5.0 sbprometheus_job_execute_time_quantile{quantile="0.9",} 13.0 sbprometheus_job_execute_time_quantile_count 11.0 sbprometheus_job_execute_time_quantile_sum 120.0 # HELP sbprometheus_job_execute_time job执行时间时间分布(分) # TYPE sbprometheus_job_execute_time histogram sbprometheus_job_execute_time_bucket{le="1.0",} 2.0 sbprometheus_job_execute_time_bucket{le="5.0",} 3.0 sbprometheus_job_execute_time_bucket{le="10.0",} 3.0 sbprometheus_job_execute_time_bucket{le="+Inf",} 3.0 sbprometheus_job_execute_time_count 3.0 sbprometheus_job_execute_time_sum 6.5 # HELP sbprometheus_class_operator_count 操做总次数 # TYPE sbprometheus_class_operator_count counter sbprometheus_class_operator_count{className="transform",} 2.0 sbprometheus_class_operator_count{className="sub",} 1.0 # HELP sbprometheus_running_job_count 正在运行的job数 # TYPE sbprometheus_running_job_count gauge sbprometheus_running_job_count 10.0
由于在spring-boot工程的pom中,咱们追加了spring-boot-maven-plugin
这个插件,因此执行mvn package
后会生成一个独立的可执行jar,因此制做镜像时,只用基于openjdk的镜像,再把这个jar copy进去,以后启动就能够了.
构建(在工程目录下)
$ mvn clean package $ cd target $ cat > Dockerfile<<EOF #基础镜像基于openjdk,利用alpine FROM openjdk:8u212-jdk-alpine #所属团队 MAINTAINER chengf #将编译好的工程jar包copy到镜像容器中 ENV TARGET_JAR="sb-prometheus-0.0.1-SNAPSHOT.jar" COPY ${TARGET_JAR} /usr/src/${TARGET_JAR} # 工做目录 WORKDIR /usr/src #程序入口 RUN echo "java -jar \${TARGET_JAR} > start.sh \ && chmod 777 start.sh CMD ./start.sh EOF $ docker build -t sb-prometheus:0.0.1 .
启动镜像,测试镜像是否正确
docker run --rm --name sb-prometheus -p 8080:8080 sb-prometheus:0.0.1
/actuator/prometheus
,看是否正常工做编辑stack文件
$ cd /opt/k8s/prometheus $ cat> sb-prom-stack.yml<<EOF version: "3" services: sbprometheus: image: sb-prometheus:0.0.1 networks: - mcsas-network deploy: restart_policy: condition: on-failure networks: mcsas-network: external: true EOF
启动服务
$ docker stack deploy -c sb-prom-stack.yml sbprom
file_sd_configs
中配置的文件在[Prometheus 监控Docker Swarm](Prometheus 监控Docker Swarm.md)中,咱们在prometheus的配置文件中指定了以下配置段:
- job_name: 'springboot' metrics_path: /actuator/prometheus file_sd_configs: - files: - /etc/prometheus/service.yaml
因此只用在挂载目录下建立service.yaml,并追加咱们要监控的服务便可
$ cd /opt/k8s/prometheus/conf $ cat>service.yaml<<EOF - targets: ['sbprometheus:8080'] EOF
修改完成后,经过Prometheus服务暴露的端口查看指标分类,能够发现咱们自定义的业务指标已经被Prometheus获取到
由于咱们业务容器没有暴露出来端口,因此为了演示,进入容器内部,经过wget访问咱们的restful接口,产生一些指标数据
$ docker ps |grep sbprometheus 8dbafd80573b sb-prometheus:0.0.1 "/bin/sh -c ./start.…" 44 minutes ago Up 44 minutes sbprom_sbprometheus.1.kuzpe4he7j2iz9i43cwrrxh3x $ docker exec -it 8dbafd80573b sh /usr/src # wget -q -O - http://localhost:8080/summary/66 /usr/src # wget -q -O - http://localhost:8080/counter/tranform /usr/src # wget -q -O - http://localhost:8080/counter/sub /usr/src # wget -q -O - http://localhost:8080/histogram/1 /usr/src # wget -q -O - http://localhost:8080/histogram/3.4
在grafana中能够对咱们的业务指标进行观察如:
也能够经过cAdvisor对我部署的这个服务对应的容器进行观察