2015-01-21 19:07:45 +00:00
|
|
|
// Copyright 2014 The Prometheus Authors
|
2014-05-28 17:44:54 +00:00
|
|
|
// 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.
|
|
|
|
|
2015-05-28 19:22:08 +00:00
|
|
|
package template
|
2014-05-28 17:44:54 +00:00
|
|
|
|
|
|
|
import (
|
2015-03-28 18:51:41 +00:00
|
|
|
"math"
|
2014-05-28 17:44:54 +00:00
|
|
|
"testing"
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
"github.com/prometheus/common/model"
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
"golang.org/x/net/context"
|
2014-05-28 17:44:54 +00:00
|
|
|
|
2016-12-25 10:34:22 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2015-03-30 17:43:19 +00:00
|
|
|
"github.com/prometheus/prometheus/promql"
|
2016-12-25 10:34:22 +00:00
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2014-05-28 17:44:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type testTemplatesScenario struct {
|
|
|
|
text string
|
|
|
|
output string
|
2014-06-11 10:32:19 +00:00
|
|
|
input interface{}
|
2014-05-28 17:44:54 +00:00
|
|
|
shouldFail bool
|
2014-06-10 14:30:06 +00:00
|
|
|
html bool
|
2014-05-28 17:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateExpansion(t *testing.T) {
|
|
|
|
scenarios := []testTemplatesScenario{
|
|
|
|
{
|
|
|
|
// No template.
|
|
|
|
text: "plain text",
|
|
|
|
output: "plain text",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Simple value.
|
|
|
|
text: "{{ 1 }}",
|
|
|
|
output: "1",
|
|
|
|
},
|
2014-06-10 14:30:06 +00:00
|
|
|
{
|
|
|
|
// HTML escaping.
|
|
|
|
text: "{{ \"<b>\" }}",
|
|
|
|
output: "<b>",
|
|
|
|
html: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Disabling HTML escaping.
|
|
|
|
text: "{{ \"<b>\" | safeHtml }}",
|
|
|
|
output: "<b>",
|
|
|
|
html: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// HTML escaping doesn't apply to non-html.
|
|
|
|
text: "{{ \"<b>\" }}",
|
|
|
|
output: "<b>",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Pass multiple arguments to templates.
|
|
|
|
text: "{{define \"x\"}}{{.arg0}} {{.arg1}}{{end}}{{template \"x\" (args 1 \"2\")}}",
|
|
|
|
output: "1 2",
|
|
|
|
},
|
2015-05-11 07:12:28 +00:00
|
|
|
{
|
|
|
|
text: "{{ query \"1.5\" | first | value }}",
|
|
|
|
output: "1.5",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Get value from scalar query.
|
|
|
|
text: "{{ query \"scalar(count(metric))\" | first | value }}",
|
|
|
|
output: "2",
|
|
|
|
},
|
2014-05-28 17:44:54 +00:00
|
|
|
{
|
|
|
|
// Get value from query.
|
|
|
|
text: "{{ query \"metric{instance='a'}\" | first | value }}",
|
|
|
|
output: "11",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Get label from query.
|
|
|
|
text: "{{ query \"metric{instance='a'}\" | first | label \"instance\" }}",
|
|
|
|
output: "a",
|
|
|
|
},
|
2015-11-28 13:45:32 +00:00
|
|
|
{
|
|
|
|
// Missing label is empty when using label function.
|
|
|
|
text: "{{ query \"metric{instance='a'}\" | first | label \"foo\" }}",
|
|
|
|
output: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Missing label is empty when not using label function.
|
|
|
|
text: "{{ $x := query \"metric\" | first }}{{ $x.Labels.foo }}",
|
|
|
|
output: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: "{{ $x := query \"metric\" | first }}{{ $x.Labels.foo }}",
|
|
|
|
output: "",
|
|
|
|
html: true,
|
|
|
|
},
|
2014-05-28 17:44:54 +00:00
|
|
|
{
|
2014-08-05 18:56:05 +00:00
|
|
|
// Range over query and sort by label.
|
|
|
|
text: "{{ range query \"metric\" | sortByLabel \"instance\" }}{{.Labels.instance}}:{{.Value}}: {{end}}",
|
2014-05-28 17:44:54 +00:00
|
|
|
output: "a:11: b:21: ",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Unparsable template.
|
|
|
|
text: "{{",
|
|
|
|
shouldFail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Error in function.
|
|
|
|
text: "{{ query \"missing\" | first }}",
|
|
|
|
shouldFail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Panic.
|
|
|
|
text: "{{ (query \"missing\").banana }}",
|
|
|
|
shouldFail: true,
|
|
|
|
},
|
2014-06-05 13:07:54 +00:00
|
|
|
{
|
|
|
|
// Regex replacement.
|
|
|
|
text: "{{ reReplaceAll \"(a)b\" \"x$1\" \"ab\" }}",
|
|
|
|
output: "xa",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Humanize.
|
2014-06-11 10:32:19 +00:00
|
|
|
text: "{{ range . }}{{ humanize . }}:{{ end }}",
|
|
|
|
input: []float64{0.0, 1.0, 1234567.0, .12},
|
|
|
|
output: "0:1:1.235M:120m:",
|
2014-06-05 13:07:54 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// Humanize1024.
|
2014-06-11 10:32:19 +00:00
|
|
|
text: "{{ range . }}{{ humanize1024 . }}:{{ end }}",
|
|
|
|
input: []float64{0.0, 1.0, 1048576.0, .12},
|
|
|
|
output: "0:1:1Mi:0.12:",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// HumanizeDuration - seconds.
|
|
|
|
text: "{{ range . }}{{ humanizeDuration . }}:{{ end }}",
|
2015-02-26 17:17:04 +00:00
|
|
|
input: []float64{0, 1, 60, 3600, 86400, 86400 + 3600, -(86400*2 + 3600*3 + 60*4 + 5), 899.99},
|
|
|
|
output: "0s:1s:1m 0s:1h 0m 0s:1d 0h 0m 0s:1d 1h 0m 0s:-2d 3h 4m 5s:14m 59s:",
|
2014-06-11 10:32:19 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// HumanizeDuration - subsecond and fractional seconds.
|
|
|
|
text: "{{ range . }}{{ humanizeDuration . }}:{{ end }}",
|
|
|
|
input: []float64{.1, .0001, .12345, 60.1, 60.5, 1.2345, 12.345},
|
2015-02-26 17:17:04 +00:00
|
|
|
output: "100ms:100us:123.5ms:1m 0s:1m 0s:1.234s:12.35s:",
|
2014-06-05 13:07:54 +00:00
|
|
|
},
|
2015-03-28 18:51:41 +00:00
|
|
|
{
|
|
|
|
// Humanize* Inf and NaN.
|
2015-06-23 15:46:57 +00:00
|
|
|
text: "{{ range . }}{{ humanize . }}:{{ humanize1024 . }}:{{ humanizeDuration . }}:{{humanizeTimestamp .}}:{{ end }}",
|
2015-03-28 18:51:41 +00:00
|
|
|
input: []float64{math.Inf(1), math.Inf(-1), math.NaN()},
|
2015-06-23 15:46:57 +00:00
|
|
|
output: "+Inf:+Inf:+Inf:+Inf:-Inf:-Inf:-Inf:-Inf:NaN:NaN:NaN:NaN:",
|
|
|
|
},
|
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
// HumanizeTimestamp - model.SampleValue input.
|
2015-06-23 15:46:57 +00:00
|
|
|
text: "{{ 1435065584.128 | humanizeTimestamp }}",
|
2015-06-23 19:15:32 +00:00
|
|
|
output: "2015-06-23 13:19:44.128 +0000 UTC",
|
2015-03-28 18:51:41 +00:00
|
|
|
},
|
2014-06-05 17:44:19 +00:00
|
|
|
{
|
|
|
|
// Title.
|
|
|
|
text: "{{ \"aa bb CC\" | title }}",
|
|
|
|
output: "Aa Bb CC",
|
|
|
|
},
|
2016-08-15 11:00:22 +00:00
|
|
|
{
|
|
|
|
// toUpper.
|
|
|
|
text: "{{ \"aa bb CC\" | toUpper }}",
|
|
|
|
output: "AA BB CC",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// toLower.
|
|
|
|
text: "{{ \"aA bB CC\" | toLower }}",
|
|
|
|
output: "aa bb cc",
|
|
|
|
},
|
2014-06-05 17:44:19 +00:00
|
|
|
{
|
|
|
|
// Match.
|
|
|
|
text: "{{ match \"a+\" \"aa\" }} {{ match \"a+\" \"b\" }}",
|
|
|
|
output: "true false",
|
|
|
|
},
|
2014-07-25 12:23:47 +00:00
|
|
|
{
|
|
|
|
// graphLink.
|
|
|
|
text: "{{ graphLink \"up\" }}",
|
2016-09-03 19:05:23 +00:00
|
|
|
output: "/graph?g0.expr=up&g0.tab=0",
|
2014-07-25 12:23:47 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// tableLink.
|
|
|
|
text: "{{ tableLink \"up\" }}",
|
2016-09-03 19:05:23 +00:00
|
|
|
output: "/graph?g0.expr=up&g0.tab=1",
|
2014-07-25 12:23:47 +00:00
|
|
|
},
|
2014-07-25 16:32:17 +00:00
|
|
|
{
|
|
|
|
// tmpl.
|
|
|
|
text: "{{ define \"a\" }}x{{ end }}{{ $name := \"a\"}}{{ tmpl $name . }}",
|
|
|
|
output: "x",
|
|
|
|
html: true,
|
|
|
|
},
|
2014-05-28 17:44:54 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
time := model.Time(0)
|
2014-05-28 17:44:54 +00:00
|
|
|
|
2016-12-25 10:34:22 +00:00
|
|
|
storage := testutil.NewStorage(t)
|
|
|
|
defer storage.Close()
|
|
|
|
|
|
|
|
app, err := storage.Appender()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("get appender: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Add(labels.FromStrings(labels.MetricName, "metric", "instance", "a"), 0, 11)
|
|
|
|
app.Add(labels.FromStrings(labels.MetricName, "metric", "instance", "b"), 0, 21)
|
|
|
|
|
|
|
|
if err := app.Commit(); err != nil {
|
|
|
|
t.Fatalf("commit samples: %s", err)
|
|
|
|
}
|
2014-05-28 17:44:54 +00:00
|
|
|
|
2015-06-15 10:49:11 +00:00
|
|
|
engine := promql.NewEngine(storage, nil)
|
2015-03-30 17:43:19 +00:00
|
|
|
|
2014-10-08 14:22:54 +00:00
|
|
|
for i, s := range scenarios {
|
2014-06-10 14:30:06 +00:00
|
|
|
var result string
|
|
|
|
var err error
|
2016-09-15 22:58:06 +00:00
|
|
|
expander := NewTemplateExpander(context.Background(), s.text, "test", s.input, time, engine, "")
|
2014-06-10 14:30:06 +00:00
|
|
|
if s.html {
|
|
|
|
result, err = expander.ExpandHTML(nil)
|
|
|
|
} else {
|
|
|
|
result, err = expander.Expand()
|
|
|
|
}
|
2014-05-28 17:44:54 +00:00
|
|
|
if s.shouldFail {
|
|
|
|
if err == nil {
|
2014-10-08 14:22:54 +00:00
|
|
|
t.Fatalf("%d. Error not returned from %v", i, s.text)
|
2014-05-28 17:44:54 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
2014-10-08 14:22:54 +00:00
|
|
|
t.Fatalf("%d. Error returned from %v: %v", i, s.text, err)
|
2014-05-28 17:44:54 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if result != s.output {
|
2014-10-08 14:22:54 +00:00
|
|
|
t.Fatalf("%d. Error in result from %v: Expected '%v' Got '%v'", i, s.text, s.output, result)
|
2014-05-28 17:44:54 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|