Skip to content

Commit a278837

Browse files
committed
adjust prom request logicalplan type and update tests
Signed-off-by: rubywtl <[email protected]>
1 parent 9ab2c12 commit a278837

10 files changed

+166
-241
lines changed

pkg/querier/tripperware/instantquery/instant_query.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ func (c instantQueryCodec) getSerializedBody(promReq *tripperware.PrometheusRequ
147147
var byteLP []byte
148148
var err error
149149

150-
if promReq.LogicalPlan != nil && *promReq.LogicalPlan != nil {
151-
byteLP, err = logicalplan.Marshal((*promReq.LogicalPlan).Root())
150+
if promReq.LogicalPlan != nil {
151+
byteLP, err = logicalplan.Marshal(promReq.LogicalPlan.Root())
152152
if err != nil {
153153
return nil, err
154154
}

pkg/querier/tripperware/instantquery/instant_query_middlewares_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func TestRoundTripWithDistributedExec(t *testing.T) {
197197
require.NotEmpty(t, body)
198198
require.NoError(t, err)
199199

200-
byteLP, err := logicalplan.Marshal((*tc.pReq.LogicalPlan).Root())
200+
byteLP, err := logicalplan.Marshal(tc.pReq.LogicalPlan.Root())
201201
require.NoError(t, err)
202202
require.Equal(t, byteLP, body)
203203

pkg/querier/tripperware/instantquery/logical_plan_gen_instant_test.go

Lines changed: 0 additions & 114 deletions
This file was deleted.
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package tripperware
2+
3+
import (
4+
"context"
5+
"github.com/stretchr/testify/require"
6+
"strconv"
7+
"testing"
8+
)
9+
10+
// TestInstantLogicalPlan ensures that the instant logical plan generation middleware
11+
// correctly produces a logical plan and insert it into the Prometheus request body.
12+
13+
func TestInstantLogicalPlan(t *testing.T) {
14+
for i, tc := range []struct {
15+
name string
16+
input *PrometheusRequest
17+
err error
18+
}{
19+
{
20+
name: "rate vector selector",
21+
input: &PrometheusRequest{
22+
Start: 100000,
23+
End: 100000,
24+
Query: "rate(node_cpu_seconds_total{mode!=\"idle\"}[5m])",
25+
},
26+
err: nil,
27+
},
28+
{
29+
name: "memory usage expression",
30+
input: &PrometheusRequest{
31+
Start: 100000,
32+
End: 100000,
33+
Query: "100 * (1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes))",
34+
},
35+
err: nil,
36+
},
37+
{
38+
name: "scalar only query",
39+
input: &PrometheusRequest{
40+
Start: 100000,
41+
End: 100000,
42+
Query: "42",
43+
},
44+
err: nil,
45+
},
46+
{
47+
name: "vector arithmetic",
48+
input: &PrometheusRequest{
49+
Start: 100000,
50+
End: 100000,
51+
Query: "node_load1 / ignoring(cpu) node_cpu_seconds_total",
52+
},
53+
err: nil,
54+
},
55+
{
56+
name: "avg_over_time with nested rate",
57+
input: &PrometheusRequest{
58+
Start: 100000,
59+
End: 100000,
60+
Query: "avg_over_time(rate(http_requests_total[5m])[30m:5m])",
61+
},
62+
err: nil,
63+
},
64+
} {
65+
tc := tc
66+
t.Run(strconv.Itoa(i), func(t *testing.T) {
67+
t.Parallel()
68+
69+
lpm := LogicalPlanGenMiddleware()
70+
handler := lpm.Wrap(HandlerFunc(func(_ context.Context, req Request) (Response, error) {
71+
return nil, nil
72+
}))
73+
74+
// Test: Execute middleware to populate the logical plan
75+
_, err := handler.Do(context.Background(), tc.input)
76+
require.NoError(t, err)
77+
require.NotEmpty(t, tc.input.LogicalPlan, "prom request should not be empty")
78+
})
79+
}
80+
}
81+
82+
// TestRangeLogicalPlan validates the range logical plan generation middleware.
83+
func TestRangeLogicalPlan(t *testing.T) {
84+
testCases := []struct {
85+
name string
86+
input *PrometheusRequest
87+
}{
88+
{
89+
name: "rate vector over time",
90+
input: &PrometheusRequest{
91+
Start: 100000,
92+
End: 200000,
93+
Step: 15000,
94+
Query: "rate(node_cpu_seconds_total{mode!=\"idle\"}[5m])",
95+
},
96+
},
97+
{
98+
name: "memory usage ratio",
99+
input: &PrometheusRequest{
100+
Start: 100000,
101+
End: 200000,
102+
Step: 30000,
103+
Query: "100 * (1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes))",
104+
},
105+
},
106+
{
107+
name: "avg_over_time function",
108+
input: &PrometheusRequest{
109+
Start: 100000,
110+
End: 200000,
111+
Step: 60000,
112+
Query: "avg_over_time(http_requests_total[5m])",
113+
},
114+
},
115+
{
116+
name: "vector arithmetic with range",
117+
input: &PrometheusRequest{
118+
Start: 100000,
119+
End: 200000,
120+
Step: 10000,
121+
Query: "rate(node_network_receive_bytes_total[1m]) / rate(node_network_transmit_bytes_total[1m])",
122+
},
123+
},
124+
{
125+
name: "simple scalar operation",
126+
input: &PrometheusRequest{
127+
Start: 100000,
128+
End: 200000,
129+
Step: 15000,
130+
Query: "2 + 2",
131+
},
132+
},
133+
}
134+
135+
for i, tc := range testCases {
136+
tc := tc
137+
t.Run(strconv.Itoa(i)+"_"+tc.name, func(t *testing.T) {
138+
t.Parallel()
139+
140+
middleware := LogicalPlanGenMiddleware()
141+
142+
handler := middleware.Wrap(HandlerFunc(func(_ context.Context, req Request) (Response, error) {
143+
return nil, nil
144+
}))
145+
146+
// Test: Execute middleware to populate the logical plan
147+
_, err := handler.Do(context.Background(), tc.input)
148+
require.NoError(t, err)
149+
require.NotEmpty(t, tc.input.LogicalPlan, "logical plan should be populated")
150+
151+
})
152+
}
153+
}

pkg/querier/tripperware/logicalplan_gen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (l logicalPlanGen) Do(ctx context.Context, r Request) (Response, error) {
8383
return nil, err
8484
}
8585

86-
promReq.LogicalPlan = newLogicalPlan
86+
promReq.LogicalPlan = *newLogicalPlan
8787

8888
return l.next.Do(ctx, r)
8989
}

pkg/querier/tripperware/query.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ type Request interface {
9797
GetStep() int64
9898
// GetQuery returns the query of the request.
9999
GetQuery() string
100-
// GetLogicalPlan returns the serialized logical plan
101-
GetLogicalPlan() *logicalplan.Plan
100+
// GetLogicalPlan returns the logical plan
101+
GetLogicalPlan() logicalplan.Plan
102102
// WithStartEnd clone the current request with different start and end timestamp.
103103
WithStartEnd(startTime int64, endTime int64) Request
104104
// WithQuery clone the current request with a different query.
@@ -155,7 +155,7 @@ type PrometheusRequest struct {
155155
Headers http.Header
156156
Stats string
157157
CachingOptions CachingOptions
158-
LogicalPlan *logicalplan.Plan
158+
LogicalPlan logicalplan.Plan
159159
}
160160

161161
func (m *PrometheusRequest) GetPath() string {
@@ -221,7 +221,7 @@ func (m *PrometheusRequest) GetStats() string {
221221
return ""
222222
}
223223

224-
func (m *PrometheusRequest) GetLogicalPlan() *logicalplan.Plan {
224+
func (m *PrometheusRequest) GetLogicalPlan() logicalplan.Plan {
225225
if m != nil {
226226
return m.LogicalPlan
227227
}

0 commit comments

Comments
 (0)