本文整理汇总了Golang中github.com/prometheus/prometheus/promql.ParseStmts函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseStmts函数的具体用法?Golang ParseStmts怎么用?Golang ParseStmts使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ParseStmts函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: loadRuleFiles
// loadRuleFiles loads alerting and recording rules from the given files.
func (m *Manager) loadRuleFiles(filenames ...string) error {
for _, fn := range filenames {
content, err := ioutil.ReadFile(fn)
if err != nil {
return err
}
stmts, err := promql.ParseStmts(string(content))
if err != nil {
return fmt.Errorf("error parsing %s: %s", fn, err)
}
for _, stmt := range stmts {
switch r := stmt.(type) {
case *promql.AlertStmt:
rule := NewAlertingRule(r.Name, r.Expr, r.Duration, r.Labels, r.Summary, r.Description, r.Runbook)
m.rules = append(m.rules, rule)
case *promql.RecordStmt:
rule := NewRecordingRule(r.Name, r.Expr, r.Labels)
m.rules = append(m.rules, rule)
default:
panic("retrieval.Manager.LoadRuleFiles: unknown statement type")
}
}
}
return nil
}
示例2: loadGroups
// loadGroups reads groups from a list of files.
// As there's currently no group syntax a single group named "default" containing
// all rules will be returned.
func (m *Manager) loadGroups(interval time.Duration, filenames ...string) (map[string]*Group, error) {
rules := []Rule{}
for _, fn := range filenames {
content, err := ioutil.ReadFile(fn)
if err != nil {
return nil, err
}
stmts, err := promql.ParseStmts(string(content))
if err != nil {
return nil, fmt.Errorf("error parsing %s: %s", fn, err)
}
for _, stmt := range stmts {
var rule Rule
switch r := stmt.(type) {
case *promql.AlertStmt:
rule = NewAlertingRule(r.Name, r.Expr, r.Duration, r.Labels, r.Annotations)
case *promql.RecordStmt:
rule = NewRecordingRule(r.Name, r.Expr, r.Labels)
default:
panic("retrieval.Manager.LoadRuleFiles: unknown statement type")
}
rules = append(rules, rule)
}
}
// Currently there is no group syntax implemented. Thus all rules
// are read into a single default group.
g := NewGroup("default", interval, rules, m.opts)
groups := map[string]*Group{g.name: g}
return groups, nil
}
示例3: checkRules
// checkRules reads rules from in. Sucessfully read rules
// are printed to out.
func checkRules(filename string, in io.Reader, out io.Writer) error {
content, err := ioutil.ReadAll(in)
if err != nil {
return err
}
rules, err := promql.ParseStmts(string(content))
if err != nil {
return err
}
fmt.Fprintf(os.Stderr, "%s: successfully loaded %d rules:\n", filename, len(rules))
for _, rule := range rules {
fmt.Fprint(out, rule.String())
fmt.Fprint(out, "\n")
}
return nil
}
示例4: checkRules
func checkRules(t cli.Term, filename string) (int, error) {
t.Infof("Checking %s", filename)
if stat, err := os.Stat(filename); err != nil {
return 0, fmt.Errorf("cannot get file info")
} else if stat.IsDir() {
return 0, fmt.Errorf("is a directory")
}
content, err := ioutil.ReadFile(filename)
if err != nil {
return 0, err
}
rules, err := promql.ParseStmts(string(content))
if err != nil {
return 0, err
}
return len(rules), nil
}