使用 Prometheus 监控 SAP ABAP 应用程序

Prometheus 是由 SoundCloud 开发的监控系统的开源版本,logo 是一个燃烧的红色火炬,这应该是有情怀的工程师们向泰坦巨神的后代,偷偷盗走天火,传给人类的普罗米修斯表示的敬意。

2016 年,由 Google 发起的 Linux 基金会(Cloud Native Computing Foundation,CNCF)将 Prometheus 纳入其第二大开源项目,在开源社区十分活跃,SAP Kyma 也使用了 Prometheus 作为其监控组件。

Prometheus 采用 Pull 方式获取监控信息,并提供了多维度的数据模型和灵活的数据查询和聚合接口。
那么 Prometheus 是云原生应用和微服务架构的专属工具么?当然不是。下面,笔者就用 SAP CRM On-Premises 为例,介绍 ABAP 技术栈如何借助 Prometheus 来实现自定义的日志监控功能。
在接触 SAP CRM 应用之前,让我们先来热热身,熟悉一下 Prometheus 的使用方法。
使用 Prometheus 监控 Linux 系统各项指标
首先在 Linux 系统上安装一个探测器 node explorer, 下载地址https://prometheus.io/docs/guides/node-exporter/这个探测器会定期将 linux 系统的各项硬件指标和内核参数通过 9100 端口和 url metrics 暴露给外部。

启动 node explorer,显示在 9100 端口上监听:

浏览器里输入 hostname:9100/metrics 就可以访问到 node explorer 收集到的各项参数:

一些例子:CPU 在不同工作模式下的使用时间:node_cpu_seconds_total

文件系统可用字节数:node_filesystem_avail_bytes

网卡收到的字节数:node_network_receive_bytes_total

下面用 Prometheus 来收集和展示通过 node explorer 暴露出来的数据。在 Prometheus 安装目录的 prometheus.yml 文件里定义一个 job,指向 Linux 系统上运行的 node explorer:

本地启动 Prometheus,打开下面的 url:http://localhost:9090/输入 node_cpu_seconds_total{mode="system"},查询该服务器上所有 CPU 工作在系统态消耗的时间:

还可以指定时间窗口,只查询过去 1 分钟之内的 CPU 运行数据:rate(node_cpu_seconds_total{mode="system"}[1m])

如何使用 Prometheus 针对自己的服务器采集自定义的参数。
用一个简单的例子来说明。
我用 express 和 http 搭了一个最简单的服务器,监听在 8081 端口上。

在 metrics endpoint 上,我会打印出这个服务器从启动至今,服务了多少次请求。这里我只是简单使用一个维护在 memory 中的计数器来模拟服务请求个数。每次 metrics 被请求,计数器加一。
localhost:8081/metrics 测试一下:

打开 Prometheus 服务器的配置文件 prometheus.yml:

添加一条作业,static_configs 的 target 配置成我自己的服务器 localhost:8081.localhost:9090 访问 Prometheus 的 web UI:

切换到图形界面,即可看到随时间推移,我的服务器响应了服务请求的趋势图。横轴为时间点,纵轴为 Prometheus 服务器从我的测试服务器每隔默认的 15 秒收集到的服务响应请求。

SAP CRM 有个 Fiori 应用叫做 My Opportunity,是 SAP 成都研究院笔者所在的 CRM Fiori 团队开发维护的。假设我们有这样一个需求,需要监控在指定时间段内,该应用收到的读请求。

我们在 Opportunity OData 服务的实现里找到了一个 BAdI 增强,CRM_OPPORTUNITY_ODATA_BD:

所有读请求都会经过这个 BAdI,所以在里面实现我们自定义的日志逻辑很合适。

创建一个自定义数据库表,用于记录读请求的明细,包括请求者的用户名,请求日期和请求时间。

BAdI 实现的逻辑很简单,依次把字段记录下来,插入数据库表:

接着是在本地安装 Prometheus 服务器,我安装的是 Windows 版本,需要先编辑 prometheus.yml 配置文件,然后启动。

下图是配置文件最核心的部分,定义了 Prometheus 连接 SAP CRM 抓取监控数据的规则。第 24 行和 25 行维护了 SAP CRM 系统的用户名和密码,第 26 行/sap/zcm 是 CRM 上为 Prometheus 暴露出来的数据采集接口的路径,第 28 行指定 Prometheus 服务器每隔 2 秒钟采集一次数据。第 33 行定义了 CRM ABAP 系统的主机名和端口号。有了这些配置信息,Prometheus 可以同 ABAP Netweaver 服务器建立连接并进行周期性的数据抓取。

最后一步,在/sap/zcm 这个路径上把我们自定义数据库表里的数据暴露出来:

一个 SELECT COUNT(*)搞定:

至此万事俱备了。回到 Fiori 应用界面,随便点击几个 Opportunity,触发读请求,回到自定义的数据库表,发现已经有一些日志记录在内了。

启动 Prometheus 服务器,马上就以 2 秒的时间间隔,往 ABAP 服务器发起数据查询请求:

localhost:9090 打开 Prometheus 的控制台,能看到从 SAP CRM 系统实时采集到的读请求个数:

切换到 Graph 面板,能看到指定时间间隔内的读请求变化趋势,比如下图的横轴是时间点,纵轴是读请求个数,图上的折线表达了过去五分钟之内,读请求数量呈线性增长的趋势。

Prometheus 提供的 dashboard,提供了各种维度的数据查询和聚合功能。如果对其基本的数据展现界面不满意,可以选择另一款效果更好的开源数据可视化工具 Grafana. 下图是 Grafana 的 dashboard:

希望笔者这个例子可以给大家一些启发:ABAP 照样可以借助现代开源工具来实现一些传统 ABAP 工具难以实现的功能。在 Jerry 看来,Prometheus 完全可以同 ABAP 的单元测试框架一起协同工作,提高基于 ABAP 技术栈的应用开发的持续集成和持续交付能力。
总结
本文首先给出了开源监控工具 Prometheus 的基本概念,接着从使用 Prometheus 监控 Linux 系统各项指标和采集服务器自定义参数的需求实现出发,介绍了基于 Prometheus 的 SAP CRM ABAP 应用的监控数据采集方法。
版权声明: 本文为 InfoQ 作者【Jerry Wang】的原创文章。
原文链接:【http://xie.infoq.cn/article/5f55917c67f3853be7c2f7f2d】。文章转载请联系作者。
评论