@@ -2036,59 +2036,140 @@ func TestDistributor_Push_ShouldGuaranteeShardingTokenConsistencyOverTheTime(t *
2036
2036
}
2037
2037
}
2038
2038
2039
+ // func TestDistributor_Push_LabelNameValidation(t *testing.T) {
2040
+ // model.NameValidationScheme = model.LegacyValidation
2041
+ // t.Parallel()
2042
+ // inputLabels := labels.Labels{
2043
+ // {Name: model.MetricNameLabel, Value: "foo"},
2044
+ // {Name: "999.illegal", Value: "baz"},
2045
+ // }
2046
+ // ctx := user.InjectOrgID(context.Background(), "user")
2047
+
2048
+ // tests := map[string]struct {
2049
+ // inputLabels labels.Labels
2050
+ // skipLabelNameValidationCfg bool
2051
+ // skipLabelNameValidationReq bool
2052
+ // errExpected bool
2053
+ // errMessage string
2054
+ // }{
2055
+ // "label name validation is on by default": {
2056
+ // inputLabels: inputLabels,
2057
+ // errExpected: true,
2058
+ // errMessage: `sample invalid label: "999.illegal" metric "foo{999.illegal=\"baz\"}"`,
2059
+ // },
2060
+ // "label name validation can be skipped via config": {
2061
+ // inputLabels: inputLabels,
2062
+ // skipLabelNameValidationCfg: true,
2063
+ // errExpected: false,
2064
+ // },
2065
+ // "label name validation can be skipped via WriteRequest parameter": {
2066
+ // inputLabels: inputLabels,
2067
+ // skipLabelNameValidationReq: true,
2068
+ // errExpected: false,
2069
+ // },
2070
+ // }
2071
+
2072
+ // for testName, tc := range tests {
2073
+ // tc := tc
2074
+ // for _, histogram := range []bool{true, false} {
2075
+ // histogram := histogram
2076
+ // t.Run(fmt.Sprintf("%s, histogram=%s", testName, strconv.FormatBool(histogram)), func(t *testing.T) {
2077
+ // t.Parallel()
2078
+ // ds, _, _, _ := prepare(t, prepConfig{
2079
+ // numIngesters: 2,
2080
+ // happyIngesters: 2,
2081
+ // numDistributors: 1,
2082
+ // shuffleShardSize: 1,
2083
+ // skipLabelNameValidation: tc.skipLabelNameValidationCfg,
2084
+ // })
2085
+ // req := mockWriteRequest([]labels.Labels{tc.inputLabels}, 42, 100000, histogram)
2086
+ // req.SkipLabelNameValidation = tc.skipLabelNameValidationReq
2087
+ // _, err := ds[0].Push(ctx, req)
2088
+ // if tc.errExpected {
2089
+ // fromError, _ := status.FromError(err)
2090
+ // assert.Equal(t, tc.errMessage, fromError.Message())
2091
+ // } else {
2092
+ // assert.Nil(t, err)
2093
+ // }
2094
+ // })
2095
+ // }
2096
+ // }
2097
+ // }
2039
2098
func TestDistributor_Push_LabelNameValidation (t * testing.T ) {
2040
- t .Parallel ()
2041
- inputLabels := labels.Labels {
2042
- {Name : model .MetricNameLabel , Value : "foo" },
2043
- {Name : "999.illegal" , Value : "baz" },
2044
- }
2045
2099
ctx := user .InjectOrgID (context .Background (), "user" )
2046
2100
2047
2101
tests := map [string ]struct {
2048
2102
inputLabels labels.Labels
2049
2103
skipLabelNameValidationCfg bool
2050
2104
skipLabelNameValidationReq bool
2105
+ useUTF8Validation bool
2051
2106
errExpected bool
2052
2107
errMessage string
2053
2108
}{
2054
2109
"label name validation is on by default" : {
2055
- inputLabels : inputLabels ,
2110
+ inputLabels : labels.Labels {
2111
+ {Name : model .MetricNameLabel , Value : "foo" },
2112
+ {Name : "999.illegal" , Value : "baz" },
2113
+ },
2056
2114
errExpected : true ,
2057
2115
errMessage : `sample invalid label: "999.illegal" metric "foo{999.illegal=\"baz\"}"` ,
2058
2116
},
2059
2117
"label name validation can be skipped via config" : {
2060
- inputLabels : inputLabels ,
2118
+ inputLabels : labels.Labels {
2119
+ {Name : model .MetricNameLabel , Value : "foo" },
2120
+ {Name : "999.illegal" , Value : "baz" },
2121
+ },
2061
2122
skipLabelNameValidationCfg : true ,
2062
2123
errExpected : false ,
2063
2124
},
2064
2125
"label name validation can be skipped via WriteRequest parameter" : {
2065
- inputLabels : inputLabels ,
2126
+ inputLabels : labels.Labels {
2127
+ {Name : model .MetricNameLabel , Value : "foo" },
2128
+ {Name : "999.illegal" , Value : "baz" },
2129
+ },
2066
2130
skipLabelNameValidationReq : true ,
2067
2131
errExpected : false ,
2068
2132
},
2133
+ "UTF-8 validation allows Unicode label names" : {
2134
+ inputLabels : labels.Labels {
2135
+ {Name : model .MetricNameLabel , Value : "foo" },
2136
+ {Name : "Cortex_😃" , Value : "baz" },
2137
+ },
2138
+ useUTF8Validation : true ,
2139
+ errExpected : false ,
2140
+ },
2069
2141
}
2070
2142
2071
2143
for testName , tc := range tests {
2072
- tc := tc
2073
2144
for _ , histogram := range []bool {true , false } {
2074
- histogram := histogram
2075
- t .Run (fmt .Sprintf ("%s, histogram=%s" , testName , strconv .FormatBool (histogram )), func (t * testing.T ) {
2076
- t .Parallel ()
2145
+ t .Run (fmt .Sprintf ("%s, histogram=%v" , testName , histogram ), func (t * testing.T ) {
2146
+ if tc .useUTF8Validation {
2147
+ // nolint:staticcheck // SA1019: using deprecated NameValidationScheme intentionally for legacy validation testing
2148
+ model .NameValidationScheme = model .UTF8Validation
2149
+ } else {
2150
+ // nolint:staticcheck // SA1019: using deprecated NameValidationScheme intentionally for legacy validation testing
2151
+ model .NameValidationScheme = model .LegacyValidation
2152
+ }
2153
+
2077
2154
ds , _ , _ , _ := prepare (t , prepConfig {
2078
2155
numIngesters : 2 ,
2079
2156
happyIngesters : 2 ,
2080
2157
numDistributors : 1 ,
2081
2158
shuffleShardSize : 1 ,
2082
2159
skipLabelNameValidation : tc .skipLabelNameValidationCfg ,
2083
2160
})
2161
+
2084
2162
req := mockWriteRequest ([]labels.Labels {tc .inputLabels }, 42 , 100000 , histogram )
2085
2163
req .SkipLabelNameValidation = tc .skipLabelNameValidationReq
2164
+
2086
2165
_ , err := ds [0 ].Push (ctx , req )
2166
+
2087
2167
if tc .errExpected {
2168
+ require .Error (t , err )
2088
2169
fromError , _ := status .FromError (err )
2089
2170
assert .Equal (t , tc .errMessage , fromError .Message ())
2090
2171
} else {
2091
- assert . Nil (t , err )
2172
+ require . NoError (t , err )
2092
2173
}
2093
2174
})
2094
2175
}
0 commit comments