Skip to content

Commit a3e4047

Browse files
authored
Merge pull request prometheus#17354 from prometheus/fix-smoothed-anchored-formatting
Fix formatting of range vector selectors with smoothed/anchored modifier
2 parents 8bbfdf2 + 9d7d544 commit a3e4047

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

promql/parser/printer.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ func (node *Call) ShortString() string {
229229
}
230230

231231
func (node *MatrixSelector) atOffset() (string, string) {
232-
// Copy the Vector selector before changing the offset
233232
vecSelector := node.VectorSelector.(*VectorSelector)
234233
offset := ""
235234
switch {
@@ -254,20 +253,21 @@ func (node *MatrixSelector) atOffset() (string, string) {
254253

255254
func (node *MatrixSelector) String() string {
256255
at, offset := node.atOffset()
257-
// Copy the Vector selector before changing the offset
256+
// Copy the Vector selector so we can modify it to not print @, offset, and other modifiers twice.
258257
vecSelector := *node.VectorSelector.(*VectorSelector)
259-
// Do not print the @ and offset twice.
260-
offsetVal, offsetExprVal, atVal, preproc := vecSelector.OriginalOffset, vecSelector.OriginalOffsetExpr, vecSelector.Timestamp, vecSelector.StartOrEnd
258+
anchored, smoothed := vecSelector.Anchored, vecSelector.Smoothed
261259
vecSelector.OriginalOffset = 0
262260
vecSelector.OriginalOffsetExpr = nil
263261
vecSelector.Timestamp = nil
264262
vecSelector.StartOrEnd = 0
263+
vecSelector.Anchored = false
264+
vecSelector.Smoothed = false
265265

266266
extendedAttribute := ""
267267
switch {
268-
case vecSelector.Anchored:
268+
case anchored:
269269
extendedAttribute = " anchored"
270-
case vecSelector.Smoothed:
270+
case smoothed:
271271
extendedAttribute = " smoothed"
272272
}
273273
rangeStr := model.Duration(node.Range).String()
@@ -276,8 +276,6 @@ func (node *MatrixSelector) String() string {
276276
}
277277
str := fmt.Sprintf("%s[%s]%s%s%s", vecSelector.String(), rangeStr, extendedAttribute, at, offset)
278278

279-
vecSelector.OriginalOffset, vecSelector.OriginalOffsetExpr, vecSelector.Timestamp, vecSelector.StartOrEnd = offsetVal, offsetExprVal, atVal, preproc
280-
281279
return str
282280
}
283281

promql/parser/printer_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,36 @@ func TestExprString(t *testing.T) {
115115
{
116116
in: `a[1h:5m] offset 1m`,
117117
},
118+
{
119+
in: `a anchored`,
120+
},
121+
{
122+
in: `a[5m] anchored`,
123+
},
124+
{
125+
in: `a{b="c"}[5m] anchored`,
126+
},
127+
{
128+
in: `a{b="c"}[5m] anchored offset 1m`,
129+
},
130+
{
131+
in: `a{b="c"}[5m] anchored @ start() offset 1m`,
132+
},
133+
{
134+
in: `a smoothed`,
135+
},
136+
{
137+
in: `a[5m] smoothed`,
138+
},
139+
{
140+
in: `a{b="c"}[5m] smoothed`,
141+
},
142+
{
143+
in: `a{b="c"}[5m] smoothed offset 1m`,
144+
},
145+
{
146+
in: `a{b="c"}[5m] smoothed @ start() offset 1m`,
147+
},
118148
{
119149
in: `{__name__="a"}`,
120150
},
@@ -222,6 +252,11 @@ func TestExprString(t *testing.T) {
222252
},
223253
}
224254

255+
EnableExtendedRangeSelectors = true
256+
t.Cleanup(func() {
257+
EnableExtendedRangeSelectors = false
258+
})
259+
225260
for _, test := range inputs {
226261
t.Run(test.in, func(t *testing.T) {
227262
expr, err := ParseExpr(test.in)

0 commit comments

Comments
 (0)