Prometheus 是一个开放性的监控解决方案,用户能够十分不便的装置和应用 Prometheus 并且可能十分不便的对其进行扩大。

在Prometheus的架构设计中,Prometheus Server 并不间接服务监控特定的指标,其次要工作负责数据的收集,存储并且对外提供数据查问反对。因而为了可能可能监控到某些货色,如主机的CPU使用率,咱们须要应用到Exporter。Prometheus周期性的从Exporter裸露的HTTP服务地址(通常是/metrics)拉取监控样本数据。

Exporter能够是一个绝对凋谢的概念,其能够是一个独立运行的程序独立于监控指标以外,也能够是间接内置在监控指标中。只有可能向Prometheus提供规范格局的监控样本数据即可。

1 环境配置

咱们在 Windows 下装置 Prometheus。

1.1 装置 Prometheus

下载地址:https://prometheus.io/download/

抉择 Windows 安装包,我抉择的是prometheus-2.41.0.windows-amd64, 下载实现后解压,间接运行 prometheus.exe 即可。

prometheus默认端口是9090,在浏览器拜访:http://localhost:9090,即可看到我的项目曾经在运行。

Prometheus 的相干配置能够在 prometheus.yaml 中批改。

1.2 装置 NodeExporter

NodeExporter 是 Prometheus 提供的一个能够采集到主机信息的应用程序,它能采集到机器的 CPU、内存、磁盘等信息。

下载地址: https://prometheus.io/download/

抉择 Windows 版本,我抉择的是windows_exporter-0.20.0-amd64,下载实现后间接运行 windows_exporter-0.20.0-amd64.exe 文件即可。

windows_exporter默认端口是9182,通过浏览器拜访:http://localhost:9182/metrics,

能够看到以后 node exporter 获取到的以后主机的所有监控数据。 其中 HELP 用于解释以后指标的含意,TYPE 则阐明以后指标的数据类型。

2 增加数据源

编辑 prometheus 的配置文件 prometheus.yml,将scrape_configs批改为如下内容:

scrape_configs:  - job_name: "prometheus"    static_configs:      - targets: ["localhost:9090"]        # node exporter 监控源  - job_name: 'prometheus2'    static_configs:      - targets: ['localhost:8080']

即配置了两个工作。一个名为 prometheus,其从「localhost:9090」地址读取数据。另一个名为 prometheus2,其从「localhost:8080」地址读取数据。 而后重启 Prometheus。

浏览器拜访:http://localhost:9090,在搜寻框输出up,点击execute,即可看到咱们配置的两个工作:

3 自定义写入的数据

新建 SpringBoot 我的项目。残缺我的项目地址:

GitHub地址:https://github.com/Snowstorm0...

Gitee地址:https://gitee.com/Snowstorm0/...

在 service 层编写插入数据的代码:

public void insertPrometheus() {    meterRegistry.clear();    setIdList();    setNameMap();    setValueMap();    for (String id : idList) {        List<Tag> list = new ArrayList<>();        list.add(Tag.of("id", id));        list.add(Tag.of("name", nameMap.get(id)));        String name = "insertPrometheus";        double value = Double.parseDouble(String.valueOf(valueMap.get(id)));        meterRegistry.gauge(name, Tags.of(list), value);    }}

在 controller 层编写读取的代码:

@RequestMapping(value = "/metric/custom", method = RequestMethod.GET,produces = "text/plain; charset=utf-8")public Object metric() {    return prometheusMeterRegistry.scrape();}

用浏览器或者Postman拜访: http://localhost:8081/metric/...

能够看到写入的数据:

# HELP insertPrometheus  # TYPE insertPrometheus gaugeinsertPrometheus{id="1002",name="钱二",} 1002.0insertPrometheus{id="1001",name="赵一",} 1001.0insertPrometheus{id="1003",name="孙三",} 1003.0

这里的数据是放在本地的,能够供 Prometheus 读取。

4 更新数据

在 service 层编写插入数据的代码:

public void updatePrometheus() {    String name = "updatePrometheus";    List<Tag> list = new ArrayList<>();    list.add(Tag.of("id", "1001"));    list.add(Tag.of("name", "测试更新"));    // 通过援用的形式将 Prometheus 的 value 存入 valueMap,批改 valueMap 即可批改 Prometheus    updateValueMap.put("1001", meterRegistry.gauge(name, Tags.of(list), new AtomicDouble(0)));    for (int value = 0; value < 12; value++) {        try {            updateValueMap.get("1001").set(value); //  批改 valueMap 中的 value            Thread.sleep(5 * 1000); // 暂停5秒        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

用浏览器或者Postman拜访: http://localhost:8081/metric/...

能够看到写入的数据:

updatePrometheus{id="1001",name="测试更新",} 1.0

 
 

学习更多编程常识,请关注我的公众号:

代码的路