当前位置:网站首页>Expose Prometheus metrics in Perl programs
Expose Prometheus metrics in Perl programs
2022-07-27 07:29:00 【TechForGeek】
SLI And SLO
SLI( Service quality indicators ) It refers to a specific quantitative indicator of the quality of a service ( If the request is delayed 、 Error rate 、QPS).
SLO( Service quality objectives ) Is a service SLI The target value , for example , We can define one SLO, requirement 95% The delay of the request of is less than 100ms.
SLI And SLO It is the transformation of traditional operation and maintenance SRE A topic that cannot be bypassed , They are SRE The core of practice . Traditional operation and maintenance usually pursues 100% The usability of , But this goal is impossible , By implementing SLO, It can bring the following benefits :
Monitor application behavior , In order to better improve the application
Prioritize your work , That is, developing new features or ensuring service stability
Use as a means of control , By comparison SLI And SLO, Decide whether to perform an operation
Establish user expectations
Definition SLI
To implement SLO, First, define SLI, and Prometheus Monitoring is the definition SLI A good tool for , The following will be introduced in Perl Exposed in the program Prometheus Metric To define SLI.
stay Perl Exposed in the program Prometheus Metric
stay Perl Exposed in the program Prometheus Metric, We use two modules :
Dancer2 [1]:Perl It's a lightweight Web Application framework .
Net::Prometheus [2]: Perl Of Prometheus Client library
With the help of these two modules , We are Perl Statistics in the application '/login' Number of calls to the interface :
#!/usr/bin/perl
use strict;
use warnings;
use Dancer2;
use Net::Prometheus;
# Use Time::HiRes Module time Function to get milliseconds Epoch Time
use Time::HiRes qw/time/;
set host => '0.0.0.0';
set port => 3678;
# initialization Prometheus client
my $client = Net::Prometheus->new;
# Set up counter Type of metric, And designate metric Of name, help, labels Information
my $counter = $client->new_counter(
name => "requests_count",
help => "Number of received requests",
labels => ['path']
);
post '/login' => sub {
# counter Add 1, Set up label by path=login
# $counter->inc({path => 'login'}); or
$counter->inc(('login'));
# do something else
};
get '/metrics' => sub {
if(request->user_agent =~ /curl|prometheus/i) {
$client->render
} else {
"<pre>" . $client->render . "</pre>"
}
};Run the top Perl Program , And open the browser , Enter the address :
http://SERVER-IP:3678/metricsamong ,SERVER-IP by Perl The server where the program is located IP.
After the browser opens the above address , You can see the output of Prometheus metrics 了 :
# HELP perl_heap_arenas Number of arenas in the Perl heap
# TYPE perl_heap_arenas gauge
perl_heap_arenas 713
# HELP perl_heap_svs Number of SVs in the Perl heap
# TYPE perl_heap_svs gauge
perl_heap_svs 120441
...
# HELP requests_count Number of received requests
# TYPE requests_count counterhowever , We don't see what we define requests_count indicators , I only see the annotation information of this indicator :
# HELP requests_count Number of received requests
# TYPE requests_count counterThis is because '/login' This interface has not been accessed , Try to visit '/login' After this interface , Refresh the browser and check again Prometheus Metrics You can see the indicators we defined :
# HELP requests_count Number of received requests
# TYPE requests_count counter
requests_count{path="login"} 1Come here , We have been successful for our Perl The application is exposed Prometheus Metrics.
With Counter Indicators of type requests_count, We can use Prometheus Of rate() function [3] To calculate access '/login' Interface QPS, So we have a definition of SLI.
Net::Prometheus In addition to supporting counter Type of metric, And support gauge,summary,histogram Type of metric, The methods of use are similar , For detailed instructions, please refer to Net::Prometheus Documents [2].
Reference resources
[1]: https://metacpan.org/pod/Dancer2
[2]: https://metacpan.org/pod/Net::Prometheus
[3]: https://prometheus.io/docs/prometheus/latest/querying/functions/#rate
As always, , If you have any questions about the content of the article , Or find any mistakes in the article , You can tell me by leaving a message ; If you like my article , Welcome to my WeChat official account. Tech For Geek.

边栏推荐
- (2022杭电多校三)1011.Taxi(曼哈顿最值+二分)
- Use shell to calculate the sum of numbers in text
- (2022 Niuke multi school III) a-ancestor (LCA)
- Please ask the big guys a question. The pgsqlcdc task can't monitor changes after running for a period of time. Just restart it. What should I do
- Use of tigervnc
- Functools module
- 【golang学习笔记2.1】 golang中的数组中的排序和查找
- Overall dichotomy?
- tigervnc的使用
- oracle的触发器的使用举例
猜你喜欢

C# Winfrom 常用功能整合-2

(2022 Hangdian multi school III) 1009.package delivery (greedy)

Synchronized锁

Single arm routing (explanation + experiment)

Essay: college entrance examination

Jmeter:接口自动化测试-BeanShell对数据库数据和返回数据比较

ClickHouse 笔记1 | 简介、特点 | 基于CentOS7系统的安装与使用 | 常用数据类型 | MergeTree 表引擎 | SQL操作

(2022 Niuke multi school III) j-journey (Dijkstra)

js做一个红绿灯

【QT】capture.obj:-1: error: LNK2019: 无法解析的外部符号 __imp_htons(解决方法)
随机推荐
Single arm routing (explanation + experiment)
SQLite 常用功能整合
Binary tree -- natural search semantics (1) Basics
35. Search insert position
Jmeter:接口自动化测试-BeanShell对数据库数据和返回数据比较
Array method and loop in JS
[Vani有约会]雨天的尾巴
单元测试系统化讲解之Mockito
Gossip: Recently, many friends talk about going abroad
Flutter实战-请求封装(一)
Firefox browser, when accessing Tencent cloud server, failed to establish a secure connection.
js做一个红绿灯
C程序代码的内存结构分析
Overall dichotomy?
How to submit C4d animation to cloud rendering farm for fast rendering?
查看服务器重启前的 dmesg 日志
Use shell to calculate the sum of numbers in text
Pg_relation_size 问题
C4D动画如何提交云渲染农场快速渲染?
ADB instruction sorting