因为答应微信群友分享 Android APM 平台分享,不拖欠了,今天开始分享,不然发红包,既然说了,就总结一下前阵子研究的 Android 性能监控平台java
运维工具:Promethues+Grafana 后端: SpringBoot + MySQL+ Mybaties 移动端:Matrix android
Promethues 与 SpringBoot 集成上报比较及时的数据,原本觉得就是拼接字符串便可,可是后来才发现须要集成 simpleclient_spring_boot 代码,按照约定俗成的方法上报才能够,若是你们作这方面,须要注册避免这个坑。git
pom.xml 依赖:github
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>0.1.0</version>
</dependency>
复制代码
上报接口:spring
@Override
public List<Collector.MetricFamilySamples> collect() {
List<MetricFamilySamples> mfs = new ArrayList<>();
String latestAppVersion = appMapper.getLatestAppVersion();
// 当前上个版本应用大小
GaugeMetricFamily pre_labeledGauge = new GaugeMetricFamily("app_pre_app_size", "previous android apk size", Collections.singletonList("labelname"));
Integer preAppSize = appMapper.getPreAppSize(latestAppVersion);
pre_labeledGauge.addMetric(Collections.singletonList("labelvalue"), preAppSize);
// 当前当前版本应用大小
GaugeMetricFamily labeledGauge = new GaugeMetricFamily("app_cur_app_size", "current android apk size", Collections.singletonList("labelname"));
Integer curAppSize = appMapper.getCurAppSize(latestAppVersion);
labeledGauge.addMetric(Collections.singletonList("labelvalue"), curAppSize);
mfs.add(pre_labeledGauge);
mfs.add(labeledGauge);
return mfs;
}
复制代码
而后是 Promethues 配置:sql
global:
scrape_interval: 10s
scrape_timeout: 10s
evaluation_interval: 10m
scrape_configs:
- job_name: app
scrape_interval: 5s
// 时间轮训查询
scrape_timeout: 5s
// 监听路径接口
metrics_path: /prometheus
scheme: http
static_configs:
- targets: ['127.0.0.1:8887']
- job_name: setuptime
scrape_interval: 2s
scrape_timeout: 2s
metrics_path: /setuptime
static_configs:
- targets: ['127.0.0.1:8887']
复制代码
Grafana 数据源有两种形式,比较实时性很是强的 Crash、 ANR、 内存泄露、 冷/热启动耗时、 关键页面耗时 用 prometheus 上报,若是实时性比较弱的话,利用 Mysql 数据上报的形式来读取。数据库
直接集成 MySQL ,流利说采用的是 MariaDB 的记录,而后在 Grafana 配置以下: 后端
首先经过命令行 prometheus 启动性能优化
./prometheus --config.file=prometheus.yml
复制代码
添加数据源: 微信