Move back to go-yaml

Signed-off-by: Goutham Veeramachaneni <cs14btech11014@iith.ac.in>
This commit is contained in:
Goutham Veeramachaneni 2017-06-16 10:46:21 +05:30
parent 5ff283a7b7
commit dc69645e92
No known key found for this signature in database
GPG key ID: F1C217E8E9023CAD
11 changed files with 33 additions and 43 deletions

View file

@ -20,7 +20,7 @@ import (
"path/filepath"
"strings"
yaml "github.com/ghodss/yaml"
yaml "gopkg.in/yaml.v2"
"github.com/prometheus/common/model"
"github.com/prometheus/common/version"
@ -244,7 +244,7 @@ func updateRules(t cli.Term, filename string) error {
yamlRules = append(yamlRules, rulefmt.Rule{
Alert: r.Name,
Expr: r.Expr.String(),
For: model.Duration(r.Duration).String(),
For: model.Duration(r.Duration),
Labels: r.Labels.Map(),
Annotations: r.Annotations.Map(),
})

View file

@ -16,10 +16,10 @@ package rulefmt
import (
"io/ioutil"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/promql"
yaml "gopkg.in/yaml.v2"
)
// Error represents semantical errors on parsing rule groups.
@ -35,8 +35,8 @@ func (err *Error) Error() string {
// RuleGroups is a set of rule groups that are typically exposed in a file.
type RuleGroups struct {
Version int `json:"version"`
Groups []RuleGroup `json:"groups"`
Version int `yaml:"version"`
Groups []RuleGroup `yaml:"groups"`
}
// Validate validates all rules in the rule groups.
@ -75,19 +75,19 @@ func (g *RuleGroups) Validate() (errs []error) {
// RuleGroup is a list of sequentially evaluated recording and alerting rules.
type RuleGroup struct {
Name string `json:"name"`
Interval string `json:"interval,omitempty"`
Rules []Rule `json:"rules"`
Name string `yaml:"name"`
Interval model.Duration `yaml:"interval,omitempty"`
Rules []Rule `yaml:"rules"`
}
// Rule describes an alerting or recording rule.
type Rule struct {
Record string `json:"record,omitempty"`
Alert string `json:"alert,omitempty"`
Expr string `json:"expr"`
For string `json:"for,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Record string `yaml:"record,omitempty"`
Alert string `yaml:"alert,omitempty"`
Expr string `yaml:"expr"`
For model.Duration `yaml:"for,omitempty"`
Labels map[string]string `yaml:"labels,omitempty"`
Annotations map[string]string `yaml:"annotations,omitempty"`
}
// Validate the rule and return a list of encountered errors.
@ -108,7 +108,7 @@ func (r *Rule) Validate() (errs []error) {
if len(r.Annotations) > 0 {
errs = append(errs, errors.Errorf("invalid field 'annotations' in recording rule"))
}
if r.For != "" {
if r.For != 0 {
errs = append(errs, errors.Errorf("invalid field 'for' in recording rule"))
}
}

View file

@ -1,5 +1,5 @@
Version: 1
Groups:
version: 1
groups:
- name: yolo
rules:
- alert: hola

View file

@ -1,5 +1,5 @@
Version: 1
Groups:
version: 1
groups:
- name: yolo
rules:
- record: yolo

View file

@ -1,5 +1,5 @@
Version: 1
Groups:
version: 1
groups:
- name: yolo
rules:
- record: hola

View file

@ -1,4 +1,4 @@
Version: 1
Groups:
version: 1
groups:
- name: yolo
- name: yolo

View file

@ -1,5 +1,5 @@
Version: 1
Groups:
version: 1
groups:
- name: yolo
rules:
- expr: 1

View file

@ -1,5 +1,5 @@
Version: 1
Groups:
version: 1
groups:
- name: yolo
rules:
- record: ylo

View file

@ -1,2 +1,2 @@
Groups:
groups:
- name: yolo

View file

@ -1,5 +1,5 @@
Version: 1
Groups:
version: 1
groups:
- name: yolo
rules:
- record: Hi

View file

@ -522,13 +522,8 @@ func (m *Manager) loadGroups(interval time.Duration, filenames ...string) (map[s
for _, rg := range rgs.Groups {
itv := interval
if rg.Interval != "" {
dur, err := model.ParseDuration(rg.Interval)
if err != nil {
return nil, err
}
itv = time.Duration(dur)
if rg.Interval != 0 {
itv = time.Duration(rg.Interval)
}
rules := make([]Rule, 0, len(rg.Rules))
@ -539,15 +534,10 @@ func (m *Manager) loadGroups(interval time.Duration, filenames ...string) (map[s
}
if r.Alert != "" {
fordur, err := model.ParseDuration(r.For)
if err != nil {
return nil, err
}
rules = append(rules, NewAlertingRule(
r.Alert,
expr,
time.Duration(fordur),
time.Duration(r.For),
labels.FromMap(r.Labels),
labels.FromMap(r.Annotations),
))