diff --git a/pkg/labels/matcher.go b/pkg/labels/matcher.go index 5d0a1ee82..7a81d7cf0 100644 --- a/pkg/labels/matcher.go +++ b/pkg/labels/matcher.go @@ -59,3 +59,18 @@ func NewMatcher(t MatchType, n, v string) (*Matcher, error) { func (m *Matcher) String() string { return fmt.Sprintf("%s%s%q", m.Name, m.Type, m.Value) } + +// Matches returns whether the matcher matches the given string value. +func (m *Matcher) Matches(s string) bool { + switch m.Type { + case MatchEqual: + return s == m.Value + case MatchNotEqual: + return s != m.Value + case MatchRegexp: + return m.re.MatchString(s) + case MatchNotRegexp: + return !m.re.MatchString(s) + } + panic("labels.Matcher.Matches: invalid match type") +} diff --git a/promql/engine.go b/promql/engine.go index 0910fa838..5cc1f84f7 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -793,7 +793,7 @@ func (ev *evaluator) VectorSelector(node *VectorSelector) Vector { } vec = append(vec, Sample{ - Metric: toLabels(node.series[i].Labels()), + Metric: node.series[i].Labels(), Point: Point{V: v, T: ev.Timestamp}, }) } @@ -811,7 +811,7 @@ func (ev *evaluator) MatrixSelector(node *MatrixSelector) Matrix { for i, it := range node.iterators { ss := Series{ - Metric: toLabels(node.series[i].Labels()), + Metric: node.series[i].Labels(), Points: make([]Point, 0, 16), } diff --git a/promql/functions.go b/promql/functions.go index 0afcfa145..0b57f5147 100644 --- a/promql/functions.go +++ b/promql/functions.go @@ -553,7 +553,7 @@ func funcAbsent(ev *evaluator, args Expressions) Value { if vs, ok := args[0].(*VectorSelector); ok { for _, ma := range vs.LabelMatchers { - if ma.Type == MatchEqual && ma.Name != labels.MetricName { + if ma.Type == labels.MatchEqual && ma.Name != labels.MetricName { m = append(m, labels.Label{Name: ma.Name, Value: ma.Value}) } } diff --git a/promql/parse.go b/promql/parse.go index 039f2da5e..053842631 100644 --- a/promql/parse.go +++ b/promql/parse.go @@ -87,7 +87,7 @@ func ParseMetric(input string) (m labels.Labels, err error) { // ParseMetricSelector parses the provided textual metric selector into a list of // label matchers. -func ParseMetricSelector(input string) (m []*LabelMatcher, err error) { +func ParseMetricSelector(input string) (m []*labels.Matcher, err error) { p := newParser(input) defer p.recover(&err) @@ -815,10 +815,10 @@ func (p *parser) labelSet() labels.Labels { // // '{' [ , ... ] '}' // -func (p *parser) labelMatchers(operators ...itemType) []*LabelMatcher { +func (p *parser) labelMatchers(operators ...itemType) []*labels.Matcher { const ctx = "label matching" - matchers := []*LabelMatcher{} + matchers := []*labels.Matcher{} p.expect(itemLeftBrace, ctx) @@ -848,21 +848,21 @@ func (p *parser) labelMatchers(operators ...itemType) []*LabelMatcher { val := p.unquoteString(p.expect(itemString, ctx).val) // Map the item to the respective match type. - var matchType MatchType + var matchType labels.MatchType switch op { case itemEQL: - matchType = MatchEqual + matchType = labels.MatchEqual case itemNEQ: - matchType = MatchNotEqual + matchType = labels.MatchNotEqual case itemEQLRegex: - matchType = MatchRegexp + matchType = labels.MatchRegexp case itemNEQRegex: - matchType = MatchNotRegexp + matchType = labels.MatchNotRegexp default: p.errorf("item %q is not a metric match type", op) } - m, err := NewLabelMatcher(matchType, label.val, val) + m, err := labels.NewMatcher(matchType, label.val, val) if err != nil { p.error(err) } @@ -941,7 +941,7 @@ func (p *parser) offset() time.Duration { // [] // func (p *parser) VectorSelector(name string) *VectorSelector { - var matchers []*LabelMatcher + var matchers []*labels.Matcher // Parse label matching if any. if t := p.peek(); t.typ == itemLeftBrace { matchers = p.labelMatchers(itemEQL, itemNEQ, itemEQLRegex, itemNEQRegex) @@ -954,7 +954,7 @@ func (p *parser) VectorSelector(name string) *VectorSelector { } } // Set name label matching. - m, err := NewLabelMatcher(MatchEqual, labels.MetricName, name) + m, err := labels.NewMatcher(labels.MatchEqual, labels.MetricName, name) if err != nil { panic(err) // Must not happen with metric.Equal. } @@ -968,7 +968,7 @@ func (p *parser) VectorSelector(name string) *VectorSelector { // implicit selection of all metrics (e.g. by a typo). notEmpty := false for _, lm := range matchers { - if !lm.matcher().Matches("") { + if !lm.Matches("") { notEmpty = true break } diff --git a/promql/parse_test.go b/promql/parse_test.go index 7dea87b36..a09f95265 100644 --- a/promql/parse_test.go +++ b/promql/parse_test.go @@ -21,8 +21,8 @@ import ( "testing" "time" - "github.com/fabxc/tsdb/labels" "github.com/prometheus/common/model" + "github.com/prometheus/prometheus/pkg/labels" ) var testExpr = []struct { @@ -142,8 +142,8 @@ var testExpr = []struct { Op: itemSUB, Expr: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, }, @@ -152,8 +152,8 @@ var testExpr = []struct { Op: itemADD, Expr: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, }, @@ -261,14 +261,14 @@ var testExpr = []struct { Op: itemMUL, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, VectorMatching: &VectorMatching{Card: CardOneToOne}, @@ -279,8 +279,8 @@ var testExpr = []struct { Op: itemEQL, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &NumberLiteral{1}, @@ -291,8 +291,8 @@ var testExpr = []struct { Op: itemEQL, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &NumberLiteral{1}, @@ -305,8 +305,8 @@ var testExpr = []struct { LHS: &NumberLiteral{2.5}, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, }, @@ -316,14 +316,14 @@ var testExpr = []struct { Op: itemLAND, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, VectorMatching: &VectorMatching{Card: CardManyToMany}, @@ -334,14 +334,14 @@ var testExpr = []struct { Op: itemLOR, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, VectorMatching: &VectorMatching{Card: CardManyToMany}, @@ -352,14 +352,14 @@ var testExpr = []struct { Op: itemLUnless, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, VectorMatching: &VectorMatching{Card: CardManyToMany}, @@ -373,14 +373,14 @@ var testExpr = []struct { Op: itemADD, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, VectorMatching: &VectorMatching{Card: CardOneToOne}, @@ -389,14 +389,14 @@ var testExpr = []struct { Op: itemLAND, LHS: &VectorSelector{ Name: "bla", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bla"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bla"), }, }, RHS: &VectorSelector{ Name: "blub", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "blub"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "blub"), }, }, VectorMatching: &VectorMatching{Card: CardManyToMany}, @@ -414,30 +414,30 @@ var testExpr = []struct { Op: itemLAND, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, VectorMatching: &VectorMatching{Card: CardManyToMany}, }, RHS: &VectorSelector{ Name: "baz", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "baz"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "baz"), }, }, VectorMatching: &VectorMatching{Card: CardManyToMany}, }, RHS: &VectorSelector{ Name: "qux", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "qux"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "qux"), }, }, VectorMatching: &VectorMatching{Card: CardManyToMany}, @@ -449,22 +449,22 @@ var testExpr = []struct { Op: itemADD, LHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, RHS: &BinaryExpr{ Op: itemDIV, LHS: &VectorSelector{ Name: "bla", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bla"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bla"), }, }, RHS: &VectorSelector{ Name: "blub", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "blub"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "blub"), }, }, VectorMatching: &VectorMatching{ @@ -486,14 +486,14 @@ var testExpr = []struct { Op: itemMUL, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, VectorMatching: &VectorMatching{ @@ -508,14 +508,14 @@ var testExpr = []struct { Op: itemMUL, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, VectorMatching: &VectorMatching{ @@ -530,14 +530,14 @@ var testExpr = []struct { Op: itemLAND, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, VectorMatching: &VectorMatching{ @@ -552,14 +552,14 @@ var testExpr = []struct { Op: itemLAND, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, VectorMatching: &VectorMatching{ @@ -574,14 +574,14 @@ var testExpr = []struct { Op: itemLAND, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, VectorMatching: &VectorMatching{ @@ -595,14 +595,14 @@ var testExpr = []struct { Op: itemLAND, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, VectorMatching: &VectorMatching{ @@ -616,14 +616,14 @@ var testExpr = []struct { Op: itemLUnless, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "baz", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "baz"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "baz"), }, }, VectorMatching: &VectorMatching{ @@ -638,14 +638,14 @@ var testExpr = []struct { Op: itemDIV, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, VectorMatching: &VectorMatching{ @@ -661,14 +661,14 @@ var testExpr = []struct { Op: itemDIV, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, VectorMatching: &VectorMatching{ @@ -683,14 +683,14 @@ var testExpr = []struct { Op: itemDIV, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, VectorMatching: &VectorMatching{ @@ -705,14 +705,14 @@ var testExpr = []struct { Op: itemSUB, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, VectorMatching: &VectorMatching{ @@ -728,14 +728,14 @@ var testExpr = []struct { Op: itemSUB, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, VectorMatching: &VectorMatching{ @@ -823,8 +823,8 @@ var testExpr = []struct { expected: &VectorSelector{ Name: "foo", Offset: 0, - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, }, { @@ -832,8 +832,8 @@ var testExpr = []struct { expected: &VectorSelector{ Name: "foo", Offset: 5 * time.Minute, - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, }, { @@ -841,9 +841,9 @@ var testExpr = []struct { expected: &VectorSelector{ Name: "foo:bar", Offset: 0, - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, "a", "bc"), - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo:bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, "a", "bc"), + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo:bar"), }, }, }, { @@ -851,9 +851,9 @@ var testExpr = []struct { expected: &VectorSelector{ Name: "foo", Offset: 0, - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, "NaN", "bc"), - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, "NaN", "bc"), + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, }, { @@ -861,12 +861,12 @@ var testExpr = []struct { expected: &VectorSelector{ Name: "foo", Offset: 0, - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, "a", "b"), - mustLabelMatcher(MatchNotEqual, "foo", "bar"), - mustLabelMatcher(MatchRegexp, "test", "test"), - mustLabelMatcher(MatchNotRegexp, "bar", "baz"), - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, "a", "b"), + mustLabelMatcher(labels.MatchNotEqual, "foo", "bar"), + mustLabelMatcher(labels.MatchRegexp, "test", "test"), + mustLabelMatcher(labels.MatchNotRegexp, "bar", "baz"), + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, }, { @@ -947,8 +947,8 @@ var testExpr = []struct { Name: "test", Offset: 0, Range: 5 * time.Second, - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "test"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "test"), }, }, }, { @@ -957,8 +957,8 @@ var testExpr = []struct { Name: "test", Offset: 0, Range: 5 * time.Minute, - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "test"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "test"), }, }, }, { @@ -967,8 +967,8 @@ var testExpr = []struct { Name: "test", Offset: 5 * time.Minute, Range: 5 * time.Hour, - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "test"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "test"), }, }, }, { @@ -977,8 +977,8 @@ var testExpr = []struct { Name: "test", Offset: 10 * time.Second, Range: 5 * 24 * time.Hour, - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "test"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "test"), }, }, }, { @@ -987,8 +987,8 @@ var testExpr = []struct { Name: "test", Offset: 14 * 24 * time.Hour, Range: 5 * 7 * 24 * time.Hour, - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "test"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "test"), }, }, }, { @@ -997,9 +997,9 @@ var testExpr = []struct { Name: "test", Offset: 3 * 24 * time.Hour, Range: 5 * 365 * 24 * time.Hour, - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, "a", "b"), - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "test"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, "a", "b"), + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "test"), }, }, }, { @@ -1057,8 +1057,8 @@ var testExpr = []struct { Op: itemSum, Expr: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, Grouping: []string{"foo"}, @@ -1070,8 +1070,8 @@ var testExpr = []struct { KeepCommonLabels: true, Expr: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, Grouping: []string{"foo"}, @@ -1083,8 +1083,8 @@ var testExpr = []struct { KeepCommonLabels: true, Expr: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, Grouping: []string{"foo", "bar"}, @@ -1095,8 +1095,8 @@ var testExpr = []struct { Op: itemAvg, Expr: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, Grouping: []string{"foo"}, @@ -1107,8 +1107,8 @@ var testExpr = []struct { Op: itemCount, Expr: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, Grouping: []string{"foo"}, @@ -1120,8 +1120,8 @@ var testExpr = []struct { Op: itemMin, Expr: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, Grouping: []string{"foo"}, @@ -1133,8 +1133,8 @@ var testExpr = []struct { Op: itemMax, Expr: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, Grouping: []string{"foo"}, @@ -1146,8 +1146,8 @@ var testExpr = []struct { Without: true, Expr: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, Grouping: []string{"foo"}, @@ -1159,8 +1159,8 @@ var testExpr = []struct { Without: true, Expr: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, Grouping: []string{"foo"}, @@ -1171,8 +1171,8 @@ var testExpr = []struct { Op: itemStddev, Expr: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, }, @@ -1182,8 +1182,8 @@ var testExpr = []struct { Op: itemStdvar, Expr: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, Grouping: []string{"foo"}, @@ -1194,8 +1194,8 @@ var testExpr = []struct { Op: itemSum, Expr: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, Grouping: []string{}, @@ -1206,8 +1206,8 @@ var testExpr = []struct { Op: itemTopK, Expr: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, Param: &NumberLiteral{5}, @@ -1218,8 +1218,8 @@ var testExpr = []struct { Op: itemCountValues, Expr: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, Param: &StringLiteral{"value"}, @@ -1232,8 +1232,8 @@ var testExpr = []struct { Without: true, Expr: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, Grouping: []string{"and", "by", "avg", "count", "alert", "annotations"}, @@ -1304,9 +1304,9 @@ var testExpr = []struct { Args: Expressions{ &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchNotEqual, "foo", "bar"), - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchNotEqual, "foo", "bar"), + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, }, @@ -1318,8 +1318,8 @@ var testExpr = []struct { Args: Expressions{ &MatrixSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, Range: 5 * time.Minute, }, @@ -1332,8 +1332,8 @@ var testExpr = []struct { Args: Expressions{ &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, }, @@ -1345,8 +1345,8 @@ var testExpr = []struct { Args: Expressions{ &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, &NumberLiteral{5}, @@ -1553,8 +1553,8 @@ var testStatement = []struct { Args: Expressions{ &MatrixSelector{ Name: "http_request_count", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "http_request_count"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "http_request_count"), }, Range: 5 * time.Minute, }, @@ -1569,8 +1569,8 @@ var testStatement = []struct { Op: itemLSS, LHS: &VectorSelector{ Name: "dc:http_request:rate5m", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "dc:http_request:rate5m"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "dc:http_request:rate5m"), }, }, RHS: &NumberLiteral{10000}, @@ -1586,9 +1586,9 @@ var testStatement = []struct { Name: "foo", Expr: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, "label1", "value1"), - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, "label1", "value1"), + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, Labels: nil, @@ -1599,8 +1599,8 @@ var testStatement = []struct { Op: itemGTR, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "foo"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "foo"), }, }, RHS: &NumberLiteral{10}, @@ -1620,10 +1620,10 @@ var testStatement = []struct { Name: "foo", Expr: &VectorSelector{ Name: "bar", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, "a", "b"), - mustLabelMatcher(MatchRegexp, "x", "y"), - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "bar"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, "a", "b"), + mustLabelMatcher(labels.MatchRegexp, "x", "y"), + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar"), }, }, Labels: labels.FromStrings("x", "", "a", "z"), @@ -1644,8 +1644,8 @@ var testStatement = []struct { Op: itemGTR, LHS: &VectorSelector{ Name: "some_metric", - LabelMatchers: []*LabelMatcher{ - mustLabelMatcher(MatchEqual, string(model.MetricNameLabel), "some_metric"), + LabelMatchers: []*labels.Matcher{ + mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"), }, }, RHS: &NumberLiteral{1}, @@ -1764,8 +1764,8 @@ func TestParseStatements(t *testing.T) { } } -func mustLabelMatcher(mt MatchType, name, val string) *LabelMatcher { - m, err := NewLabelMatcher(mt, name, val) +func mustLabelMatcher(mt labels.MatchType, name, val string) *labels.Matcher { + m, err := labels.NewMatcher(mt, name, val) if err != nil { panic(err) } diff --git a/promql/printer.go b/promql/printer.go index 969043c95..0b7d60545 100644 --- a/promql/printer.go +++ b/promql/printer.go @@ -217,7 +217,7 @@ func (node *VectorSelector) String() string { labelStrings := make([]string, 0, len(node.LabelMatchers)-1) for _, matcher := range node.LabelMatchers { // Only include the __name__ label if its no equality matching. - if matcher.Name == labels.MetricName && matcher.Type == MatchEqual { + if matcher.Name == labels.MetricName && matcher.Type == labels.MatchEqual { continue } labelStrings = append(labelStrings, matcher.String()) diff --git a/promql/printer_test.go b/promql/printer_test.go index 7407f717c..2ecb88988 100644 --- a/promql/printer_test.go +++ b/promql/printer_test.go @@ -27,8 +27,8 @@ func TestStatementString(t *testing.T) { Op: itemGTR, LHS: &VectorSelector{ Name: "foo", - LabelMatchers: []*LabelMatcher{ - {Type: MatchEqual, Name: MetricNameLabel, Value: "bar"}, + LabelMatchers: []*labels.Matcher{ + {Type: labels.MatchEqual, Name: labels.MetricName, Value: "bar"}, }, }, RHS: &NumberLiteral{10}, diff --git a/promql/test.go b/promql/test.go index fbb02e84e..612712fc9 100644 --- a/promql/test.go +++ b/promql/test.go @@ -23,11 +23,10 @@ import ( "time" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/pkg/labels" "golang.org/x/net/context" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/storage" - "github.com/prometheus/prometheus/storage/local" "github.com/prometheus/prometheus/util/testutil" ) @@ -52,11 +51,11 @@ type Test struct { cmds []testCommand - storage local.Storage - closeStorage func() - queryEngine *Engine - context context.Context - cancelCtx context.CancelFunc + storage storage.Storage + + queryEngine *Engine + context context.Context + cancelCtx context.CancelFunc } // NewTest returns an initialized empty Test. @@ -90,7 +89,7 @@ func (t *Test) Context() context.Context { } // Storage returns the test's storage. -func (t *Test) Storage() local.Storage { +func (t *Test) Storage() storage.Storage { return t.storage } @@ -281,7 +280,7 @@ func (cmd *loadCmd) set(m labels.Labels, vals ...sequenceValue) { } // append the defined time series to the storage. -func (cmd *loadCmd) append(a storage.SampleAppender) { +func (cmd *loadCmd) append(a storage.Appender) { // TODO(fabxc): commented out until Appender refactoring. // for fp, samples := range cmd.defs { // met := cmd.metrics[fp] @@ -469,8 +468,15 @@ func (t *Test) exec(tc testCommand) error { t.clear() case *loadCmd: - cmd.append(t.storage) - t.storage.WaitForIndexing() + app, err := t.storage.Appender() + if err != nil { + return err + } + cmd.append(app) + + if err := app.Commit(); err != nil { + return err + } case *evalCmd: q := t.queryEngine.newQuery(cmd.expr, cmd.start, cmd.end, cmd.interval) @@ -498,17 +504,16 @@ func (t *Test) exec(tc testCommand) error { // clear the current test storage of all inserted samples. func (t *Test) clear() { - if t.closeStorage != nil { - t.closeStorage() + if t.storage != nil { + if err := t.storage.Close(); err != nil { + t.T.Fatalf("closing test storage: %s", err) + } } if t.cancelCtx != nil { t.cancelCtx() } + t.storage = testutil.NewStorage(t) - var closer testutil.Closer - t.storage, closer = local.NewTestStorage(t, 2) - - t.closeStorage = closer.Close // TODO(fabxc): add back // t.queryEngine = NewEngine(t.storage, nil) t.context, t.cancelCtx = context.WithCancel(context.Background()) @@ -517,7 +522,10 @@ func (t *Test) clear() { // Close closes resources associated with the Test. func (t *Test) Close() { t.cancelCtx() - t.closeStorage() + + if err := t.storage.Close(); err != nil { + t.T.Fatalf("closing test storage: %s", err) + } } // samplesAlmostEqual returns true if the two sample lines only differ by a diff --git a/rules/recording_test.go b/rules/recording_test.go index bbef4903c..279f6897b 100644 --- a/rules/recording_test.go +++ b/rules/recording_test.go @@ -22,12 +22,13 @@ import ( "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/promql" - "github.com/prometheus/prometheus/storage/local" + "github.com/prometheus/prometheus/util/testutil" ) func TestRuleEval(t *testing.T) { - storage, closer := local.NewTestStorage(t) + storage := testutil.NewStorage(t) defer closer.Close() + engine := promql.NewEngine(storage, nil) ctx, cancelCtx := context.WithCancel(context.Background()) defer cancelCtx() diff --git a/storage/buffer.go b/storage/buffer.go index 2f3f3c5c5..2b23058f3 100644 --- a/storage/buffer.go +++ b/storage/buffer.go @@ -1,5 +1,14 @@ package storage +import "math" + +// BufferedSeriesIterator wraps an iterator with a look-back buffer. +type BufferedSeriesIterator struct { + it SeriesIterator + buf *sampleRing + + lastTime int64 +} // NewBuffer returns a new iterator that buffers the values within the time range // of the current element and the duration of delta before. diff --git a/storage/interface.go b/storage/interface.go index 2f18563b8..911dc7d6f 100644 --- a/storage/interface.go +++ b/storage/interface.go @@ -40,7 +40,7 @@ type Storage interface { // Querier provides reading access to time series data. type Querier interface { // Select returns a set of series that matches the given label matchers. - Select(...*labels.Matcher) (SeriesSet, error) + Select(...*labels.Matcher) SeriesSet // LabelValues returns all potential values for a label name. LabelValues(name string) ([]string, error) diff --git a/storage/tsdb/tsdb.go b/storage/tsdb/tsdb.go index f9ec8c806..d2aa4f8c1 100644 --- a/storage/tsdb/tsdb.go +++ b/storage/tsdb/tsdb.go @@ -9,8 +9,8 @@ import ( "github.com/prometheus/prometheus/storage" ) -// db implements a storage.Storage around TSDB. -type db struct { +// adapter implements a storage.Storage around TSDB. +type adapter struct { db *tsdb.DB } @@ -20,36 +20,28 @@ func Open(path string) (storage.Storage, error) { if err != nil { return nil, err } - return &storage{db: db} + return adapter{db: db}, nil } -func (db *db) Querier(mint, maxt int64) (storage.Querier, error) { - q, err := db.db.Querier(mint, maxt) - if err != nil { - return nil, err - } - return querier{q: q}, nil +func (a adapter) Querier(mint, maxt int64) (storage.Querier, error) { + return querier{q: a.db.Querier(mint, maxt)}, nil } // Appender returns a new appender against the storage. -func (db *db) Appender() (storage.Appender, error) { - a, err := db.db.Appender() - if err != nil { - return nil, err - } - return appender{a: a}, nil +func (a adapter) Appender() (storage.Appender, error) { + return appender{a: a.db.Appender()}, nil } // Close closes the storage and all its underlying resources. -func (db *db) Close() error { - return db.Close() +func (a adapter) Close() error { + return a.db.Close() } type querier struct { q tsdb.Querier } -func (q *querier) Select(oms ...*labels.Matcher) (storage.SeriesSet, error) { +func (q querier) Select(oms ...*labels.Matcher) storage.SeriesSet { ms := make([]tsdbLabels.Matcher, 0, len(oms)) for _, om := range oms { @@ -61,47 +53,47 @@ func (q *querier) Select(oms ...*labels.Matcher) (storage.SeriesSet, error) { return seriesSet{set: set} } -func (q *querier) LabelValues(name string) ([]string, error) { return q.q.LabelValues(name) } -func (q *querier) Close() error { return q.q.Close() } +func (q querier) LabelValues(name string) ([]string, error) { return q.q.LabelValues(name) } +func (q querier) Close() error { return q.q.Close() } type seriesSet struct { set tsdb.SeriesSet } -func (s *seriesSet) Next() bool { return s.set.Next() } -func (s *seriesSet) Err() error { return s.set.Err() } -func (s *seriesSet) Series() storage.Series { return series{s: s.set.Series()} } +func (s seriesSet) Next() bool { return s.set.Next() } +func (s seriesSet) Err() error { return s.set.Err() } +func (s seriesSet) Series() storage.Series { return series{s: s.set.Series()} } type series struct { s tsdb.Series } -func (s *series) Labels() labels.Labels { return toLabels(s.s.Labels()) } -func (s *series) Iterator() storage.SeriesIterator { return storage.SeriesIterator(s.s.Iterator()) } +func (s series) Labels() labels.Labels { return toLabels(s.s.Labels()) } +func (s series) Iterator() storage.SeriesIterator { return storage.SeriesIterator(s.s.Iterator()) } type appender struct { a tsdb.Appender } -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) Add(lset labels.Labels, t int64, v float64) { a.a.Add(toTSDBLabels(lset), t, v) } +func (a appender) Commit() error { return a.a.Commit() } func convertMatcher(m *labels.Matcher) tsdbLabels.Matcher { switch m.Type { - case MatchEqual: + case labels.MatchEqual: return tsdbLabels.NewEqualMatcher(m.Name, m.Value) - case MatchNotEqual: + case labels.MatchNotEqual: return tsdbLabels.Not(tsdbLabels.NewEqualMatcher(m.Name, m.Value)) - case MatchRegexp: + case labels.MatchRegexp: res, err := tsdbLabels.NewRegexpMatcher(m.Name, m.Value) if err != nil { panic(err) } return res - case MatchNotRegexp: + case labels.MatchNotRegexp: res, err := tsdbLabels.NewRegexpMatcher(m.Name, m.Value) if err != nil { panic(err) diff --git a/template/template_test.go b/template/template_test.go index 3f919ab1b..08b902410 100644 --- a/template/template_test.go +++ b/template/template_test.go @@ -20,8 +20,9 @@ import ( "github.com/prometheus/common/model" "golang.org/x/net/context" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/promql" - "github.com/prometheus/prometheus/storage/local" + "github.com/prometheus/prometheus/util/testutil" ) type testTemplatesScenario struct { @@ -200,21 +201,20 @@ func TestTemplateExpansion(t *testing.T) { time := model.Time(0) - storage, closer := local.NewTestStorage(t, 2) - defer closer.Close() - storage.Append(&model.Sample{ - Metric: model.Metric{ - model.MetricNameLabel: "metric", - "instance": "a"}, - Value: 11, - }) - storage.Append(&model.Sample{ - Metric: model.Metric{ - model.MetricNameLabel: "metric", - "instance": "b"}, - Value: 21, - }) - storage.WaitForIndexing() + 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) + } engine := promql.NewEngine(storage, nil) diff --git a/util/testutil/storage.go b/util/testutil/storage.go index bdb0a06ad..b24e6f5f9 100644 --- a/util/testutil/storage.go +++ b/util/testutil/storage.go @@ -4,31 +4,31 @@ import ( "io/ioutil" "os" - "github.com/fabxc/tsdb" "github.com/prometheus/prometheus/storage" + "github.com/prometheus/prometheus/storage/tsdb" ) -// NewTestStorage returns a new storage for testing purposes +// NewStorage returns a new storage for testing purposes // that removes all associated files on closing. -func NewTestStorage(t T) storage.Storage { +func NewStorage(t T) storage.Storage { dir, err := ioutil.TempDir("", "test_storage") if err != nil { - t.Fatalf("Opening test dir failed: %s", errs) + t.Fatalf("Opening test dir failed: %s", err) } db, err := tsdb.Open(dir) if err != nil { t.Fatalf("Opening test storage failed: %s", err) } - return testStorage{DB: db, dir: dir} + return testStorage{Storage: db, dir: dir} } type testStorage struct { - *tsdb.DB + storage.Storage dir string } func (s testStorage) Close() error { - if err := s.db.Close(); err != nil { + if err := s.Storage.Close(); err != nil { return err } return os.RemoveAll(s.dir)