rules: remove classic UI code (#10730)

Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
This commit is contained in:
Julien Pivotto 2022-05-23 16:21:50 +02:00 committed by GitHub
parent 44e5f220c0
commit 0d94cdf107
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 0 additions and 100 deletions

View File

@ -16,7 +16,6 @@ package rules
import (
"context"
"fmt"
html_template "html/template"
"net/url"
"strings"
"sync"
@ -35,7 +34,6 @@ import (
"github.com/prometheus/prometheus/promql/parser"
"github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/template"
"github.com/prometheus/prometheus/util/strutil"
)
const (
@ -44,8 +42,6 @@ const (
// AlertForStateMetricName is the metric name for 'for' state of alert.
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 = "alertstate"
)
@ -548,37 +544,3 @@ func (r *AlertingRule) String() string {
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)
}

View File

@ -15,7 +15,6 @@ package rules
import (
"context"
"html/template"
"testing"
"time"
@ -31,23 +30,6 @@ import (
"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=&#34;&lt;b&gt;BOLD&lt;b&gt;&#34;}</a>
labels:
html: '&lt;b&gt;BOLD&lt;/b&gt;'
annotations:
html: '&lt;b&gt;BOLD&lt;/b&gt;'
`)
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) {
tests := []struct {
name string

View File

@ -15,7 +15,6 @@ package rules
import (
"context"
html_template "html/template"
"math"
"net/url"
"sort"
@ -235,9 +234,6 @@ type Rule interface {
// GetEvaluationTimestamp returns last evaluation timestamp.
// NOTE: Used dynamically by rules.html template.
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.

View File

@ -16,7 +16,6 @@ package rules
import (
"context"
"fmt"
"html/template"
"net/url"
"sync"
"time"
@ -27,7 +26,6 @@ import (
"github.com/prometheus/prometheus/model/rulefmt"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/promql/parser"
"github.com/prometheus/prometheus/util/strutil"
)
// A RecordingRule records its vector expression into new timeseries.
@ -179,25 +177,3 @@ func (rule *RecordingRule) GetEvaluationTimestamp() time.Time {
defer rule.mtx.Unlock()
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)
}

View File

@ -15,7 +15,6 @@ package rules
import (
"context"
"html/template"
"testing"
"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=&#34;&lt;b&gt;BOLD&lt;b&gt;&#34;}</a>
labels:
html: '&lt;b&gt;BOLD&lt;/b&gt;'
`)
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.
func TestRuleEvalDuplicate(t *testing.T) {
storage := teststorage.New(t)