Skip to content

Commit 81da3a3

Browse files
authored
Merge pull request #669 from slok/slok/contrib-plugins-sync
Simplify victoria metrics validator SLO plugin
2 parents 9056bfb + 11e112d commit 81da3a3

2 files changed

Lines changed: 29 additions & 36 deletions

File tree

internal/plugin/slo/contrib/validate_victoria_metrics_v1/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,17 @@ sloth generate \
4545
-s '{"id": "sloth.dev/core/metadata_rules/v1"}' \
4646
-s '{"id": "sloth.dev/core/alert_rules/v1"}'
4747
```
48+
49+
### Explicit usage using env vars in script
50+
51+
```bash
52+
#! /bin/bash
53+
54+
export SLOTH_DISABLE_DEFAULT_SLO_PLUGINS=true
55+
export SLOTH_SLO_PLUGINS='{"id": "sloth.dev/contrib/validate_victoria_metrics/v1"}
56+
{"id": "sloth.dev/core/sli_rules/v1"}
57+
{"id": "sloth.dev/core/metadata_rules/v1"}
58+
{"id": "sloth.dev/core/alert_rules/v1"}'
59+
60+
sloth generate -i ./examples/victoria-metrics.yml
61+
```

internal/plugin/slo/contrib/validate_victoria_metrics_v1/plugin.go

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"text/template"
99

1010
"github.com/VictoriaMetrics/metricsql"
11-
prommodel "github.com/prometheus/common/model"
1211

1312
"github.com/slok/sloth/pkg/common/validation"
1413
pluginslov1 "github.com/slok/sloth/pkg/prometheus/plugin/slo/v1"
@@ -39,51 +38,31 @@ func (p plugin) ProcessSLO(ctx context.Context, request *pluginslov1.Request, re
3938
}
4039

4140
// VictoriaMetricsDialectValidator is the SLO flavour validator for victoria metrics backends dialect.
42-
const VictoriaMetricsDialectValidator = victoriaMetricsDialectValidator(false)
43-
44-
type victoriaMetricsDialectValidator bool
45-
46-
func (victoriaMetricsDialectValidator) ValidateLabelKey(k string) error {
47-
if k == prommodel.MetricNameLabel {
48-
return fmt.Errorf("the label key %q is not allowed", prommodel.MetricNameLabel)
49-
}
50-
51-
if !prommodel.UTF8Validation.IsValidLabelName(k) {
52-
return fmt.Errorf("the label key %q is not valid", k)
53-
}
54-
55-
return nil
41+
var VictoriaMetricsDialectValidator = victoriaMetricsDialectValidator{
42+
promValidator: validation.PromQLDialectValidator,
5643
}
5744

58-
func (victoriaMetricsDialectValidator) ValidateLabelValue(k string) error {
59-
if k == "" {
60-
return fmt.Errorf("the label value is required")
61-
}
62-
63-
if !prommodel.LabelValue(k).IsValid() {
64-
return fmt.Errorf("the label value %q is not valid", k)
65-
}
66-
67-
return nil
45+
type victoriaMetricsDialectValidator struct {
46+
promValidator validation.SLODialectValidator
6847
}
6948

70-
func (victoriaMetricsDialectValidator) ValidateAnnotationKey(k string) error {
71-
if !prommodel.UTF8Validation.IsValidLabelName(k) {
72-
return fmt.Errorf("the annotation key %q is not valid", k)
73-
}
49+
var promExprTplAllowedFakeData = map[string]string{"window": "1m"}
7450

75-
return nil
51+
func (v victoriaMetricsDialectValidator) ValidateLabelKey(key string) error {
52+
return v.promValidator.ValidateLabelKey(key)
7653
}
7754

78-
func (victoriaMetricsDialectValidator) ValidateAnnotationValue(k string) error {
79-
if k == "" {
80-
return fmt.Errorf("the annotation value is required")
81-
}
55+
func (v victoriaMetricsDialectValidator) ValidateLabelValue(label string) error {
56+
return v.promValidator.ValidateLabelValue(label)
57+
}
8258

83-
return nil
59+
func (v victoriaMetricsDialectValidator) ValidateAnnotationKey(key string) error {
60+
return v.promValidator.ValidateAnnotationKey(key)
8461
}
8562

86-
var promExprTplAllowedFakeData = map[string]string{"window": "1m"}
63+
func (v victoriaMetricsDialectValidator) ValidateAnnotationValue(annot string) error {
64+
return v.promValidator.ValidateAnnotationValue(annot)
65+
}
8766

8867
func (victoriaMetricsDialectValidator) ValidateQueryExpression(queryExpression string) error {
8968
if queryExpression == "" {

0 commit comments

Comments
 (0)