2013-04-24 02:51:40 -07:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-06-13 07:10:05 -07:00
|
|
|
"html/template"
|
2014-12-10 07:16:49 -08:00
|
|
|
"reflect"
|
2013-06-13 07:10:05 -07:00
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
2013-04-24 02:51:40 -07:00
|
|
|
"github.com/prometheus/prometheus/rules/ast"
|
2013-06-03 08:07:03 -07:00
|
|
|
"github.com/prometheus/prometheus/stats"
|
2014-06-06 02:55:53 -07:00
|
|
|
"github.com/prometheus/prometheus/storage/local"
|
2013-04-24 02:51:40 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// A RecordingRule records its vector expression into new timeseries.
|
|
|
|
type RecordingRule struct {
|
|
|
|
name string
|
|
|
|
vector ast.VectorNode
|
2013-06-25 05:02:27 -07:00
|
|
|
labels clientmodel.LabelSet
|
2013-04-24 02:51:40 -07:00
|
|
|
permanent bool
|
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// Name returns the rule name.
|
2013-04-24 02:51:40 -07:00
|
|
|
func (rule RecordingRule) Name() string { return rule.name }
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// EvalRaw returns the raw value of the rule expression.
|
2014-09-16 06:47:24 -07:00
|
|
|
func (rule RecordingRule) EvalRaw(timestamp clientmodel.Timestamp, storage local.Storage) (ast.Vector, error) {
|
2013-06-03 08:07:03 -07:00
|
|
|
return ast.EvalVectorInstant(rule.vector, timestamp, storage, stats.NewTimerGroup())
|
2013-04-24 02:51:40 -07:00
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// Eval evaluates the rule and then overrides the metric names and labels accordingly.
|
2014-09-16 06:47:24 -07:00
|
|
|
func (rule RecordingRule) Eval(timestamp clientmodel.Timestamp, storage local.Storage) (ast.Vector, error) {
|
2013-05-15 22:38:31 -07:00
|
|
|
vector, err := rule.EvalRaw(timestamp, storage)
|
2013-04-24 02:51:40 -07:00
|
|
|
if err != nil {
|
2013-05-15 22:38:31 -07:00
|
|
|
return nil, err
|
2013-04-24 02:51:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Override the metric name and labels.
|
|
|
|
for _, sample := range vector {
|
2014-12-08 07:55:49 -08:00
|
|
|
sample.Metric.Set(clientmodel.MetricNameLabel, clientmodel.LabelValue(rule.name))
|
2013-04-24 02:51:40 -07:00
|
|
|
for label, value := range rule.labels {
|
|
|
|
if value == "" {
|
2014-12-08 07:55:49 -08:00
|
|
|
sample.Metric.Delete(label)
|
2013-04-24 02:51:40 -07:00
|
|
|
} else {
|
2014-12-08 07:55:49 -08:00
|
|
|
sample.Metric.Set(label, value)
|
2013-04-24 02:51:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-15 22:38:31 -07:00
|
|
|
|
|
|
|
return vector, nil
|
2013-04-24 02:51:40 -07:00
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// ToDotGraph returns the text representation of a dot graph.
|
2013-04-26 07:02:52 -07:00
|
|
|
func (rule RecordingRule) ToDotGraph() string {
|
2014-12-10 07:16:49 -08:00
|
|
|
graph := fmt.Sprintf(
|
|
|
|
`digraph "Rules" {
|
2013-04-26 07:02:52 -07:00
|
|
|
%#p[shape="box",label="%s = "];
|
2014-12-10 07:16:49 -08:00
|
|
|
%#p -> %x;
|
2013-04-26 07:02:52 -07:00
|
|
|
%s
|
2014-12-10 07:16:49 -08:00
|
|
|
}`,
|
|
|
|
&rule, rule.name,
|
|
|
|
&rule, reflect.ValueOf(rule.vector).Pointer(),
|
|
|
|
rule.vector.NodeTreeToDotGraph(),
|
|
|
|
)
|
2013-04-24 02:51:40 -07:00
|
|
|
return graph
|
|
|
|
}
|
|
|
|
|
2013-06-06 06:12:37 -07:00
|
|
|
func (rule RecordingRule) String() string {
|
|
|
|
return fmt.Sprintf("%s%s = %s\n", rule.name, rule.labels, rule.vector)
|
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// HTMLSnippet returns an HTML snippet representing this rule.
|
2013-06-13 07:10:05 -07:00
|
|
|
func (rule RecordingRule) HTMLSnippet() template.HTML {
|
|
|
|
ruleExpr := rule.vector.String()
|
|
|
|
return template.HTML(fmt.Sprintf(
|
|
|
|
`<a href="%s">%s</a>%s = <a href="%s">%s</a>`,
|
2014-07-25 05:23:47 -07:00
|
|
|
GraphLinkForExpression(rule.name),
|
2013-06-13 07:10:05 -07:00
|
|
|
rule.name,
|
|
|
|
rule.labels,
|
2014-07-25 05:23:47 -07:00
|
|
|
GraphLinkForExpression(ruleExpr),
|
2013-06-13 07:10:05 -07:00
|
|
|
ruleExpr))
|
|
|
|
}
|