totalRequests := prometheus.NewCounter(prometheus.CounterOpts{
Name: "http_requests_total",
Help: "The total number of handled HTTP requests.",
})
1
2
3
4
写入数据
totalRequests.Inc()
totalRequests.Add(23)
1
2
数据采集结果
# HELP http_requests_total The total number of handled HTTP requests.
# TYPE http_requests_total counter
http_requests_total 7734
1
2
3
Guage(仪表盘)
队列长度
queueLength := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "queue_length",
Help: "The number of items in the queue.",
})
1
2
3
4
写入数据
// Use Set() when you know the absolute value from some other source.
queueLength.Set(0)
// Use these methods when your code directly observes the increase or decrease of something, such as adding an item to a queue.
queueLength.Inc() // Increment by 1.
queueLength.Dec() // Decrement by 1.
queueLength.Add(23)
queueLength.Sub(42)
1
2
3
4
5
6
7
8
数据采集结果
# HELP queue_length The number of items in the queue.
# TYPE queue_length gauge
queue_length 42
1
2
3
Histogram(直方图)
采集HTTP请求处理时长直方图
requestDurations := prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "A histogram of the HTTP request durations in seconds.",
// Bucket configuration: the first bucket includes all requests finishing in 0.05 seconds, the last one includes all requests finishing in 10 seconds.
Buckets: []float64{0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10},
})
1
2
3
4
5
6
写入数据
requestDurations.Observe(0.42)
1
数据采集结果
# HELP http_request_duration_seconds A histogram of the HTTP request durations in seconds.
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{le="0.05"} 4599
http_request_duration_seconds_bucket{le="0.1"} 24128
http_request_duration_seconds_bucket{le="0.25"} 45311
http_request_duration_seconds_bucket{le="0.5"} 59983
http_request_duration_seconds_bucket{le="1"} 60345
http_request_duration_seconds_bucket{le="2.5"} 114003
http_request_duration_seconds_bucket{le="5"} 201325
http_request_duration_seconds_bucket{le="+Inf"} 227420
http_request_duration_seconds_sum 88364.234
http_request_duration_seconds_count 227420
1
2
3
4
5
6
7
8
9
10
11
12
Summary(摘要)
采集HTTP请求处理时长分布图
requestDurations := prometheus.NewSummary(prometheus.SummaryOpts{
Name: "http_request_duration_seconds",
Help: "A summary of the HTTP request durations in seconds.",
Objectives: map[float64]float64{
0.5: 0.05, // 50th percentile with a max. absolute error of 0.05.
0.9: 0.01, // 90th percentile with a max. absolute error of 0.01.
0.99: 0.001, // 99th percentile with a max. absolute error of 0.001.
},
},
)
1
2
3
4
5
6
7
8
9
10
写入数据
requestDurations.Observe(0.42)
1
数据采集结果
# HELP http_request_duration_seconds A summary of the HTTP request durations in seconds.
# TYPE http_request_duration_seconds summary
http_request_duration_seconds{quantile="0.5"} 0.052
http_request_duration_seconds{quantile="0.90"} 0.564
http_request_duration_seconds{quantile="0.99"} 2.372
http_request_duration_seconds_sum 88364.234
http_request_duration_seconds_count 227420
http_requests_total{job="prometheus"}[5m]
up{instance=~"192.168.1.12:9100"}[5m]
ms - milliseconds
s - seconds
m - minutes
h - hours
d - days - assuming a day has always 24h
w - weeks - assuming a week has always 7d
y - years - assuming a year has always 365d
- ignoring(code) 忽略code字段
- on (label list) 只匹配label list内的标签
1
2
3
查询每颗CPU下各个时间占比
sum by (instance,cpu) (node_cpu_seconds_total)
1
node_cpu_seconds_total / on (instance,cpu) group_left sum by (instance,cpu) (node_cpu_seconds_total)
1
各时间占比总CPU时间
sum by (instance,mode) (node_cpu_seconds_total) / ignoring (mode) group_left sum by (instance) (node_cpu_seconds_total)
1
8、聚合函数
聚合函数列表
- sum (计算维度的总和)
- min (选择最小尺寸)
- max (选择最大尺寸)
- avg (计算维度上的平均值)
- group (结果向量中的所有值都是 1)
- stddev (计算维度上的总体标准偏差)
- stdvar (计算维度上的总体标准方差)
- count (计算向量中元素的数量)
- count_values (计算具有相同值的元素数)
- bottomk (样本值的最小 k 个元素)
- topk (样本值最大的 k 个元素)
- quantile (在维度上计算 φ-分位数 (0 ≤ φ ≤ 1))
1
2
3
4
5
6
7
8
9
10
11
12
常规使用格式
[without|by ()] ([parameter,] )
1
count_values,quantile,topk和 bottomk`需要传入参数
([parameter,] ) [without|by ()]
1
9、各函数功能说明
调整值
abs(v instant-vector): 绝对值
ceil(): 上取整
clamp_max(v instant-vector, max scalar): 函数,输入一个瞬时向量和最大值,样本数据值若大于 max,则改为 max,否则不变。
clamp_min(v instant-vector, min scalar): 输入一个瞬时向量和最小值,样本数据值若小于 min,则改为 min,否则不变。
round(v instant-vector, to_nearest=1 scalar):与 ceil 和 floor 函数类似,返回向量中所有样本值的最接近的整数。
scalar(v instant-vector): 参数是一个单元素的瞬时向量,它返回其唯一的时间序列的值作为一个标量。
sqrt(v instant-vector): 计算向量 v 中所有元素的平方根。
vector(s scalar): 将标量 s 作为没有标签的向量返回,即返回结果为:key: value= {}, s。
floor(v instant-vector): 与 ceil() 函数相反,将 v 中所有元素的样本值向下四舍五入到最接近的整数。
1
2
3
4
5
6
7
8
9
时间相关
day_of_month(v=vector(time()) instant-vector): 返回被给定 UTC 时间所在月的第几天。返回值范围:1~31。
day_of_week(v=vector(time()) instant-vector): 返回被给定 UTC 时间所在周的第几天。返回值范围:0~6,0 表示星期天。
days_in_month(v=vector(time()) instant-vector): 返回当月一共有多少天。返回值范围:28~31。
hour(v=vector(time()) instant-vector): 返回被给定 UTC 时间的当前第几个小时,时间范围:0~23。
minute(v=vector(time()) instant-vector): 返回给定 UTC 时间当前小时的第多少分钟。结果范围:0~59。
month(v=vector(time()) instant-vector): 返回给定 UTC 时间当前属于第几个月,结果范围:0~12。
time(): 函数返回从 1970-01-01 到现在的秒数。
timestamp(v instant-vector): 返回向量 v 中的每个样本的时间戳(从 1970-01-01 到现在的秒数)。
year(v=vector(time()) instant-vector): 函数返回被给定 UTC 时间的当前年份。
1
2
3
4
5
6
7
8
9
排序
sort(v instant-vector): 对向量按元素的值进行升序排序,返回结果:key: value = 度量指标:样本值[升序排列]。
sort_desc(v instant-vector): 对向量按元素的值进行降序排序,返回结果:key: value = 度量指标:样本值[降序排列]。
1
2
其他
absent(v instant-vector): 判断瞬时向量如果有元素返回空向量,没有返回1,判断时间序列下是否有数据发送报警
absent_over_time(v range-vector): 判断范围向量
changes(v range-vector):范围向量发生变化的次数
resets(v range-vector): 参数是一个区间向量。对于每个时间序列,它都返回一个计数器重置的次数。两个连续样本之间的值的减少被认为是一次计数器重置。
delta(v range-vector): 参数是一个区间向量,返回一个瞬时向量。它计算一个区间向量 v 的第一个元素和最后一个元素之间的差值。由于这个值被外推到指定的整个时间范围,所以即使样本值都是整数,你仍然可能会得到一个非整数值。
histogram_quantile(φ float, b instant-vector): 从 bucket 类型的向量 b 中计算 φ (0 ≤ φ ≤ 1) 分位数(百分位数的一般形式)的样本的最大值。(有关 φ 分位数的详细说明以及直方图指标类型的使用,请参阅直方图和摘要)。向量 b 中的样本是每个 bucket 的采样点数量。每个样本的 labels 中必须要有 le 这个 label 来表示每个 bucket 的上边界,没有 le 标签的样本会被忽略。直方图指标类型自动提供带有 _bucket 后缀和相应标签的时间序列。
1
2
3
4
5
6
7
速率计算和线性回归
increase(v range-vector): 获取区间向量中的第一个和最后一个样本并返回其增长量.
irate(v range-vector): 用于计算区间向量的增长率,但是其反应出的是瞬时增长率。
rate(v range-vector): 计算范围向量中时间序列的每秒平均增长率。
predict_linear(v range-vector, t scalar): 可以预测时间序列 v 在 t 秒后的值。它基于简单线性回归的方式,对时间窗口内的样本数据进行统计,从而可以对时间序列的变化趋势做出预测。
deriv(v range-vector): 参数是一个区间向量,返回一个瞬时向量。它使用简单的线性回归计算区间向量 v 中各个时间序列的导数。
exp(v instant-vector): 输入一个瞬时向量,返回各个样本值的 e 的指数值,即 e 的 N 次方。当 N 的值足够大时会返回 +Inf