bosun

Bosun 详解:https://bosun.org/expressions#data-types

 

回顾下时序数据库

> select * from "done.amount"
name: done.amount
time                _level host      lost_user new_user res_name value
----                ------ ----      --------- -------- -------- -----
1706462080262203600 hour   127.0.0.1 1         0        invite     1
1706462080270913500 hour   127.0.0.1 2         0        invite     2
1706462080277566600 hour   127.0.0.1 3         0        invite     3
1706462080282418900 hour   127.0.0.1 2         1        invite     1
1706462080287032700 hour   127.0.0.1 1         2        invite     2
1706462080291329400 hour   127.0.0.1 0         3        invite     3
1706462530356167900 hour   127.0.0.1 3         0        red_packet 100

// 其他维度,暂时不展示
1706462080291329400 day    127.0.0.1 0         3        invite     3
1706462080291329400 total  127.0.0.1 0         3        invite     3
对于 Tag {_level=hour,new_user=0} 求和,出来的结果应该是下面的总和
> select * from "done.amount" where new_user = '0' and _level = 'hour'
name: done.amount
time                _level host      lost_user new_user res_name value
----                ------ ----      --------- -------- -------- -----
1706462080262203600 hour   127.0.0.1 1         0        invite     1
1706462080270913500 hour   127.0.0.1 2         0        invite     2
1706462080277566600 hour   127.0.0.1 3         0        invite     3
1706462530356167900 hour   127.0.0.1 3         0        red_packet 100

// 结果
> select sum(value) from "done.amount" where new_user = '0' and _level = 'hour'
name: done.amount
time sum
---- ---
0    106
对于 Tag {_level=hour,new_user=0} 求最大值,出来的结果应该是下面的最大值
// 先看 filter 的结果
> select * from "done.amount" where new_user = '0' and _level = 'hour'
name: done.amount
time                _level host      lost_user new_user res_name value
----                ------ ----      --------- -------- -------- -----
1706462080262203600 hour   127.0.0.1 1         0        invite     1
1706462080270913500 hour   127.0.0.1 2         0        invite     2
1706462080277566600 hour   127.0.0.1 3         0        invite     3
1706462530356167900 hour   127.0.0.1 3         0        red_packet 100

// 求最大值结果
> select max(value) from "done.amount" where new_user = '0' and _level = 'hour'
name: done.amount
time                max
----                ---
1706462530356167900 100

降采样

// 先看 filter 的结果
> select * from "done.amount" where new_user = '0' and _level = 'hour' and time >= '2024-01-28T17:00:00Z' AND time < '2024-01-28T17:30:00Z'
name: done.amount
time                _level host      lost_user new_user res_name   value
----                ------ ----      --------- -------- --------   -----
1706461385018375000 hour   127.0.0.1 3         0        invite     0
1706462080262203600 hour   127.0.0.1 1         0        invite     1
1706462080270913500 hour   127.0.0.1 2         0        invite     2
1706462080277566600 hour   127.0.0.1 3         0        invite     3
1706462530356167900 hour   127.0.0.1 3         0        red_packet 100

// 2024-01-29 01:03:05
// 2024-01-29 01:14:40
// 2024-01-29 01:14:40
// 2024-01-29 01:14:40
// 2024-01-29 01:22:10

//  10min 降采样取 max
> select max(value) from "done.amount" where new_user = '0' and _level = 'hour' and time >= '2024-01-28T17:00:00Z' AND time < '2024-01-28T17:30:00Z' group by time(10m)
name: done.amount
time                max
----                ---
1706461200000000000 0
1706461800000000000 3
1706462400000000000 100

// 15min 降采样取 max
> select max(value) from "done.amount" where new_user = '0' and _level = 'hour' and time >= '2024-01-28T17:00:00Z' AND time < '2024-01-28T17:30:00Z' group by time(15m)
name: done.amount
time                max
----                ---
1706461200000000000 3
1706462100000000000 100

// 30min 降采样取 max
> select max(value) from "done.amount" where new_user = '0' and _level = 'hour' and time >= '2024-01-28T17:00:00Z' AND time < '2024-01-28T17:30:00Z' group by time(30m)
name: done.amount
time                max
----                ---
1706461200000000000 100

// 10min 降采样取 sum
> select sum(value) from "done.amount" where new_user = '0' and _level = 'hour' and time >= '2024-01-28T17:00:00Z' AND time < '2024-01-28T17:30:00Z' group by time(10m)
name: done.amount
time                sum
----                ---
1706461200000000000 0
1706461800000000000 6
1706462400000000000 100

  

query

1. 得到数量

q("count:30s-avg-zero:", "$start", "")

epoch number to seriers

$now = epoch()
$ago = $now-d("$start")
series("name=quota", $ago, $limit_qps, $now, $limit_qps)

left join

$metric_key = xxx
$qps_begin=max(q("sum:$metric_key{tag1=*,tag2=*}{}", "$start", "1m"))
// 两个减,得到一个 value 全是 0 的 seriers set,但是保持了所有 tags 组合
$qps_begin=$qps_begin-$qps_begin

$qps_0=max(q("sum:$metric_key{tag1=*,tag2=*}{percent=1}", "$start", "1m"))
$qps_1=max(q("sum:$metric_key{tag1=*,tag2=*}{percent=2}", "$start", "1m"))

// 加回去第一个,这样即使第一个没有 tags2,或者第一个直接是空,那也有一个默认的0值
$qps_begin=$qps_begin+$qps_0

// leftjoin,会以 $qps_begin 的 tags 为准
leftjoin("tag1,tag2", "percent_1,percent_2", $qps_begin,$qps_1)

 

posted on 2023-02-06 13:28  stupid_one  阅读(78)  评论(0编辑  收藏  举报

导航