-
Notifications
You must be signed in to change notification settings - Fork 3
/
querier_logs_optimizer.go
191 lines (170 loc) · 4.52 KB
/
querier_logs_optimizer.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package chstorage
import (
"context"
"github.com/go-faster/oteldb/internal/logql"
"github.com/go-faster/oteldb/internal/logql/logqlengine"
)
// ClickhouseOptimizer replaces LogQL engine execution
// nodes with optimzied Clickhouse queries.
type ClickhouseOptimizer struct{}
var _ logqlengine.Optimizer = (*ClickhouseOptimizer)(nil)
// Name returns optimizer name.
func (o *ClickhouseOptimizer) Name() string {
return "ClickhouseOptimizer"
}
// Optimize implements [Optimizer].
func (o *ClickhouseOptimizer) Optimize(ctx context.Context, q logqlengine.Query) (logqlengine.Query, error) {
switch q := q.(type) {
case *logqlengine.LogQuery:
q.Root = o.optimizePipeline(q.Root)
case *logqlengine.MetricQuery:
if err := logqlengine.VisitNode(q.Root, func(n *logqlengine.SamplingNode) error {
n.Input = o.optimizePipeline(n.Input)
return nil
}); err != nil {
return nil, err
}
q.Root = o.optimizeSampling(q.Root)
}
return q, nil
}
func (o *ClickhouseOptimizer) optimizeSampling(n logqlengine.MetricNode) logqlengine.MetricNode {
switch n := n.(type) {
case *logqlengine.VectorAggregation:
switch n.Expr.Op {
case logql.VectorOpBottomk, logql.VectorOpTopk,
logql.VectorOpSort, logql.VectorOpSortDesc:
return n
}
labels, ok := getGroupByLabels(n.Expr.Grouping)
if !ok {
return n
}
rn, ok := n.Input.(*logqlengine.RangeAggregation)
if !ok {
return n
}
n.Input = o.buildRangeAggregationSampling(rn, labels)
return n
case *logqlengine.LabelReplace:
n.Input = o.optimizeSampling(n.Input)
return n
case *logqlengine.LiteralBinOp:
n.Input = o.optimizeSampling(n.Input)
return n
case *logqlengine.BinOp:
n.Left = o.optimizeSampling(n.Left)
n.Right = o.optimizeSampling(n.Right)
return n
default:
return n
}
}
func (o *ClickhouseOptimizer) buildRangeAggregationSampling(n *logqlengine.RangeAggregation, grouping []logql.Label) logqlengine.MetricNode {
if g := n.Expr.Grouping; g != nil {
return n
}
sampleNode, ok := n.Input.(*logqlengine.SamplingNode)
if !ok {
return n
}
// If it is possible to offload the pipeline to Clickhouse entirely
// preceding optimizer should replace node with [InputNode].
pipelineNode, ok := sampleNode.Input.(*InputNode)
if !ok {
return n
}
samplingOp, ok := getSamplingOp(n.Expr)
if !ok {
return n
}
n.Input = &SamplingNode{
Sampling: samplingOp,
GroupingLabels: grouping,
Labels: pipelineNode.Labels,
Line: pipelineNode.Line,
q: pipelineNode.q,
}
return n
}
func getGroupByLabels(g *logql.Grouping) ([]logql.Label, bool) {
if g == nil || g.Without || len(g.Labels) == 0 {
return nil, false
}
return g.Labels, true
}
func getSamplingOp(e *logql.RangeAggregationExpr) (op SamplingOp, _ bool) {
if er := e.Range; er.Unwrap != nil || er.Offset != nil {
return op, false
}
switch e.Op {
case logql.RangeOpCount:
return CountSampling, true
case logql.RangeOpBytes:
return BytesSampling, true
default:
return op, false
}
}
func (o *ClickhouseOptimizer) optimizePipeline(n logqlengine.PipelineNode) logqlengine.PipelineNode {
pn, ok := n.(*logqlengine.ProcessorNode)
if !ok {
return n
}
sn, ok := pn.Input.(*InputNode)
if !ok {
// NOTE(tdakkota): this should not happen as long
// as there is only one possible node made by storage.
return n
}
var (
line []logql.LineFilter
skippedStages int
)
stageLoop:
for _, stage := range pn.Pipeline {
switch stage := stage.(type) {
case *logql.LineFilter:
if !o.canOffloadLineFilter(stage) {
skippedStages++
continue
}
// TODO(tdakkota): remove stages from pipeline.
line = append(line, *stage)
case *logql.JSONExpressionParser,
*logql.LogfmtExpressionParser,
*logql.RegexpLabelParser,
*logql.PatternLabelParser,
*logql.LabelFilter,
*logql.LabelFormatExpr,
*logql.DropLabelsExpr,
*logql.KeepLabelsExpr,
*logql.DistinctFilter:
// Do nothing on line, just skip.
skippedStages++
case *logql.LineFormat,
*logql.DecolorizeExpr,
*logql.UnpackLabelParser:
// Stage modify the line, can't offload line filters after this stage.
skippedStages++
break stageLoop
}
}
sn.Line = line
// Replace original node with [InputNode], since we can execute filtering entirely in
// Clickhouse.
if skippedStages == 0 && !pn.EnableOTELAdapter {
return sn
}
return n
}
func (o *ClickhouseOptimizer) canOffloadLineFilter(lf *logql.LineFilter) bool {
switch lf.Op {
case logql.OpPattern, logql.OpNotPattern:
return false
}
if lf.By.IP || len(lf.Or) > 0 {
return false
}
return true
}