-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathexplain.go
More file actions
112 lines (91 loc) · 2.78 KB
/
explain.go
File metadata and controls
112 lines (91 loc) · 2.78 KB
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
// Copyright (c) The Thanos Community Authors.
// Licensed under the Apache License 2.0.
package engine
import (
"sync"
"github.com/thanos-io/promql-engine/execution/model"
"github.com/thanos-io/promql-engine/execution/telemetry"
"github.com/prometheus/prometheus/promql"
)
type ExplainableQuery interface {
promql.Query
Explain() *ExplainOutputNode
Analyze() *AnalyzeOutputNode
}
type AnalyzeOutputNode struct {
OperatorTelemetry telemetry.OperatorTelemetry `json:"telemetry,omitempty"`
Children []*AnalyzeOutputNode `json:"children,omitempty"`
once sync.Once
totalSamples int64
peakSamples int64
totalSamplesPerStep []int64
}
type ExplainOutputNode struct {
OperatorName string `json:"name,omitempty"`
Children []ExplainOutputNode `json:"children,omitempty"`
}
var _ ExplainableQuery = &compatibilityQuery{}
func (a *AnalyzeOutputNode) TotalSamples() int64 {
a.aggregateSamples()
return a.totalSamples
}
func (a *AnalyzeOutputNode) TotalSamplesPerStep() []int64 {
a.aggregateSamples()
return a.totalSamplesPerStep
}
func (a *AnalyzeOutputNode) PeakSamples() int64 {
a.aggregateSamples()
return a.peakSamples
}
func (a *AnalyzeOutputNode) aggregateSamples() {
a.once.Do(func() {
if nodeSamples := a.OperatorTelemetry.Samples(); nodeSamples != nil {
a.totalSamples += nodeSamples.TotalSamples
a.peakSamples += int64(nodeSamples.PeakSamples)
a.totalSamplesPerStep = nodeSamples.TotalSamplesPerStep
}
for _, child := range a.Children {
childPeak := child.PeakSamples()
a.peakSamples = max(a.peakSamples, childPeak)
switch {
case a.OperatorTelemetry.IsSubquery():
// Skip aggregating samples for subquery
case a.OperatorTelemetry.IsStepInvariant():
childSamples := child.TotalSamples()
for i := range a.totalSamplesPerStep {
a.totalSamples += childSamples
a.totalSamplesPerStep[i] += childSamples
}
default:
a.totalSamples += child.TotalSamples()
for i, s := range child.TotalSamplesPerStep() {
a.totalSamplesPerStep[i] += s
}
}
}
})
}
func analyzeQuery(obsv telemetry.ObservableVectorOperator) *AnalyzeOutputNode {
children := obsv.Explain()
var childTelemetry []*AnalyzeOutputNode
for _, child := range children {
if obsChild, ok := child.(telemetry.ObservableVectorOperator); ok {
childTelemetry = append(childTelemetry, analyzeQuery(obsChild))
}
}
return &AnalyzeOutputNode{
OperatorTelemetry: obsv,
Children: childTelemetry,
}
}
func explainVector(v model.VectorOperator) *ExplainOutputNode {
vectors := v.Explain()
var children []ExplainOutputNode
for _, vector := range vectors {
children = append(children, *explainVector(vector))
}
return &ExplainOutputNode{
OperatorName: v.String(),
Children: children,
}
}