一步步教你用Prometheus搭建实时监控系统系列(二)——详细分析拉取和推送两种不同模式 (2)

file

这个测试用例为意思是,推送一个指标aaa,标签为bbb=BBB,ccc=CCC,值为111.1到一个组上,这个组为job=pushgateway,instance=demo。

其实你可以简单的理解为这个指标aaa带有4个标签:job,instance,bbb,ccc。只是job和instance是属于组上的标签。

同一个组里的相同的指标,Prometheus每次只取最新的,不同组内可以有相同的指标。

关于数据结构和标签结构系列的下一篇文章会详细介绍。

总之,你提交这个POST请求后,可以在:9091上看到如下数据:

file

可以看到,aaa这个标签已经成功的被提交到Pushgateway里了。

接下来,我们在Prometheus里查询这个指标:

file

可以看到,Prometheus也成功的拉取到了这个指标。

Java端利用SDK进行推送

虽然我们在java服务端也能利用httpclient等工具进行提交,但是需要自行组装很多请求体。Prometheus官方提供了一个SDK。

首先在Maven中引入依赖包:

<dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_pushgateway</artifactId> <version>0.9.0</version> </dependency>

对Gauge,Timer,Counter,Summary四种常见的指标进行推送示例:

public void run(String... args) throws Exception { Gauge guage = Gauge.build("my_custom_metric", "This is my custom metric.") .labelNames("aaa","bbb").register(); Gauge.Child child = guage.labels("AAA","BBB"); child.set(334.5); Gauge timerGauge = Gauge.build("my_timer_metric","this is my timer metric.").register(); Gauge.Timer timer = timerGauge.startTimer(); Thread.sleep(3000L); Counter counter = Counter.build("my_count_metric","this is my count metric.").register(); counter.inc(); counter.inc(); Summary summary = Summary.build("my_summary_metric","this is my summary metric.").register(); summary.observe(45.6); summary.observe(54.5); String url = "xxx.xxx.xxx.xxx:9091"; PushGateway pg = new PushGateway(url); Map<String, String> groupingKey = new HashMap<>(); groupingKey.put("instance", "my_instance"); pg.pushAdd(CollectorRegistry.defaultRegistry, "my_job", groupingKey); }

这段代码演示了4个指标批量提交的场景。通过注册到CollectorRegistry.defaultRegistry里,最后一起pushAdd。

我们可以在Pushgateway里查询到提交的指标:

file

同样在Prometheus里也能查询到这4个指标,具体图示就不贴了。可以自己尝试下。

最后

这个系列旨在利用实战操作教你一步步搭建自己系统和业务监控大盘。后面会继续更新。下一个章节将分析:Prometheus中的数据格式分析以及PromQL的使用。

关注作者

如果你喜欢作者的文章,欢迎微信公众号关注 「元人部落」,一个只做原创的技术科技分享号

关注后回复“资料”获取50G的技术资料

file

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgggpx.html