mirror of
https://github.com/prometheus/prometheus.git
synced 2025-01-24 04:02:04 -08:00
*: fix recording tests, migrate matcher types
This commit is contained in:
parent
0492ddbd4d
commit
622ece6273
|
@ -20,12 +20,13 @@ import (
|
||||||
"github.com/prometheus/common/model"
|
"github.com/prometheus/common/model"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
|
"github.com/prometheus/prometheus/pkg/labels"
|
||||||
"github.com/prometheus/prometheus/promql"
|
"github.com/prometheus/prometheus/promql"
|
||||||
"github.com/prometheus/prometheus/storage/local"
|
"github.com/prometheus/prometheus/storage/local"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRuleEval(t *testing.T) {
|
func TestRuleEval(t *testing.T) {
|
||||||
storage, closer := local.NewTestStorage(t, 2)
|
storage, closer := local.NewTestStorage(t)
|
||||||
defer closer.Close()
|
defer closer.Close()
|
||||||
engine := promql.NewEngine(storage, nil)
|
engine := promql.NewEngine(storage, nil)
|
||||||
ctx, cancelCtx := context.WithCancel(context.Background())
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
||||||
|
@ -36,27 +37,27 @@ func TestRuleEval(t *testing.T) {
|
||||||
suite := []struct {
|
suite := []struct {
|
||||||
name string
|
name string
|
||||||
expr promql.Expr
|
expr promql.Expr
|
||||||
labels model.LabelSet
|
labels labels.Labels
|
||||||
result model.Vector
|
result promql.Vector
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "nolabels",
|
name: "nolabels",
|
||||||
expr: &promql.NumberLiteral{Val: 1},
|
expr: &promql.NumberLiteral{Val: 1},
|
||||||
labels: model.LabelSet{},
|
labels: labels.Labels{},
|
||||||
result: model.Vector{&model.Sample{
|
result: promql.Vector{promql.Sample{
|
||||||
Value: 1,
|
Value: 1,
|
||||||
Timestamp: now,
|
Timestamp: now,
|
||||||
Metric: model.Metric{"__name__": "nolabels"},
|
Metric: labels.FromStrings("__name__", "nolabels"),
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "labels",
|
name: "labels",
|
||||||
expr: &promql.NumberLiteral{Val: 1},
|
expr: &promql.NumberLiteral{Val: 1},
|
||||||
labels: model.LabelSet{"foo": "bar"},
|
labels: labels.FromStrings("foo", "bar"),
|
||||||
result: model.Vector{&model.Sample{
|
result: promql.Vector{promql.Sample{
|
||||||
Value: 1,
|
Value: 1,
|
||||||
Timestamp: now,
|
Timestamp: now,
|
||||||
Metric: model.Metric{"__name__": "labels", "foo": "bar"},
|
Metric: labels.FromStrings("__name__", "labels", "foo", "bar"),
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -78,7 +79,7 @@ func TestRecordingRuleHTMLSnippet(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
rule := NewRecordingRule("testrule", expr, model.LabelSet{"html": "<b>BOLD</b>"})
|
rule := NewRecordingRule("testrule", expr, labels.FromStrings("html", "<b>BOLD</b>"))
|
||||||
|
|
||||||
const want = `<a href="/test/prefix/graph?g0.expr=testrule&g0.tab=0">testrule</a>{html="<b>BOLD</b>"} = <a href="/test/prefix/graph?g0.expr=foo%7Bhtml%3D%22%3Cb%3EBOLD%3Cb%3E%22%7D&g0.tab=0">foo{html="<b>BOLD<b>"}</a>`
|
const want = `<a href="/test/prefix/graph?g0.expr=testrule&g0.tab=0">testrule</a>{html="<b>BOLD</b>"} = <a href="/test/prefix/graph?g0.expr=foo%7Bhtml%3D%22%3Cb%3EBOLD%3Cb%3E%22%7D&g0.tab=0">foo{html="<b>BOLD<b>"}</a>`
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/prometheus/prometheus/pkg/labels"
|
"github.com/prometheus/prometheus/pkg/labels"
|
||||||
"github.com/prometheus/prometheus/util/testutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -25,10 +24,6 @@ var (
|
||||||
ErrDuplicateSampleForTimestamp = errors.New("duplicate sample for timestamp")
|
ErrDuplicateSampleForTimestamp = errors.New("duplicate sample for timestamp")
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewTestStorage(t testutil.T) Storage {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Storage ingests and manages samples, along with various indexes. All methods
|
// Storage ingests and manages samples, along with various indexes. All methods
|
||||||
// are goroutine-safe. Storage implements storage.SampleAppender.
|
// are goroutine-safe. Storage implements storage.SampleAppender.
|
||||||
type Storage interface {
|
type Storage interface {
|
||||||
|
|
|
@ -6,10 +6,11 @@ import (
|
||||||
"github.com/fabxc/tsdb"
|
"github.com/fabxc/tsdb"
|
||||||
tsdbLabels "github.com/fabxc/tsdb/labels"
|
tsdbLabels "github.com/fabxc/tsdb/labels"
|
||||||
"github.com/prometheus/prometheus/pkg/labels"
|
"github.com/prometheus/prometheus/pkg/labels"
|
||||||
"github.com/prometheus/prometheus/promql"
|
"github.com/prometheus/prometheus/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
type storage struct {
|
// db implements a storage.Storage around TSDB.
|
||||||
|
type db struct {
|
||||||
db *tsdb.DB
|
db *tsdb.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +23,7 @@ func Open(path string) (storage.Storage, error) {
|
||||||
return &storage{db: db}
|
return &storage{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *storage) Querier(mint, maxt int64) (storage.Querier, error) {
|
func (db *db) Querier(mint, maxt int64) (storage.Querier, error) {
|
||||||
q, err := db.db.Querier(mint, maxt)
|
q, err := db.db.Querier(mint, maxt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -31,7 +32,7 @@ func (db *storage) Querier(mint, maxt int64) (storage.Querier, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Appender returns a new appender against the storage.
|
// Appender returns a new appender against the storage.
|
||||||
func (db *storage) Appender() (Appender, error) {
|
func (db *db) Appender() (storage.Appender, error) {
|
||||||
a, err := db.db.Appender()
|
a, err := db.db.Appender()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -40,7 +41,7 @@ func (db *storage) Appender() (Appender, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close closes the storage and all its underlying resources.
|
// Close closes the storage and all its underlying resources.
|
||||||
func (db *storage) Close() error {
|
func (db *db) Close() error {
|
||||||
return db.Close()
|
return db.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +49,7 @@ type querier struct {
|
||||||
q tsdb.Querier
|
q tsdb.Querier
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *querier) Select(oms ...*promql.LabelMatcher) (storage.SeriesSet, error) {
|
func (q *querier) Select(oms ...*labels.Matcher) (storage.SeriesSet, error) {
|
||||||
ms := make([]tsdbLabels.Matcher, 0, len(oms))
|
ms := make([]tsdbLabels.Matcher, 0, len(oms))
|
||||||
|
|
||||||
for _, om := range oms {
|
for _, om := range oms {
|
||||||
|
@ -85,7 +86,7 @@ type appender struct {
|
||||||
func (a *appender) Add(lset labels.Labels, t int64, v float64) { a.Add(toTSDBLabels(lset), t, v) }
|
func (a *appender) Add(lset labels.Labels, t int64, v float64) { a.Add(toTSDBLabels(lset), t, v) }
|
||||||
func (a *appender) Commit() error { a.a.Commit() }
|
func (a *appender) Commit() error { a.a.Commit() }
|
||||||
|
|
||||||
func convertMatcher(m *promql.LabelMatcher) tsdbLabels.Matcher {
|
func convertMatcher(m *labels.Matcher) tsdbLabels.Matcher {
|
||||||
switch m.Type {
|
switch m.Type {
|
||||||
case MatchEqual:
|
case MatchEqual:
|
||||||
return tsdbLabels.NewEqualMatcher(m.Name, m.Value)
|
return tsdbLabels.NewEqualMatcher(m.Name, m.Value)
|
||||||
|
@ -107,7 +108,7 @@ func convertMatcher(m *promql.LabelMatcher) tsdbLabels.Matcher {
|
||||||
}
|
}
|
||||||
return tsdbLabels.Not(res)
|
return tsdbLabels.Not(res)
|
||||||
}
|
}
|
||||||
panic("promql.LabelMatcher.matcher: invalid matcher type")
|
panic("storage.convertMatcher: invalid matcher type")
|
||||||
}
|
}
|
||||||
|
|
||||||
func toTSDBLabels(l labels.Labels) tsdbLabels.Labels {
|
func toTSDBLabels(l labels.Labels) tsdbLabels.Labels {
|
||||||
|
|
35
util/testutil/storage.go
Normal file
35
util/testutil/storage.go
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package testutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/fabxc/tsdb"
|
||||||
|
"github.com/prometheus/prometheus/storage"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewTestStorage returns a new storage for testing purposes
|
||||||
|
// that removes all associated files on closing.
|
||||||
|
func NewTestStorage(t T) storage.Storage {
|
||||||
|
dir, err := ioutil.TempDir("", "test_storage")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Opening test dir failed: %s", errs)
|
||||||
|
}
|
||||||
|
db, err := tsdb.Open(dir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Opening test storage failed: %s", err)
|
||||||
|
}
|
||||||
|
return testStorage{DB: db, dir: dir}
|
||||||
|
}
|
||||||
|
|
||||||
|
type testStorage struct {
|
||||||
|
*tsdb.DB
|
||||||
|
dir string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s testStorage) Close() error {
|
||||||
|
if err := s.db.Close(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return os.RemoveAll(s.dir)
|
||||||
|
}
|
Loading…
Reference in a new issue