Mac 基于Grafana + Promthues + Pushgateway 实现的Android端APM监控实践(3)
上期Mac 基于Grafana + Promthues + Pushgateway 实现的Android端APM监控实践(2)已经介绍了基于Grafana,Promthues,Pushgateway服务环境搭建,简单数据上传,简单面板配置等,由于本次APM主要是在客户端上进行数据采集,所以对应客户端数据封装,客户端数据采集也很重要,考虑到网络库和协议封装能够实现android和ios两端共用的话,本次使用的是扩平台技术方案KMM方式来实现,下面看一下整体架构图:
KMM层重要使用看kotlin推出的一种跨平台实现方案,特别适合sdk这种无界面开发,KMM框架这里就不在详细说明了,有兴趣可以官网了解一下kotlinlang.org/docs/mobile…, 使用KMM目的是网络和协议这块,包括sdk对接业务层这块封装都能共用一套代码
APM平台-数据上报
从1,2期来看,上报的数据是先上报的PushWay,然后prometheus再pull数据到自己的服务,存储,所以想要上报数据必选遵循prometheus数据格式才行,prometheus数据格式查看地址prometheus.io/docs/instru…
发送数据的时候需要以为本的方式进行发送,version版本为0.0.4,不指定的话默认使用最新版本,然后再看看body内容格式,由于prometheus有四大度量指标,分别:
-
Counter(计数器)
Counter类型代表一个累积的指标数据,其单调递增,只增不减。应用场景有类似请求次数,错误数量等等就比较适合使用Counter来做指标类型
-
Gauge(仪表盘)
Gauge 类型代表一个可以任意变化的指标数据,其可增可减。在应用场景中,在系统中统计 CPU、Memory 等等时很常见,而在业务场景中,业务队列的数量也可以用 Gauge 来统计,实时观察队列数量,及时发现堆积情况,因为其是浮动的数值,并非固定的,侧重于反馈当前的情况
-
Histogram(累积直方图)
Histogram 类型将会在一段时间范围内对数据进行采样(通常是请求持续时间或响应大小等等),并将其计入可配置的存储桶(bucket)中,后续可通过指定区间筛选样本,也可以统计样本总数。
Histogram 类型在应用场景中非常的常用,因为其代表的就是分组区间的统计,而在分布式场景盛行的现在,链路追踪系统是必不可少的,那么针对不同的链路的分析统计就非常的有必要,例如像是对 RPC、SQL、HTTP、Redis 的 P90、P95、P99 进行计算统计,并且更进一步的做告警,就能够及时的发现应用链路缓慢,进而发现和减少第三方系统的影响。
-
Summary(摘要)
Summary 类型将会在一段时间范围内对数据进行采样,但是与 Histogram 类型不同的是 Summary 类型将会存储分位数(在客户端进行计算),而不像 Histogram 类型,根据所设置的区间情况统计存储。提供三种摘要指标: 样本值的分位数分布情况,所有样本值的大小总和,样本总数
而上报时候body数据格式需要遵循
# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="post",code="200"} 1027 1395066363000
http_requests_total{method="post",code="400"} 3 1395066363000
# Escaping in label values:
msdos_file_access_time_seconds{path="C:\DIR\FILE.TXT",error="Cannot find file:\n"FILE.TXT""} 1.458255915e9
# Minimalistic line:
metric_without_timestamp_and_labels 12.47
# A weird metric from before the epoch:
something_weird{problem="division by zero"} +Inf -3982045
# A histogram, which has a pretty complex representation in the text format:
# HELP http_request_duration_seconds A histogram of the request duration.
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{le="0.05"} 24054
http_request_duration_seconds_bucket{le="0.1"} 33444
http_request_duration_seconds_bucket{le="0.2"} 100392
http_request_duration_seconds_bucket{le="0.5"} 129389
http_request_duration_seconds_bucket{le="1"} 133988
http_request_duration_seconds_bucket{le="+Inf"} 144320
http_request_duration_seconds_sum 53423
http_request_duration_seconds_count 144320
# Finally a summary, which has a complex representation, too:
# HELP rpc_duration_seconds A summary of the RPC duration in seconds.
# TYPE rpc_duration_seconds summary
rpc_duration_seconds{quantile="0.01"} 3102
rpc_duration_seconds{quantile="0.05"} 3272
rpc_duration_seconds{quantile="0.5"} 4773
rpc_duration_seconds{quantile="0.9"} 9001
rpc_duration_seconds{quantile="0.99"} 76656
rpc_duration_seconds_sum 1.7560473e+07
rpc_duration_seconds_count 2693
从上面格式看,固定的头部都是必须包含
# HELP 指标名称 说明
# TYPE 指标名称 指标类型(上面说的四大类型)
后面的就是不同指标对应的不同内容格式了,拿最常用的Gauge类举例,就是需要下面格式
metric_name [
"{" label_name "=" `"` label_value `"` { "," label_name "=" `"` label_value `"` } [ "," ] "}"
] value
拿我们收集启动时间耗时性能指标为例子,最终上报的body内容如下:
# HELP StartupGauge 应用启动耗时统计
# TYPE StartupGauge gauge
StartupGauge{appid="12345678",appinfo="1.0",application_create="6090",application_create_scene="159",appname="ApmDemo",ctype="android",instance="172.26.177.179",is_warm_start_up="false",job="ApmCollect",machine="MIDDLE",mobileinfo="samsung_SM-G9600",nt="0",osver="10",splash_activity_duration="",stage_between_app_and_activity="",startup_duration="6837",time_interval="5-7s",uid="1234",uuid="f23ed8f4-0093-4b3b-af14-7cf559482efb"} 6837
其他类型指标只需要参照官网的格式上报就行
对应KMM网络封装和协议封装细节这里不详细说了,最后附上代码地址github.com/dengqu/KMMN…
APM平台-告警配置
上面介绍了数据采集上报,数据的可视化性配置,最后来讲一下告警相关配置,配置相关告警,针对关心的重要性的性能指标出异常的时候能够及时发现,首先还是先进去Grafana首页
点击Notification channels进去选择New channel
进去之后,可以设置名称,告警通知的type,这里面有很多选择,例如Email,钉钉,Prometheus Alertmanager等,这里发现,Grafana这里没有直接支持企业微信,但是我们可以通过选择告警的方式为Prometheus Alertmanager,Prometheus Alertmanager又是Prometheus服务中告警的一个组件(前面有介绍),它可以实现配置企业微信相关告警,上面已经介绍Prometheus Alertmanager服务搭建,想要对接企业微信,需要按照这个流程www.cnblogs.com/miaocbin/p/…去企业微信申请相关企业微信ID,告警组ID,申请后可以进去Prometheus Alertmanager目录,编辑alertmanager.yml文件
global:
resolve_timeout: 1m # 每1分钟检测一次是否恢复
wechat_api_url: 'https://qyapi.weixin.qq.com/cgi-bin/'
wechat_api_corp_id: 'ww9ba8cb7b4ad14e66' # 企业微信中企业ID
wechat_api_secret: 'Buhe4HSBhBIGr9Rx2j_OIaQRGqmu7e_zEiVlk_GdyMc' # 企业微信中,应用的Secret
templates:
- '/Users/dengqu/Downloads/alertmanager-0.23.0-rc.0.darwin-amd64/template/*.tmpl'
route:
receiver: 'wechat'
group_by: ['env','instance','type','group','job','alertname']
group_wait: 10s # 初次发送告警延时
group_interval: 10s # 距离第一次发送告警,等待多久再次发送告警
repeat_interval: 5m # 告警重发时间
receivers:
- name: 'wechat'
wechat_configs:
- send_resolved: true
message: '{{ template "wechat.default.message" . }}'
to_party: '2' # 企业微信中创建的接收告警的部门【告警机器人】的部门ID
agent_id: '1000002' # 企业微信中创建的应用的ID
api_secret: 'Buhe4HSBhBIGr9Rx2j_OIaQRGqmu7e_zEiVlk_GdyMc' # 企业微信中,应用的Secret
上面是企业微信相关配置,templates:是告警模板配置位置,在/Users/dengqu/Downloads/alertmanager-0.23.0-rc.0.darwin-amd64/template目录下,可以新建一个wechat.tmpl告警模板
{{ define "grafana.default.message" }}{{ range .Alerts }}
{{ .StartsAt.Format "2006-01-02 15:03:04" }}
{{ range .Annotations.SortedPairs }}{{ .Name }} = {{ .Value }}
{{ end }}{{ end }}{{ end }}
{{ define "wechat.default.message" }}
{{ if eq .Status "firing"}}[Warning]:{{ template "grafana.default.message" . }}{{ end }}
{{ if eq .Status "resolved" }}[Resolved]:{{ template "grafana.default.message" . }}{{ end }}
{{ end }}
重启Prometheus Alertmanager服务就行,至此告警服务已经搭建完成,Grafana已经配置了对接Prometheus Alertmanager告警了,下面进行对想要监控的指标进行配置,首先进入指标面板编辑页面,选择Alert,配置Rule,例如配置一个我的博客访问量低于50的时候告警
然后选择刚才通知的渠道,设置message,然后Test rule
当告警触发的时候便会收到企业微信通知了
告警这块只简单描述一下,更新详细配置可以自行google一下,导致整个APM平台便形成了一个闭环 到此整个APM系列就结束了,当然客户端采集使用的是腾讯开源框架Matrix框架,有兴趣也可以阅读一下相关源码