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

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

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

Prometheus架构和组件

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

使用Prometheus实现大规模的应用程序监视_第1张图片

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会自动将JobInstance标签添加到每个度量标准,以分别跟踪数据目标的已配置作业名称和已刮取目标URL的段。

普罗米修斯的例子:异常Machina异常检测实验

在进入示例之前,请按照此入门指南下载并开始使用开源Prometheus。

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

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

使用Prometheus实现大规模的应用程序监视_第2张图片

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

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

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

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


     
     
     
     
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依赖项:


     
     
     
     

你可能感兴趣的:(使用Prometheus实现大规模的应用程序监视)