/
logql.go
135 lines (119 loc) · 3.71 KB
/
logql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package metrics
import (
"fmt"
"github.com/onsi/gomega/gmeasure"
"github.com/prometheus/common/model"
)
const (
LogQLAnnotation = gmeasure.Annotation("logql")
)
func LogQLQueryRate(duration model.Duration) Measurement {
return Measurement{
Name: "LogQL query rate",
Query: fmt.Sprintf(
`sum(rate(logql_query_duration_seconds_count[%s]))`,
duration,
),
Unit: QueriesPerSecondUnit,
Annotation: LogQLAnnotation,
}
}
func LogQLQueryDurationAverage(duration model.Duration) Measurement {
numerator := fmt.Sprintf(
`sum(rate(logql_query_duration_seconds_sum[%s]))`,
duration,
)
denomintator := fmt.Sprintf(
`sum(rate(logql_query_duration_seconds_count[%s]))`,
duration,
)
return Measurement{
Name: "LogQL query duration avg",
Query: fmt.Sprintf(`(%s / %s) * %d`, numerator, denomintator, SecondsToMillisecondsMultiplier),
Unit: MillisecondsUnit,
Annotation: LogQLAnnotation,
}
}
func LogQLQueryDurationQuantile(percentile int, duration model.Duration) Measurement {
return Measurement{
Name: fmt.Sprintf("LogQL query duration P%d", percentile),
Query: fmt.Sprintf(
`histogram_quantile(0.%d, sum by (le) (rate(logql_query_duration_seconds_bucket[%s]))) * %d`,
percentile, duration, SecondsToMillisecondsMultiplier,
),
Unit: MillisecondsUnit,
Annotation: LogQLAnnotation,
}
}
func LogQLQueryLatencyAverage(
code, pod string,
duration model.Duration,
annotation gmeasure.Annotation,
) Measurement {
numerator := fmt.Sprintf(
`sum(rate(loki_logql_querystats_latency_seconds_sum{pod=~"%s.*", status_code=~"%s"}[%s]))`,
pod, code, duration,
)
denomintator := fmt.Sprintf(
`sum(rate(loki_logql_querystats_latency_seconds_count{pod=~"%s.*", status_code=~"%s"}[%s]))`,
pod, code, duration,
)
return Measurement{
Name: "LogQL query latency avg",
Query: fmt.Sprintf(`(%s / %s) * %d`, numerator, denomintator, SecondsToMillisecondsMultiplier),
Unit: MillisecondsUnit,
Annotation: annotation,
}
}
func LogQLQueryLatencyQuantile(
code, pod string,
percentile int,
duration model.Duration,
annotation gmeasure.Annotation,
) Measurement {
return Measurement{
Name: fmt.Sprintf("LogQL query latency P%d", percentile),
Query: fmt.Sprintf(
`histogram_quantile(0.%d, sum by (job, le) (rate(loki_logql_querystats_latency_seconds_bucket{pod=~"%s.*", status_code=~"%s"}[%s]))) * %d`,
percentile, pod, code, duration, SecondsToMillisecondsMultiplier,
),
Unit: MillisecondsUnit,
Annotation: annotation,
}
}
func LogQLQueryMBpSProcessedAverage(
code, pod string,
duration model.Duration,
annotation gmeasure.Annotation,
) Measurement {
numerator := fmt.Sprintf(
`sum(rate(loki_logql_querystats_bytes_processed_per_seconds_sum{pod=~"%s.*", status_code=~"%s"}[%s]))`,
pod, code, duration,
)
denomintator := fmt.Sprintf(
`sum(rate(loki_logql_querystats_bytes_processed_per_seconds_count{pod=~"%s.*", status_code=~"%s"}[%s]))`,
pod, code, duration,
)
return Measurement{
Name: "LogQL query MBps processed avg",
Query: fmt.Sprintf(`(%s / %s) / %d`, numerator, denomintator, BytesToMegabytesMultiplier),
Unit: MegabytesPerSecondUnit,
Annotation: annotation,
}
}
func LogQLQueryMBpSProcessedQuantile(
code, pod string,
percentile int,
duration model.Duration,
annotation gmeasure.Annotation,
) Measurement {
return Measurement{
Name: fmt.Sprintf("LogQL query MBps processed P%d", percentile),
Query: fmt.Sprintf(
`histogram_quantile(0.%d, sum by (job, le) (rate(loki_logql_querystats_bytes_processed_per_seconds_bucket{pod=~"%s.*", status_code=~"%s"}[%s]))) / %d`,
percentile, pod, code, duration, BytesToMegabytesMultiplier,
),
Unit: MegabytesPerSecondUnit,
Annotation: annotation,
}
}