Skip to content

Commit 37d2a49

Browse files
committed
feat(pkg/streamingpromql): use utf8 naming scheme in count_values unit test
1 parent 940085b commit 37d2a49

File tree

10 files changed

+64
-54
lines changed

10 files changed

+64
-54
lines changed

pkg/streamingpromql/compat/name_validating_engine.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"time"
88

99
"github.com/grafana/dskit/tenant"
10-
prom_validation "github.com/prometheus/prometheus/model/validation"
10+
"github.com/prometheus/common/model"
1111
"github.com/prometheus/prometheus/promql"
1212
"github.com/prometheus/prometheus/storage"
1313

@@ -25,64 +25,64 @@ func NameValidatingEngine(engine promql.QueryEngine, limits *validation.Override
2525
return &nameValidatingEngine{engine: engine, limits: limits}
2626
}
2727

28-
type optsWithNamingScheme struct {
28+
type optsWithValidationScheme struct {
2929
promql.QueryOpts
30-
namingScheme prom_validation.NamingScheme
30+
validationScheme model.ValidationScheme
3131
}
3232

33-
func (o optsWithNamingScheme) EnablePerStepStats() bool {
33+
func (o optsWithValidationScheme) EnablePerStepStats() bool {
3434
return o.QueryOpts.EnablePerStepStats()
3535
}
3636

37-
func (o optsWithNamingScheme) LookbackDelta() time.Duration {
37+
func (o optsWithValidationScheme) LookbackDelta() time.Duration {
3838
return o.QueryOpts.LookbackDelta()
3939
}
4040

41-
func (o optsWithNamingScheme) NameValidationScheme() prom_validation.NamingScheme {
42-
return o.namingScheme
41+
func (o optsWithValidationScheme) ValidationScheme() model.ValidationScheme {
42+
return o.validationScheme
4343
}
4444

4545
func (e nameValidatingEngine) NewInstantQuery(ctx context.Context, q storage.Queryable, opts promql.QueryOpts, qs string, ts time.Time) (promql.Query, error) {
46-
namingScheme, err := e.getNamingScheme(ctx)
46+
validationScheme, err := e.getValidationScheme(ctx)
4747
if err != nil {
4848
return nil, err
4949
}
5050
if opts == nil {
51-
opts = promql.NewPrometheusQueryOpts(false, 0, prom_validation.UTF8NamingScheme)
51+
opts = promql.NewPrometheusQueryOpts(false, 0, model.UTF8Validation)
5252
}
53-
opts = &optsWithNamingScheme{
54-
QueryOpts: opts,
55-
namingScheme: namingScheme,
53+
opts = &optsWithValidationScheme{
54+
QueryOpts: opts,
55+
validationScheme: validationScheme,
5656
}
5757
return e.engine.NewInstantQuery(ctx, q, opts, qs, ts)
5858
}
5959

6060
func (e nameValidatingEngine) NewRangeQuery(ctx context.Context, q storage.Queryable, opts promql.QueryOpts, qs string, start, end time.Time, interval time.Duration) (promql.Query, error) {
61-
namingScheme, err := e.getNamingScheme(ctx)
61+
validationScheme, err := e.getValidationScheme(ctx)
6262
if err != nil {
6363
return nil, err
6464
}
6565
if opts == nil {
66-
opts = promql.NewPrometheusQueryOpts(false, 0, prom_validation.UTF8NamingScheme)
66+
opts = promql.NewPrometheusQueryOpts(false, 0, model.UTF8Validation)
6767
}
68-
opts = &optsWithNamingScheme{
69-
QueryOpts: opts,
70-
namingScheme: namingScheme,
68+
opts = &optsWithValidationScheme{
69+
QueryOpts: opts,
70+
validationScheme: validationScheme,
7171
}
7272
return e.engine.NewRangeQuery(ctx, q, opts, qs, start, end, interval)
7373
}
7474

75-
// getNamingScheme retrieves the name validation scheme to use from a context containing tenant IDs.
75+
// getValidationScheme retrieves the name validation scheme to use from a context containing tenant IDs.
7676
// Returns legacy validation scheme if at least one tenant uses legacy validation.
77-
func (e nameValidatingEngine) getNamingScheme(ctx context.Context) (prom_validation.NamingScheme, error) {
77+
func (e nameValidatingEngine) getValidationScheme(ctx context.Context) (model.ValidationScheme, error) {
7878
tenantIDs, err := tenant.TenantIDs(ctx)
7979
if err != nil {
80-
return "", err
80+
return model.UnsetValidation, err
8181
}
8282
for _, tenantID := range tenantIDs {
83-
if e.limits.NameValidationScheme(tenantID) == prom_validation.LegacyNamingScheme {
84-
return prom_validation.LegacyNamingScheme, nil
83+
if e.limits.ValidationScheme(tenantID) == model.LegacyValidation {
84+
return model.LegacyValidation, nil
8585
}
8686
}
87-
return prom_validation.UTF8NamingScheme, nil
87+
return model.UTF8Validation, nil
8888
}

pkg/streamingpromql/engine_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ import (
2121
"github.com/prometheus/client_golang/prometheus"
2222
"github.com/prometheus/client_golang/prometheus/testutil"
2323
dto "github.com/prometheus/client_model/go"
24+
"github.com/prometheus/common/model"
2425
"github.com/prometheus/prometheus/model/histogram"
2526
"github.com/prometheus/prometheus/model/labels"
2627
"github.com/prometheus/prometheus/model/timestamp"
27-
"github.com/prometheus/prometheus/model/validation"
2828
"github.com/prometheus/prometheus/promql"
2929
"github.com/prometheus/prometheus/promql/parser"
3030
"github.com/prometheus/prometheus/promql/parser/posrange"
@@ -3208,7 +3208,7 @@ func TestQueryStats(t *testing.T) {
32083208
runQueryAndGetSamplesStats := func(t *testing.T, engine promql.QueryEngine, expr string, isInstantQuery bool) *promstats.QuerySamples {
32093209
var q promql.Query
32103210
var err error
3211-
opts := promql.NewPrometheusQueryOpts(true, 0, validation.LegacyNamingScheme)
3211+
opts := promql.NewPrometheusQueryOpts(true, 0, model.LegacyValidation)
32123212
if isInstantQuery {
32133213
q, err = engine.NewInstantQuery(context.Background(), storage, opts, expr, end)
32143214
} else {
@@ -3502,7 +3502,7 @@ func TestQueryStatsUpstreamTestCases(t *testing.T) {
35023502
runQueryAndGetSamplesStats := func(t *testing.T, engine promql.QueryEngine, expr string, start, end time.Time, interval time.Duration) *promstats.QuerySamples {
35033503
var q promql.Query
35043504
var err error
3505-
opts := promql.NewPrometheusQueryOpts(true, 0, validation.LegacyNamingScheme)
3505+
opts := promql.NewPrometheusQueryOpts(true, 0, model.LegacyValidation)
35063506

35073507
if interval == 0 {
35083508
// Instant query
@@ -3883,7 +3883,7 @@ func TestQueryStatementLookbackDelta(t *testing.T) {
38833883
require.NoError(t, err)
38843884

38853885
t.Run("lookback delta not set in query options", func(t *testing.T) {
3886-
queryOpts := promql.NewPrometheusQueryOpts(false, 0, validation.LegacyNamingScheme)
3886+
queryOpts := promql.NewPrometheusQueryOpts(false, 0, model.LegacyValidation)
38873887
runTest(t, engine, queryOpts, defaultLookbackDelta)
38883888
})
38893889

@@ -3892,7 +3892,7 @@ func TestQueryStatementLookbackDelta(t *testing.T) {
38923892
})
38933893

38943894
t.Run("lookback delta set in query options", func(t *testing.T) {
3895-
queryOpts := promql.NewPrometheusQueryOpts(false, 14*time.Minute, validation.LegacyNamingScheme)
3895+
queryOpts := promql.NewPrometheusQueryOpts(false, 14*time.Minute, model.LegacyValidation)
38963896
runTest(t, engine, queryOpts, 14*time.Minute)
38973897
})
38983898
})
@@ -3904,7 +3904,7 @@ func TestQueryStatementLookbackDelta(t *testing.T) {
39043904
require.NoError(t, err)
39053905

39063906
t.Run("lookback delta not set in query options", func(t *testing.T) {
3907-
queryOpts := promql.NewPrometheusQueryOpts(false, 0, validation.LegacyNamingScheme)
3907+
queryOpts := promql.NewPrometheusQueryOpts(false, 0, model.LegacyValidation)
39083908
runTest(t, engine, queryOpts, 12*time.Minute)
39093909
})
39103910

@@ -3913,7 +3913,7 @@ func TestQueryStatementLookbackDelta(t *testing.T) {
39133913
})
39143914

39153915
t.Run("lookback delta set in query options", func(t *testing.T) {
3916-
queryOpts := promql.NewPrometheusQueryOpts(false, 14*time.Minute, validation.LegacyNamingScheme)
3916+
queryOpts := promql.NewPrometheusQueryOpts(false, 14*time.Minute, model.LegacyValidation)
39173917
runTest(t, engine, queryOpts, 14*time.Minute)
39183918
})
39193919
})

pkg/streamingpromql/operators/aggregations/aggregation_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"testing"
88
"time"
99

10+
"github.com/prometheus/common/model"
1011
"github.com/prometheus/prometheus/model/histogram"
1112
"github.com/prometheus/prometheus/model/labels"
1213
"github.com/prometheus/prometheus/model/timestamp"
13-
"github.com/prometheus/prometheus/model/validation"
1414
"github.com/prometheus/prometheus/promql"
1515
"github.com/prometheus/prometheus/promql/parser"
1616
"github.com/prometheus/prometheus/promql/parser/posrange"
@@ -346,7 +346,7 @@ func TestAggregations_ReturnIncompleteGroupsOnEarlyClose(t *testing.T) {
346346
false,
347347
memoryConsumptionTracker,
348348
posrange.PositionRange{},
349-
validation.LegacyNamingScheme,
349+
model.LegacyValidation,
350350
), nil
351351
},
352352
instant: true,

pkg/streamingpromql/operators/aggregations/count_values.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"strconv"
1313
"sync"
1414

15+
"github.com/prometheus/common/model"
1516
"github.com/prometheus/prometheus/model/labels"
16-
"github.com/prometheus/prometheus/model/validation"
1717
"github.com/prometheus/prometheus/promql"
1818
"github.com/prometheus/prometheus/promql/parser/posrange"
1919

@@ -40,7 +40,7 @@ type CountValues struct {
4040
labelsBytesBuffer []byte
4141
valueBuffer []byte
4242

43-
nameValidationScheme validation.NamingScheme
43+
nameValidationScheme model.ValidationScheme
4444
}
4545

4646
var _ types.InstantVectorOperator = &CountValues{}
@@ -53,7 +53,7 @@ func NewCountValues(
5353
without bool,
5454
memoryConsumptionTracker *limiter.MemoryConsumptionTracker,
5555
expressionPosition posrange.PositionRange,
56-
nameValidationScheme validation.NamingScheme,
56+
nameValidationScheme model.ValidationScheme,
5757
) *CountValues {
5858
if without {
5959
grouping = append(grouping, labels.MetricName)
@@ -155,7 +155,7 @@ func (c *CountValues) SeriesMetadata(ctx context.Context) ([]types.SeriesMetadat
155155

156156
func (c *CountValues) loadLabelName() error {
157157
c.resolvedLabelName = c.LabelName.GetValue()
158-
if !c.nameValidationScheme.IsValidLabelName(c.resolvedLabelName) {
158+
if !model.LabelName(c.resolvedLabelName).IsValid(c.nameValidationScheme) {
159159
return fmt.Errorf("invalid label name %q", c.resolvedLabelName)
160160
}
161161

pkg/streamingpromql/operators/aggregations/count_values_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"context"
77
"testing"
88

9+
"github.com/prometheus/common/model"
910
"github.com/prometheus/prometheus/model/labels"
1011
"github.com/prometheus/prometheus/model/timestamp"
11-
"github.com/prometheus/prometheus/model/validation"
1212
"github.com/prometheus/prometheus/promql"
1313
"github.com/prometheus/prometheus/promql/parser/posrange"
1414
"github.com/stretchr/testify/require"
@@ -50,6 +50,11 @@ func TestCountValues_GroupLabelling(t *testing.T) {
5050
inputSeries: labels.FromStrings(labels.MetricName, "my_metric", "env", "prod", "foo", "bar"),
5151
expectedOutputSeries: labels.FromStrings("env", "prod", "value", "123"),
5252
},
53+
"grouping with 'by', single utf8 grouping label, input does have grouping label": {
54+
grouping: []string{"env😀"},
55+
inputSeries: labels.FromStrings(labels.MetricName, "my_metric", "env😀", "prod", "foo", "bar"),
56+
expectedOutputSeries: labels.FromStrings("env😀", "prod", "value", "123"),
57+
},
5358
"grouping with 'by', multiple grouping labels, input has only metric name": {
5459
grouping: []string{"cluster", "env"},
5560
inputSeries: labels.FromStrings(labels.MetricName, "my_metric"),
@@ -110,6 +115,12 @@ func TestCountValues_GroupLabelling(t *testing.T) {
110115
inputSeries: labels.FromStrings(labels.MetricName, "my_metric", "env", "prod", "a-label", "a-value", "f-label", "f-value"),
111116
expectedOutputSeries: labels.FromStrings("a-label", "a-value", "f-label", "f-value", "value", "123"),
112117
},
118+
"grouping with 'without', single utf8 grouping label, input does have grouping label": {
119+
grouping: []string{"env😀"},
120+
without: true,
121+
inputSeries: labels.FromStrings(labels.MetricName, "my_metric", "env😀", "prod", "a-label", "a-value", "f-label", "f-value"),
122+
expectedOutputSeries: labels.FromStrings("a-label", "a-value", "f-label", "f-value", "value", "123"),
123+
},
113124
"grouping with 'without', multiple grouping labels, input has only metric name": {
114125
grouping: []string{"cluster", "env"},
115126
without: true,
@@ -231,7 +242,7 @@ func TestCountValues_GroupLabelling(t *testing.T) {
231242
testCase.without,
232243
memoryConsumptionTracker,
233244
posrange.PositionRange{},
234-
validation.LegacyNamingScheme,
245+
model.UTF8Validation,
235246
)
236247

237248
metadata, err := aggregator.SeriesMetadata(context.Background())

pkg/streamingpromql/operators/functions/factories.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77
"math"
88

9-
"github.com/prometheus/prometheus/model/validation"
9+
"github.com/prometheus/common/model"
1010
"github.com/prometheus/prometheus/promql/parser/posrange"
1111
"github.com/prometheus/prometheus/util/annotations"
1212

@@ -25,7 +25,7 @@ type InstantVectorFunctionOperatorFactory func(
2525
type InstantVectorFunctionOperatorParams struct {
2626
MemoryConsumptionTracker *limiter.MemoryConsumptionTracker
2727
Annotations *annotations.Annotations
28-
NameValidationScheme validation.NamingScheme
28+
NameValidationScheme model.ValidationScheme
2929
ExpressionPosition posrange.PositionRange
3030
TimeRange types.QueryTimeRange
3131
}

pkg/streamingpromql/operators/functions/label.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ import (
1111
"strings"
1212

1313
"github.com/grafana/regexp"
14+
"github.com/prometheus/common/model"
1415
"github.com/prometheus/prometheus/model/labels"
15-
"github.com/prometheus/prometheus/model/validation"
1616

1717
"github.com/grafana/mimir/pkg/streamingpromql/types"
1818
"github.com/grafana/mimir/pkg/util/limiter"
1919
)
2020

21-
func LabelJoinFactory(dstLabelOp, separatorOp types.StringOperator, srcLabelOps []types.StringOperator, namingScheme validation.NamingScheme) SeriesMetadataFunction {
21+
func LabelJoinFactory(dstLabelOp, separatorOp types.StringOperator, srcLabelOps []types.StringOperator, validationScheme model.ValidationScheme) SeriesMetadataFunction {
2222
return func(seriesMetadata []types.SeriesMetadata, _ *limiter.MemoryConsumptionTracker) ([]types.SeriesMetadata, error) {
2323
dst := dstLabelOp.GetValue()
24-
if !namingScheme.IsValidLabelName(dst) {
24+
if !model.LabelName(dst).IsValid(validationScheme) {
2525
return nil, fmt.Errorf("invalid destination label name in label_join(): %s", dst)
2626
}
2727
separator := separatorOp.GetValue()
2828
srcLabels := make([]string, len(srcLabelOps))
2929
for i, op := range srcLabelOps {
3030
src := op.GetValue()
31-
if !namingScheme.IsValidLabelName(src) {
31+
if !model.LabelName(src).IsValid(validationScheme) {
3232
return nil, fmt.Errorf("invalid source label name in label_join(): %s", dst)
3333
}
3434
srcLabels[i] = src
@@ -58,15 +58,15 @@ func LabelJoinFactory(dstLabelOp, separatorOp types.StringOperator, srcLabelOps
5858
}
5959
}
6060

61-
func LabelReplaceFactory(dstLabelOp, replacementOp, srcLabelOp, regexOp types.StringOperator, namingScheme validation.NamingScheme) SeriesMetadataFunction {
61+
func LabelReplaceFactory(dstLabelOp, replacementOp, srcLabelOp, regexOp types.StringOperator, validationScheme model.ValidationScheme) SeriesMetadataFunction {
6262
return func(seriesMetadata []types.SeriesMetadata, _ *limiter.MemoryConsumptionTracker) ([]types.SeriesMetadata, error) {
6363
regexStr := regexOp.GetValue()
6464
regex, err := regexp.Compile("^(?s:" + regexStr + ")$")
6565
if err != nil {
6666
return nil, fmt.Errorf("invalid regular expression in label_replace(): %s", regexStr)
6767
}
6868
dst := dstLabelOp.GetValue()
69-
if !namingScheme.IsValidLabelName(dst) {
69+
if !model.LabelName(dst).IsValid(validationScheme) {
7070
return nil, fmt.Errorf("invalid destination label name in label_replace(): %s", dst)
7171
}
7272
repl := replacementOp.GetValue()

pkg/streamingpromql/planning.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/prometheus/client_golang/prometheus/promauto"
1717
"github.com/prometheus/common/model"
1818
"github.com/prometheus/prometheus/model/timestamp"
19-
"github.com/prometheus/prometheus/model/validation"
2019
"github.com/prometheus/prometheus/promql"
2120
"github.com/prometheus/prometheus/promql/parser"
2221
"github.com/prometheus/prometheus/storage"
@@ -421,7 +420,7 @@ func isKnownFunction(name string) bool {
421420
// Materialize converts a query plan into an executable query.
422421
func (e *Engine) Materialize(ctx context.Context, plan *planning.QueryPlan, queryable storage.Queryable, opts promql.QueryOpts) (promql.Query, error) {
423422
if opts == nil {
424-
opts = promql.NewPrometheusQueryOpts(false, 0, validation.UTF8NamingScheme)
423+
opts = promql.NewPrometheusQueryOpts(false, 0, model.UTF8Validation)
425424
}
426425

427426
queryID, err := e.activeQueryTracker.Insert(ctx, plan.OriginalExpression+" # (materialization)")

pkg/streamingpromql/planning/plan.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010

1111
"github.com/gogo/protobuf/proto"
1212
prototypes "github.com/gogo/protobuf/types"
13+
"github.com/prometheus/common/model"
1314
"github.com/prometheus/prometheus/model/timestamp"
14-
"github.com/prometheus/prometheus/model/validation"
1515
"github.com/prometheus/prometheus/promql/parser"
1616
"github.com/prometheus/prometheus/storage"
1717
"github.com/prometheus/prometheus/util/annotations"
@@ -91,7 +91,7 @@ type OperatorParameters struct {
9191
Annotations *annotations.Annotations
9292
LookbackDelta time.Duration
9393
EagerLoadSelectors bool
94-
NameValidationScheme validation.NamingScheme
94+
NameValidationScheme model.ValidationScheme
9595
}
9696

9797
func (p *QueryPlan) ToEncodedPlan(includeDescriptions bool, includeDetails bool) (*EncodedQueryPlan, error) {

0 commit comments

Comments
 (0)