Compare commits

..

2 Commits

Author SHA1 Message Date
Hui Wang
72eb3c6cf6 Merge branch 'master' into improve-template 2025-01-13 14:17:03 +08:00
Haley Wang
2489095595 vmalert: improve template performance 2025-01-13 14:10:23 +08:00
73 changed files with 2661 additions and 3368 deletions

View File

@@ -567,7 +567,7 @@ golangci-lint: install-golangci-lint
golangci-lint run
install-golangci-lint:
which golangci-lint || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.63.4
which golangci-lint || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.60.3
remove-golangci-lint:
rm -rf `which golangci-lint`

View File

@@ -176,7 +176,7 @@ func writeCompactObject(w io.Writer, fields []logstorage.Field) error {
_, err := fmt.Fprintf(w, "%s\n", fields[0].Value)
return err
}
if len(fields) == 2 && (fields[0].Name == "_time" || fields[1].Name == "_time") {
if len(fields) == 2 && fields[0].Name == "_time" || fields[1].Name == "_time" {
// Write _time\tfieldValue as is
if fields[0].Name == "_time" {
_, err := fmt.Fprintf(w, "%s\t%s\n", fields[0].Value, fields[1].Value)

View File

@@ -8,6 +8,8 @@ tests:
input_series:
- series: 'up{job="vmagent2", instance="localhost:9090"}'
values: "0+0x1440"
- series: "test_query"
values: "1x200"
metricsql_expr_test:
- expr: suquery_interval_test
@@ -32,7 +34,8 @@ tests:
- eval_time: 0
alertname: AlwaysFiring
exp_alerts:
- {}
- exp_annotations:
queryValue: "1"
- eval_time: 0
alertname: InstanceDown

View File

@@ -12,6 +12,8 @@ groups:
dashboard: '{{ $externalURL }}/d/dashboard?orgId=1'
- alert: AlwaysFiring
expr: 1
annotations:
queryValue: "{{ query \"test_query\" | first | value }}"
- alert: SameAlertNameWithDifferentGroup
expr: absent(test)
for: 1m

View File

@@ -10,6 +10,8 @@ tests:
input_series:
- series: "test"
values: "_x5 1x5 _ stale"
- series: "test_query"
values: "1x100"
alert_rule_test:
- eval_time: 1m
@@ -50,6 +52,8 @@ tests:
values: "0+0x1440"
- series: "test"
values: "0+1x1440"
- series: "test_query"
values: "0+1x100"
metricsql_expr_test:
- expr: count(ALERTS) by (alertgroup, alertname, alertstate)
@@ -97,6 +101,17 @@ tests:
exp_alerts:
- exp_labels:
cluster: prod
exp_annotations:
queryValue: "0"
- eval_time: 5m
groupname: group1
alertname: AlwaysFiring
exp_alerts:
- exp_labels:
cluster: prod
exp_annotations:
queryValue: "5"
- eval_time: 0
groupname: alerts

View File

@@ -8,6 +8,8 @@ tests:
input_series:
- series: 'up{job="vmagent2", instance="localhost:9090"}'
values: "0+0x1440"
- series: "test_query"
values: "0+1x200"
metricsql_expr_test:
- expr: suquery_interval_test
@@ -37,6 +39,8 @@ tests:
exp_alerts:
- exp_labels:
cluster: prod
exp_annotations:
queryValue: "0"
- eval_time: 0
groupname: group1

View File

@@ -64,7 +64,18 @@ func UnitTest(files []string, disableGroupLabel bool, externalLabels []string, e
if err != nil {
logger.Fatalf("failed to parse external URL: %w", err)
}
if err := templates.Load([]string{}, *eu); err != nil {
labels := make(map[string]string)
for _, s := range externalLabels {
if len(s) == 0 {
continue
}
n := strings.IndexByte(s, '=')
if n < 0 {
logger.Fatalf("missing '=' in `-label`. It must contain label in the form `name=value`; got %q", s)
}
labels[s[:n]] = s[n+1:]
}
if err := templates.Init([]string{}, labels, *eu); err != nil {
logger.Fatalf("failed to load template: %v", err)
}
storagePath = filepath.Join(os.TempDir(), testStoragePath)
@@ -84,19 +95,7 @@ func UnitTest(files []string, disableGroupLabel bool, externalLabels []string, e
if len(testfiles) == 0 {
logger.Fatalf("no test file found")
}
labels := make(map[string]string)
for _, s := range externalLabels {
if len(s) == 0 {
continue
}
n := strings.IndexByte(s, '=')
if n < 0 {
logger.Fatalf("missing '=' in `-label`. It must contain label in the form `name=value`; got %q", s)
}
labels[s[:n]] = s[n+1:]
}
_, err = notifier.Init(nil, labels, externalURL)
_, err = notifier.Init(nil)
if err != nil {
logger.Fatalf("failed to init notifier: %v", err)
}

View File

@@ -9,14 +9,13 @@ import (
"testing"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/notifier"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/templates"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
"gopkg.in/yaml.v2"
)
func TestMain(m *testing.M) {
if err := templates.Load([]string{"testdata/templates/*good.tmpl"}, url.URL{}); err != nil {
if err := templates.Init([]string{"testdata/templates/*good.tmpl"}, nil, url.URL{}); err != nil {
os.Exit(1)
}
os.Exit(m.Run())
@@ -79,7 +78,7 @@ groups:
for i, u := range urls {
urls[i] = srv.URL + u
}
_, err := Parse(urls, notifier.ValidateTemplates, true)
_, err := Parse(urls, templates.ValidateTemplates, true)
if err != nil && !expErr {
t.Fatalf("error parsing URLs %s", err)
}
@@ -95,7 +94,7 @@ groups:
}
func TestParse_Success(t *testing.T) {
_, err := Parse([]string{"testdata/rules/*good.rules", "testdata/dir/*good.*"}, notifier.ValidateTemplates, true)
_, err := Parse([]string{"testdata/rules/*good.rules", "testdata/dir/*good.*"}, templates.ValidateTemplates, true)
if err != nil {
t.Fatalf("error parsing files %s", err)
}
@@ -105,7 +104,7 @@ func TestParse_Failure(t *testing.T) {
f := func(paths []string, errStrExpected string) {
t.Helper()
_, err := Parse(paths, notifier.ValidateTemplates, true)
_, err := Parse(paths, templates.ValidateTemplates, true)
if err == nil {
t.Fatalf("expected to get error")
}
@@ -116,7 +115,7 @@ func TestParse_Failure(t *testing.T) {
f([]string{"testdata/rules/rules_interval_bad.rules"}, "eval_offset should be smaller than interval")
f([]string{"testdata/rules/rules0-bad.rules"}, "unexpected token")
f([]string{"testdata/dir/rules0-bad.rules"}, "error parsing annotation")
f([]string{"testdata/dir/rules0-bad.rules"}, "failed to parse text")
f([]string{"testdata/dir/rules1-bad.rules"}, "duplicate in file")
f([]string{"testdata/dir/rules2-bad.rules"}, "function \"unknown\" not defined")
f([]string{"testdata/dir/rules3-bad.rules"}, "either `record` or `alert` must be set")
@@ -330,7 +329,7 @@ func TestGroupValidate_Success(t *testing.T) {
var validateTplFn ValidateTplFn
if validateAnnotations {
validateTplFn = notifier.ValidateTemplates
validateTplFn = templates.ValidateTemplates
}
err := group.Validate(validateTplFn, validateExpressions)
if err != nil {

View File

@@ -83,7 +83,6 @@ absolute path to all .tpl files in root.
var (
alertURLGeneratorFn notifier.AlertURLGenerator
extURL *url.URL
)
func main() {
@@ -99,18 +98,29 @@ func main() {
logger.Init()
var err error
extURL, err = getExternalURL(*externalURL)
extURL, err := getExternalURL(*externalURL)
if err != nil {
logger.Fatalf("failed to init external.url %q: %s", *externalURL, err)
}
externalls := make(map[string]string)
for _, s := range *externalLabels {
if len(s) == 0 {
continue
}
n := strings.IndexByte(s, '=')
if n < 0 {
logger.Fatalf("wrong format in `-external.label`, it must contain label as `Name=value`; got %q", s)
}
externalls[s[:n]] = s[n+1:]
}
err = templates.Load(*ruleTemplatesPath, *extURL)
err = templates.Init(*ruleTemplatesPath, externalls, *extURL)
if err != nil {
logger.Fatalf("failed to load template %q: %s", *ruleTemplatesPath, err)
}
if *dryRun {
groups, err := config.Parse(*rulePath, notifier.ValidateTemplates, true)
groups, err := config.Parse(*rulePath, templates.ValidateTemplates, true)
if err != nil {
logger.Fatalf("failed to parse %q: %s", *rulePath, err)
}
@@ -127,7 +137,7 @@ func main() {
var validateTplFn config.ValidateTplFn
if *validateTemplates {
validateTplFn = notifier.ValidateTemplates
validateTplFn = templates.ValidateTemplates
}
if *replayFrom != "" {
@@ -156,7 +166,7 @@ func main() {
}
ctx, cancel := context.WithCancel(context.Background())
manager, err := newManager(ctx)
manager, err := newManager(ctx, externalls)
if err != nil {
logger.Fatalf("failed to init: %s", err)
}
@@ -203,25 +213,13 @@ var (
configTimestamp = metrics.NewCounter(`vmalert_config_last_reload_success_timestamp_seconds`)
)
func newManager(ctx context.Context) (*manager, error) {
func newManager(ctx context.Context, externalls map[string]string) (*manager, error) {
q, err := datasource.Init(nil)
if err != nil {
return nil, fmt.Errorf("failed to init datasource: %w", err)
}
labels := make(map[string]string)
for _, s := range *externalLabels {
if len(s) == 0 {
continue
}
n := strings.IndexByte(s, '=')
if n < 0 {
return nil, fmt.Errorf("missing '=' in `-label`. It must contain label in the form `Name=value`; got %q", s)
}
labels[s[:n]] = s[n+1:]
}
nts, err := notifier.Init(alertURLGeneratorFn, labels, *externalURL)
nts, err := notifier.Init(alertURLGeneratorFn)
if err != nil {
return nil, fmt.Errorf("failed to init notifier: %w", err)
}
@@ -229,7 +227,7 @@ func newManager(ctx context.Context) (*manager, error) {
groups: make(map[uint64]*rule.Group),
querierBuilder: q,
notifiers: nts,
labels: labels,
labels: externalls,
}
rw, err := remotewrite.Init(ctx)
if err != nil {
@@ -293,24 +291,36 @@ func getAlertURLGenerator(externalURL *url.URL, externalAlertSource string, vali
}, nil
}
if validateTemplate {
if err := notifier.ValidateTemplates(map[string]string{
if err := templates.ValidateTemplates(map[string]string{
"tpl": externalAlertSource,
}); err != nil {
return nil, fmt.Errorf("error validating source template %s: %w", externalAlertSource, err)
return nil, fmt.Errorf("cannot parse `external.alert.source` %q: %w", externalAlertSource, err)
}
}
m := map[string]string{
"tpl": externalAlertSource,
var err error
tmpl := templates.GetCurrentTmpl()
tmpl, err = templates.ParseWithFixedHeader(externalAlertSource, tmpl)
if err != nil {
return nil, err
}
return func(alert notifier.Alert) string {
qFn := func(_ string) ([]datasource.Metric, error) {
return nil, fmt.Errorf("`query` template isn't supported for alert source template")
// recreate template if it was changed during config reload
cm := templates.GetCurrentTmpl()
if tmpl.Name() != cm.Name() {
tmpl = cm
tmpl, err = templates.ParseWithFixedHeader(externalAlertSource, tmpl)
if err != nil {
logger.Errorf("cannot parse `external.alert.source` %q: %w", externalAlertSource, err)
return fmt.Sprintf("%s/%s", externalURL, externalAlertSource)
}
}
templated, err := alert.ExecTemplate(qFn, alert.Labels, m)
tplData := alert.ToTplData()
rr, err := templates.ExecuteWithTemplate(tplData, tmpl)
if err != nil {
logger.Errorf("cannot template alert source: %s", err)
logger.Errorf("can not template alert source: %v", err)
return fmt.Sprintf("%s/%s", externalURL, externalAlertSource)
}
return fmt.Sprintf("%s/%s", externalURL, templated["tpl"])
return fmt.Sprintf("%s/%s", externalURL, rr)
}, nil
}
@@ -334,7 +344,7 @@ func configReload(ctx context.Context, m *manager, groupsCfg []config.Group, sig
var validateTplFn config.ValidateTplFn
if *validateTemplates {
validateTplFn = notifier.ValidateTemplates
validateTplFn = templates.ValidateTemplates
}
// init metrics for config state with positive values to improve alerting conditions
@@ -363,7 +373,7 @@ func configReload(ctx context.Context, m *manager, groupsCfg []config.Group, sig
logger.Errorf("failed to reload notifier config: %s", err)
continue
}
err := templates.Load(*ruleTemplatesPath, *extURL)
err := templates.LoadTemplateFile(*ruleTemplatesPath)
if err != nil {
setConfigError(err)
logger.Errorf("failed to load new templates: %s", err)
@@ -376,7 +386,6 @@ func configReload(ctx context.Context, m *manager, groupsCfg []config.Group, sig
continue
}
if configsEqual(newGroupsCfg, groupsCfg) {
templates.Reload()
// set success to 1 since previous reload could have been unsuccessful
// do not update configTimestamp as config version remains old.
configSuccess.Set(1)
@@ -390,7 +399,6 @@ func configReload(ctx context.Context, m *manager, groupsCfg []config.Group, sig
logger.Errorf("error while reloading rules: %s", err)
continue
}
templates.Reload()
groupsCfg = newGroupsCfg
setConfigSuccessAt(fasttime.UnixTimestamp())
logger.Infof("Rules reloaded successfully from %q", *rulePath)

View File

@@ -74,10 +74,7 @@ func TestGetAlertURLGenerator(t *testing.T) {
func TestConfigReload(t *testing.T) {
originalRulePath := *rulePath
originalExternalURL := extURL
extURL = &url.URL{}
defer func() {
extURL = originalExternalURL
*rulePath = originalRulePath
}()

View File

@@ -160,8 +160,8 @@ func (m *manager) update(ctx context.Context, groupsCfg []config.Group, restore
// it is important to call InterruptEval before the update, because cancel fn
// can be re-assigned during the update.
item.old.InterruptEval()
go func(oldGroup *rule.Group, newGroup *rule.Group) {
oldGroup.UpdateWith(newGroup)
go func(old *rule.Group, new *rule.Group) {
old.UpdateWith(new)
wg.Done()
}(item.old, item.new)
}

View File

@@ -19,7 +19,7 @@ import (
)
func TestMain(m *testing.M) {
if err := templates.Load([]string{"testdata/templates/*good.tmpl"}, url.URL{}); err != nil {
if err := templates.Init([]string{"testdata/templates/*good.tmpl"}, nil, url.URL{}); err != nil {
os.Exit(1)
}
os.Exit(m.Run())
@@ -72,7 +72,7 @@ func TestManagerUpdateConcurrent(t *testing.T) {
r := rand.New(rand.NewSource(int64(n)))
for i := 0; i < iterations; i++ {
rnd := r.Intn(len(paths))
cfg, err := config.Parse([]string{paths[rnd]}, notifier.ValidateTemplates, true)
cfg, err := config.Parse([]string{paths[rnd]}, templates.ValidateTemplates, true)
if err != nil { // update can fail and this is expected
continue
}
@@ -135,7 +135,7 @@ func TestManagerUpdate_Success(t *testing.T) {
t.Fatalf("failed to complete initial rules update: %s", err)
}
cfgUpdate, err := config.Parse([]string{updatePath}, notifier.ValidateTemplates, true)
cfgUpdate, err := config.Parse([]string{updatePath}, templates.ValidateTemplates, true)
if err == nil { // update can fail and that's expected
_ = m.update(ctx, cfgUpdate, false)
}
@@ -326,7 +326,7 @@ func loadCfg(t *testing.T, path []string, validateAnnotations, validateExpressio
t.Helper()
var validateTplFn config.ValidateTplFn
if validateAnnotations {
validateTplFn = notifier.ValidateTemplates
validateTplFn = templates.ValidateTemplates
}
cfg, err := config.Parse(path, validateTplFn, validateExpressions)
if err != nil {

View File

@@ -1,15 +1,9 @@
package notifier
import (
"bytes"
"fmt"
"io"
"strings"
textTpl "text/template"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/templates"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/utils"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promrelabel"
)
@@ -76,118 +70,18 @@ func (as AlertState) String() string {
return "inactive"
}
// AlertTplData is used to execute templating
type AlertTplData struct {
Labels map[string]string
Value float64
Expr string
AlertID uint64
GroupID uint64
ActiveAt time.Time
For time.Duration
}
var tplHeaders = []string{
"{{ $value := .Value }}",
"{{ $labels := .Labels }}",
"{{ $expr := .Expr }}",
"{{ $externalLabels := .ExternalLabels }}",
"{{ $externalURL := .ExternalURL }}",
"{{ $alertID := .AlertID }}",
"{{ $groupID := .GroupID }}",
"{{ $activeAt := .ActiveAt }}",
"{{ $for := .For }}",
}
// ExecTemplate executes the Alert template for given
// map of annotations.
// Every alert could have a different datasource, so function
// requires a queryFunction as an argument.
func (a *Alert) ExecTemplate(q templates.QueryFn, labels, annotations map[string]string) (map[string]string, error) {
tplData := AlertTplData{
// ToTplData converts Alert to AlertTplData,
// which only exposes necessary fields for template.
func (a Alert) ToTplData() templates.AlertTplData {
return templates.AlertTplData{
Value: a.Value,
Labels: labels,
Labels: a.Labels,
Expr: a.Expr,
AlertID: a.ID,
GroupID: a.GroupID,
ActiveAt: a.ActiveAt,
For: a.For,
}
return ExecTemplate(q, annotations, tplData)
}
// ExecTemplate executes the given template for given annotations map.
func ExecTemplate(q templates.QueryFn, annotations map[string]string, tplData AlertTplData) (map[string]string, error) {
tmpl, err := templates.GetWithFuncs(templates.FuncsWithQuery(q))
if err != nil {
return nil, fmt.Errorf("error cloning template: %w", err)
}
return templateAnnotations(annotations, tplData, tmpl, true)
}
// ValidateTemplates validate annotations for possible template error, uses empty data for template population
func ValidateTemplates(annotations map[string]string) error {
tmpl, err := templates.GetWithFuncs(nil)
if err != nil {
return err
}
_, err = templateAnnotations(annotations, AlertTplData{
Labels: map[string]string{},
Value: 0,
}, tmpl, false)
return err
}
func templateAnnotations(annotations map[string]string, data AlertTplData, tmpl *textTpl.Template, execute bool) (map[string]string, error) {
var builder strings.Builder
var buf bytes.Buffer
eg := new(utils.ErrGroup)
r := make(map[string]string, len(annotations))
tData := tplData{data, externalLabels, externalURL}
header := strings.Join(tplHeaders, "")
for key, text := range annotations {
// simple check to skip text without template
if !strings.Contains(text, "{{") || !strings.Contains(text, "}}") {
r[key] = text
continue
}
buf.Reset()
builder.Reset()
builder.Grow(len(header) + len(text))
builder.WriteString(header)
builder.WriteString(text)
// clone a new template for each parse to avoid collision
ctmpl, _ := tmpl.Clone()
ctmpl = ctmpl.Option("missingkey=zero")
if err := templateAnnotation(&buf, builder.String(), tData, ctmpl, execute); err != nil {
r[key] = text
eg.Add(fmt.Errorf("key %q, template %q: %w", key, text, err))
continue
}
r[key] = buf.String()
}
return r, eg.Err()
}
type tplData struct {
AlertTplData
ExternalLabels map[string]string
ExternalURL string
}
func templateAnnotation(dst io.Writer, text string, data tplData, tpl *textTpl.Template, execute bool) error {
tpl, err := tpl.Parse(text)
if err != nil {
return fmt.Errorf("error parsing annotation template: %w", err)
}
if !execute {
return nil
}
if err = tpl.Execute(dst, data); err != nil {
return fmt.Errorf("error evaluating annotation template: %w", err)
}
return nil
}
func (a Alert) applyRelabelingIfNeeded(relabelCfg *promrelabel.ParsedConfigs) []prompbmarshal.Label {

View File

@@ -1,207 +1,13 @@
package notifier
import (
"fmt"
"reflect"
"testing"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promrelabel"
)
func TestAlertExecTemplate(t *testing.T) {
extLabels := make(map[string]string)
const (
extCluster = "prod"
extDC = "east"
extURL = "https://foo.bar"
)
extLabels["cluster"] = extCluster
extLabels["dc"] = extDC
_, err := Init(nil, extLabels, extURL)
checkErr(t, err)
f := func(alert *Alert, annotations map[string]string, tplExpected map[string]string) {
t.Helper()
if err := ValidateTemplates(annotations); err != nil {
t.Fatalf("cannot validate annotations: %s", err)
}
qFn := func(_ string) ([]datasource.Metric, error) {
return []datasource.Metric{
{
Labels: []prompbmarshal.Label{
{Name: "foo", Value: "bar"},
{Name: "baz", Value: "qux"},
},
Values: []float64{1},
Timestamps: []int64{1},
},
{
Labels: []prompbmarshal.Label{
{Name: "foo", Value: "garply"},
{Name: "baz", Value: "fred"},
},
Values: []float64{2},
Timestamps: []int64{1},
},
}, nil
}
tpl, err := alert.ExecTemplate(qFn, alert.Labels, annotations)
if err != nil {
t.Fatalf("cannot execute template: %s", err)
}
if len(tpl) != len(tplExpected) {
t.Fatalf("unexpected number of elements; got %d; want %d", len(tpl), len(tplExpected))
}
for k := range tplExpected {
got, exp := tpl[k], tplExpected[k]
if got != exp {
t.Fatalf("unexpected template for key=%q; got %q; want %q", k, got, exp)
}
}
}
// empty-alert
f(&Alert{}, map[string]string{}, map[string]string{})
// no-template
f(&Alert{
Value: 1e4,
Labels: map[string]string{
"instance": "localhost",
},
}, map[string]string{
"summary": "it's a test summary",
"description": "it's a test description",
}, map[string]string{
"summary": "it's a test summary",
"description": "it's a test description",
})
// label-template
f(&Alert{
Value: 1e4,
Labels: map[string]string{
"job": "staging",
"instance": "localhost",
},
For: 5 * time.Minute,
}, map[string]string{
"summary": "Too high connection number for {{$labels.instance}} for job {{$labels.job}}",
"description": "It is {{ $value }} connections for {{$labels.instance}} for more than {{ .For }}",
}, map[string]string{
"summary": "Too high connection number for localhost for job staging",
"description": "It is 10000 connections for localhost for more than 5m0s",
})
// label template override
f(&Alert{
Value: 1e4,
}, map[string]string{
"summary": `{{- define "default.template" -}} {{ printf "summary" }} {{- end -}} {{ template "default.template" . }}`,
"description": `{{ template "default.template" . }}`,
"value": `{{$value }}`,
}, map[string]string{
"summary": "summary",
"description": "",
"value": "10000",
})
// expression-template
f(&Alert{
Expr: `vm_rows{"label"="bar"}<0`,
}, map[string]string{
"exprEscapedQuery": "{{ $expr|queryEscape }}",
"exprEscapedPath": "{{ $expr|pathEscape }}",
"exprEscapedJSON": "{{ $expr|jsonEscape }}",
"exprEscapedQuotes": "{{ $expr|quotesEscape }}",
"exprEscapedHTML": "{{ $expr|htmlEscape }}",
}, map[string]string{
"exprEscapedQuery": "vm_rows%7B%22label%22%3D%22bar%22%7D%3C0",
"exprEscapedPath": "vm_rows%7B%22label%22=%22bar%22%7D%3C0",
"exprEscapedJSON": `"vm_rows{\"label\"=\"bar\"}\u003c0"`,
"exprEscapedQuotes": `vm_rows{\"label\"=\"bar\"}\u003c0`,
"exprEscapedHTML": "vm_rows{&quot;label&quot;=&quot;bar&quot;}&lt;0",
})
// query
f(&Alert{
Expr: `vm_rows{"label"="bar"}>0`,
}, map[string]string{
"summary": `{{ query "foo" | first | value }}`,
"desc": `{{ range query "bar" }}{{ . | label "foo" }} {{ . | value }};{{ end }}`,
}, map[string]string{
"summary": "1",
"desc": "bar 1;garply 2;",
})
// external
f(&Alert{
Value: 1e4,
Labels: map[string]string{
"job": "staging",
"instance": "localhost",
},
}, map[string]string{
"url": "{{ $externalURL }}",
"summary": "Issues with {{$labels.instance}} (dc-{{$externalLabels.dc}}) for job {{$labels.job}}",
"description": "It is {{ $value }} connections for {{$labels.instance}} (cluster-{{$externalLabels.cluster}})",
}, map[string]string{
"url": extURL,
"summary": fmt.Sprintf("Issues with localhost (dc-%s) for job staging", extDC),
"description": fmt.Sprintf("It is 10000 connections for localhost (cluster-%s)", extCluster),
})
// alert and group IDs
f(&Alert{
ID: 42,
GroupID: 24,
}, map[string]string{
"url": "/api/v1/alert?alertID={{$alertID}}&groupID={{$groupID}}",
}, map[string]string{
"url": "/api/v1/alert?alertID=42&groupID=24",
})
// ActiveAt time
f(&Alert{
ActiveAt: time.Date(2022, 8, 19, 20, 34, 58, 651387237, time.UTC),
}, map[string]string{
"diagram": "![](http://example.com?render={{$activeAt.Unix}}",
}, map[string]string{
"diagram": "![](http://example.com?render=1660941298",
})
// ActiveAt time is nil
f(&Alert{}, map[string]string{
"default_time": "{{$activeAt}}",
}, map[string]string{
"default_time": "0001-01-01 00:00:00 +0000 UTC",
})
// ActiveAt custom format
f(&Alert{
ActiveAt: time.Date(2022, 8, 19, 20, 34, 58, 651387237, time.UTC),
}, map[string]string{
"fire_time": `{{$activeAt.Format "2006/01/02 15:04:05"}}`,
}, map[string]string{
"fire_time": "2022/08/19 20:34:58",
})
// ActiveAt query range
f(&Alert{
ActiveAt: time.Date(2022, 8, 19, 20, 34, 58, 651387237, time.UTC),
}, map[string]string{
"grafana_url": `vm-grafana.com?from={{($activeAt.Add (parseDurationTime "1h")).Unix}}&to={{($activeAt.Add (parseDurationTime "-1h")).Unix}}`,
}, map[string]string{
"grafana_url": "vm-grafana.com?from=1660944898&to=1660937698",
})
}
func TestAlert_toPromLabels(t *testing.T) {
fn := func(labels map[string]string, exp []prompbmarshal.Label, relabel *promrelabel.ParsedConfigs) {
t.Helper()

View File

@@ -3,7 +3,6 @@ package notifier
import (
"flag"
"fmt"
"net/url"
"strings"
"time"
@@ -73,15 +72,6 @@ func Reload() error {
var staticNotifiersFn func() []Notifier
var (
// externalLabels is a global variable for holding external labels configured via flags
// It is supposed to be inited via Init function only.
externalLabels map[string]string
// externalURL is a global variable for holding external URL value configured via flag
// It is supposed to be inited via Init function only.
externalURL string
)
// Init returns a function for retrieving actual list of Notifier objects.
// Init works in two mods:
// - configuration via flags (for backward compatibility). Is always static
@@ -89,14 +79,7 @@ var (
// - configuration via file. Supports live reloads and service discovery.
//
// Init returns an error if both mods are used.
func Init(gen AlertURLGenerator, extLabels map[string]string, extURL string) (func() []Notifier, error) {
externalURL = extURL
externalLabels = extLabels
_, err := url.Parse(externalURL)
if err != nil {
return nil, fmt.Errorf("failed to parse external URL: %w", err)
}
func Init(gen AlertURLGenerator) (func() []Notifier, error) {
if *blackHole {
if len(*addrs) > 0 || *configPath != "" {
return nil, fmt.Errorf("only one of -notifier.blackhole, -notifier.url and -notifier.config flags must be specified")
@@ -126,6 +109,7 @@ func Init(gen AlertURLGenerator, extLabels map[string]string, extURL string) (fu
return staticNotifiersFn, nil
}
var err error
cw, err = newWatcher(*configPath, gen)
if err != nil {
return nil, fmt.Errorf("failed to init config watcher: %w", err)

View File

@@ -12,7 +12,7 @@ func TestInit(t *testing.T) {
*addrs = flagutil.ArrayString{"127.0.0.1", "127.0.0.2"}
fn, err := Init(nil, nil, "")
fn, err := Init(nil)
if err != nil {
t.Fatalf("%s", err)
}
@@ -52,7 +52,7 @@ func TestInitNegative(t *testing.T) {
*configPath = path
*addrs = flagutil.ArrayString{addr}
*blackHole = bh
if _, err := Init(nil, nil, ""); err == nil {
if _, err := Init(nil); err == nil {
t.Fatalf("expected to get error; got nil instead")
}
}
@@ -69,7 +69,7 @@ func TestBlackHole(t *testing.T) {
*blackHole = true
fn, err := Init(nil, nil, "")
fn, err := Init(nil)
if err != nil {
t.Fatalf("%s", err)
}

View File

@@ -9,7 +9,7 @@ import (
)
func TestMain(m *testing.M) {
if err := templates.Load([]string{"testdata/templates/*good.tmpl"}, url.URL{}); err != nil {
if err := templates.Init([]string{"testdata/templates/*good.tmpl"}, nil, url.URL{}); err != nil {
os.Exit(1)
}
os.Exit(m.Run())

View File

@@ -7,6 +7,7 @@ import (
"sort"
"strings"
"sync"
textTpl "text/template"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/config"
@@ -47,6 +48,12 @@ type AlertingRule struct {
state *ruleState
metrics *alertingRuleMetrics
// set rootTemplateName with loaded global template name,
// so we can check if the template has changed when evaluating.
rootTemplateName string
LabelTemplates map[string]*textTpl.Template
AnnotationsTemplates map[string]*textTpl.Template
}
type alertingRuleMetrics struct {
@@ -142,9 +149,47 @@ func NewAlertingRule(qb datasource.QuerierBuilder, group *Group, cfg config.Rule
}
return seriesFetched
})
ar.initTemplate()
return ar
}
// initTemplate pre-creates templates that can be reused in execution.
func (ar *AlertingRule) initTemplate() {
currentTmpl := templates.GetCurrentTmpl()
ar.rootTemplateName = currentTmpl.Name()
ar.LabelTemplates = make(map[string]*textTpl.Template, len(ar.Labels))
ar.AnnotationsTemplates = make(map[string]*textTpl.Template, len(ar.Annotations))
for k, v := range ar.Labels {
var err error
tmpl, _ := currentTmpl.Clone()
tmpl, err = templates.ParseWithFixedHeader(v, tmpl)
if err != nil {
// parse can fail in two cases:
// 1. the text contains `query` function, which is not supported during rule initialization.
// 2. the text itself is invalid.
// In both case, we skip the error here, and try it again during rule execution.
continue
}
ar.LabelTemplates[k] = tmpl
}
for k, v := range ar.Annotations {
var err error
tmpl, _ := currentTmpl.Clone()
tmpl, err = templates.ParseWithFixedHeader(v, tmpl)
if err != nil {
// parse can fail in two cases:
// 1. the text contains `query` function, which is not supported during rule initialization.
// 2. the text itself is invalid.
// In both case, we skip the error here, and try it again during rule execution.
continue
}
ar.AnnotationsTemplates[k] = tmpl
}
}
// close unregisters rule metrics
func (ar *AlertingRule) close() {
ar.metrics.active.Unregister()
@@ -225,6 +270,8 @@ func (ar *AlertingRule) updateWith(r Rule) error {
ar.KeepFiringFor = nr.KeepFiringFor
ar.Labels = nr.Labels
ar.Annotations = nr.Annotations
ar.initTemplate()
ar.EvalInterval = nr.EvalInterval
ar.Debug = nr.Debug
ar.q = nr.q
@@ -279,15 +326,32 @@ func (ar *AlertingRule) toLabels(m datasource.Metric, qFn templates.QueryFn) (*l
}
ls.processed[l.Name] = l.Value
}
extraLabels, err := notifier.ExecTemplate(qFn, ar.Labels, notifier.AlertTplData{
extraLabels := make(map[string]string, len(ar.Labels))
// compare to annotation, label value can only use limited variables for now
labelTplData := templates.AlertTplData{
Labels: ls.origin,
Value: m.Values[0],
Expr: ar.Expr,
})
if err != nil {
return nil, fmt.Errorf("failed to expand labels: %w", err)
}
for k := range ar.Labels {
if ar.LabelTemplates[k] == nil {
// this label may contain `query` function, which requires creating new template with query function in each evaluation.
v, err := templates.ExecuteWithoutTemplate(qFn, ar.Labels[k], labelTplData)
if err != nil {
logger.Errorf("error templating label %q for rule %q: %w", ar.Labels[k], ar.Name, err)
v = ar.Labels[k]
}
extraLabels[k] = v
continue
}
v, err := templates.ExecuteWithTemplate(labelTplData, ar.LabelTemplates[k])
if err != nil {
logger.Errorf("error templating label %q for rule %q: %w", ar.Labels[k], ar.Name, err)
v = ar.Labels[k]
}
extraLabels[k] = v
}
for k, v := range extraLabels {
ls.add(k, v)
}
@@ -513,12 +577,18 @@ func (ar *AlertingRule) exec(ctx context.Context, ts time.Time, limit int) ([]pr
}
func (ar *AlertingRule) expandTemplates(m datasource.Metric, qFn templates.QueryFn, ts time.Time) (*labelSet, map[string]string, error) {
// check if the rule template has changed during reload,
// if so, label&annotation templates must be re-created.
if ar.rootTemplateName != templates.GetCurrentTmpl().Name() {
ar.initTemplate()
}
ls, err := ar.toLabels(m, qFn)
if err != nil {
return nil, nil, fmt.Errorf("failed to expand labels: %w", err)
}
tplData := notifier.AlertTplData{
extraAnnotation := make(map[string]string, len(ar.Annotations))
annotationTplData := templates.AlertTplData{
Value: m.Values[0],
Labels: ls.origin,
Expr: ar.Expr,
@@ -527,11 +597,26 @@ func (ar *AlertingRule) expandTemplates(m datasource.Metric, qFn templates.Query
ActiveAt: ts,
For: ar.For,
}
as, err := notifier.ExecTemplate(qFn, ar.Annotations, tplData)
if err != nil {
return nil, nil, fmt.Errorf("failed to template annotations: %w", err)
for k := range ar.Annotations {
if ar.AnnotationsTemplates[k] == nil {
// this label may contain `query` function, which requires creating new template with query function in each evaluation.
v, err := templates.ExecuteWithoutTemplate(qFn, ar.Annotations[k], annotationTplData)
if err != nil {
logger.Errorf("error templating annotation %q for rule %q: %w", ar.Annotations[k], ar.Name, err)
v = ar.Annotations[k]
}
extraAnnotation[k] = v
continue
}
v, err := templates.ExecuteWithTemplate(annotationTplData, ar.AnnotationsTemplates[k])
if err != nil {
logger.Errorf("error templating annotation %q for rule %q: %w", ar.Annotations[k], ar.Name, err)
v = ar.Annotations[k]
}
extraAnnotation[k] = v
}
return ls, as, nil
return ls, extraAnnotation, nil
}
// toTimeSeries creates `ALERTS` and `ALERTS_FOR_STATE` for active alerts

View File

@@ -1046,6 +1046,7 @@ func TestAlertingRule_Template(t *testing.T) {
}
fq.Add(metrics...)
rule.initTemplate()
if _, err := rule.exec(context.TODO(), time.Now(), 0); err != nil {
t.Fatalf("unexpected error: %s", err)
}
@@ -1262,6 +1263,7 @@ func newTestAlertingRuleWithCustomFields(name string, waitFor, evalInterval, kee
}
rule.KeepFiringFor = keepFiringFor
rule.Annotations = annotation
rule.initTemplate()
return rule
}

View File

@@ -443,8 +443,8 @@ func (g *Group) Start(ctx context.Context, nts func() []notifier.Notifier, rw re
}
// UpdateWith inserts new group to updateCh
func (g *Group) UpdateWith(newGroup *Group) {
g.updateCh <- newGroup
func (g *Group) UpdateWith(new *Group) {
g.updateCh <- new
}
// DeepCopy returns a deep copy of group

View File

@@ -27,7 +27,7 @@ func init() {
}
func TestMain(m *testing.M) {
if err := templates.Load([]string{}, url.URL{}); err != nil {
if err := templates.Init([]string{}, nil, url.URL{}); err != nil {
fmt.Println("failed to load template for test")
os.Exit(1)
}

View File

@@ -0,0 +1,553 @@
package templates
import (
"errors"
"fmt"
htmlTpl "html/template"
"io"
"math"
"net"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"sync"
textTpl "text/template"
"time"
"github.com/bmatcuk/doublestar/v4"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/formatutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
)
var (
tplMu sync.RWMutex
masterTmpl *textTpl.Template
// externalLabels is a global variable for holding external labels configured via flags
// It is supposed to be initiated via Init function only.
externalLabels map[string]string
// externalURL is a global variable for holding external URL value configured via flag
// It is supposed to be initiated via Init function only.
externalURL url.URL
)
// Init initializes global externalLabels and externalURL variables, and load templates from pathPatterns.
func Init(pathPatterns []string, extLabels map[string]string, extURL url.URL) error {
externalURL = extURL
externalLabels = extLabels
return LoadTemplateFile(pathPatterns)
}
// LoadTemplateFile loads templates from multiple globs specified in pathPatterns:
// 1. if it's the first load, sets them directly to current template;
// 2. if it's not the first load, only update masterTmpl when the contents change.
func LoadTemplateFile(pathPatterns []string) error {
templateName := fmt.Sprintf("rule-template-%d", time.Now().UnixMilli())
// using Load timestamp as template root name,
// so we can check if this global template has been reloaded when use it elsewhere, like during alerting rules execution.
tmpl := newTemplate(templateName)
tmpl = tmpl.Funcs(funcsWithExternalURL(externalURL))
for _, tp := range pathPatterns {
p, err := doublestar.FilepathGlob(tp)
if err != nil {
return fmt.Errorf("failed to retrieve a template glob %q: %w", tp, err)
}
if len(p) > 0 {
tmpl, err = tmpl.ParseFiles(p...)
if err != nil {
return fmt.Errorf("failed to parse template glob %q: %w", tp, err)
}
}
}
err := tmpl.Execute(io.Discard, nil)
if err != nil {
return fmt.Errorf("failed to test rule template: %w", err)
}
tplMu.Lock()
defer tplMu.Unlock()
if masterTmpl == nil {
masterTmpl = tmpl
} else {
// only update the masterTmpl when content has changed
if !isTemplatesTheSame(masterTmpl, tmpl) {
masterTmpl = tmpl
}
}
return nil
}
func newTemplate(name string) *textTpl.Template {
tmpl := textTpl.New(name).Funcs(templateFuncs())
return textTpl.Must(tmpl.Parse(""))
}
// isTemplatesTheSame returns true if the content of two templates are the same,
// the root template name difference is ignored.
func isTemplatesTheSame(t1, t2 *textTpl.Template) bool {
getRootString := func(t *textTpl.Template) string {
if t.Tree == nil || t.Tree.Root == nil {
return ""
}
return t.Tree.Root.String()
}
if getRootString(t1) != getRootString(t2) {
return false
}
t1Templates := make(map[string]string)
t2Templates := make(map[string]string)
for _, tmpl := range t1.Templates() {
// skip root template, since it's null and changes every time
if tmpl.Name() == t1.Name() {
continue
}
t1Templates[tmpl.Name()] = getRootString(tmpl)
}
for _, tmpl := range t2.Templates() {
if tmpl.Name() == t2.Name() {
continue
}
t2Templates[tmpl.Name()] = getRootString(tmpl)
}
if len(t1Templates) != len(t2Templates) {
return false
}
for k, v := range t1Templates {
if t2Templates[k] != v {
return false
}
delete(t2Templates, k)
}
return len(t2Templates) == 0
}
// funcsWithExternalURL returns a function map that depends on externalURL value
func funcsWithExternalURL(externalURL url.URL) textTpl.FuncMap {
return textTpl.FuncMap{
"externalURL": func() string {
return externalURL.String()
},
"pathPrefix": func() string {
return externalURL.Path
},
}
}
// templateFuncs initiates template helper functions
func templateFuncs() textTpl.FuncMap {
// See https://prometheus.io/docs/prometheus/latest/configuration/template_reference/
// and https://github.com/prometheus/prometheus/blob/fa6e05903fd3ce52e374a6e1bf4eb98c9f1f45a7/template/template.go#L150
return textTpl.FuncMap{
/* Strings */
// title returns a copy of the string s with all Unicode letters
// that begin words mapped to their Unicode title case.
// alias for https://golang.org/pkg/strings/#Title
"title": strings.Title,
// toUpper returns s with all Unicode letters mapped to their upper case.
// alias for https://golang.org/pkg/strings/#ToUpper
"toUpper": strings.ToUpper,
// toLower returns s with all Unicode letters mapped to their lower case.
// alias for https://golang.org/pkg/strings/#ToLower
"toLower": strings.ToLower,
// crlfEscape replaces '\n' and '\r' chars with `\\n` and `\\r`.
// This function is deprecated.
//
// It is better to use quotesEscape, jsonEscape, queryEscape or pathEscape instead -
// these functions properly escape `\n` and `\r` chars according to their purpose.
"crlfEscape": func(q string) string {
q = strings.Replace(q, "\n", `\n`, -1)
return strings.Replace(q, "\r", `\r`, -1)
},
// quotesEscape escapes the string, so it can be safely put inside JSON string.
//
// See also jsonEscape.
"quotesEscape": quotesEscape,
// jsonEscape converts the string to properly encoded JSON string.
//
// See also quotesEscape.
"jsonEscape": jsonEscape,
// htmlEscape applies html-escaping to q, so it can be safely embedded as plaintext into html.
//
// See also safeHtml.
"htmlEscape": htmlEscape,
// stripPort splits string into host and port, then returns only host.
"stripPort": func(hostPort string) string {
host, _, err := net.SplitHostPort(hostPort)
if err != nil {
return hostPort
}
return host
},
// stripDomain removes the domain part of a FQDN. Leaves port untouched.
"stripDomain": func(hostPort string) string {
host, port, err := net.SplitHostPort(hostPort)
if err != nil {
host = hostPort
}
ip := net.ParseIP(host)
if ip != nil {
return hostPort
}
host = strings.Split(host, ".")[0]
if port != "" {
return net.JoinHostPort(host, port)
}
return host
},
// match reports whether the string s
// contains any match of the regular expression pattern.
// alias for https://golang.org/pkg/regexp/#MatchString
"match": regexp.MatchString,
// reReplaceAll ReplaceAllString returns a copy of src, replacing matches of the Regexp with
// the replacement string repl. Inside repl, $ signs are interpreted as in Expand,
// so for instance $1 represents the text of the first submatch.
// alias for https://golang.org/pkg/regexp/#Regexp.ReplaceAllString
"reReplaceAll": func(pattern, repl, text string) string {
re := regexp.MustCompile(pattern)
return re.ReplaceAllString(text, repl)
},
// parseDuration parses a duration string such as "1h" into the number of seconds it represents
"parseDuration": func(s string) (float64, error) {
d, err := promutils.ParseDuration(s)
if err != nil {
return 0, err
}
return d.Seconds(), nil
},
// same with parseDuration but returns a time.Duration
"parseDurationTime": func(s string) (time.Duration, error) {
d, err := promutils.ParseDuration(s)
if err != nil {
return 0, err
}
return d, nil
},
/* Numbers */
// humanize converts given number to a human readable format
// by adding metric prefixes https://en.wikipedia.org/wiki/Metric_prefix
"humanize": func(i any) (string, error) {
v, err := toFloat64(i)
if err != nil {
return "", err
}
if v == 0 || math.IsNaN(v) || math.IsInf(v, 0) {
return fmt.Sprintf("%.4g", v), nil
}
if math.Abs(v) >= 1 {
prefix := ""
for _, p := range []string{"k", "M", "G", "T", "P", "E", "Z", "Y"} {
if math.Abs(v) < 1000 {
break
}
prefix = p
v /= 1000
}
return fmt.Sprintf("%.4g%s", v, prefix), nil
}
prefix := ""
for _, p := range []string{"m", "u", "n", "p", "f", "a", "z", "y"} {
if math.Abs(v) >= 1 {
break
}
prefix = p
v *= 1000
}
return fmt.Sprintf("%.4g%s", v, prefix), nil
},
// humanize1024 converts given number to a human readable format with 1024 as base
"humanize1024": func(i any) (string, error) {
v, err := toFloat64(i)
if err != nil {
return "", err
}
if math.Abs(v) <= 1 || math.IsNaN(v) || math.IsInf(v, 0) {
return fmt.Sprintf("%.4g", v), nil
}
return formatutil.HumanizeBytes(v), nil
},
// humanizeDuration converts given seconds to a human-readable duration
"humanizeDuration": func(i any) (string, error) {
v, err := toFloat64(i)
if err != nil {
return "", err
}
if math.IsNaN(v) || math.IsInf(v, 0) {
return fmt.Sprintf("%.4g", v), nil
}
if v == 0 {
return fmt.Sprintf("%.4gs", v), nil
}
if math.Abs(v) >= 1 {
sign := ""
if v < 0 {
sign = "-"
v = -v
}
seconds := int64(v) % 60
minutes := (int64(v) / 60) % 60
hours := (int64(v) / 60 / 60) % 24
days := int64(v) / 60 / 60 / 24
// For days to minutes, we display seconds as an integer.
if days != 0 {
return fmt.Sprintf("%s%dd %dh %dm %ds", sign, days, hours, minutes, seconds), nil
}
if hours != 0 {
return fmt.Sprintf("%s%dh %dm %ds", sign, hours, minutes, seconds), nil
}
if minutes != 0 {
return fmt.Sprintf("%s%dm %ds", sign, minutes, seconds), nil
}
// For seconds, we display 4 significant digits.
return fmt.Sprintf("%s%.4gs", sign, v), nil
}
prefix := ""
for _, p := range []string{"m", "u", "n", "p", "f", "a", "z", "y"} {
if math.Abs(v) >= 1 {
break
}
prefix = p
v *= 1000
}
return fmt.Sprintf("%.4g%ss", v, prefix), nil
},
// humanizePercentage converts given ratio value to a fraction of 100
"humanizePercentage": func(i any) (string, error) {
v, err := toFloat64(i)
if err != nil {
return "", err
}
return fmt.Sprintf("%.4g%%", v*100), nil
},
// humanizeTimestamp converts given timestamp to a human readable time equivalent
"humanizeTimestamp": func(i any) (string, error) {
v, err := toFloat64(i)
if err != nil {
return "", err
}
if math.IsNaN(v) || math.IsInf(v, 0) {
return fmt.Sprintf("%.4g", v), nil
}
t := timeFromUnixTimestamp(v).Time().UTC()
return fmt.Sprint(t), nil
},
// toTime converts given timestamp to a time.Time.
"toTime": func(i any) (time.Time, error) {
v, err := toFloat64(i)
if err != nil {
return time.Time{}, err
}
if math.IsNaN(v) || math.IsInf(v, 0) {
return time.Time{}, fmt.Errorf("cannot convert %v to time.Time", v)
}
t := timeFromUnixTimestamp(v).Time().UTC()
return t, nil
},
/* URLs */
// externalURL returns value of `external.url` flag
"externalURL": func() string {
// externalURL function supposed to be substituted at FuncsWithExteralURL().
// it is present here only for validation purposes, when there is no
// provided datasource.
//
// return non-empty slice to pass validation with chained functions in template
return ""
},
// pathPrefix returns a Path segment from the URL value in `external.url` flag
"pathPrefix": func() string {
// pathPrefix function supposed to be substituted at FuncsWithExteralURL().
// it is present here only for validation purposes, when there is no
// provided datasource.
//
// return non-empty slice to pass validation with chained functions in template
return ""
},
// pathEscape escapes the string so it can be safely placed inside a URL path segment.
//
// See also queryEscape.
"pathEscape": url.PathEscape,
// queryEscape escapes the string so it can be safely placed inside a query arg in URL.
//
// See also queryEscape.
"queryEscape": url.QueryEscape,
// first returns the first by order element from the given metrics list.
// usually used alongside with `query` template function.
"first": func(metrics []metric) (metric, error) {
if len(metrics) > 0 {
return metrics[0], nil
}
return metric{}, errors.New("first() called on vector with no elements")
},
// label returns the value of the given label name for the given metric.
// usually used alongside with `query` template function.
"label": func(label string, m metric) string {
return m.Labels[label]
},
// value returns the value of the given metric.
// usually used alongside with `query` template function.
"value": func(m metric) float64 {
return m.Value
},
// strvalue returns metric name.
"strvalue": func(m metric) string {
return m.Labels["__name__"]
},
// sortByLabel sorts the given metrics by provided label key
"sortByLabel": func(label string, metrics []metric) []metric {
sort.SliceStable(metrics, func(i, j int) bool {
return metrics[i].Labels[label] < metrics[j].Labels[label]
})
return metrics
},
/* Helpers */
// Converts a list of objects to a map with keys arg0, arg1 etc.
// This is intended to allow multiple arguments to be passed to templates.
"args": func(args ...any) map[string]any {
result := make(map[string]any)
for i, a := range args {
result[fmt.Sprintf("arg%d", i)] = a
}
return result
},
// safeHtml marks string as HTML not requiring auto-escaping.
//
// See also htmlEscape.
"safeHtml": func(text string) htmlTpl.HTML {
return htmlTpl.HTML(text)
},
}
}
// metric is private copy of datasource.Metric,
// it is used for templating annotations,
// Labels as map simplifies templates evaluation.
type metric struct {
Labels map[string]string
Timestamp int64
Value float64
}
// datasourceMetricsToTemplateMetrics converts Metrics from datasource package to private copy for templating.
func datasourceMetricsToTemplateMetrics(ms []datasource.Metric) []metric {
mss := make([]metric, 0, len(ms))
for _, m := range ms {
labelsMap := make(map[string]string, len(m.Labels))
for _, labelValue := range m.Labels {
labelsMap[labelValue.Name] = labelValue.Value
}
mss = append(mss, metric{
Labels: labelsMap,
Timestamp: m.Timestamps[0],
Value: m.Values[0]})
}
return mss
}
// QueryFn is used to wrap a call to datasource into simple-to-use function
// for templating functions.
type QueryFn func(query string) ([]datasource.Metric, error)
// FuncsWithQuery returns a function map that depends on metric data
func FuncsWithQuery(query QueryFn) textTpl.FuncMap {
return textTpl.FuncMap{
"query": func(q string) ([]metric, error) {
if query == nil {
return nil, fmt.Errorf("cannot execute query %q: query is not available in this context", q)
}
result, err := query(q)
if err != nil {
return nil, err
}
return datasourceMetricsToTemplateMetrics(result), nil
},
}
}
// Time is the number of milliseconds since the epoch
// (1970-01-01 00:00 UTC) excluding leap seconds.
type Time int64
// timeFromUnixTimestamp returns the Time equivalent to t in unix timestamp.
func timeFromUnixTimestamp(t float64) Time {
return Time(t * 1e3)
}
// The number of nanoseconds per minimum tick.
const nanosPerTick = int64(minimumTick / time.Nanosecond)
// MinimumTick is the minimum supported time resolution. This has to be
// at least time.Second in order for the code below to work.
const minimumTick = time.Millisecond
// second is the Time duration equivalent to one second.
const second = int64(time.Second / minimumTick)
// Time returns the time.Time representation of t.
func (t Time) Time() time.Time {
return time.Unix(int64(t)/second, (int64(t)%second)*nanosPerTick)
}
func toFloat64(v any) (float64, error) {
switch i := v.(type) {
case float64:
return i, nil
case float32:
return float64(i), nil
case int64:
return float64(i), nil
case int32:
return float64(i), nil
case int:
return float64(i), nil
case uint64:
return float64(i), nil
case uint32:
return float64(i), nil
case uint:
return float64(i), nil
case string:
return strconv.ParseFloat(i, 64)
default:
return 0, fmt.Errorf("unexpected value type %v", i)
}
}

View File

@@ -0,0 +1,261 @@
package templates
import (
"fmt"
"math"
"net/url"
"os"
"strings"
"testing"
textTpl "text/template"
)
func TestMain(m *testing.M) {
if err := Init([]string{}, nil, url.URL{}); err != nil {
fmt.Println("failed to load template for test")
os.Exit(1)
}
os.Exit(m.Run())
}
func TestTemplateFuncs_Match(t *testing.T) {
funcs := templateFuncs()
// check "match" func
matchFunc := funcs["match"].(func(pattern, s string) (bool, error))
if _, err := matchFunc("invalid[regexp", "abc"); err == nil {
t.Fatalf("expecting non-nil error on invalid regexp")
}
ok, err := matchFunc("abc", "def")
if err != nil {
t.Fatalf("unexpected error")
}
if ok {
t.Fatalf("unexpected match")
}
ok, err = matchFunc("a.+b", "acsdb")
if err != nil {
t.Fatalf("unexpected error")
}
if !ok {
t.Fatalf("unexpected mismatch")
}
}
func TestTemplateFuncs_Formatting(t *testing.T) {
f := func(funcName string, p any, resultExpected string) {
t.Helper()
funcs := templateFuncs()
v := funcs[funcName]
fLocal := v.(func(s any) (string, error))
result, err := fLocal(p)
if err != nil {
t.Fatalf("unexpected error for %s(%f): %s", funcName, p, err)
}
if result != resultExpected {
t.Fatalf("unexpected result for %s(%f); got\n%s\nwant\n%s", funcName, p, result, resultExpected)
}
}
f("humanize1024", float64(0), "0")
f("humanize1024", math.Inf(0), "+Inf")
f("humanize1024", math.NaN(), "NaN")
f("humanize1024", float64(127087), "124.1ki")
f("humanize1024", float64(130137088), "124.1Mi")
f("humanize1024", float64(133260378112), "124.1Gi")
f("humanize1024", float64(136458627186688), "124.1Ti")
f("humanize1024", float64(139733634239168512), "124.1Pi")
f("humanize1024", float64(143087241460908556288), "124.1Ei")
f("humanize1024", float64(146521335255970361638912), "124.1Zi")
f("humanize1024", float64(150037847302113650318245888), "124.1Yi")
f("humanize1024", float64(153638755637364377925883789312), "1.271e+05Yi")
f("humanize", float64(127087), "127.1k")
f("humanize", float64(136458627186688), "136.5T")
f("humanizeDuration", 1, "1s")
f("humanizeDuration", 0.2, "200ms")
f("humanizeDuration", 42000, "11h 40m 0s")
f("humanizeDuration", 16790555, "194d 8h 2m 35s")
f("humanizePercentage", 1, "100%")
f("humanizePercentage", 0.8, "80%")
f("humanizePercentage", 0.015, "1.5%")
f("humanizeTimestamp", 1679055557, "2023-03-17 12:19:17 +0000 UTC")
}
func TestTemplateFuncs_StringConversion(t *testing.T) {
f := func(funcName, s, resultExpected string) {
t.Helper()
funcs := templateFuncs()
v := funcs[funcName]
fLocal := v.(func(s string) string)
result := fLocal(s)
if result != resultExpected {
t.Fatalf("unexpected result for %s(%q); got\n%s\nwant\n%s", funcName, s, result, resultExpected)
}
}
f("title", "foo bar", "Foo Bar")
f("toUpper", "foo", "FOO")
f("toLower", "FOO", "foo")
f("pathEscape", "foo/bar\n+baz", "foo%2Fbar%0A+baz")
f("queryEscape", "foo+bar\n+baz", "foo%2Bbar%0A%2Bbaz")
f("jsonEscape", `foo{bar="baz"}`+"\n + 1", `"foo{bar=\"baz\"}\n + 1"`)
f("quotesEscape", `foo{bar="baz"}`+"\n + 1", `foo{bar=\"baz\"}\n + 1`)
f("htmlEscape", "foo < 10\nabc", "foo &lt; 10\nabc")
f("crlfEscape", "foo\nbar\rx", `foo\nbar\rx`)
f("stripPort", "foo", "foo")
f("stripPort", "foo:1234", "foo")
f("stripDomain", "foo.bar.baz", "foo")
f("stripDomain", "foo.bar:123", "foo:123")
}
func TestTemplatesLoad_Success(t *testing.T) {
f := func(pathPatterns []string, expectedTmpl *textTpl.Template) {
t.Helper()
masterTmplOrig := masterTmpl
defer func() {
masterTmpl = masterTmplOrig
}()
masterTmpl = nil
if err := LoadTemplateFile(pathPatterns); err != nil {
t.Fatalf("cannot load templates: %s", err)
}
if !isTemplatesTheSame(masterTmpl, expectedTmpl) {
t.Fatalf("unexpected template\ngot\n%+v\nwant\n%+v", masterTmpl, expectedTmpl)
}
}
// non existing path
pathPatterns := []string{
"templates/non-existing/good-*.tpl",
"templates/absent/good-*.tpl",
}
expectedTmpl := textTpl.Must(newTemplate("").Parse(""))
f(pathPatterns, expectedTmpl)
// existing path
pathPatterns = []string{
"templates/test/good0-*.tpl",
}
expectedTmpl = textTpl.Must(newTemplate("").Parse(`
{{- define "good0-test.tpl" -}}{{- end -}}
{{- define "test.0" -}}
{{ printf "Hello %s!" externalURL }}
{{- end -}}
{{- define "test.2" -}}
{{ printf "Hello %s!" externalURL }}
{{- end -}}
{{- define "test.3" -}}
{{ printf "Hello %s!" externalURL }}
{{- end -}}
`))
f(pathPatterns, expectedTmpl)
// template update
pathPatterns = []string{
"templates/other/nested/good0-*.tpl",
}
expectedTmpl = textTpl.Must(newTemplate("").Parse(`
{{- define "good0-test.tpl" -}}{{- end -}}
{{- define "test.0" -}}
{{ printf "Hello %s!" externalURL }}
{{- end -}}
{{- define "test.1" -}}
{{ printf "Hello %s!" externalURL }}
{{- end -}}
{{- define "test.3" -}}
{{ printf "Hello %s!" externalURL }}
{{- end -}}
`))
f(pathPatterns, expectedTmpl)
}
func TestTemplatesLoad_Failure(t *testing.T) {
f := func(pathPatterns []string, expectedErrStr string) {
t.Helper()
err := LoadTemplateFile(pathPatterns)
if err == nil {
t.Fatalf("expecting non-nil error")
}
errStr := err.Error()
if !strings.Contains(errStr, expectedErrStr) {
t.Fatalf("the returned error %q doesn't contain %q", errStr, expectedErrStr)
}
}
// load template with syntax error
f([]string{
"templates/other/nested/bad0-*.tpl",
"templates/test/good0-*.tpl",
}, "failed to parse template glob")
}
func TestTemplatesReload(t *testing.T) {
masterTmplOrig := masterTmpl
defer func() {
masterTmpl = masterTmplOrig
}()
masterTmpl = nil
// load with non existing path
pathPatterns := []string{
"templates/non-existing/good-*.tpl",
"templates/absent/good-*.tpl",
}
if err := LoadTemplateFile(pathPatterns); err != nil {
t.Fatalf("cannot load templates: %s", err)
}
tpl1 := GetCurrentTmpl()
// reload with existing path
pathPatterns = []string{
"templates/test/good0-*.tpl",
}
if err := LoadTemplateFile(pathPatterns); err != nil {
t.Fatalf("cannot load templates: %s", err)
}
tpl2 := GetCurrentTmpl()
if isTemplatesTheSame(tpl1, tpl2) {
t.Fatalf("tpl1 should be different from tpl2")
}
// reload the same path
pathPatterns = []string{
"templates/test/good0-*.tpl",
}
if err := LoadTemplateFile(pathPatterns); err != nil {
t.Fatalf("cannot load templates: %s", err)
}
tpl3 := GetCurrentTmpl()
if !isTemplatesTheSame(tpl2, tpl3) || tpl2.Name() != tpl3.Name() {
t.Fatalf("tpl3 should be the same as tpl2")
}
}
func TestIsTemplateTheSame(t *testing.T) {
f := func(tmpl1, tmpl2 *textTpl.Template, isTheSame bool) {
t.Helper()
if isTemplatesTheSame(tmpl1, tmpl2) != isTheSame {
t.Fatalf("unexpected result for isTemplatesTheSame")
}
}
tmpl1 := textTpl.Must(newTemplate("t1").Parse("{{- define \"test\" -}}{{- end -}}"))
tmpl2 := textTpl.Must(newTemplate("t2").Parse("{{- define \"test\" -}}{{- end -}}"))
f(tmpl1, tmpl2, true)
tmpl1, _ = tmpl1.Parse("{{- define \"test2\" -}}{{- end -}}")
f(tmpl1, tmpl2, false)
tmpl2, _ = tmpl2.Parse("{{- define \"test3\" -}}{{- end -}}")
f(tmpl1, tmpl2, false)
}

View File

@@ -1,554 +1,95 @@
// Copyright 2013 The Prometheus Authors
// 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 templates
import (
"errors"
"bytes"
"fmt"
htmlTpl "html/template"
"io"
"math"
"net"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"sync"
textTpl "text/template"
"time"
"github.com/bmatcuk/doublestar/v4"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/formatutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
)
// go template execution fails when it's tree is empty
const defaultTemplate = `{{- define "default.template" -}}{{- end -}}`
// supported variables are list in https://docs.victoriametrics.com/vmalert/#templating.
const tplHeaders = `{{ $value := .Value }}{{ $labels := .Labels }}{{ $expr := .Expr }}{{ $externalLabels := .ExternalLabels }}{{ $externalURL := .ExternalURL }}{{ $alertID := .AlertID }}{{ $groupID := .GroupID }}{{ $activeAt := .ActiveAt }}{{ $for := .For }}`
var tplMu sync.RWMutex
type textTemplate struct {
current *textTpl.Template
replacement *textTpl.Template
// AlertTplData is used to execute templating
type AlertTplData struct {
Labels map[string]string
Value float64
Expr string
AlertID uint64
GroupID uint64
ActiveAt time.Time
For time.Duration
}
var masterTmpl textTemplate
func newTemplate() *textTpl.Template {
tmpl := textTpl.New("").Option("missingkey=zero").Funcs(templateFuncs())
return textTpl.Must(tmpl.Parse(defaultTemplate))
}
// Load func loads templates from multiple globs specified in pathPatterns and either
// sets them directly to current template if it's the first init;
// or sets replacement templates and wait for Reload() to replace current template with replacement.
func Load(pathPatterns []string, externalURL url.URL) error {
tmpl := newTemplate()
for _, tp := range pathPatterns {
p, err := doublestar.FilepathGlob(tp)
// ValidateTemplates validates the given annotations,
// mock the `query` function during validation.
func ValidateTemplates(annotations map[string]string) error {
// it's ok to reuse one template for multiple text validations.
tmpl := GetCurrentTmpl()
tmpl = tmpl.Funcs(FuncsWithQuery(nil))
for _, v := range annotations {
_, err := tmpl.Parse(tplHeaders + v)
if err != nil {
return fmt.Errorf("failed to retrieve a template glob %q: %w", tp, err)
return fmt.Errorf("failed to parse text %q into template: %w", v, err)
}
if len(p) > 0 {
tmpl, err = tmpl.ParseFiles(p...)
if err != nil {
return fmt.Errorf("failed to parse template glob %q: %w", tp, err)
}
}
}
if len(tmpl.Templates()) > 0 {
err := tmpl.Execute(io.Discard, nil)
if err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}
}
tplMu.Lock()
defer tplMu.Unlock()
tmpl = tmpl.Funcs(funcsWithExternalURL(externalURL))
if masterTmpl.current == nil {
masterTmpl.current = tmpl
} else {
masterTmpl.replacement = tmpl
}
return nil
}
// Reload func replaces current template with a replacement template
// which was set by Load with override=false
func Reload() {
tplMu.Lock()
defer tplMu.Unlock()
if masterTmpl.replacement != nil {
masterTmpl.current = masterTmpl.replacement
masterTmpl.replacement = nil
}
}
// metric is private copy of datasource.Metric,
// it is used for templating annotations,
// Labels as map simplifies templates evaluation.
type metric struct {
Labels map[string]string
Timestamp int64
Value float64
}
// datasourceMetricsToTemplateMetrics converts Metrics from datasource package to private copy for templating.
func datasourceMetricsToTemplateMetrics(ms []datasource.Metric) []metric {
mss := make([]metric, 0, len(ms))
for _, m := range ms {
labelsMap := make(map[string]string, len(m.Labels))
for _, labelValue := range m.Labels {
labelsMap[labelValue.Name] = labelValue.Value
}
mss = append(mss, metric{
Labels: labelsMap,
Timestamp: m.Timestamps[0],
Value: m.Values[0]})
}
return mss
}
// QueryFn is used to wrap a call to datasource into simple-to-use function
// for templating functions.
type QueryFn func(query string) ([]datasource.Metric, error)
// GetWithFuncs returns a copy of current template with additional FuncMap
// provided with funcs argument
func GetWithFuncs(funcs textTpl.FuncMap) (*textTpl.Template, error) {
// GetCurrentTmpl returns a copy of the current global template
func GetCurrentTmpl() *textTpl.Template {
tplMu.RLock()
defer tplMu.RUnlock()
tmpl, err := masterTmpl.current.Clone()
tmpl, err := masterTmpl.Clone()
if err != nil {
return nil, err
logger.Panicf("failed to clone current rule template: %w", err)
}
// Clone() doesn't copy tpl Options, so we set them manually
tmpl = tmpl.Option("missingkey=zero")
return tmpl.Funcs(funcs), nil
return tmpl
}
// FuncsWithQuery returns a function map that depends on metric data
func FuncsWithQuery(query QueryFn) textTpl.FuncMap {
return textTpl.FuncMap{
"query": func(q string) ([]metric, error) {
if query == nil {
return nil, fmt.Errorf("cannot execute query %q: query is not available in this context", q)
}
type tplData struct {
AlertTplData
ExternalLabels map[string]string
ExternalURL string
}
result, err := query(q)
if err != nil {
return nil, err
}
return datasourceMetricsToTemplateMetrics(result), nil
},
// ParseWithFixedHeader parses the text with the fixed tplHeaders into the given template
func ParseWithFixedHeader(text string, tpl *textTpl.Template) (*textTpl.Template, error) {
return tpl.Parse(tplHeaders + text)
}
// ExecuteWithoutTemplate retrieves the current global templates, parses the text and executes with the given data
func ExecuteWithoutTemplate(q QueryFn, text string, data AlertTplData) (string, error) {
if !strings.Contains(text, "{{") || !strings.Contains(text, "}}") {
return text, nil
}
}
// funcsWithExternalURL returns a function map that depends on externalURL value
func funcsWithExternalURL(externalURL url.URL) textTpl.FuncMap {
return textTpl.FuncMap{
"externalURL": func() string {
return externalURL.String()
},
var err error
tmpl := GetCurrentTmpl()
tmpl = tmpl.Funcs(FuncsWithQuery(q))
tmpl, err = tmpl.Parse(tplHeaders + text)
if err != nil {
return "", fmt.Errorf("failed to parse text %q into template: %w", text, err)
"pathPrefix": func() string {
return externalURL.Path
},
}
return ExecuteWithTemplate(data, tmpl)
}
// templateFuncs initiates template helper functions
func templateFuncs() textTpl.FuncMap {
// See https://prometheus.io/docs/prometheus/latest/configuration/template_reference/
// and https://github.com/prometheus/prometheus/blob/fa6e05903fd3ce52e374a6e1bf4eb98c9f1f45a7/template/template.go#L150
return textTpl.FuncMap{
/* Strings */
// title returns a copy of the string s with all Unicode letters
// that begin words mapped to their Unicode title case.
// alias for https://golang.org/pkg/strings/#Title
"title": strings.Title,
// toUpper returns s with all Unicode letters mapped to their upper case.
// alias for https://golang.org/pkg/strings/#ToUpper
"toUpper": strings.ToUpper,
// toLower returns s with all Unicode letters mapped to their lower case.
// alias for https://golang.org/pkg/strings/#ToLower
"toLower": strings.ToLower,
// crlfEscape replaces '\n' and '\r' chars with `\\n` and `\\r`.
// This function is deprecated.
//
// It is better to use quotesEscape, jsonEscape, queryEscape or pathEscape instead -
// these functions properly escape `\n` and `\r` chars according to their purpose.
"crlfEscape": func(q string) string {
q = strings.Replace(q, "\n", `\n`, -1)
return strings.Replace(q, "\r", `\r`, -1)
},
// quotesEscape escapes the string, so it can be safely put inside JSON string.
//
// See also jsonEscape.
"quotesEscape": quotesEscape,
// jsonEscape converts the string to properly encoded JSON string.
//
// See also quotesEscape.
"jsonEscape": jsonEscape,
// htmlEscape applies html-escaping to q, so it can be safely embedded as plaintext into html.
//
// See also safeHtml.
"htmlEscape": htmlEscape,
// stripPort splits string into host and port, then returns only host.
"stripPort": func(hostPort string) string {
host, _, err := net.SplitHostPort(hostPort)
if err != nil {
return hostPort
}
return host
},
// stripDomain removes the domain part of a FQDN. Leaves port untouched.
"stripDomain": func(hostPort string) string {
host, port, err := net.SplitHostPort(hostPort)
if err != nil {
host = hostPort
}
ip := net.ParseIP(host)
if ip != nil {
return hostPort
}
host = strings.Split(host, ".")[0]
if port != "" {
return net.JoinHostPort(host, port)
}
return host
},
// match reports whether the string s
// contains any match of the regular expression pattern.
// alias for https://golang.org/pkg/regexp/#MatchString
"match": regexp.MatchString,
// reReplaceAll ReplaceAllString returns a copy of src, replacing matches of the Regexp with
// the replacement string repl. Inside repl, $ signs are interpreted as in Expand,
// so for instance $1 represents the text of the first submatch.
// alias for https://golang.org/pkg/regexp/#Regexp.ReplaceAllString
"reReplaceAll": func(pattern, repl, text string) string {
re := regexp.MustCompile(pattern)
return re.ReplaceAllString(text, repl)
},
// parseDuration parses a duration string such as "1h" into the number of seconds it represents
"parseDuration": func(s string) (float64, error) {
d, err := promutils.ParseDuration(s)
if err != nil {
return 0, err
}
return d.Seconds(), nil
},
// same with parseDuration but returns a time.Duration
"parseDurationTime": func(s string) (time.Duration, error) {
d, err := promutils.ParseDuration(s)
if err != nil {
return 0, err
}
return d, nil
},
/* Numbers */
// humanize converts given number to a human readable format
// by adding metric prefixes https://en.wikipedia.org/wiki/Metric_prefix
"humanize": func(i any) (string, error) {
v, err := toFloat64(i)
if err != nil {
return "", err
}
if v == 0 || math.IsNaN(v) || math.IsInf(v, 0) {
return fmt.Sprintf("%.4g", v), nil
}
if math.Abs(v) >= 1 {
prefix := ""
for _, p := range []string{"k", "M", "G", "T", "P", "E", "Z", "Y"} {
if math.Abs(v) < 1000 {
break
}
prefix = p
v /= 1000
}
return fmt.Sprintf("%.4g%s", v, prefix), nil
}
prefix := ""
for _, p := range []string{"m", "u", "n", "p", "f", "a", "z", "y"} {
if math.Abs(v) >= 1 {
break
}
prefix = p
v *= 1000
}
return fmt.Sprintf("%.4g%s", v, prefix), nil
},
// humanize1024 converts given number to a human readable format with 1024 as base
"humanize1024": func(i any) (string, error) {
v, err := toFloat64(i)
if err != nil {
return "", err
}
if math.Abs(v) <= 1 || math.IsNaN(v) || math.IsInf(v, 0) {
return fmt.Sprintf("%.4g", v), nil
}
return formatutil.HumanizeBytes(v), nil
},
// humanizeDuration converts given seconds to a human-readable duration
"humanizeDuration": func(i any) (string, error) {
v, err := toFloat64(i)
if err != nil {
return "", err
}
if math.IsNaN(v) || math.IsInf(v, 0) {
return fmt.Sprintf("%.4g", v), nil
}
if v == 0 {
return fmt.Sprintf("%.4gs", v), nil
}
if math.Abs(v) >= 1 {
sign := ""
if v < 0 {
sign = "-"
v = -v
}
seconds := int64(v) % 60
minutes := (int64(v) / 60) % 60
hours := (int64(v) / 60 / 60) % 24
days := int64(v) / 60 / 60 / 24
// For days to minutes, we display seconds as an integer.
if days != 0 {
return fmt.Sprintf("%s%dd %dh %dm %ds", sign, days, hours, minutes, seconds), nil
}
if hours != 0 {
return fmt.Sprintf("%s%dh %dm %ds", sign, hours, minutes, seconds), nil
}
if minutes != 0 {
return fmt.Sprintf("%s%dm %ds", sign, minutes, seconds), nil
}
// For seconds, we display 4 significant digits.
return fmt.Sprintf("%s%.4gs", sign, v), nil
}
prefix := ""
for _, p := range []string{"m", "u", "n", "p", "f", "a", "z", "y"} {
if math.Abs(v) >= 1 {
break
}
prefix = p
v *= 1000
}
return fmt.Sprintf("%.4g%ss", v, prefix), nil
},
// humanizePercentage converts given ratio value to a fraction of 100
"humanizePercentage": func(i any) (string, error) {
v, err := toFloat64(i)
if err != nil {
return "", err
}
return fmt.Sprintf("%.4g%%", v*100), nil
},
// humanizeTimestamp converts given timestamp to a human readable time equivalent
"humanizeTimestamp": func(i any) (string, error) {
v, err := toFloat64(i)
if err != nil {
return "", err
}
if math.IsNaN(v) || math.IsInf(v, 0) {
return fmt.Sprintf("%.4g", v), nil
}
t := timeFromUnixTimestamp(v).Time().UTC()
return fmt.Sprint(t), nil
},
// toTime converts given timestamp to a time.Time.
"toTime": func(i any) (time.Time, error) {
v, err := toFloat64(i)
if err != nil {
return time.Time{}, err
}
if math.IsNaN(v) || math.IsInf(v, 0) {
return time.Time{}, fmt.Errorf("cannot convert %v to time.Time", v)
}
t := timeFromUnixTimestamp(v).Time().UTC()
return t, nil
},
/* URLs */
// externalURL returns value of `external.url` flag
"externalURL": func() string {
// externalURL function supposed to be substituted at FuncsWithExteralURL().
// it is present here only for validation purposes, when there is no
// provided datasource.
//
// return non-empty slice to pass validation with chained functions in template
return ""
},
// pathPrefix returns a Path segment from the URL value in `external.url` flag
"pathPrefix": func() string {
// pathPrefix function supposed to be substituted at FuncsWithExteralURL().
// it is present here only for validation purposes, when there is no
// provided datasource.
//
// return non-empty slice to pass validation with chained functions in template
return ""
},
// pathEscape escapes the string so it can be safely placed inside a URL path segment.
//
// See also queryEscape.
"pathEscape": url.PathEscape,
// queryEscape escapes the string so it can be safely placed inside a query arg in URL.
//
// See also queryEscape.
"queryEscape": url.QueryEscape,
// query executes the MetricsQL/PromQL query against
// configured `datasource.url` address.
// For example, {{ query "foo" | first | value }} will
// execute "/api/v1/query?query=foo" request and will return
// the first value in response.
"query": func(_ string) ([]metric, error) {
// query function supposed to be substituted at FuncsWithQuery().
// it is present here only for validation purposes, when there is no
// provided datasource.
//
// return non-empty slice to pass validation with chained functions in template
// see issue #989 for details
return []metric{{}}, nil
},
// first returns the first by order element from the given metrics list.
// usually used alongside with `query` template function.
"first": func(metrics []metric) (metric, error) {
if len(metrics) > 0 {
return metrics[0], nil
}
return metric{}, errors.New("first() called on vector with no elements")
},
// label returns the value of the given label name for the given metric.
// usually used alongside with `query` template function.
"label": func(label string, m metric) string {
return m.Labels[label]
},
// value returns the value of the given metric.
// usually used alongside with `query` template function.
"value": func(m metric) float64 {
return m.Value
},
// strvalue returns metric name.
"strvalue": func(m metric) string {
return m.Labels["__name__"]
},
// sortByLabel sorts the given metrics by provided label key
"sortByLabel": func(label string, metrics []metric) []metric {
sort.SliceStable(metrics, func(i, j int) bool {
return metrics[i].Labels[label] < metrics[j].Labels[label]
})
return metrics
},
/* Helpers */
// Converts a list of objects to a map with keys arg0, arg1 etc.
// This is intended to allow multiple arguments to be passed to templates.
"args": func(args ...any) map[string]any {
result := make(map[string]any)
for i, a := range args {
result[fmt.Sprintf("arg%d", i)] = a
}
return result
},
// safeHtml marks string as HTML not requiring auto-escaping.
//
// See also htmlEscape.
"safeHtml": func(text string) htmlTpl.HTML {
return htmlTpl.HTML(text)
},
// ExecuteWithTemplate executes with the given template and data
func ExecuteWithTemplate(data AlertTplData, tpl *textTpl.Template) (string, error) {
fullData := tplData{
data,
externalLabels,
externalURL.String(),
}
}
// Time is the number of milliseconds since the epoch
// (1970-01-01 00:00 UTC) excluding leap seconds.
type Time int64
// timeFromUnixTimestamp returns the Time equivalent to t in unix timestamp.
func timeFromUnixTimestamp(t float64) Time {
return Time(t * 1e3)
}
// The number of nanoseconds per minimum tick.
const nanosPerTick = int64(minimumTick / time.Nanosecond)
// MinimumTick is the minimum supported time resolution. This has to be
// at least time.Second in order for the code below to work.
const minimumTick = time.Millisecond
// second is the Time duration equivalent to one second.
const second = int64(time.Second / minimumTick)
// Time returns the time.Time representation of t.
func (t Time) Time() time.Time {
return time.Unix(int64(t)/second, (int64(t)%second)*nanosPerTick)
}
func toFloat64(v any) (float64, error) {
switch i := v.(type) {
case float64:
return i, nil
case float32:
return float64(i), nil
case int64:
return float64(i), nil
case int32:
return float64(i), nil
case int:
return float64(i), nil
case uint64:
return float64(i), nil
case uint32:
return float64(i), nil
case uint:
return float64(i), nil
case string:
return strconv.ParseFloat(i, 64)
default:
return 0, fmt.Errorf("unexpected value type %v", i)
var buf bytes.Buffer
// returns the zero value for the map type's element
tpl.Option("missingkey=zero")
if err := tpl.Execute(&buf, fullData); err != nil {
return "", fmt.Errorf("failed to execute template: %w", err)
}
return buf.String(), nil
}

View File

@@ -1,239 +1,226 @@
package templates
import (
"math"
"fmt"
"net/url"
"strings"
"testing"
textTpl "text/template"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
)
func TestTemplateFuncs_StringConversion(t *testing.T) {
f := func(funcName, s, resultExpected string) {
func TestValidateTemplates(t *testing.T) {
f := func(annotations map[string]string, isValid bool) {
t.Helper()
funcs := templateFuncs()
v := funcs[funcName]
fLocal := v.(func(s string) string)
result := fLocal(s)
if result != resultExpected {
t.Fatalf("unexpected result for %s(%q); got\n%s\nwant\n%s", funcName, s, result, resultExpected)
err := ValidateTemplates(annotations)
if (err == nil) != isValid {
t.Fatalf("failed to validate template, got %t; want %t", (err == nil), isValid)
}
}
f("title", "foo bar", "Foo Bar")
f("toUpper", "foo", "FOO")
f("toLower", "FOO", "foo")
f("pathEscape", "foo/bar\n+baz", "foo%2Fbar%0A+baz")
f("queryEscape", "foo+bar\n+baz", "foo%2Bbar%0A%2Bbaz")
f("jsonEscape", `foo{bar="baz"}`+"\n + 1", `"foo{bar=\"baz\"}\n + 1"`)
f("quotesEscape", `foo{bar="baz"}`+"\n + 1", `foo{bar=\"baz\"}\n + 1`)
f("htmlEscape", "foo < 10\nabc", "foo &lt; 10\nabc")
f("crlfEscape", "foo\nbar\rx", `foo\nbar\rx`)
f("stripPort", "foo", "foo")
f("stripPort", "foo:1234", "foo")
f("stripDomain", "foo.bar.baz", "foo")
f("stripDomain", "foo.bar:123", "foo:123")
// empty
f(map[string]string{}, true)
// wrong text
f(map[string]string{
"summary": "{{",
}, false)
// valid
f(map[string]string{
"value": "{{$value}}",
"summary": "it's a test summary",
}, true)
// invalid variable
f(map[string]string{
"value": "{{$invalidValue}}",
"summary": "it's a test summary",
}, false)
}
func TestTemplateFuncs_Match(t *testing.T) {
funcs := templateFuncs()
// check "match" func
matchFunc := funcs["match"].(func(pattern, s string) (bool, error))
if _, err := matchFunc("invalid[regexp", "abc"); err == nil {
t.Fatalf("expecting non-nil error on invalid regexp")
}
ok, err := matchFunc("abc", "def")
func TestExecuteWithoutTemplate(t *testing.T) {
extLabels := make(map[string]string)
const (
extCluster = "prod"
extDC = "east"
extURL = "https://foo.bar"
)
url, _ := url.Parse(extURL)
extLabels["cluster"] = extCluster
extLabels["dc"] = extDC
err := Init(nil, extLabels, *url)
if err != nil {
t.Fatalf("unexpected error")
t.Fatalf("cannot init templates: %s", err)
}
if ok {
t.Fatalf("unexpected match")
}
ok, err = matchFunc("a.+b", "acsdb")
if err != nil {
t.Fatalf("unexpected error")
}
if !ok {
t.Fatalf("unexpected mismatch")
}
}
func TestTemplateFuncs_Formatting(t *testing.T) {
f := func(funcName string, p any, resultExpected string) {
f := func(data AlertTplData, annotations, expResults map[string]string) {
t.Helper()
funcs := templateFuncs()
v := funcs[funcName]
fLocal := v.(func(s any) (string, error))
result, err := fLocal(p)
if err != nil {
t.Fatalf("unexpected error for %s(%f): %s", funcName, p, err)
qFn := func(_ string) ([]datasource.Metric, error) {
return []datasource.Metric{
{
Labels: []prompbmarshal.Label{
{Name: "foo", Value: "bar"},
{Name: "baz", Value: "qux"},
},
Values: []float64{1},
Timestamps: []int64{1},
},
{
Labels: []prompbmarshal.Label{
{Name: "foo", Value: "garply"},
{Name: "baz", Value: "fred"},
},
Values: []float64{2},
Timestamps: []int64{1},
},
}, nil
}
if result != resultExpected {
t.Fatalf("unexpected result for %s(%f); got\n%s\nwant\n%s", funcName, p, result, resultExpected)
}
}
f("humanize1024", float64(0), "0")
f("humanize1024", math.Inf(0), "+Inf")
f("humanize1024", math.NaN(), "NaN")
f("humanize1024", float64(127087), "124.1ki")
f("humanize1024", float64(130137088), "124.1Mi")
f("humanize1024", float64(133260378112), "124.1Gi")
f("humanize1024", float64(136458627186688), "124.1Ti")
f("humanize1024", float64(139733634239168512), "124.1Pi")
f("humanize1024", float64(143087241460908556288), "124.1Ei")
f("humanize1024", float64(146521335255970361638912), "124.1Zi")
f("humanize1024", float64(150037847302113650318245888), "124.1Yi")
f("humanize1024", float64(153638755637364377925883789312), "1.271e+05Yi")
f("humanize", float64(127087), "127.1k")
f("humanize", float64(136458627186688), "136.5T")
f("humanizeDuration", 1, "1s")
f("humanizeDuration", 0.2, "200ms")
f("humanizeDuration", 42000, "11h 40m 0s")
f("humanizeDuration", 16790555, "194d 8h 2m 35s")
f("humanizePercentage", 1, "100%")
f("humanizePercentage", 0.8, "80%")
f("humanizePercentage", 0.015, "1.5%")
f("humanizeTimestamp", 1679055557, "2023-03-17 12:19:17 +0000 UTC")
}
func mkTemplate(current, replacement any) textTemplate {
tmpl := textTemplate{}
if current != nil {
switch val := current.(type) {
case string:
tmpl.current = textTpl.Must(newTemplate().Parse(val))
}
}
if replacement != nil {
switch val := replacement.(type) {
case string:
tmpl.replacement = textTpl.Must(newTemplate().Parse(val))
}
}
return tmpl
}
func equalTemplates(tmpls ...*textTpl.Template) bool {
var cmp *textTpl.Template
for i, tmpl := range tmpls {
if i == 0 {
cmp = tmpl
} else {
if cmp == nil || tmpl == nil {
if cmp != tmpl {
return false
}
continue
for k := range annotations {
v, err := ExecuteWithoutTemplate(qFn, annotations[k], data)
if err != nil {
t.Fatalf("cannot execute template: %s", err)
}
if len(tmpl.Templates()) != len(cmp.Templates()) {
return false
}
for _, t := range tmpl.Templates() {
tp := cmp.Lookup(t.Name())
if tp == nil {
return false
}
if tp.Root.String() != t.Root.String() {
return false
}
if v != expResults[k] {
t.Fatalf("unexpected result; got %s; want %s", v, expResults[k])
}
}
}
return true
}
func TestTemplatesLoad_Failure(t *testing.T) {
f := func(pathPatterns []string, expectedErrStr string) {
t.Helper()
err := Load(pathPatterns, url.URL{})
if err == nil {
t.Fatalf("expecting non-nil error")
}
errStr := err.Error()
if !strings.Contains(errStr, expectedErrStr) {
t.Fatalf("the returned error %q doesn't contain %q", errStr, expectedErrStr)
}
}
// load template with syntax error
f([]string{
"templates/other/nested/bad0-*.tpl",
"templates/test/good0-*.tpl",
}, "failed to parse template glob")
}
func TestTemplatesLoad_Success(t *testing.T) {
f := func(pathPatterns []string, expectedTmpl textTemplate) {
t.Helper()
masterTmplOrig := masterTmpl
defer func() {
masterTmpl = masterTmplOrig
}()
if err := Load(pathPatterns, url.URL{}); err != nil {
t.Fatalf("cannot load templates: %s", err)
}
Reload()
if !equalTemplates(masterTmpl.replacement, expectedTmpl.replacement) {
t.Fatalf("unexpected replacement template\ngot\n%+v\nwant\n%+v", masterTmpl.replacement, expectedTmpl.replacement)
}
if !equalTemplates(masterTmpl.current, expectedTmpl.current) {
t.Fatalf("unexpected current template\ngot\n%+v\nwant\n%+v", masterTmpl.current, expectedTmpl.current)
}
}
// non existing path
pathPatterns := []string{
"templates/non-existing/good-*.tpl",
"templates/absent/good-*.tpl",
}
expectedTmpl := mkTemplate(``, nil)
f(pathPatterns, expectedTmpl)
// existing path
pathPatterns = []string{
"templates/test/good0-*.tpl",
}
expectedTmpl = mkTemplate(`
{{- define "good0-test.tpl" -}}{{- end -}}
{{- define "test.0" -}}
{{ printf "Hello %s!" externalURL }}
{{- end -}}
{{- define "test.2" -}}
{{ printf "Hello %s!" externalURL }}
{{- end -}}
{{- define "test.3" -}}
{{ printf "Hello %s!" externalURL }}
{{- end -}}
`, nil)
f(pathPatterns, expectedTmpl)
// existing path defined template override
pathPatterns = []string{
"templates/other/nested/good0-*.tpl",
}
expectedTmpl = mkTemplate(`
{{- define "good0-test.tpl" -}}{{- end -}}
{{- define "test.0" -}}
{{ printf "Hello %s!" externalURL }}
{{- end -}}
{{- define "test.1" -}}
{{ printf "Hello %s!" externalURL }}
{{- end -}}
{{- define "test.3" -}}
{{ printf "Hello %s!" externalURL }}
{{- end -}}
`, nil)
f(pathPatterns, expectedTmpl)
// empty-alert
f(AlertTplData{}, map[string]string{}, map[string]string{})
// no-template
f(AlertTplData{
Value: 1e4,
Labels: map[string]string{
"instance": "localhost",
},
}, map[string]string{
"summary": "it's a test summary",
"description": "it's a test description",
}, map[string]string{
"summary": "it's a test summary",
"description": "it's a test description",
})
// label-template
f(AlertTplData{
Value: 1e4,
Labels: map[string]string{
"job": "staging",
"instance": "localhost",
},
For: 5 * time.Minute,
}, map[string]string{
"summary": "Too high connection number for {{$labels.instance}} for job {{$labels.job}}",
"description": "It is {{ $value }} connections for {{$labels.instance}} for more than {{ .For }}",
"non-existing-label": "{{$labels.nonexisting}}",
}, map[string]string{
"summary": "Too high connection number for localhost for job staging",
"description": "It is 10000 connections for localhost for more than 5m0s",
"non-existing-label": "",
})
// label template override
f(AlertTplData{
Value: 1e4,
}, map[string]string{
"summary": `{{- define "default.template" -}} {{ printf "summary" }} {{- end -}} {{ template "default.template" . }}`,
"description": `{{- define "default.template" -}} {{ printf "description" }} {{- end -}} {{ template "default.template" . }}`,
"value": `{{$value }}`,
}, map[string]string{
"summary": "summary",
"description": "description",
"value": "10000",
})
// expression-template
f(AlertTplData{
Expr: `vm_rows{"label"="bar"}<0`,
}, map[string]string{
"exprEscapedQuery": "{{ $expr|queryEscape }}",
"exprEscapedPath": "{{ $expr|pathEscape }}",
"exprEscapedJSON": "{{ $expr|jsonEscape }}",
"exprEscapedQuotes": "{{ $expr|quotesEscape }}",
"exprEscapedHTML": "{{ $expr|htmlEscape }}",
}, map[string]string{
"exprEscapedQuery": "vm_rows%7B%22label%22%3D%22bar%22%7D%3C0",
"exprEscapedPath": "vm_rows%7B%22label%22=%22bar%22%7D%3C0",
"exprEscapedJSON": `"vm_rows{\"label\"=\"bar\"}\u003c0"`,
"exprEscapedQuotes": `vm_rows{\"label\"=\"bar\"}\u003c0`,
"exprEscapedHTML": "vm_rows{&quot;label&quot;=&quot;bar&quot;}&lt;0",
})
// query function
f(AlertTplData{
Expr: `vm_rows{"label"="bar"}>0`,
}, map[string]string{
"summary": `{{ query "foo" | first | value }}`,
"desc": `{{ range query "bar" }}{{ . | label "foo" }} {{ . | value }};{{ end }}`,
}, map[string]string{
"summary": "1",
"desc": "bar 1;garply 2;",
})
// external
f(AlertTplData{
Value: 1e4,
Labels: map[string]string{
"job": "staging",
"instance": "localhost",
},
}, map[string]string{
"url": "{{ $externalURL }}",
"summary": "Issues with {{$labels.instance}} (dc-{{$externalLabels.dc}}) for job {{$labels.job}}",
"description": "It is {{ $value }} connections for {{$labels.instance}} (cluster-{{$externalLabels.cluster}})",
}, map[string]string{
"url": extURL,
"summary": fmt.Sprintf("Issues with localhost (dc-%s) for job staging", extDC),
"description": fmt.Sprintf("It is 10000 connections for localhost (cluster-%s)", extCluster),
})
// alert, group IDs & ActiveAt time
f(AlertTplData{
AlertID: 42,
GroupID: 24,
ActiveAt: time.Date(2022, 8, 19, 20, 34, 58, 651387237, time.UTC),
}, map[string]string{
"url": "/api/v1/alert?alertID={{$alertID}}&groupID={{$groupID}}",
"diagram": "![](http://example.com?render={{$activeAt.Unix}}",
}, map[string]string{
"url": "/api/v1/alert?alertID=42&groupID=24",
"diagram": "![](http://example.com?render=1660941298",
})
// ActiveAt time is nil
f(AlertTplData{}, map[string]string{
"default_time": "{{$activeAt}}",
}, map[string]string{
"default_time": "0001-01-01 00:00:00 +0000 UTC",
})
// ActiveAt custom format
f(AlertTplData{
ActiveAt: time.Date(2022, 8, 19, 20, 34, 58, 651387237, time.UTC),
}, map[string]string{
"fire_time": `{{$activeAt.Format "2006/01/02 15:04:05"}}`,
}, map[string]string{
"fire_time": "2022/08/19 20:34:58",
})
// ActiveAt query range
f(AlertTplData{
ActiveAt: time.Date(2022, 8, 19, 20, 34, 58, 651387237, time.UTC),
}, map[string]string{
"grafana_url": `vm-grafana.com?from={{($activeAt.Add (parseDurationTime "1h")).Unix}}&to={{($activeAt.Add (parseDurationTime "-1h")).Unix}}`,
}, map[string]string{
"grafana_url": "vm-grafana.com?from=1660944898&to=1660937698",
})
}

View File

@@ -40,15 +40,15 @@ type filter struct {
labelValue string
}
func (f filter) inRange(minV, maxV int64) bool {
func (f filter) inRange(min, max int64) bool {
fmin, fmax := f.min, f.max
if minV == 0 {
fmin = minV
if min == 0 {
fmin = min
}
if fmax == 0 {
fmax = maxV
fmax = max
}
return minV <= fmax && fmin <= maxV
return min <= fmax && fmin <= max
}
// NewClient creates and validates new Client
@@ -59,13 +59,13 @@ func NewClient(cfg Config) (*Client, error) {
return nil, fmt.Errorf("failed to open snapshot %q: %s", cfg.Snapshot, err)
}
c := &Client{DBReadOnly: db}
minTime, maxTime, err := parseTime(cfg.Filter.TimeMin, cfg.Filter.TimeMax)
min, max, err := parseTime(cfg.Filter.TimeMin, cfg.Filter.TimeMax)
if err != nil {
return nil, fmt.Errorf("failed to parse time in filter: %s", err)
}
c.filter = filter{
min: minTime,
max: maxTime,
min: min,
max: max,
label: cfg.Filter.Label,
labelValue: cfg.Filter.LabelValue,
}

View File

@@ -98,13 +98,13 @@ func aggrMin(values []float64) float64 {
if pos < 0 {
return nan
}
minV := values[pos]
min := values[pos]
for _, v := range values[pos+1:] {
if !math.IsNaN(v) && v < minV {
minV = v
if !math.IsNaN(v) && v < min {
min = v
}
}
return minV
return min
}
func aggrMax(values []float64) float64 {
@@ -112,13 +112,13 @@ func aggrMax(values []float64) float64 {
if pos < 0 {
return nan
}
maxV := values[pos]
max := values[pos]
for _, v := range values[pos+1:] {
if !math.IsNaN(v) && v > maxV {
maxV = v
if !math.IsNaN(v) && v > max {
max = v
}
}
return maxV
return max
}
func aggrDiff(values []float64) float64 {
@@ -177,12 +177,12 @@ func aggrCount(values []float64) float64 {
}
func aggrRange(values []float64) float64 {
minV := aggrMin(values)
if math.IsNaN(minV) {
min := aggrMin(values)
if math.IsNaN(min) {
return nan
}
maxV := aggrMax(values)
return maxV - minV
max := aggrMax(values)
return max - min
}
func aggrMultiply(values []float64) float64 {

View File

@@ -2594,17 +2594,17 @@ func transformMinMax(ec *evalConfig, fe *graphiteql.FuncExpr) (nextSeriesFunc, e
}
f := nextSeriesConcurrentWrapper(nextSeries, func(s *series) (*series, error) {
values := s.Values
minV := aggrMin(values)
if math.IsNaN(minV) {
minV = 0
min := aggrMin(values)
if math.IsNaN(min) {
min = 0
}
maxV := aggrMax(values)
if math.IsNaN(maxV) {
maxV = 0
max := aggrMax(values)
if math.IsNaN(max) {
max = 0
}
vRange := maxV - minV
vRange := max - min
for i, v := range values {
v = (v - minV) / vRange
v = (v - min) / vRange
if math.IsInf(v, 0) {
v = 0
}
@@ -2975,9 +2975,9 @@ func transformRemoveAbovePercentile(ec *evalConfig, fe *graphiteql.FuncExpr) (ne
}
f := nextSeriesConcurrentWrapper(nextSeries, func(s *series) (*series, error) {
values := s.Values
maxV := aggrFunc(values)
max := aggrFunc(values)
for i, v := range values {
if v > maxV {
if v > max {
values[i] = nan
}
}
@@ -3035,9 +3035,9 @@ func transformRemoveBelowPercentile(ec *evalConfig, fe *graphiteql.FuncExpr) (ne
}
f := nextSeriesConcurrentWrapper(nextSeries, func(s *series) (*series, error) {
values := s.Values
minV := aggrFunc(values)
min := aggrFunc(values)
for i, v := range values {
if v < minV {
if v < min {
values[i] = nan
}
}
@@ -4514,11 +4514,11 @@ func transformOffsetToZero(ec *evalConfig, fe *graphiteql.FuncExpr) (nextSeriesF
}
f := nextSeriesConcurrentWrapper(nextSeries, func(s *series) (*series, error) {
values := s.Values
minV := aggrMin(values)
min := aggrMin(values)
for i, v := range values {
values[i] = v - minV
values[i] = v - min
}
s.Tags["offsetToZero"] = fmt.Sprintf("%g", minV)
s.Tags["offsetToZero"] = fmt.Sprintf("%g", min)
s.Name = fmt.Sprintf("offsetToZero(%s)", s.Name)
s.expr = fe
s.pathExpression = s.Name
@@ -4567,29 +4567,29 @@ func transformPerSecond(ec *evalConfig, fe *graphiteql.FuncExpr) (nextSeriesFunc
return f, nil
}
func nonNegativeDelta(currV, prevV, maxV, minV float64) (float64, float64) {
if !math.IsNaN(maxV) && currV > maxV {
func nonNegativeDelta(curr, prev, max, min float64) (float64, float64) {
if !math.IsNaN(max) && curr > max {
return nan, nan
}
if !math.IsNaN(minV) && currV < minV {
if !math.IsNaN(min) && curr < min {
return nan, nan
}
if math.IsNaN(currV) || math.IsNaN(prevV) {
return nan, currV
if math.IsNaN(curr) || math.IsNaN(prev) {
return nan, curr
}
if currV >= prevV {
return currV - prevV, currV
if curr >= prev {
return curr - prev, curr
}
if !math.IsNaN(maxV) {
if math.IsNaN(minV) {
minV = float64(0)
if !math.IsNaN(max) {
if math.IsNaN(min) {
min = float64(0)
}
return maxV + 1 + currV - prevV - minV, currV
return max + 1 + curr - prev - min, curr
}
if !math.IsNaN(minV) {
return currV - minV, currV
if !math.IsNaN(min) {
return curr - min, curr
}
return nan, currV
return nan, curr
}
// See https://graphite.readthedocs.io/en/stable/functions.html#graphite.render.functions.threshold
@@ -4941,8 +4941,8 @@ func transformSortByMinima(ec *evalConfig, fe *graphiteql.FuncExpr) (nextSeriesF
}
// Filter out series with all the values smaller than 0
f := nextSeriesConcurrentWrapper(nextSeries, func(s *series) (*series, error) {
maxV := aggrMax(s.Values)
if math.IsNaN(maxV) || maxV <= 0 {
max := aggrMax(s.Values)
if math.IsNaN(max) || max <= 0 {
return nil, nil
}
return s, nil

View File

@@ -295,13 +295,13 @@ func aggrFuncMin(tss []*timeseries) []*timeseries {
}
dst := tss[0]
for i := range dst.Values {
minV := dst.Values[i]
min := dst.Values[i]
for _, ts := range tss {
if math.IsNaN(minV) || ts.Values[i] < minV {
minV = ts.Values[i]
if math.IsNaN(min) || ts.Values[i] < min {
min = ts.Values[i]
}
}
dst.Values[i] = minV
dst.Values[i] = min
}
return tss[:1]
}
@@ -313,13 +313,13 @@ func aggrFuncMax(tss []*timeseries) []*timeseries {
}
dst := tss[0]
for i := range dst.Values {
maxV := dst.Values[i]
max := dst.Values[i]
for _, ts := range tss {
if math.IsNaN(maxV) || ts.Values[i] > maxV {
maxV = ts.Values[i]
if math.IsNaN(max) || ts.Values[i] > max {
max = ts.Values[i]
}
}
dst.Values[i] = maxV
dst.Values[i] = max
}
return tss[:1]
}
@@ -793,7 +793,7 @@ func fillNaNsAtIdx(idx int, k float64, tss []*timeseries) {
}
}
func getIntK(k float64, maxV int) int {
func getIntK(k float64, max int) int {
if math.IsNaN(k) {
return 0
}
@@ -801,38 +801,38 @@ func getIntK(k float64, maxV int) int {
if kn < 0 {
return 0
}
if kn > maxV {
return maxV
if kn > max {
return max
}
return kn
}
func minValue(values []float64) float64 {
minV := nan
for len(values) > 0 && math.IsNaN(minV) {
minV = values[0]
min := nan
for len(values) > 0 && math.IsNaN(min) {
min = values[0]
values = values[1:]
}
for _, v := range values {
if !math.IsNaN(v) && v < minV {
minV = v
if !math.IsNaN(v) && v < min {
min = v
}
}
return minV
return min
}
func maxValue(values []float64) float64 {
maxV := nan
for len(values) > 0 && math.IsNaN(maxV) {
maxV = values[0]
max := nan
for len(values) > 0 && math.IsNaN(max) {
max = values[0]
values = values[1:]
}
for _, v := range values {
if !math.IsNaN(v) && v > maxV {
maxV = v
if !math.IsNaN(v) && v > max {
max = v
}
}
return maxV
return max
}
func avgValue(values []float64) float64 {

View File

@@ -1685,9 +1685,9 @@ func rollupRateOverSum(rfa *rollupFuncArg) float64 {
}
func rollupRange(rfa *rollupFuncArg) float64 {
maxV := rollupMax(rfa)
minV := rollupMin(rfa)
return maxV - minV
max := rollupMax(rfa)
min := rollupMin(rfa)
return max - min
}
func rollupSum2(rfa *rollupFuncArg) float64 {
@@ -2195,38 +2195,38 @@ func rollupClose(rfa *rollupFuncArg) float64 {
func rollupHigh(rfa *rollupFuncArg) float64 {
values := getCandlestickValues(rfa)
maxV := getFirstValueForCandlestick(rfa)
if math.IsNaN(maxV) {
max := getFirstValueForCandlestick(rfa)
if math.IsNaN(max) {
if len(values) == 0 {
return nan
}
maxV = values[0]
max = values[0]
values = values[1:]
}
for _, v := range values {
if v > maxV {
maxV = v
if v > max {
max = v
}
}
return maxV
return max
}
func rollupLow(rfa *rollupFuncArg) float64 {
values := getCandlestickValues(rfa)
minV := getFirstValueForCandlestick(rfa)
if math.IsNaN(minV) {
min := getFirstValueForCandlestick(rfa)
if math.IsNaN(min) {
if len(values) == 0 {
return nan
}
minV = values[0]
min = values[0]
values = values[1:]
}
for _, v := range values {
if v < minV {
minV = v
if v < min {
min = v
}
}
return minV
return min
}
func rollupModeOverTime(rfa *rollupFuncArg) float64 {

View File

@@ -45,7 +45,7 @@ services:
# storing logs and serving read queries.
victorialogs:
container_name: victorialogs
image: victoriametrics/victoria-logs:v1.5.0-victorialogs
image: victoriametrics/victoria-logs:v1.4.0-victorialogs
command:
- "--storageDataPath=/vlogs"
- "--httpListenAddr=:9428"

View File

@@ -1,7 +1,7 @@
services:
# meta service will be ignored by compose
.victorialogs:
image: docker.io/victoriametrics/victoria-logs:v1.5.0-victorialogs
image: docker.io/victoriametrics/victoria-logs:v1.4.0-victorialogs
command:
- -storageDataPath=/vlogs
- -loggerFormat=json

View File

@@ -3,7 +3,7 @@ version: "3"
services:
# Run `make package-victoria-logs` to build victoria-logs image
vlogs:
image: docker.io/victoriametrics/victoria-logs:v1.5.0-victorialogs
image: docker.io/victoriametrics/victoria-logs:v1.4.0-victorialogs
volumes:
- vlogs:/vlogs
ports:

View File

@@ -70,7 +70,6 @@ Bumping the limits may significantly improve build speed.
* linux/386
This step can be run manually with the command `make publish` from the needed git tag.
1. Verify that created images are stable and don't introduce regressions on [test environment](https://github.com/VictoriaMetrics/VictoriaMetrics-enterprise/blob/master/Release-Guide.md#testing-releases).
1. Test new images on [sandbox](https://github.com/VictoriaMetrics/VictoriaMetrics-enterprise/blob/master/Release-Guide.md#testing-releases).
1. Push the tags `v1.xx.y` and `v1.xx.y-cluster` created at previous steps to public GitHub repository at https://github.com/VictoriaMetrics/VictoriaMetrics.
Push the tags `v1.xx.y`, `v1.xx.y-cluster`, `v1.xx.y-enterprise` and `v1.xx.y-enterprise-cluster` to the corresponding
branches in private repository.
@@ -89,6 +88,7 @@ Bumping the limits may significantly improve build speed.
file created at the step `a`.
- To run the command `TAG=v1.xx.y make github-create-release github-upload-assets`, so new release is created
and all the needed assets are re-uploaded to it.
1. Test new images on [sandbox](https://github.com/VictoriaMetrics/VictoriaMetrics-enterprise/blob/master/Release-Guide.md#testing-releases).
1. Go to <https://github.com/VictoriaMetrics/VictoriaMetrics/releases> and verify that draft release with the name `TAG` has been created
and this release contains all the needed binaries and checksums.
1. Update the release description with the content of [CHANGELOG](https://github.com/VictoriaMetrics/VictoriaMetrics/blob/master/docs/CHANGELOG.md) for this release.

View File

@@ -16,12 +16,6 @@ according to [these docs](https://docs.victoriametrics.com/victorialogs/quicksta
## tip
* FEATURE: add [`histogram` stats function](https://docs.victoriametrics.com/victorialogs/logsql/#histogram-stats) for calculating [VictoriaMetrics histogram buckets](https://valyala.medium.com/improving-histogram-usability-for-prometheus-and-grafana-bc7e5df0e350) over the given [log field](https://docs.victoriametrics.com/victorialogs/keyconcepts/#data-model).
## [v1.5.0](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.5.0-victorialogs)
Released at 2025-01-13
* FEATURE: [`count_uniq` stats function](https://docs.victoriametrics.com/victorialogs/logsql/#count_uniq-stats): improve performance by up to 50% and reduce memory usage by up to 4x when this function is applied to fields with big number of unique integer values.
* FEATURE: [`stats` pipe](https://docs.victoriametrics.com/victorialogs/logsql/#stats-pipe): improve performance and reduce memory usage by up to 50% for `log_field` with big number of unique values at `stats by (log_field) ...`.
* FEATURE: [`math` pipe](https://docs.victoriametrics.com/victorialogs/logsql/#math-pipe): improve performance by up to 8x.

View File

@@ -2762,7 +2762,6 @@ See also:
- [`uniq` pipe](#uniq-pipe)
- [`stats` pipe](#stats-pipe)
- [`sort` pipe](#sort-pipe)
- [`histogram` stats function](#histogram-stats)
### uniq pipe
@@ -3107,7 +3106,6 @@ LogsQL supports the following functions for [`stats` pipe](#stats-pipe):
- [`count_empty`](#count_empty-stats) returns the number logs with empty [log fields](https://docs.victoriametrics.com/victorialogs/keyconcepts/#data-model).
- [`count_uniq`](#count_uniq-stats) returns the number of unique non-empty values for the given [log fields](https://docs.victoriametrics.com/victorialogs/keyconcepts/#data-model).
- [`count_uniq_hash`](#count_uniq_hash-stats) returns the number of unique hashes for non-empty values at the given [log fields](https://docs.victoriametrics.com/victorialogs/keyconcepts/#data-model).
- [`histogram`](#histogram-stats) returns [VictoriaMetrics histogram](https://valyala.medium.com/improving-histogram-usability-for-prometheus-and-grafana-bc7e5df0e350) for the given [log field](https://docs.victoriametrics.com/victorialogs/keyconcepts/#data-model).
- [`max`](#max-stats) returns the maximum value over the given [log fields](https://docs.victoriametrics.com/victorialogs/keyconcepts/#data-model).
- [`median`](#median-stats) returns the [median](https://en.wikipedia.org/wiki/Median) value over the given [log fields](https://docs.victoriametrics.com/victorialogs/keyconcepts/#data-model).
- [`min`](#min-stats) returns the minimum value over the given [log fields](https://docs.victoriametrics.com/victorialogs/keyconcepts/#data-model).
@@ -3255,25 +3253,6 @@ See also:
- [`uniq_values`](#uniq_values-stats)
- [`count`](#count-stats)
### histogram stats
`histogram(field)` [stats pipe function](#stats-pipe-functions) returns [VictoriaMetrics histogram buckets](https://docs.victoriametrics.com/victorialogs/keyconcepts/#data-model)
for the given [`field`](https://docs.victoriametrics.com/victorialogs/keyconcepts/#data-model).
For example, the following query returns histogram buckets for the `response_size` field grouped by `host` field, across logs for the last 5 minutes:
```logsql
_time:5m | stats by (host) histogram(response_size)
```
If the field contains [duration value](#duration-values), then `histogram` normalizes it to nanoseconds. For example, `1.25ms` is normalized to `1_250_000`.
If the field contains [short numeric value](#short-numeric-values), then `histogram` normalizes it to numeric value without any suffixes. For example, `1KiB` is converted to `1024`.
See also:
- [`quantile`](#quantile-stats)
### max stats
`max(field1, ..., fieldN)` [stats pipe function](#stats-pipe-functions) returns the maximum value across
@@ -3351,7 +3330,6 @@ _time:5m | stats
See also:
- [`histogram`](#histogram-stats)
- [`min`](#min-stats)
- [`max`](#max-stats)
- [`median`](#median-stats)

View File

@@ -33,8 +33,8 @@ Just download archive for the needed Operating system and architecture, unpack i
For example, the following commands download VictoriaLogs archive for Linux/amd64, unpack and run it:
```sh
curl -L -O https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/v1.5.0-victorialogs/victoria-logs-linux-amd64-v1.5.0-victorialogs.tar.gz
tar xzf victoria-logs-linux-amd64-v1.5.0-victorialogs.tar.gz
curl -L -O https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/v1.4.0-victorialogs/victoria-logs-linux-amd64-v1.4.0-victorialogs.tar.gz
tar xzf victoria-logs-linux-amd64-v1.4.0-victorialogs.tar.gz
./victoria-logs-prod
```
@@ -58,7 +58,7 @@ Here is the command to run VictoriaLogs in a Docker container:
```sh
docker run --rm -it -p 9428:9428 -v ./victoria-logs-data:/victoria-logs-data \
docker.io/victoriametrics/victoria-logs:v1.5.0-victorialogs
docker.io/victoriametrics/victoria-logs:v1.4.0-victorialogs
```
See also:

View File

@@ -23,15 +23,15 @@ or from [docker images](https://hub.docker.com/r/victoriametrics/vlogscli/tags):
### Running `vlogscli` from release binary
```sh
curl -L -O https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/v1.5.0-victorialogs/vlogscli-linux-amd64-v1.5.0-victorialogs.tar.gz
tar xzf vlogscli-linux-amd64-v1.5.0-victorialogs.tar.gz
curl -L -O https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/v1.4.0-victorialogs/vlogscli-linux-amd64-v1.4.0-victorialogs.tar.gz
tar xzf vlogscli-linux-amd64-v1.4.0-victorialogs.tar.gz
./vlogscli-prod
```
### Running `vlogscli` from Docker image
```sh
docker run --rm -it docker.io/victoriametrics/vlogscli:v1.5.0-victorialogs
docker run --rm -it docker.io/victoriametrics/vlogscli:v1.4.0-victorialogs
```
## Configuration

View File

@@ -155,9 +155,7 @@ How often to completely retrain the models. If not set, value of `infer_every` i
<tr>
<td>
<span>
`start_from`{{% available_from "v1.18.5" anomaly %}}
</span>
</td>
<td>str, Optional</td>
<td>
@@ -171,9 +169,8 @@ Specifies when to initiate the first `fit_every` call. Accepts either an ISO 860
</tr>
<tr>
<td>
<span>
`tz`{{% available_from "v1.18.5" anomaly %}}
</span>
</td>
<td>str, Optional</td>
<td>

View File

@@ -31,9 +31,8 @@ Future updates will introduce additional export methods, offering users more fle
`class`
</td>
<td>
<span>
`writer.vm.VmWriter` or `vm`{{% available_from "v1.13.0" anomaly %}}
</span>
</td>
<td>
@@ -60,9 +59,8 @@ Datasource URL address
`tenant_id`
</td>
<td>
<span>
`0:0`, `multitenant`{{% available_from "v1.16.2" anomaly %}}
</span>
</td>
<td>
@@ -249,9 +247,8 @@ Token is passed in the standard format with header: `Authorization: bearer {toke
`path_to_file`
</td>
<td>
<span>
Path to a file, which contains token, that is passed in the standard format with header: `Authorization: bearer {token}`{{% available_from "v1.15.9" anomaly %}}
</span> </td>
</td>
</tr>
</tbody>
</table>

File diff suppressed because it is too large Load Diff

View File

@@ -1,11 +1,11 @@
---
weight: 7
weight: 6
title: Year 2020
menu:
docs:
identifier: vm-changelog-2020
parent: vm-changelog
weight: 7
weight: 6
aliases:
- /CHANGELOG_2020.html
- /changelog_2020

View File

@@ -1,11 +1,11 @@
---
weight: 6
weight: 5
title: Year 2021
menu:
docs:
identifier: vm-changelog-2021
parent: vm-changelog
weight: 6
weight: 5
aliases:
- /CHANGELOG_2021.html
- /changelog_2021

View File

@@ -1,11 +1,11 @@
---
weight: 5
weight: 4
title: Year 2022
menu:
docs:
identifier: vm-changelog-2022
parent: vm-changelog
weight: 5
weight: 4
aliases:
- /CHANGELOG_2022.html
- /changelog_2022

View File

@@ -1,11 +1,11 @@
---
weight: 4
weight: 3
title: Year 2023
menu:
docs:
identifier: vm-changelog-2023
parent: vm-changelog
weight: 4
weight: 3
aliases:
- /CHANGELOG_2023.html
- /changelog_2023

File diff suppressed because it is too large Load Diff

View File

@@ -1,16 +1,6 @@
## Next release
- TODO
## 0.7.1
**Release date:** 10 Jan 2025
![Helm: v3](https://img.shields.io/badge/Helm-v3.14%2B-informational?color=informational&logo=helm&link=https%3A%2F%2Fgithub.com%2Fhelm%2Fhelm%2Freleases%2Ftag%2Fv3.14.0) ![AppVersion: v1.108.1](https://img.shields.io/badge/v1.108.1-success?logo=VictoriaMetrics&labelColor=gray&link=https%3A%2F%2Fdocs.victoriametrics.com%2Fchangelog%23v11081)
- updated common dependency 0.0.35 -> 0.0.37
- fixed typo useMultitenantMode -> useMultiTenantMode in remotewrite settings
- allow passing additional remotewrite setings
## 0.7.0

View File

@@ -1,6 +1,6 @@
![Version](https://img.shields.io/badge/0.7.1-gray?logo=Helm&labelColor=gray&link=https%3A%2F%2Fdocs.victoriametrics.com%2Fhelm%2Fvictoria-metrics-distributed%2Fchangelog%2F%23071)
![Version](https://img.shields.io/badge/0.7.0-gray?logo=Helm&labelColor=gray&link=https%3A%2F%2Fdocs.victoriametrics.com%2Fhelm%2Fvictoria-metrics-distributed%2Fchangelog%2F%23070)
![ArtifactHub](https://img.shields.io/badge/ArtifactHub-informational?logoColor=white&color=417598&logo=artifacthub&link=https%3A%2F%2Fartifacthub.io%2Fpackages%2Fhelm%2Fvictoriametrics%2Fvictoria-metrics-distributed)
![License](https://img.shields.io/github/license/VictoriaMetrics/helm-charts?labelColor=green&label=&link=https%3A%2F%2Fgithub.com%2FVictoriaMetrics%2Fhelm-charts%2Fblob%2Fmaster%2FLICENSE)
![Slack](https://img.shields.io/badge/Join-4A154B?logo=slack&link=https%3A%2F%2Fslack.victoriametrics.com)

View File

@@ -2,14 +2,6 @@
- TODO
## 0.33.3
**Release date:** 13 Jan 2025
![Helm: v3](https://img.shields.io/badge/Helm-v3.14%2B-informational?color=informational&logo=helm&link=https%3A%2F%2Fgithub.com%2Fhelm%2Fhelm%2Freleases%2Ftag%2Fv3.14.0) ![AppVersion: v1.108.1](https://img.shields.io/badge/v1.108.1-success?logo=VictoriaMetrics&labelColor=gray&link=https%3A%2F%2Fdocs.victoriametrics.com%2Fchangelog%23v11081)
- updates operator to [v0.51.3](https://github.com/VictoriaMetrics/operator/releases/tag/v0.51.3) version
## 0.33.2
**Release date:** 06 Jan 2025

View File

@@ -1,6 +1,6 @@
![Version](https://img.shields.io/badge/0.33.3-gray?logo=Helm&labelColor=gray&link=https%3A%2F%2Fdocs.victoriametrics.com%2Fhelm%2Fvictoria-metrics-k8s-stack%2Fchangelog%2F%230333)
![Version](https://img.shields.io/badge/0.33.2-gray?logo=Helm&labelColor=gray&link=https%3A%2F%2Fdocs.victoriametrics.com%2Fhelm%2Fvictoria-metrics-k8s-stack%2Fchangelog%2F%230332)
![ArtifactHub](https://img.shields.io/badge/ArtifactHub-informational?logoColor=white&color=417598&logo=artifacthub&link=https%3A%2F%2Fartifacthub.io%2Fpackages%2Fhelm%2Fvictoriametrics%2Fvictoria-metrics-k8s-stack)
![License](https://img.shields.io/github/license/VictoriaMetrics/helm-charts?labelColor=green&label=&link=https%3A%2F%2Fgithub.com%2FVictoriaMetrics%2Fhelm-charts%2Fblob%2Fmaster%2FLICENSE)
![Slack](https://img.shields.io/badge/Join-4A154B?logo=slack&link=https%3A%2F%2Fslack.victoriametrics.com)

View File

@@ -2,14 +2,6 @@
- TODO
## 0.40.4
**Release date:** 13 Jan 2025
![Helm: v3](https://img.shields.io/badge/Helm-v3.14%2B-informational?color=informational&logo=helm&link=https%3A%2F%2Fgithub.com%2Fhelm%2Fhelm%2Freleases%2Ftag%2Fv3.14.0) ![AppVersion: v0.51.3](https://img.shields.io/badge/v0.51.3-success?logo=VictoriaMetrics&labelColor=gray&link=https%3A%2F%2Fdocs.victoriametrics.com%2Foperator%2Fchangelog%23v0513)
- updates operator to [v0.51.3](https://github.com/VictoriaMetrics/operator/releases/tag/v0.51.3) version
## 0.40.3
**Release date:** 06 Jan 2025

View File

@@ -1,6 +1,6 @@
![Version](https://img.shields.io/badge/0.40.4-gray?logo=Helm&labelColor=gray&link=https%3A%2F%2Fdocs.victoriametrics.com%2Fhelm%2Fvictoria-metrics-operator%2Fchangelog%2F%230404)
![Version](https://img.shields.io/badge/0.40.3-gray?logo=Helm&labelColor=gray&link=https%3A%2F%2Fdocs.victoriametrics.com%2Fhelm%2Fvictoria-metrics-operator%2Fchangelog%2F%230403)
![ArtifactHub](https://img.shields.io/badge/ArtifactHub-informational?logoColor=white&color=417598&logo=artifacthub&link=https%3A%2F%2Fartifacthub.io%2Fpackages%2Fhelm%2Fvictoriametrics%2Fvictoria-metrics-operator)
![License](https://img.shields.io/github/license/VictoriaMetrics/helm-charts?labelColor=green&label=&link=https%3A%2F%2Fgithub.com%2FVictoriaMetrics%2Fhelm-charts%2Fblob%2Fmaster%2FLICENSE)
![Slack](https://img.shields.io/badge/Join-4A154B?logo=slack&link=https%3A%2F%2Fslack.victoriametrics.com)

View File

@@ -1,7 +0,0 @@
---
page: search
layout: search
draft: false
weight: 0
search: true
---

View File

@@ -149,7 +149,7 @@ or [Prometheus recording rules definition format](https://prometheus.io/docs/pro
There are limitations for the rules files:
1. All files may contain no more than 100 rules in total. If you need to upload more rules contact us via [support-cloud@victoriametrics.com](mailto:support-cloud@victoriametrics.com).
1. All files may contain no more than 100 rules in total. If you need to upload more rules contact us via [support@victoriametrics.com](mailto:support@victoriametrics.com).
2. The maximum file size is 20mb.
3. The names of the groups in the files should be unique.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 KiB

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

After

Width:  |  Height:  |  Size: 52 KiB

View File

@@ -18,7 +18,7 @@ The tier parameters are derived from testing in typical monitoring environments,
| **Active Time Series Count** | Per Tier Limits | Number of [active time series](https://docs.victoriametrics.com/faq/#what-is-an-active-time-series) that received at least one data point in the last hour. |
| **Read Rate** | Per Tier Limits | Number of datapoints retrieved from the database per second. |
| **New Series Over 24 Hours** (churn rate) | `<= Active Time Series Count` | Number of new series created in 24 hours. High [churn rate](https://docs.victoriametrics.com/faq/#what-is-high-churn-rate) leads to higher resource consumption. |
| **Concurrent Requests per Token** | `<= 600` | Maximum concurrent requests per access token. It is recommended to create separate tokens for different clients and environments. This can be adjusted via [support](mailto:support-cloud@victoriametrics.com). |
| **Concurrent Requests per Token** | `<= 600` | Maximum concurrent requests per access token. It is recommended to create separate tokens for different clients and environments. This can be adjusted via [support](mailto:support@victoriametrics.com). |
For a detailed explanation of each parameter, visit the guide on [Understanding Your Setup Size](https://docs.victoriametrics.com/guides/understand-your-setup-size.html).
@@ -26,7 +26,7 @@ For a detailed explanation of each parameter, visit the guide on [Understanding
| **Flag** | **Default Value** | **Description** |
|-----------------------------------|---------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **Max Label Value Length** | `<= 1kb` (Default: `4kb`) | Maximum length of label values. Longer values are truncated. Large label values can lead to high RAM consumption. This can be adjusted via [support](mailto:support-cloud@victoriametrics.com). |
| **Max Label Value Length** | `<= 1kb` (Default: `4kb`) | Maximum length of label values. Longer values are truncated. Large label values can lead to high RAM consumption. This can be adjusted via [support](mailto:support@victoriametrics.com). |
| **Max Labels per Time Series** | `<= 30` | Maximum number of labels per time series. Excess labels are dropped. Higher values can increase [cardinality](https://docs.victoriametrics.com/keyconcepts/#cardinality) and resource usage. This can be configured in [deployment settings](https://docs.victoriametrics.com/victoriametrics-cloud/quickstart/#modifying-an-existing-deployment). |

View File

@@ -273,6 +273,7 @@ expr: <string>
# Labels to add or overwrite for each alert.
# In case of conflicts, original labels are kept with prefix `exported_`.
# label value supports [templating functions](#template-functions), [reusable templates](#reusable-templates) and limited variables, such as {{$value}}, {{$expr}}.
labels:
[ <labelname>: <tmpl_string> ]

View File

@@ -5,7 +5,6 @@ import (
"slices"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"unsafe"
@@ -228,16 +227,6 @@ func (br *blockResult) cloneValues(values []string) []string {
return br.valuesBuf[valuesBufLen:]
}
func (br *blockResult) addValues(values []string) {
valuesBufLen := len(br.valuesBuf)
br.valuesBuf = slicesutil.SetLength(br.valuesBuf, valuesBufLen+len(values))
valuesBuf := br.valuesBuf[valuesBufLen:]
_ = valuesBuf[len(values)-1]
for i, v := range values {
valuesBuf[i] = br.a.copyString(v)
}
}
func (br *blockResult) addValue(v string) {
valuesBuf := br.valuesBuf
if len(valuesBuf) > 0 && v == valuesBuf[len(valuesBuf)-1] {
@@ -501,57 +490,73 @@ func (br *blockResult) newValuesEncodedFromColumnHeader(bs *blockSearch, bm *bit
switch ch.valueType {
case valueTypeString:
visitValuesReadonly(bs, ch, bm, br.addValues)
visitValuesReadonly(bs, ch, bm, br.addValue)
case valueTypeDict:
visitValuesReadonly(bs, ch, bm, func(values []string) {
checkValuesSize(bs, ch, values, 1, "dict")
for _, v := range values {
dictIdx := v[0]
if int(dictIdx) >= len(ch.valuesDict.values) {
logger.Panicf("FATAL: %s: too big dict index for column %q: %d; should be smaller than %d", bs.partPath(), ch.name, dictIdx, len(ch.valuesDict.values))
}
visitValuesReadonly(bs, ch, bm, func(v string) {
if len(v) != 1 {
logger.Panicf("FATAL: %s: unexpected dict value size for column %q; got %d bytes; want 1 byte", bs.partPath(), ch.name, len(v))
}
br.addValues(values)
dictIdx := v[0]
if int(dictIdx) >= len(ch.valuesDict.values) {
logger.Panicf("FATAL: %s: too big dict index for column %q: %d; should be smaller than %d", bs.partPath(), ch.name, dictIdx, len(ch.valuesDict.values))
}
br.addValue(v)
})
case valueTypeUint8:
visitValuesReadonly(bs, ch, bm, func(values []string) {
checkValuesSize(bs, ch, values, 1, "uint8")
br.addValues(values)
visitValuesReadonly(bs, ch, bm, func(v string) {
if len(v) != 1 {
logger.Panicf("FATAL: %s: unexpected size for uint8 column %q; got %d bytes; want 1 byte", bs.partPath(), ch.name, len(v))
}
br.addValue(v)
})
case valueTypeUint16:
visitValuesReadonly(bs, ch, bm, func(values []string) {
checkValuesSize(bs, ch, values, 2, "uint16")
br.addValues(values)
visitValuesReadonly(bs, ch, bm, func(v string) {
if len(v) != 2 {
logger.Panicf("FATAL: %s: unexpected size for uint16 column %q; got %d bytes; want 2 bytes", bs.partPath(), ch.name, len(v))
}
br.addValue(v)
})
case valueTypeUint32:
visitValuesReadonly(bs, ch, bm, func(values []string) {
checkValuesSize(bs, ch, values, 4, "uint32")
br.addValues(values)
visitValuesReadonly(bs, ch, bm, func(v string) {
if len(v) != 4 {
logger.Panicf("FATAL: %s: unexpected size for uint32 column %q; got %d bytes; want 4 bytes", bs.partPath(), ch.name, len(v))
}
br.addValue(v)
})
case valueTypeUint64:
visitValuesReadonly(bs, ch, bm, func(values []string) {
checkValuesSize(bs, ch, values, 8, "uint64")
br.addValues(values)
visitValuesReadonly(bs, ch, bm, func(v string) {
if len(v) != 8 {
logger.Panicf("FATAL: %s: unexpected size for uint64 column %q; got %d bytes; want 8 bytes", bs.partPath(), ch.name, len(v))
}
br.addValue(v)
})
case valueTypeInt64:
visitValuesReadonly(bs, ch, bm, func(values []string) {
checkValuesSize(bs, ch, values, 8, "int64")
br.addValues(values)
visitValuesReadonly(bs, ch, bm, func(v string) {
if len(v) != 8 {
logger.Panicf("FATAL: %s: unexpected size for int64 column %q; got %d bytes; want 8 bytes", bs.partPath(), ch.name, len(v))
}
br.addValue(v)
})
case valueTypeFloat64:
visitValuesReadonly(bs, ch, bm, func(values []string) {
checkValuesSize(bs, ch, values, 8, "float64")
br.addValues(values)
visitValuesReadonly(bs, ch, bm, func(v string) {
if len(v) != 8 {
logger.Panicf("FATAL: %s: unexpected size for float64 column %q; got %d bytes; want 8 bytes", bs.partPath(), ch.name, len(v))
}
br.addValue(v)
})
case valueTypeIPv4:
visitValuesReadonly(bs, ch, bm, func(values []string) {
checkValuesSize(bs, ch, values, 4, "ipv4")
br.addValues(values)
visitValuesReadonly(bs, ch, bm, func(v string) {
if len(v) != 4 {
logger.Panicf("FATAL: %s: unexpected size for ipv4 column %q; got %d bytes; want 4 bytes", bs.partPath(), ch.name, len(v))
}
br.addValue(v)
})
case valueTypeTimestampISO8601:
visitValuesReadonly(bs, ch, bm, func(values []string) {
checkValuesSize(bs, ch, values, 8, "iso8601")
br.addValues(values)
visitValuesReadonly(bs, ch, bm, func(v string) {
if len(v) != 8 {
logger.Panicf("FATAL: %s: unexpected size for timestmap column %q; got %d bytes; want 8 bytes", bs.partPath(), ch.name, len(v))
}
br.addValue(v)
})
default:
logger.Panicf("FATAL: %s: unknown valueType=%d for column %q", bs.partPath(), ch.valueType, ch.name)
@@ -560,14 +565,6 @@ func (br *blockResult) newValuesEncodedFromColumnHeader(bs *blockSearch, bm *bit
return br.valuesBuf[valuesBufLen:]
}
func checkValuesSize(bs *blockSearch, ch *columnHeader, values []string, sizeExpected int, typeStr string) {
for _, v := range values {
if len(v) != sizeExpected {
logger.Panicf("FATAL: %s: unexpected size for %s column %q; got %d bytes; want %d bytes", typeStr, bs.partPath(), ch.name, len(v), sizeExpected)
}
}
}
// addColumn adds column for the given ch to br.
//
// The added column is valid until ch is changed.
@@ -2141,46 +2138,17 @@ func getEmptyStrings(rowsLen int) []string {
var emptyStrings atomic.Pointer[[]string]
func visitValuesReadonly(bs *blockSearch, ch *columnHeader, bm *bitmap, f func(values []string)) {
func visitValuesReadonly(bs *blockSearch, ch *columnHeader, bm *bitmap, f func(value string)) {
if bm.isZero() {
// Fast path - nothing to visit
return
}
values := bs.getValuesForColumn(ch)
if bm.areAllBitsSet() {
// Faster path - visit all the values
f(values)
return
}
// Slower path - visit only the selected values
vb := getValuesBuf()
bm.forEachSetBitReadonly(func(idx int) {
vb.values = append(vb.values, values[idx])
f(values[idx])
})
f(vb.values)
putValuesBuf(vb)
}
type valuesBuf struct {
values []string
}
func getValuesBuf() *valuesBuf {
v := valuesBufPool.Get()
if v == nil {
return &valuesBuf{}
}
return v.(*valuesBuf)
}
func putValuesBuf(vb *valuesBuf) {
vb.values = vb.values[:0]
valuesBufPool.Put(vb)
}
var valuesBufPool sync.Pool
func getCanonicalColumnName(columnName string) string {
if columnName == "" {
return "_msg"

View File

@@ -12,7 +12,6 @@ type chunkedAllocator struct {
countEmptyProcessors []statsCountEmptyProcessor
countUniqProcessors []statsCountUniqProcessor
countUniqHashProcessors []statsCountUniqHashProcessor
histogramProcessors []statsHistogramProcessor
maxProcessors []statsMaxProcessor
medianProcessors []statsMedianProcessor
minProcessors []statsMinProcessor
@@ -61,11 +60,6 @@ func (a *chunkedAllocator) newStatsCountUniqHashProcessor() (p *statsCountUniqHa
return p
}
func (a *chunkedAllocator) newStatsHistogramProcessor() (p *statsHistogramProcessor) {
a.histogramProcessors, p = addNewItem(a.histogramProcessors, a)
return p
}
func (a *chunkedAllocator) newStatsMaxProcessor() (p *statsMaxProcessor) {
a.maxProcessors, p = addNewItem(a.maxProcessors, a)
return p

View File

@@ -1207,10 +1207,6 @@ func TestParseQuerySuccess(t *testing.T) {
f(`* | stats sum_len(*) x`, `* | stats sum_len(*) as x`)
f(`* | stats sum_len(foo,*,bar) x`, `* | stats sum_len(*) as x`)
// stats pipe histogram
f(`* | stats histogram(foo) bar`, `* | stats histogram(foo) as bar`)
f(`* | histogram(foo)`, `* | stats histogram(foo) as "histogram(foo)"`)
// stats pipe quantile
f(`* | stats quantile(0, foo) bar`, `* | stats quantile(0, foo) as bar`)
f(`* | stats quantile(1, foo) bar`, `* | stats quantile(1, foo) as bar`)
@@ -1746,12 +1742,6 @@ func TestParseQueryFailure(t *testing.T) {
// invalid stats sum_len
f(`foo | stats sum_len`)
// invalid stats histogram
f(`foo | stats histogram`)
f(`foo | stats histogram()`)
f(`foo | stats histogram(a, b)`)
f(`foo | stats histogram(*)`)
// invalid stats quantile
f(`foo | stats quantile`)
f(`foo | stats quantile() foo`)
@@ -1987,7 +1977,6 @@ func TestQueryGetNeededColumns(t *testing.T) {
f(`* | stats max() q`, `*`, ``)
f(`* | stats max(*) q`, `*`, ``)
f(`* | stats max(x) q`, `x`, ``)
f(`* | stats histogram(foo)`, `foo`, ``)
f(`* | stats quantile(0.5) q`, `*`, ``)
f(`* | stats quantile(0.5, *) q`, `*`, ``)
f(`* | stats quantile(0.5, x) q`, `x`, ``)

View File

@@ -10,7 +10,6 @@ import (
"unsafe"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/memory"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/stringsutil"
)
@@ -280,7 +279,7 @@ func (shard *pipeTopkProcessorShard) addRow(br *blockResult, byColumns []string,
b := shard.partitionKey[:0]
for _, c := range shard.partitionColumns {
v := c.getValueAtRow(br, rowIdx)
b = encoding.MarshalBytes(b, bytesutil.ToUnsafeBytes(v))
b = marshalJSONKeyValue(b, c.name, v)
}
shard.partitionKey = b
@@ -409,8 +408,8 @@ func (ptp *pipeTopkProcessor) flush() error {
// Obtain all the partition keys
partitionKeysMap := make(map[string]struct{})
var partitionKeys []string
for i := range shards {
for k := range shards[i].rowsByPartition {
for _, shard := range shards {
for k := range shard.rowsByPartition {
if _, ok := partitionKeysMap[k]; !ok {
partitionKeysMap[k] = struct{}{}
partitionKeys = append(partitionKeys, k)
@@ -421,9 +420,6 @@ func (ptp *pipeTopkProcessor) flush() error {
// Merge sorted results across shards per each partitionKey
for _, k := range partitionKeys {
if needStop(ptp.stopCh) {
return nil
}
var rss []*pipeTopkRows
for _, shard := range shards {
rs, ok := shard.rowsByPartition[k]
@@ -432,6 +428,9 @@ func (ptp *pipeTopkProcessor) flush() error {
}
}
ptp.mergeAndFlushRows(rss)
if needStop(ptp.stopCh) {
return nil
}
}
return nil

View File

@@ -874,8 +874,8 @@ func (psp *pipeStatsProcessor) mergeShardsParallel() ([]*pipeStatsGroupMap, erro
}
k := unsafe.Slice((*byte)(unsafe.Pointer(&n)), 8)
h := xxhash.Sum64(k)
cpuIdx := h % uint64(len(perCPU))
perCPU[cpuIdx].u64[n] = psg
shardIdx := h % uint64(len(perCPU))
perCPU[shardIdx].u64[n] = psg
}
for n, psg := range psm.negative64 {
if needStop(psp.stopCh) {
@@ -883,16 +883,16 @@ func (psp *pipeStatsProcessor) mergeShardsParallel() ([]*pipeStatsGroupMap, erro
}
k := unsafe.Slice((*byte)(unsafe.Pointer(&n)), 8)
h := xxhash.Sum64(k)
cpuIdx := h % uint64(len(perCPU))
perCPU[cpuIdx].negative64[n] = psg
shardIdx := h % uint64(len(perCPU))
perCPU[shardIdx].negative64[n] = psg
}
for k, psg := range psm.strings {
if needStop(psp.stopCh) {
return
}
h := xxhash.Sum64(bytesutil.ToUnsafeBytes(k))
cpuIdx := h % uint64(len(perCPU))
perCPU[cpuIdx].strings[k] = psg
shardIdx := h % uint64(len(perCPU))
perCPU[shardIdx].strings[k] = psg
}
perShardMaps[idx] = perCPU
@@ -903,6 +903,9 @@ func (psp *pipeStatsProcessor) mergeShardsParallel() ([]*pipeStatsGroupMap, erro
if needStop(psp.stopCh) {
return nil, nil
}
if n := psp.stateSizeBudget.Load(); n < 0 {
return nil, fmt.Errorf("cannot calculate [%s], since it requires more than %dMB of memory", psp.ps.String(), psp.maxStateSize/(1<<20))
}
// Merge per-shard entries into perShardMaps[0]
for i := 0; i < cpusCount; i++ {
@@ -921,6 +924,9 @@ func (psp *pipeStatsProcessor) mergeShardsParallel() ([]*pipeStatsGroupMap, erro
if needStop(psp.stopCh) {
return nil, nil
}
if n := psp.stateSizeBudget.Load(); n < 0 {
return nil, fmt.Errorf("cannot calculate [%s], since it requires more than %dMB of memory", psp.ps.String(), psp.maxStateSize/(1<<20))
}
// Filter out maps without entries
psms := perShardMaps[0]
@@ -1049,12 +1055,6 @@ func parseStatsFunc(lex *lexer) (statsFunc, error) {
return nil, fmt.Errorf("cannot parse 'count_uniq_hash' func: %w", err)
}
return sus, nil
case lex.isKeyword("histogram"):
shs, err := parseStatsHistogram(lex)
if err != nil {
return nil, fmt.Errorf("cannot parse 'histogram' func: %w", err)
}
return shs, nil
case lex.isKeyword("max"):
sms, err := parseStatsMax(lex)
if err != nil {
@@ -1144,7 +1144,6 @@ var statsNames = []string{
"count_empty",
"count_uniq",
"count_uniq_hash",
"histogram",
"max",
"median",
"min",

View File

@@ -6,7 +6,6 @@ import (
"slices"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"unsafe"
@@ -14,7 +13,6 @@ import (
"github.com/cespare/xxhash/v2"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/memory"
@@ -93,7 +91,6 @@ func (pt *pipeTop) newPipeProcessor(workersCount int, stopCh <-chan struct{}, ca
pt: pt,
},
}
shards[i].m.init(&shards[i])
}
ptp := &pipeTopProcessor{
@@ -134,8 +131,11 @@ type pipeTopProcessorShardNopad struct {
// pt points to the parent pipeTop.
pt *pipeTop
// m holds per-value hits.
m pipeTopMap
// a reduces memory allocations when counting the number of hits over big number of unique values.
a chunkedAllocator
// m holds per-row hits.
m map[string]*uint64
// keyBuf is a temporary buffer for building keys for m.
keyBuf []byte
@@ -148,134 +148,6 @@ type pipeTopProcessorShardNopad struct {
stateSizeBudget int
}
type pipeTopMap struct {
shard *pipeTopProcessorShard
u64 map[uint64]*uint64
negative64 map[uint64]*uint64
strings map[string]*uint64
// a reduces memory allocations when counting the number of hits over big number of unique values.
a chunkedAllocator
}
func (ptm *pipeTopMap) reset() {
ptm.shard = nil
ptm.u64 = nil
ptm.negative64 = nil
ptm.strings = nil
}
func (ptm *pipeTopMap) init(shard *pipeTopProcessorShard) {
ptm.shard = shard
ptm.u64 = make(map[uint64]*uint64)
ptm.negative64 = make(map[uint64]*uint64)
ptm.strings = make(map[string]*uint64)
}
func (ptm *pipeTopMap) updateStateGeneric(key string, hits uint64) {
if n, ok := tryParseUint64(key); ok {
ptm.updateStateUint64(n, hits)
return
}
if len(key) > 0 && key[0] == '-' {
if n, ok := tryParseInt64(key); ok {
ptm.updateStateNegativeInt64(n, hits)
return
}
}
ptm.updateStateString(bytesutil.ToUnsafeBytes(key), hits)
}
func (ptm *pipeTopMap) updateStateInt64(n int64, hits uint64) {
if n >= 0 {
ptm.updateStateUint64(uint64(n), hits)
} else {
ptm.updateStateNegativeInt64(n, hits)
}
}
func (ptm *pipeTopMap) updateStateUint64(n, hits uint64) {
pHits := ptm.u64[n]
if pHits != nil {
*pHits += hits
return
}
pHits = ptm.a.newUint64()
*pHits += hits
ptm.u64[n] = pHits
ptm.shard.stateSizeBudget -= int(unsafe.Sizeof(*pHits) + unsafe.Sizeof(pHits))
}
func (ptm *pipeTopMap) updateStateNegativeInt64(n int64, hits uint64) {
pHits := ptm.negative64[uint64(n)]
if pHits != nil {
*pHits += hits
return
}
pHits = ptm.a.newUint64()
*pHits += hits
ptm.negative64[uint64(n)] = pHits
ptm.shard.stateSizeBudget -= int(unsafe.Sizeof(*pHits) + unsafe.Sizeof(pHits))
}
func (ptm *pipeTopMap) updateStateString(key []byte, hits uint64) {
pHits := ptm.strings[string(key)]
if pHits != nil {
*pHits += hits
return
}
keyCopy := ptm.a.cloneBytesToString(key)
pHits = ptm.a.newUint64()
*pHits += hits
ptm.strings[keyCopy] = pHits
ptm.shard.stateSizeBudget -= len(keyCopy) + int(unsafe.Sizeof(keyCopy)+unsafe.Sizeof(*pHits)+unsafe.Sizeof(pHits))
}
func (ptm *pipeTopMap) mergeState(src *pipeTopMap, stopCh <-chan struct{}) {
for n, pHitsSrc := range src.u64 {
if needStop(stopCh) {
return
}
pHitsDst := ptm.u64[n]
if pHitsDst == nil {
ptm.u64[n] = pHitsSrc
} else {
*pHitsDst += *pHitsSrc
}
}
for n, pHitsSrc := range src.negative64 {
if needStop(stopCh) {
return
}
pHitsDst := ptm.negative64[n]
if pHitsDst == nil {
ptm.negative64[n] = pHitsSrc
} else {
*pHitsDst += *pHitsSrc
}
}
for k, pHitsSrc := range src.strings {
if needStop(stopCh) {
return
}
pHitsDst := ptm.strings[k]
if pHitsDst == nil {
ptm.strings[k] = pHitsSrc
} else {
*pHitsDst += *pHitsSrc
}
}
}
// writeBlock writes br to shard.
func (shard *pipeTopProcessorShard) writeBlock(br *blockResult) {
byFields := shard.pt.byFields
@@ -283,21 +155,35 @@ func (shard *pipeTopProcessorShard) writeBlock(br *blockResult) {
// Take into account all the columns in br.
keyBuf := shard.keyBuf
cs := br.getColumns()
for rowIdx := 0; rowIdx < br.rowsLen; rowIdx++ {
for i := 0; i < br.rowsLen; i++ {
keyBuf = keyBuf[:0]
for _, c := range cs {
v := c.getValueAtRow(br, rowIdx)
v := c.getValueAtRow(br, i)
keyBuf = encoding.MarshalBytes(keyBuf, bytesutil.ToUnsafeBytes(c.name))
keyBuf = encoding.MarshalBytes(keyBuf, bytesutil.ToUnsafeBytes(v))
}
shard.m.updateStateString(keyBuf, 1)
shard.updateState(bytesutil.ToUnsafeString(keyBuf), 1)
}
shard.keyBuf = keyBuf
return
}
if len(byFields) == 1 {
// Fast path for a single field.
shard.updateStatsSingleColumn(br, byFields[0])
c := br.getColumnByName(byFields[0])
if c.isConst {
v := c.valuesEncoded[0]
shard.updateState(v, uint64(br.rowsLen))
return
}
if c.valueType == valueTypeDict {
c.forEachDictValueWithHits(br, shard.updateState)
return
}
values := c.getValues(br)
for _, v := range values {
shard.updateState(v, 1)
}
return
}
@@ -311,62 +197,33 @@ func (shard *pipeTopProcessorShard) writeBlock(br *blockResult) {
shard.columnValues = columnValues
keyBuf := shard.keyBuf
for rowIdx := 0; rowIdx < br.rowsLen; rowIdx++ {
for i := 0; i < br.rowsLen; i++ {
keyBuf = keyBuf[:0]
for _, values := range columnValues {
keyBuf = encoding.MarshalBytes(keyBuf, bytesutil.ToUnsafeBytes(values[rowIdx]))
keyBuf = encoding.MarshalBytes(keyBuf, bytesutil.ToUnsafeBytes(values[i]))
}
shard.m.updateStateString(keyBuf, 1)
shard.updateState(bytesutil.ToUnsafeString(keyBuf), 1)
}
shard.keyBuf = keyBuf
}
func (shard *pipeTopProcessorShard) updateStatsSingleColumn(br *blockResult, fieldName string) {
c := br.getColumnByName(fieldName)
if c.isConst {
v := c.valuesEncoded[0]
shard.m.updateStateGeneric(v, uint64(br.rowsLen))
return
func (shard *pipeTopProcessorShard) updateState(v string, hits uint64) {
m := shard.getM()
pHits := m[v]
if pHits == nil {
vCopy := shard.a.cloneString(v)
pHits = shard.a.newUint64()
m[vCopy] = pHits
shard.stateSizeBudget -= len(vCopy) + int(unsafe.Sizeof(vCopy)+unsafe.Sizeof(hits)+unsafe.Sizeof(pHits))
}
switch c.valueType {
case valueTypeDict:
c.forEachDictValueWithHits(br, shard.m.updateStateGeneric)
case valueTypeUint8:
values := c.getValuesEncoded(br)
for _, v := range values {
n := unmarshalUint8(v)
shard.m.updateStateUint64(uint64(n), 1)
}
case valueTypeUint16:
values := c.getValuesEncoded(br)
for _, v := range values {
n := unmarshalUint16(v)
shard.m.updateStateUint64(uint64(n), 1)
}
case valueTypeUint32:
values := c.getValuesEncoded(br)
for _, v := range values {
n := unmarshalUint32(v)
shard.m.updateStateUint64(uint64(n), 1)
}
case valueTypeUint64:
values := c.getValuesEncoded(br)
for _, v := range values {
n := unmarshalUint64(v)
shard.m.updateStateUint64(n, 1)
}
case valueTypeInt64:
values := c.getValuesEncoded(br)
for _, v := range values {
n := unmarshalInt64(v)
shard.m.updateStateInt64(n, 1)
}
default:
values := c.getValues(br)
for _, v := range values {
shard.m.updateStateGeneric(v, 1)
}
*pHits += hits
}
func (shard *pipeTopProcessorShard) getM() map[string]*uint64 {
if shard.m == nil {
shard.m = make(map[string]*uint64)
}
return shard.m
}
func (ptp *pipeTopProcessor) writeBlock(workerID uint, br *blockResult) {
@@ -521,84 +378,101 @@ func (ptp *pipeTopProcessor) mergeShardsParallel() ([]*pipeTopEntry, error) {
shards := ptp.shards
shardsLen := len(shards)
cpusCount := cgroup.AvailableCPUs()
if shardsLen == 1 {
ptm := &shards[0].m
entries := getTopEntries(ptm, limit, ptp.stopCh)
entries := getTopEntries(shards[0].getM(), limit, ptp.stopCh)
return entries, nil
}
var wg sync.WaitGroup
perShardMaps := make([][]pipeTopMap, shardsLen)
perShardMaps := make([][]map[string]*uint64, shardsLen)
for i := range shards {
wg.Add(1)
go func(idx int) {
defer wg.Done()
perCPU := make([]pipeTopMap, cpusCount)
for i := range perCPU {
perCPU[i].init(&shards[idx])
shardMaps := make([]map[string]*uint64, shardsLen)
for i := range shardMaps {
shardMaps[i] = make(map[string]*uint64)
}
ptm := &shards[idx].m
for n, pHits := range ptm.u64 {
if needStop(ptp.stopCh) {
return
}
k := unsafe.Slice((*byte)(unsafe.Pointer(&n)), 8)
h := xxhash.Sum64(k)
cpuIdx := h % uint64(len(perCPU))
perCPU[cpuIdx].u64[n] = pHits
}
for n, pHits := range ptm.negative64 {
if needStop(ptp.stopCh) {
return
}
k := unsafe.Slice((*byte)(unsafe.Pointer(&n)), 8)
h := xxhash.Sum64(k)
cpuIdx := h % uint64(len(perCPU))
perCPU[cpuIdx].negative64[n] = pHits
}
for k, pHits := range ptm.strings {
n := int64(0)
nTotal := int64(0)
for k, pHits := range shards[idx].getM() {
if needStop(ptp.stopCh) {
return
}
h := xxhash.Sum64(bytesutil.ToUnsafeBytes(k))
cpuIdx := h % uint64(len(perCPU))
perCPU[cpuIdx].strings[k] = pHits
m := shardMaps[h%uint64(len(shardMaps))]
n += updatePipeTopMap(m, k, pHits)
if n > stateSizeBudgetChunk {
if nRemaining := ptp.stateSizeBudget.Add(-n); nRemaining < 0 {
return
}
nTotal += n
n = 0
}
}
nTotal += n
ptp.stateSizeBudget.Add(-n)
perShardMaps[idx] = perCPU
ptm.reset()
perShardMaps[idx] = shardMaps
// Clean the original map and return its state size budget back.
shards[idx].m = nil
ptp.stateSizeBudget.Add(nTotal)
}(i)
}
wg.Wait()
if needStop(ptp.stopCh) {
return nil, nil
}
if n := ptp.stateSizeBudget.Load(); n < 0 {
return nil, fmt.Errorf("cannot calculate [%s], since it requires more than %dMB of memory", ptp.pt.String(), ptp.maxStateSize/(1<<20))
}
// Obtain topN entries per each shard
entriess := make([][]*pipeTopEntry, cpusCount)
entriess := make([][]*pipeTopEntry, shardsLen)
for i := range entriess {
wg.Add(1)
go func(cpuIdx int) {
go func(idx int) {
defer wg.Done()
ptm := &perShardMaps[0][cpuIdx]
for _, perCPU := range perShardMaps[1:] {
ptm.mergeState(&perCPU[cpuIdx], ptp.stopCh)
perCPU[cpuIdx].reset()
}
m := perShardMaps[0][idx]
for i := 1; i < len(perShardMaps); i++ {
n := int64(0)
nTotal := int64(0)
for k, pHits := range perShardMaps[i][idx] {
if needStop(ptp.stopCh) {
return
}
n += updatePipeTopMap(m, k, pHits)
if n > stateSizeBudgetChunk {
if nRemaining := ptp.stateSizeBudget.Add(-n); nRemaining < 0 {
return
}
nTotal += n
n = 0
}
}
nTotal += n
ptp.stateSizeBudget.Add(-n)
entriess[cpuIdx] = getTopEntries(ptm, limit, ptp.stopCh)
// Clean the original map and return its state size budget back.
perShardMaps[i][idx] = nil
ptp.stateSizeBudget.Add(nTotal)
}
perShardMaps[0][idx] = nil
entriess[idx] = getTopEntries(m, limit, ptp.stopCh)
}(i)
}
wg.Wait()
if needStop(ptp.stopCh) {
return nil, nil
}
if n := ptp.stateSizeBudget.Load(); n < 0 {
return nil, fmt.Errorf("cannot calculate [%s], since it requires more than %dMB of memory", ptp.pt.String(), ptp.maxStateSize/(1<<20))
}
// merge entriess
entries := entriess[0]
@@ -614,57 +488,31 @@ func (ptp *pipeTopProcessor) mergeShardsParallel() ([]*pipeTopEntry, error) {
return entries, nil
}
func getTopEntries(ptm *pipeTopMap, limit uint64, stopCh <-chan struct{}) []*pipeTopEntry {
func getTopEntries(m map[string]*uint64, limit uint64, stopCh <-chan struct{}) []*pipeTopEntry {
if limit == 0 {
return nil
}
var eh topEntriesHeap
var e pipeTopEntry
for k, pHits := range m {
if needStop(stopCh) {
return nil
}
pushEntry := func(k string, hits uint64, kCopy bool) {
e.k = k
e.hits = hits
e := pipeTopEntry{
k: k,
hits: *pHits,
}
if uint64(len(eh)) < limit {
eCopy := e
if kCopy {
eCopy.k = strings.Clone(eCopy.k)
}
heap.Push(&eh, &eCopy)
return
continue
}
if !eh[0].less(&e) {
return
if eh[0].less(&e) {
eCopy := e
eh[0] = &eCopy
heap.Fix(&eh, 0)
}
eCopy := e
if kCopy {
eCopy.k = strings.Clone(eCopy.k)
}
eh[0] = &eCopy
heap.Fix(&eh, 0)
}
var b []byte
for n, pHits := range ptm.u64 {
if needStop(stopCh) {
return nil
}
b = marshalUint64String(b[:0], n)
pushEntry(bytesutil.ToUnsafeString(b), *pHits, true)
}
for n, pHits := range ptm.negative64 {
if needStop(stopCh) {
return nil
}
b = marshalInt64String(b[:0], int64(n))
pushEntry(bytesutil.ToUnsafeString(b), *pHits, true)
}
for k, pHits := range ptm.strings {
if needStop(stopCh) {
return nil
}
pushEntry(k, *pHits, false)
}
result := ([]*pipeTopEntry)(eh)
@@ -676,6 +524,17 @@ func getTopEntries(ptm *pipeTopMap, limit uint64, stopCh <-chan struct{}) []*pip
return result
}
func updatePipeTopMap(m map[string]*uint64, k string, pHitsSrc *uint64) int64 {
pHitsDst := m[k]
if pHitsDst != nil {
*pHitsDst += *pHitsSrc
return 0
}
m[k] = pHitsSrc
return int64(unsafe.Sizeof(k) + unsafe.Sizeof(pHitsSrc))
}
type topEntriesHeap []*pipeTopEntry
func (h *topEntriesHeap) Less(i, j int) bool {
@@ -755,9 +614,7 @@ func (wctx *pipeTopWriteContext) writeRow(rowFields []Field) {
}
wctx.rowsCount++
// The 64_000 limit provides the best performance results.
if wctx.valuesLen >= 64_000 {
if wctx.valuesLen >= 1_000_000 {
wctx.flush()
}
}
@@ -813,41 +670,37 @@ func parsePipeTop(lex *lexer) (pipe, error) {
byFields = bfs
}
hitsFieldName := "hits"
if lex.isKeyword("hits") {
lex.nextToken()
if lex.isKeyword("as") {
lex.nextToken()
}
s, err := getCompoundToken(lex)
if err != nil {
return nil, fmt.Errorf("cannot parse 'hits' name: %w", err)
}
hitsFieldName = s
}
for slices.Contains(byFields, hitsFieldName) {
hitsFieldName += "s"
}
pt := &pipeTop{
byFields: byFields,
limit: limit,
limitStr: limitStr,
hitsFieldName: "hits",
hitsFieldName: hitsFieldName,
}
for {
switch {
case lex.isKeyword("hits"):
lex.nextToken()
if lex.isKeyword("as") {
lex.nextToken()
}
s, err := getCompoundToken(lex)
if err != nil {
return nil, fmt.Errorf("cannot parse 'hits' name: %w", err)
}
pt.hitsFieldName = s
case lex.isKeyword("rank"):
rankFieldName, err := parseRankFieldName(lex)
if err != nil {
return nil, fmt.Errorf("cannot parse rank field name in [%s]: %w", pt, err)
}
pt.rankFieldName = rankFieldName
for slices.Contains(byFields, pt.rankFieldName) {
pt.rankFieldName += "s"
}
default:
for slices.Contains(byFields, pt.hitsFieldName) {
pt.hitsFieldName += "s"
}
return pt, nil
if lex.isKeyword("rank") {
rankFieldName, err := parseRankFieldName(lex)
if err != nil {
return nil, fmt.Errorf("cannot parse rank field name in [%s]: %w", pt, err)
}
pt.rankFieldName = rankFieldName
}
return pt, nil
}
func parseRankFieldName(lex *lexer) (string, error) {

View File

@@ -1,189 +0,0 @@
package logstorage
import (
"fmt"
"github.com/VictoriaMetrics/metrics"
)
type statsHistogram struct {
fieldName string
}
func (sh *statsHistogram) String() string {
return "histogram(" + quoteTokenIfNeeded(sh.fieldName) + ")"
}
func (sh *statsHistogram) updateNeededFields(neededFields fieldsSet) {
updateNeededFieldsForStatsFunc(neededFields, []string{sh.fieldName})
}
func (sh *statsHistogram) newStatsProcessor(a *chunkedAllocator) statsProcessor {
return a.newStatsHistogramProcessor()
}
type statsHistogramProcessor struct {
h metrics.Histogram
}
func (shp *statsHistogramProcessor) updateStatsForAllRows(sf statsFunc, br *blockResult) int {
sh := sf.(*statsHistogram)
c := br.getColumnByName(sh.fieldName)
if c.isConst {
v := c.valuesEncoded[0]
f, ok := tryParseNumber(v)
if ok {
for rowIdx := 0; rowIdx < br.rowsLen; rowIdx++ {
shp.h.Update(f)
}
}
return 0
}
switch c.valueType {
case valueTypeUint8:
values := c.getValuesEncoded(br)
for _, v := range values {
n := unmarshalUint8(v)
shp.h.Update(float64(n))
}
case valueTypeUint16:
values := c.getValuesEncoded(br)
for _, v := range values {
n := unmarshalUint16(v)
shp.h.Update(float64(n))
}
case valueTypeUint32:
values := c.getValuesEncoded(br)
for _, v := range values {
n := unmarshalUint32(v)
shp.h.Update(float64(n))
}
case valueTypeUint64:
values := c.getValuesEncoded(br)
for _, v := range values {
n := unmarshalUint64(v)
shp.h.Update(float64(n))
}
case valueTypeInt64:
values := c.getValuesEncoded(br)
for _, v := range values {
n := unmarshalInt64(v)
shp.h.Update(float64(n))
}
case valueTypeFloat64:
values := c.getValuesEncoded(br)
for _, v := range values {
f := unmarshalFloat64(v)
shp.h.Update(f)
}
case valueTypeIPv4:
// skip ipv4 values, since they cannot be represented as numbers
case valueTypeTimestampISO8601:
// skip iso8601 values, since they cannot be represented as numbers
default:
values := c.getValues(br)
for _, v := range values {
f, ok := tryParseNumber(v)
if ok {
shp.h.Update(f)
}
}
}
return 0
}
func (shp *statsHistogramProcessor) updateStatsForRow(sf statsFunc, br *blockResult, rowIdx int) int {
sh := sf.(*statsHistogram)
c := br.getColumnByName(sh.fieldName)
if c.isConst {
v := c.valuesEncoded[0]
f, ok := tryParseNumber(v)
if ok {
shp.h.Update(f)
}
return 0
}
switch c.valueType {
case valueTypeUint8:
values := c.getValuesEncoded(br)
v := values[rowIdx]
n := unmarshalUint8(v)
shp.h.Update(float64(n))
case valueTypeUint16:
values := c.getValuesEncoded(br)
v := values[rowIdx]
n := unmarshalUint16(v)
shp.h.Update(float64(n))
case valueTypeUint32:
values := c.getValuesEncoded(br)
v := values[rowIdx]
n := unmarshalUint32(v)
shp.h.Update(float64(n))
case valueTypeUint64:
values := c.getValuesEncoded(br)
v := values[rowIdx]
n := unmarshalUint64(v)
shp.h.Update(float64(n))
case valueTypeInt64:
values := c.getValuesEncoded(br)
v := values[rowIdx]
n := unmarshalInt64(v)
shp.h.Update(float64(n))
case valueTypeFloat64:
values := c.getValuesEncoded(br)
v := values[rowIdx]
f := unmarshalFloat64(v)
shp.h.Update(f)
case valueTypeIPv4:
// skip ipv4 values, since they cannot be represented as numbers
case valueTypeTimestampISO8601:
// skip iso8601 values, since they cannot be represented as numbers
default:
v := c.getValueAtRow(br, rowIdx)
f, ok := tryParseNumber(v)
if ok {
shp.h.Update(f)
}
}
return 0
}
func (shp *statsHistogramProcessor) mergeState(_ statsFunc, sfp statsProcessor) {
src := sfp.(*statsHistogramProcessor)
shp.h.Merge(&src.h)
}
func (shp *statsHistogramProcessor) finalizeStats(_ statsFunc, dst []byte, _ <-chan struct{}) []byte {
dst = append(dst, '[')
shp.h.VisitNonZeroBuckets(func(vmrange string, count uint64) {
dst = append(dst, `{"vmrange":"`...)
dst = append(dst, vmrange...)
dst = append(dst, `","hits":`...)
dst = marshalUint64String(dst, count)
dst = append(dst, `},`...)
})
dst = dst[:len(dst)-1]
dst = append(dst, ']')
return dst
}
func parseStatsHistogram(lex *lexer) (*statsHistogram, error) {
fields, err := parseStatsFuncFields(lex, "histogram")
if err != nil {
return nil, fmt.Errorf("cannot parse field name: %w", err)
}
if len(fields) != 1 {
return nil, fmt.Errorf("unexpected number of fields; got %d; want 1", len(fields))
}
sh := &statsHistogram{
fieldName: fields[0],
}
return sh, nil
}

View File

@@ -1,52 +0,0 @@
package logstorage
import (
"testing"
)
func TestParseStatsHistogramSuccess(t *testing.T) {
f := func(pipeStr string) {
t.Helper()
expectParseStatsFuncSuccess(t, pipeStr)
}
f(`histogram(foo)`)
}
func TestParseStatsHistogramFailure(t *testing.T) {
f := func(pipeStr string) {
t.Helper()
expectParseStatsFuncFailure(t, pipeStr)
}
f(`histogram`)
f(`histogram(a, b)`)
f(`histogram(a) abc`)
}
func TestStatsHistogram(t *testing.T) {
f := func(pipeStr string, rows, rowsExpected [][]Field) {
t.Helper()
expectPipeResults(t, pipeStr, rows, rowsExpected)
}
f("stats histogram(a) as x", [][]Field{
{
{"_msg", `abc`},
{"a", `2`},
{"b", `3`},
},
{
{"_msg", `def`},
{"a", `1.9`},
},
{
{"a", `3.05`},
{"b", `54`},
},
}, [][]Field{
{
{"x", `[{"vmrange":"1.896e+00...2.154e+00","hits":2},{"vmrange":"2.783e+00...3.162e+00","hits":1}]`},
},
})
}

View File

@@ -273,17 +273,17 @@ func (d *Dogsketch) unmarshalProtobuf(src []byte) (err error) {
}
d.Cnt = cnt
case 3:
v, ok := fc.Double()
min, ok := fc.Double()
if !ok {
return fmt.Errorf("cannot read min")
}
d.Min = v
d.Min = min
case 4:
v, ok := fc.Double()
max, ok := fc.Double()
if !ok {
return fmt.Errorf("cannot read max")
}
d.Max = v
d.Max = max
case 6:
sum, ok := fc.Double()
if !ok {

View File

@@ -792,17 +792,17 @@ func (dp *ExponentialHistogramDataPoint) unmarshalProtobuf(src []byte) (err erro
}
dp.Flags = flags
case 12:
v, ok := fc.Double()
min, ok := fc.Double()
if !ok {
return fmt.Errorf("cannot read Min")
}
dp.Min = &v
dp.Min = &min
case 13:
v, ok := fc.Double()
max, ok := fc.Double()
if !ok {
return fmt.Errorf("cannot read Max")
}
dp.Max = &v
dp.Max = &max
case 14:
zeroThreshold, ok := fc.Double()
if !ok {

View File

@@ -6,7 +6,6 @@ import (
"math"
"strconv"
"sync"
"time"
"github.com/VictoriaMetrics/metrics"
@@ -54,8 +53,6 @@ func ParseStream(r io.Reader, isGzipped bool, processBody func([]byte) ([]byte,
return nil
}
var skippedSampleLogger = logger.WithThrottler("otlp_skipped_sample", 5*time.Second)
func (wr *writeContext) appendSamplesFromScopeMetrics(sc *pb.ScopeMetrics) {
for _, m := range sc.Metrics {
if len(m.Name) == 0 {
@@ -71,7 +68,6 @@ func (wr *writeContext) appendSamplesFromScopeMetrics(sc *pb.ScopeMetrics) {
case m.Sum != nil:
if m.Sum.AggregationTemporality != pb.AggregationTemporalityCumulative {
rowsDroppedUnsupportedSum.Inc()
skippedSampleLogger.Warnf("unsupported delta temporality for %q ('sum'): skipping it", metricName)
continue
}
for _, p := range m.Sum.DataPoints {
@@ -84,7 +80,6 @@ func (wr *writeContext) appendSamplesFromScopeMetrics(sc *pb.ScopeMetrics) {
case m.Histogram != nil:
if m.Histogram.AggregationTemporality != pb.AggregationTemporalityCumulative {
rowsDroppedUnsupportedHistogram.Inc()
skippedSampleLogger.Warnf("unsupported delta temporality for %q ('histogram'): skipping it", metricName)
continue
}
for _, p := range m.Histogram.DataPoints {
@@ -93,7 +88,6 @@ func (wr *writeContext) appendSamplesFromScopeMetrics(sc *pb.ScopeMetrics) {
case m.ExponentialHistogram != nil:
if m.ExponentialHistogram.AggregationTemporality != pb.AggregationTemporalityCumulative {
rowsDroppedUnsupportedExponentialHistogram.Inc()
skippedSampleLogger.Warnf("unsupported delta temporality for %q ('exponential histogram'): skipping it", metricName)
continue
}
for _, p := range m.ExponentialHistogram.DataPoints {
@@ -101,7 +95,7 @@ func (wr *writeContext) appendSamplesFromScopeMetrics(sc *pb.ScopeMetrics) {
}
default:
rowsDroppedUnsupportedMetricType.Inc()
skippedSampleLogger.Warnf("unsupported type for metric %q", metricName)
logger.Warnf("unsupported type for metric %q", metricName)
}
}
}
@@ -145,7 +139,7 @@ func (wr *writeContext) appendSamplesFromHistogram(metricName string, p *pb.Hist
}
if len(p.BucketCounts) != len(p.ExplicitBounds)+1 {
// fast path, broken data format
skippedSampleLogger.Warnf("opentelemetry bad histogram format: %q, size of buckets: %d, size of bounds: %d", metricName, len(p.BucketCounts), len(p.ExplicitBounds))
logger.Warnf("opentelemetry bad histogram format: %q, size of buckets: %d, size of bounds: %d", metricName, len(p.BucketCounts), len(p.ExplicitBounds))
return
}

View File

@@ -1822,8 +1822,8 @@ func testStorageVariousDataPatternsConcurrently(t *testing.T, registerOnly bool,
func testStorageVariousDataPatterns(t *testing.T, registerOnly bool, op func(s *Storage, mrs []MetricRow), concurrency int, splitBatches bool) {
f := func(t *testing.T, sameBatchMetricNames, sameRowMetricNames, sameBatchDates, sameRowDates bool) {
batches, wantCounts := testGenerateMetricRowBatches(&batchOptions{
numBatches: 3,
numRowsPerBatch: 30,
numBatches: 4,
numRowsPerBatch: 100,
registerOnly: registerOnly,
sameBatchMetricNames: sameBatchMetricNames,
sameRowMetricNames: sameRowMetricNames,

View File

@@ -67,13 +67,13 @@ func (as *maxAggrState) flushState(ctx *flushCtx) {
sv := v.(*maxStateValue)
sv.mu.Lock()
maxV := sv.max
max := sv.max
// Mark the entry as deleted, so it won't be updated anymore by concurrent pushSample() calls.
sv.deleted = true
sv.mu.Unlock()
key := k.(string)
ctx.appendSeries(key, "max", maxV)
ctx.appendSeries(key, "max", max)
return true
})
}

View File

@@ -67,12 +67,12 @@ func (as *minAggrState) flushState(ctx *flushCtx) {
sv := v.(*minStateValue)
sv.mu.Lock()
minV := sv.min
min := sv.min
// Mark the entry as deleted, so it won't be updated anymore by concurrent pushSample() calls.
sv.deleted = true
sv.mu.Unlock()
key := k.(string)
ctx.appendSeries(key, "min", minV)
ctx.appendSeries(key, "min", min)
return true
})
}