mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-25 05:34:05 -08:00
rules: remove classic UI code (#10730)
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
This commit is contained in:
parent
44e5f220c0
commit
0d94cdf107
|
@ -16,7 +16,6 @@ package rules
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
html_template "html/template"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -35,7 +34,6 @@ import (
|
||||||
"github.com/prometheus/prometheus/promql/parser"
|
"github.com/prometheus/prometheus/promql/parser"
|
||||||
"github.com/prometheus/prometheus/storage"
|
"github.com/prometheus/prometheus/storage"
|
||||||
"github.com/prometheus/prometheus/template"
|
"github.com/prometheus/prometheus/template"
|
||||||
"github.com/prometheus/prometheus/util/strutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -44,8 +42,6 @@ const (
|
||||||
// AlertForStateMetricName is the metric name for 'for' state of alert.
|
// AlertForStateMetricName is the metric name for 'for' state of alert.
|
||||||
alertForStateMetricName = "ALERTS_FOR_STATE"
|
alertForStateMetricName = "ALERTS_FOR_STATE"
|
||||||
|
|
||||||
// AlertNameLabel is the label name indicating the name of an alert.
|
|
||||||
alertNameLabel = "alertname"
|
|
||||||
// AlertStateLabel is the label name indicating the state of an alert.
|
// AlertStateLabel is the label name indicating the state of an alert.
|
||||||
alertStateLabel = "alertstate"
|
alertStateLabel = "alertstate"
|
||||||
)
|
)
|
||||||
|
@ -548,37 +544,3 @@ func (r *AlertingRule) String() string {
|
||||||
|
|
||||||
return string(byt)
|
return string(byt)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTMLSnippet returns an HTML snippet representing this alerting rule. The
|
|
||||||
// resulting snippet is expected to be presented in a <pre> element, so that
|
|
||||||
// line breaks and other returned whitespace is respected.
|
|
||||||
func (r *AlertingRule) HTMLSnippet(pathPrefix string) html_template.HTML {
|
|
||||||
alertMetric := model.Metric{
|
|
||||||
model.MetricNameLabel: alertMetricName,
|
|
||||||
alertNameLabel: model.LabelValue(r.name),
|
|
||||||
}
|
|
||||||
|
|
||||||
labelsMap := make(map[string]string, len(r.labels))
|
|
||||||
for _, l := range r.labels {
|
|
||||||
labelsMap[l.Name] = html_template.HTMLEscapeString(l.Value)
|
|
||||||
}
|
|
||||||
|
|
||||||
annotationsMap := make(map[string]string, len(r.annotations))
|
|
||||||
for _, l := range r.annotations {
|
|
||||||
annotationsMap[l.Name] = html_template.HTMLEscapeString(l.Value)
|
|
||||||
}
|
|
||||||
|
|
||||||
ar := rulefmt.Rule{
|
|
||||||
Alert: fmt.Sprintf("<a href=%q>%s</a>", pathPrefix+strutil.TableLinkForExpression(alertMetric.String()), r.name),
|
|
||||||
Expr: fmt.Sprintf("<a href=%q>%s</a>", pathPrefix+strutil.TableLinkForExpression(r.vector.String()), html_template.HTMLEscapeString(r.vector.String())),
|
|
||||||
For: model.Duration(r.holdDuration),
|
|
||||||
Labels: labelsMap,
|
|
||||||
Annotations: annotationsMap,
|
|
||||||
}
|
|
||||||
|
|
||||||
byt, err := yaml.Marshal(ar)
|
|
||||||
if err != nil {
|
|
||||||
return html_template.HTML(fmt.Sprintf("error marshaling alerting rule: %q", html_template.HTMLEscapeString(err.Error())))
|
|
||||||
}
|
|
||||||
return html_template.HTML(byt)
|
|
||||||
}
|
|
||||||
|
|
|
@ -15,7 +15,6 @@ package rules
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"html/template"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -31,23 +30,6 @@ import (
|
||||||
"github.com/prometheus/prometheus/util/teststorage"
|
"github.com/prometheus/prometheus/util/teststorage"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAlertingRuleHTMLSnippet(t *testing.T) {
|
|
||||||
expr, err := parser.ParseExpr(`foo{html="<b>BOLD<b>"}`)
|
|
||||||
require.NoError(t, err)
|
|
||||||
rule := NewAlertingRule("testrule", expr, 0, labels.FromStrings("html", "<b>BOLD</b>"), labels.FromStrings("html", "<b>BOLD</b>"), nil, "", false, nil)
|
|
||||||
|
|
||||||
const want = template.HTML(`alert: <a href="/test/prefix/graph?g0.expr=ALERTS%7Balertname%3D%22testrule%22%7D&g0.tab=1">testrule</a>
|
|
||||||
expr: <a href="/test/prefix/graph?g0.expr=foo%7Bhtml%3D%22%3Cb%3EBOLD%3Cb%3E%22%7D&g0.tab=1">foo{html="<b>BOLD<b>"}</a>
|
|
||||||
labels:
|
|
||||||
html: '<b>BOLD</b>'
|
|
||||||
annotations:
|
|
||||||
html: '<b>BOLD</b>'
|
|
||||||
`)
|
|
||||||
|
|
||||||
got := rule.HTMLSnippet("/test/prefix")
|
|
||||||
require.Equal(t, want, got, "incorrect HTML snippet; want:\n\n|%v|\n\ngot:\n\n|%v|", want, got)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAlertingRuleState(t *testing.T) {
|
func TestAlertingRuleState(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|
|
@ -15,7 +15,6 @@ package rules
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
html_template "html/template"
|
|
||||||
"math"
|
"math"
|
||||||
"net/url"
|
"net/url"
|
||||||
"sort"
|
"sort"
|
||||||
|
@ -235,9 +234,6 @@ type Rule interface {
|
||||||
// GetEvaluationTimestamp returns last evaluation timestamp.
|
// GetEvaluationTimestamp returns last evaluation timestamp.
|
||||||
// NOTE: Used dynamically by rules.html template.
|
// NOTE: Used dynamically by rules.html template.
|
||||||
GetEvaluationTimestamp() time.Time
|
GetEvaluationTimestamp() time.Time
|
||||||
// HTMLSnippet returns a human-readable string representation of the rule,
|
|
||||||
// decorated with HTML elements for use the web frontend.
|
|
||||||
HTMLSnippet(pathPrefix string) html_template.HTML
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group is a set of rules that have a logical relation.
|
// Group is a set of rules that have a logical relation.
|
||||||
|
|
|
@ -16,7 +16,6 @@ package rules
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -27,7 +26,6 @@ import (
|
||||||
"github.com/prometheus/prometheus/model/rulefmt"
|
"github.com/prometheus/prometheus/model/rulefmt"
|
||||||
"github.com/prometheus/prometheus/promql"
|
"github.com/prometheus/prometheus/promql"
|
||||||
"github.com/prometheus/prometheus/promql/parser"
|
"github.com/prometheus/prometheus/promql/parser"
|
||||||
"github.com/prometheus/prometheus/util/strutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// A RecordingRule records its vector expression into new timeseries.
|
// A RecordingRule records its vector expression into new timeseries.
|
||||||
|
@ -179,25 +177,3 @@ func (rule *RecordingRule) GetEvaluationTimestamp() time.Time {
|
||||||
defer rule.mtx.Unlock()
|
defer rule.mtx.Unlock()
|
||||||
return rule.evaluationTimestamp
|
return rule.evaluationTimestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTMLSnippet returns an HTML snippet representing this rule.
|
|
||||||
func (rule *RecordingRule) HTMLSnippet(pathPrefix string) template.HTML {
|
|
||||||
ruleExpr := rule.vector.String()
|
|
||||||
labels := make(map[string]string, len(rule.labels))
|
|
||||||
for _, l := range rule.labels {
|
|
||||||
labels[l.Name] = template.HTMLEscapeString(l.Value)
|
|
||||||
}
|
|
||||||
|
|
||||||
r := rulefmt.Rule{
|
|
||||||
Record: fmt.Sprintf(`<a href="%s">%s</a>`, pathPrefix+strutil.TableLinkForExpression(rule.name), rule.name),
|
|
||||||
Expr: fmt.Sprintf(`<a href="%s">%s</a>`, pathPrefix+strutil.TableLinkForExpression(ruleExpr), template.HTMLEscapeString(ruleExpr)),
|
|
||||||
Labels: labels,
|
|
||||||
}
|
|
||||||
|
|
||||||
byt, err := yaml.Marshal(r)
|
|
||||||
if err != nil {
|
|
||||||
return template.HTML(fmt.Sprintf("error marshaling recording rule: %q", template.HTMLEscapeString(err.Error())))
|
|
||||||
}
|
|
||||||
|
|
||||||
return template.HTML(byt)
|
|
||||||
}
|
|
||||||
|
|
|
@ -15,7 +15,6 @@ package rules
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"html/template"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -84,21 +83,6 @@ func TestRuleEval(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRecordingRuleHTMLSnippet(t *testing.T) {
|
|
||||||
expr, err := parser.ParseExpr(`foo{html="<b>BOLD<b>"}`)
|
|
||||||
require.NoError(t, err)
|
|
||||||
rule := NewRecordingRule("testrule", expr, labels.FromStrings("html", "<b>BOLD</b>"))
|
|
||||||
|
|
||||||
const want = template.HTML(`record: <a href="/test/prefix/graph?g0.expr=testrule&g0.tab=1">testrule</a>
|
|
||||||
expr: <a href="/test/prefix/graph?g0.expr=foo%7Bhtml%3D%22%3Cb%3EBOLD%3Cb%3E%22%7D&g0.tab=1">foo{html="<b>BOLD<b>"}</a>
|
|
||||||
labels:
|
|
||||||
html: '<b>BOLD</b>'
|
|
||||||
`)
|
|
||||||
|
|
||||||
got := rule.HTMLSnippet("/test/prefix")
|
|
||||||
require.Equal(t, want, got, "incorrect HTML snippet; want:\n\n%s\n\ngot:\n\n%s", want, got)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestRuleEvalDuplicate tests for duplicate labels in recorded metrics, see #5529.
|
// TestRuleEvalDuplicate tests for duplicate labels in recorded metrics, see #5529.
|
||||||
func TestRuleEvalDuplicate(t *testing.T) {
|
func TestRuleEvalDuplicate(t *testing.T) {
|
||||||
storage := teststorage.New(t)
|
storage := teststorage.New(t)
|
||||||
|
|
Loading…
Reference in a new issue