(t *testing.T)
| 269 | } |
| 270 | |
| 271 | func TestValidateMetadata(t *testing.T) { |
| 272 | cfg := new(Limits) |
| 273 | cfg.EnforceMetadataMetricName = true |
| 274 | cfg.MaxMetadataLength = 22 |
| 275 | reg := prometheus.NewRegistry() |
| 276 | validateMetrics := NewValidateMetrics(reg) |
| 277 | userID := "testUser" |
| 278 | |
| 279 | for _, c := range []struct { |
| 280 | desc string |
| 281 | metadata *cortexpb.MetricMetadata |
| 282 | err error |
| 283 | }{ |
| 284 | { |
| 285 | "with a valid config", |
| 286 | &cortexpb.MetricMetadata{MetricFamilyName: "go_goroutines", Type: cortexpb.COUNTER, Help: "Number of goroutines.", Unit: ""}, |
| 287 | nil, |
| 288 | }, |
| 289 | { |
| 290 | "with no metric name", |
| 291 | &cortexpb.MetricMetadata{MetricFamilyName: "", Type: cortexpb.COUNTER, Help: "Number of goroutines.", Unit: ""}, |
| 292 | httpgrpc.Errorf(http.StatusBadRequest, "metadata missing metric name"), |
| 293 | }, |
| 294 | { |
| 295 | "with a long metric name", |
| 296 | &cortexpb.MetricMetadata{MetricFamilyName: "go_goroutines_and_routines_and_routines", Type: cortexpb.COUNTER, Help: "Number of goroutines.", Unit: ""}, |
| 297 | httpgrpc.Errorf(http.StatusBadRequest, "metadata 'METRIC_NAME' value too long: \"go_goroutines_and_routines_and_routines\" metric \"go_goroutines_and_routines_and_routines\""), |
| 298 | }, |
| 299 | { |
| 300 | "with a long help", |
| 301 | &cortexpb.MetricMetadata{MetricFamilyName: "go_goroutines", Type: cortexpb.COUNTER, Help: "Number of goroutines that currently exist.", Unit: ""}, |
| 302 | httpgrpc.Errorf(http.StatusBadRequest, "metadata 'HELP' value too long: \"Number of goroutines that currently exist.\" metric \"go_goroutines\""), |
| 303 | }, |
| 304 | { |
| 305 | "with a long unit", |
| 306 | &cortexpb.MetricMetadata{MetricFamilyName: "go_goroutines", Type: cortexpb.COUNTER, Help: "Number of goroutines.", Unit: "a_made_up_unit_that_is_really_long"}, |
| 307 | httpgrpc.Errorf(http.StatusBadRequest, "metadata 'UNIT' value too long: \"a_made_up_unit_that_is_really_long\" metric \"go_goroutines\""), |
| 308 | }, |
| 309 | } { |
| 310 | t.Run(c.desc, func(t *testing.T) { |
| 311 | err := ValidateMetadata(validateMetrics, cfg, userID, c.metadata) |
| 312 | assert.Equal(t, c.err, err, "wrong error") |
| 313 | }) |
| 314 | } |
| 315 | |
| 316 | validateMetrics.DiscardedMetadata.WithLabelValues("random reason", "different user").Inc() |
| 317 | |
| 318 | require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(` |
| 319 | # HELP cortex_discarded_metadata_total The total number of metadata that were discarded. |
| 320 | # TYPE cortex_discarded_metadata_total counter |
| 321 | cortex_discarded_metadata_total{reason="help_too_long",user="testUser"} 1 |
| 322 | cortex_discarded_metadata_total{reason="metric_name_too_long",user="testUser"} 1 |
| 323 | cortex_discarded_metadata_total{reason="missing_metric_name",user="testUser"} 1 |
| 324 | cortex_discarded_metadata_total{reason="unit_too_long",user="testUser"} 1 |
| 325 | |
| 326 | cortex_discarded_metadata_total{reason="random reason",user="different user"} 1 |
| 327 | `), "cortex_discarded_metadata_total")) |
| 328 |
nothing calls this directly
no test coverage detected