Loading [MathJax]/jax/input/TeX/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >使用Prometheus实现大规模的应用程序监视【Containers】

使用Prometheus实现大规模的应用程序监视【Containers】

作者头像
王欣壳
修改于 2019-11-11 03:06:08
修改于 2019-11-11 03:06:08
1.6K00
代码可运行
举报
运行总次数:0
代码可运行

我们有充分的理由证明Prometheus是一个日益流行的开源工具。开源工具可以为应用程序和服务器提供监视和警报。 Prometheus的强大优势在于监视服务器端指标,并将其存储为时间序列数据。尽管Prometheus并不适合于应用程序性能管理,主动控制或用户体验监视(尽管GitHub扩展确实使Prometheus可以使用用户浏览器指标),但Prometheus作为监视系统的能力是很强的,并且能够通过联盟实现高可扩展性服务器的数量使Prometheus成为各种使用案例的强大选择。

在本文中,我们将仔细研究Prometheus的体系结构和功能,然后研究该工具的详细实例。

Prometheus架构和组件

Prometheus由Prometheus服务器(通过PromQL查询语言处理服务发现,度量标准检索和存储以及时间序列数据分析),度量标准的数据模型,图形GUI和对Grafana的本机支持组成。还有一个可选的警报管理器,允许用户通过查询语言定义警报,以及一个可选的推送网关,用于短期应用程序监视。这些组件的位置如下图所示。

Prometheus可以通过使用代理在应用程序环境中执行通用代码来自动捕获标准指标。 它还可以通过检测来捕获自定义指标,将自定义代码放在受监视应用程序的源代码中。 Prometheus正式支持Go,Python,Ruby和Java / Scala的客户端库,还使用户能够编写自己的库。 此外,还有许多其他语言的非官方库。

开发人员还可以利用第三方出口商自动激活可能正在使用的许多流行软件解决方案的工具。例如,基于JVM的应用程序(例如开源Apache Kafka和Apache Cassandra)的用户可以利用现有的JMX导出器轻松收集指标。在其他情况下,将不需要导出程序,因为该应用程序将公开Prometheus格式的指标。 Cassandra上的用户可能还会发现Instaclustr的可免费获得的Prosmetheus Cassandra Exporter很有帮助,因为它将Cassandra指标从一个自管群集中集成到Prometheus应用程序监视中。

同样重要的是:开发人员可以利用可用的节点导出器来监视内核指标和主机硬件。 Prometheus还提供了Java客户端,具有许多功能,这些功能可以通过单个 DefaultExports.initialize()进行逐项注册或一次注册。命令包括内存池,垃圾回收,JMX,类加载和线程计数。

Prometheus数据建模和指标

Prometheus提供了四种度量标准类型:

  • 计数器:计算增量值;重新启动可以将这些值恢复为零
  • 量规:跟踪可以上升和下降的指标
  • 直方图:根据指定的响应大小或持续时间观察数据,并对观察值的总和以及可配置存储桶中的计数进行计数
  • 摘要:对类似于直方图的观察数据进行计数,并提供可配置的分位数,这些分位数在滑动时间窗口内计算

Prometheus时间序列数据度量标准每个都包含一个字符串名称,该名称遵循命名约定,以包括受监视数据主体的名称,逻辑类型和所使用的度量单位。每个度量标准都包括时间戳减少到毫秒的64位浮点值流,以及一组标注其测量尺寸的key:value对。 Prometheus会自动将Job和Instance标签添加到每个度量标准,以分别跟踪数据目标的已配置作业名称和已抓取目标URL的<host>:<port>段。

普罗米修斯的例子:the Anomalia Machina的异常检测试验

为了演示如何将Prometheus付诸实践并进行大规模的应用程序监视,让我们看一下我们最近在Instaclustr完成的实验性Anomalia Machina项目。这个项目只是一个测试用例,而不是商业上可用的解决方案,它在Kubernetes部署的应用程序中利用Kafka和Cassandra,该应用程序对流数据执行异常检测。 (这种检测对于包括IoT应用程序和数字广告欺诈在内的用例非常重要。)试验性应用程序在很大程度上依赖于Prometheus来收集分布式实例中的应用程序指标并使其易于查看。

此图显示了实验的体系结构:

我们利用Prometheus的目标包括监视应用程序的更通用指标,例如吞吐量,以及由Kafka负载生成器(Kafka生产者),Kafka使用者和负责检测应用程序中任何异常的Cassandra客户端提供的响应时间。 数据。 Prometheus还监视系统的硬件指标,例如运行该应用程序的每个AWS EC2实例的CPU。 该项目还依靠Prometheus来监视特定于应用程序的指标,例如每个Cassandra读取返回的总行数,以及至关重要的是,它检测到的异常数。 为了简化起见,所有这些监视都是集中的。

实际上,这意味着使用生产者,消费者和检测者方法以及以下三个指标形成测试管道:

  • 每次执行每个流水线级都不会发生意外时,称为prometheusTest_requests_total的计数器会增加,而级标签允许跟踪每个级的成功执行,而总标签则跟踪总流水线数量。
  • 另一个称为prometheusTest_anomalies_total的计数器衡量任何检测到的异常。
  • 最后,一个称为prometheusTest_duration_seconds的度量标准会跟踪每个阶段的持续时间(再次使用阶段标签和总标签)。

这些测量背后的代码使用inc()方法增加计数器指标,并使用setToTime()方法设置量表指标的时间值。 在以下带注释的示例代码中对此进行了演示:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import java.io.IOException;
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.exporter.HTTPServer;
import io.prometheus.client.hotspot.DefaultExports;
 
// https://github.com/prometheus/client_java
// Demo of how we plan to use Prometheus Java client to instrument Anomalia Machina.
// Note that the Anomalia Machina application will have Kafka Producer and Kafka consumer and rest of pipeline running in multiple separate processes/instances.
// So metrics from each will have different host/port combinations.
public class PrometheusBlog {  
static String appName = "prometheusTest";
// counters can only increase in value (until process restart)
// Execution count. Use a single Counter for all stages of the pipeline, stages are distinguished by labels
static final Counter pipelineCounter = Counter.build()
    .name(appName + "_requests_total").help("Count of executions of pipeline stages")
    .labelNames("stage")
    .register();
// in theory could also use pipelineCounter to count anomalies found using another label
// but less potential for confusion having another counter. Doesn't need a label
static final Counter anomalyCounter = Counter.build()
    .name(appName + "_anomalies_total").help("Count of anomalies detected")
    .register();
// A Gauge can go up and down, and is used to measure current value of some variable.
// pipelineGauge will measure duration in seconds of each stage using labels.
static final Gauge pipelineGauge = Gauge.build()
    .name(appName + "_duration_seconds").help("Gauge of stage durations in seconds")
    .labelNames("stage")
    .register();
 
public static void main(String[] args) {
// Allow default JVM metrics to be exported
   DefaultExports.initialize();
 
   // Metrics are pulled by Prometheus, create an HTTP server as the endpoint
   // Note if there are multiple processes running on the same server need to change port number.
   // And add all IPs and port numbers to the Prometheus configuration file.
HTTPServer server = null;
try {
server = new HTTPServer(1234);
} catch (IOException e) {
e.printStackTrace();
}
// now run 1000 executions of the complete pipeline with random time delays and increasing rate
int max = 1000;
for (int i=0; i < max; i++)
{
// total time for complete pipeline, and increment anomalyCounter
pipelineGauge.labels("total").setToTime(() -> {
producer();
consumer();
if (detector())
anomalyCounter.inc();
});
// total pipeline count
pipelineCounter.labels("total").inc();
System.out.println("i=" + i);
 
// increase the rate of execution
try {
Thread.sleep(max-i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
server.stop();
}
// the 3 stages of the pipeline, for each we increase the stage counter and set the Gauge duration time
public  static void producer() {
class Local {};
String name = Local.class.getEnclosingMethod().getName();
pipelineGauge.labels(name).setToTime(() -> {
try {
Thread.sleep(1 + (long)(Math.random()*20));
} catch (InterruptedException e) {
e.printStackTrace();
}
});
pipelineCounter.labels(name).inc();
   }
public  static void consumer() {
class Local {};
String name = Local.class.getEnclosingMethod().getName();
pipelineGauge.labels(name).setToTime(() -> {
try {
Thread.sleep(1 + (long)(Math.random()*10));
} catch (InterruptedException e) {
e.printStackTrace();
}
});
pipelineCounter.labels(name).inc();
   }
// detector returns true if anomaly detected else false
public  static boolean detector() {
class Local {};
String name = Local.class.getEnclosingMethod().getName();
pipelineGauge.labels(name).setToTime(() -> {
try {
Thread.sleep(1 + (long)(Math.random()*200));
} catch (InterruptedException e) {
e.printStackTrace();
}
});
pipelineCounter.labels(name).inc();
return (Math.random() > 0.95);
   }
}

Prometheus通过轮询(“抓取”)检测到的代码来收集指标(与其他一些通过推送方法接收指标的监视解决方案不同)。 上面的代码示例在端口1234上创建了一个必需的HTTP服务器,以便Prometheus可以根据需要抓取度量标准。 以下示例代码解决了Maven依赖项:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<!-- The client -->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>LATEST</version>
</dependency>
<!-- Hotspot JVM metrics-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>LATEST</version>
</dependency>
<!-- Exposition HTTPServer-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_httpserver</artifactId>
<version>LATEST</version>
</dependency>
<!-- Pushgateway exposition-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_pushgateway</artifactId>
<version>LATEST</version>
</dependency>

下面的代码示例告诉Prometheus应该在哪里寻找指标。 只需将这些代码添加到配置文件(默认值:Prometheus.yml)中,即可进行基本部署和测试。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
global:
 scrape_interval:    15s # By default, scrape targets every 15 seconds.
 
# scrape_configs has jobs and targets to scrape for each.
scrape_configs:
# job 1 is for testing prometheus instrumentation from multiple application processes.
 # The job name is added as a label job=<job_name> to any timeseries scraped from this config.
 - job_name: 'testprometheus'
 
   # Override the global default and scrape targets from this job every 5 seconds.
   scrape_interval: 5s
   
   # this is where to put multiple targets, e.g. for Kafka load generators and detectors
   static_configs:
     - targets: ['localhost:1234', 'localhost:1235']
     
 # job 2 provides operating system metrics (e.g. CPU, memory etc).
 - job_name: 'node'
 
  # Override the global default and scrape targets from this job every 5 seconds.
   scrape_interval: 5s
   
   static_configs:
     - targets: ['localhost:9100']

注意此配置文件中使用端口9100的名为“ node”的作业;此作业提供了节点指标,并且需要在运行应用程序的同一台服务器上运行Prometheus节点导出器。度量指标的轮询应格外小心:过于频繁地执行可能会使应用程序过载,而过于频繁地执行则会导致延迟。在无法轮询应用程序指标的地方,Prometheus还提供了一个推送网关。

查看Prometheus指标和结果

我们的实验最初使用表达式,后来使用Grafana来可视化数据并克服Prometheus缺少默认仪表板的问题。使用Prometheus界面(或http:// localhost:9090 / metrics),按名称选择指标,然后在表达式框中输入它们以执行。 (请注意,在此阶段通常会遇到错误消息,因此,如果遇到一些问题,请不要气.。)使用正确运行的表达式,结果将可以适当地显示在表格或图形中。

在计数器指标上使用irate或rate函数将产生有用的比率图:

这是一个量规指标的类似图形:

Grafana提供了更强大的图形功能和内置的Prometheus支持,其中的图形能够显示多个指标:

要启用Grafana,请安装它,导航到http://localhost:3000,创建Prometheus数据源,然后使用表达式添加Prometheus图。 此处需要注意:空图表通常指向时间范围问题,通常可以使用“最近5分钟”设置来解决。

创建此实验应用程序提供了极好的机会,使我们了解Prometheus的功能,并开发出了大规模的实验生产应用程序,该应用程序可以每天监控190亿次实时数据事件的异常情况。 通过遵循本指南和我们的示例,希望更多的开发人员可以成功地将Prometheus付诸实践。

本文系外文翻译,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系外文翻译,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
docker安装prometheus
安装基于docker的prometheus来监控java微服务的各项目性能指标。 使用docker来安装最简单省事。
潇洒
2023/10/20
8240
docker安装prometheus
使用Java与Prometheus集成
本文将详细阐述如何使用java实现Prometheus的集成。主要内容会包括Prometheus的Java客户端库使用,以及如何将Prometheus metrics export到Prometheus Server。
很酷的站长
2023/09/21
1.5K0
使用Java与Prometheus集成
prometheus学习笔记(2)-利用java client写入数据
继续学习prometheus,上一节演示了用http方式使用curl向pushgateway发送数据,本节将研究如何利用client jar包,以java代码的方式写入数据。
菩提树下的杨过
2020/09/22
6.5K0
Grafana+Prometheus系统监控之SpringBoot
前言 前一段时间使用SpringBoot创建了一个webhook项目,由于近期项目中也使用了不少SpringBoot相关的项目,趁着周末,配置一下使用prometheus监控微服务Springboot。 项目配置 引入坐标 <!-- Exposition spring_boot --> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_spring_boot</artifactId> <ve
小柒2012
2018/04/13
3.9K0
Grafana+Prometheus系统监控之SpringBoot
Grafana+Prometheus系统监控之SpringBoot
前一段时间使用SpringBoot创建了一个webhook项目,由于近期项目中也使用了不少SpringBoot相关的项目,趁着周末,配置一下使用prometheus监控微服务Springboot。
小柒2012
2019/12/09
5150
Grafana+Prometheus系统监控之SpringBoot
springmvc老旧系统prometheus 暴漏端点改造
springmvc 老破旧系统如何更好的接入prometheus+grafana的怀抱呢
西门呀在吹雪
2021/11/24
4.5K0
springmvc老旧系统prometheus 暴漏端点改造
从 Prometheus 到 OpenTelemetry: 指标监控的演进与实践
在上一篇:从 Dapper 到 OpenTelemetry:分布式追踪的演进之旅我们讲解了 Trace 的一些核心概念:
crossoverJie
2024/06/18
1K0
Prometheus 使用 PushGateway 进行数据上报采集
本次演示环境,我是在虚拟机上安装 Linux 系统来执行操作,以下是安装的软件及版本:
哎_小羊
2019/11/03
29.3K0
Spring Boot服务监控(Prometheus)
我生命里的的最大突破之一,就是我不再为别人的看法而担忧。此后,我真的能自由的去做我认为对自己最好的事,只有在我们不需要外来的赞许时,才变得自由。
凯哥的Java技术活
2022/07/08
7190
Spring Boot服务监控(Prometheus)
用prometheus监控java应用
2. http://192.168.1.208:6060 可以看到metrics的信息。
山行AI
2019/09/05
9.6K1
用prometheus监控java应用
Spring Boot Actuator详解与深入应用(三):Prometheus+Grafana应用监控
本文系《Spring Boot Actuator详解与深入应用》中的第三篇。在前两篇文章,我们主要讲了Spring Boot Actuator 1.x与 2.x 的应用与定制端点。相比于Actuator 1.x,基于Spring Boot 2.0的Actuator 2.x 在使用和定制方面有很大变化,对于Actuator的扩展也更加灵活。建议读者重点关注一下Actuator 2.x,关于Spring Boot 2.x流行的趋势是显而易见的。
aoho求索
2018/12/19
2.5K0
Spring Boot Actuator详解与深入应用(三):Prometheus+Grafana应用监控
Prometheus + Grafana 监控 SpringBoot
Prometheus 是监控系统,可以从 Springboot 获取监控数据,以时序数据的形式存储,并提供了监控数据的查询服务。
dys
2020/02/19
2K0
Spring Boot + Prometheus + Grafana 打造可视化监控,一目了然!
点击关注公众号,Java干货及时送达 作者:烟味i 链接:https://www.cnblogs.com/2YSP/p/12827487.html 一、背景 Spring Boot 的应用监控方案比较多,SpringBoot + Prometheus + Grafana 是目前比较常用的方案之一。 它们三者之间的关系大概如下图: 关系图 二、开发SpringBoot应用 首先,创建一个SpringBoot项目,pom文件如下: <dependency>     <groupId>org.springfr
Java技术栈
2022/03/15
6950
Spring Boot 应用可视化监控
利用账号密码访问 http://localhost:8080/application/prometheus ,可以看到 Prometheus 格式的指标数据
二哥聊运营工具
2021/12/17
3850
Spring Boot 应用可视化监控
Prometheus为你的微服务保驾护航
前面我们介绍了 Prometheus 的作用和整体的架构,相信大家对 Prometheus 有了一定的了解。
猿天地
2021/02/05
6880
Prometheus为你的微服务保驾护航
SpringBoot掌握的差不多了,就剩下一个Actuator没搞定了,本文详细来介绍!!!
  通过前面的介绍我们明白了SpringBoot为什么能够很方便快捷的构建Web应用,那么应用部署上线后的健康问题怎么发现呢?在SpringBoot中给我们提供了Actuator来解决这个问题。
用户4919348
2021/09/08
1.6K0
SpringBoot掌握的差不多了,就剩下一个Actuator没搞定了,本文详细来介绍!!!
彻底搞懂监控系统,使用Prometheus监控Spring Boot应用,自定义应用监控指标!
前面我们介绍了使用Prometheus + Grafana 构建了监控系统,那么我们的应用平台怎么监控呢?应用平台中的核心业务的执行情况能否监控呢?那么接下来我们使用Actuator,Micrometer,Prometheus和Grafana监控Spring Boot应用程序,自定义应用监控指标。
架构师精进
2023/03/23
14.7K2
彻底搞懂监控系统,使用Prometheus监控Spring Boot应用,自定义应用监控指标!
使用 Prometheus 来监控你的应用程序
Prometheus 是一个开源的系统监控和警报工具,最初由 SoundCloud 开发,并于 2012 年发布为开源项目。它是一个非常强大和灵活的工具,用于监控应用程序和系统的性能,并根据预定义的规则触发警报。以下是对 Prometheus 的详细介绍:
孟斯特
2023/09/25
5640
使用 Prometheus 来监控你的应用程序
快来试试 Spring Boot 应用可视化监控,一目了然!
利用账号密码访问 http://localhost:8080/application/prometheus ,可以看到 Prometheus 格式的指标数据
好好学java
2021/03/29
2980
快来试试 Spring Boot 应用可视化监控,一目了然!
Spring Boot + Prometheus + Grafana 打造可视化监控,一目了然!
点击上方“芋道源码”,选择“设为星标” 管她前浪,还是后浪? 能浪的浪,才是好浪! 每天 10:33 更新文章,每天掉亿点点头发... 源码精品专栏 原创 | Java 2021 超神之路,很肝~ 中文详细注释的开源项目 RPC 框架 Dubbo 源码解析 网络应用框架 Netty 源码解析 消息中间件 RocketMQ 源码解析 数据库中间件 Sharding-JDBC 和 MyCAT 源码解析 作业调度中间件 Elastic-Job 源码解析 分布式事务中间件 TCC-Transaction
芋道源码
2022/04/19
1.3K0
Spring Boot + Prometheus + Grafana 打造可视化监控,一目了然!
推荐阅读
相关推荐
docker安装prometheus
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验