Replace metric.LabelPair with model.LabelPair

This commit is contained in:
Fabian Reinartz 2015-08-22 13:32:13 +02:00
parent 3a0145c09e
commit c9d396f476
8 changed files with 40 additions and 191 deletions

View file

@ -38,8 +38,6 @@ import (
"sync" "sync"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/storage/metric"
) )
// A byteReader is an io.ByteReader that also implements the vanilla io.Reader // A byteReader is an io.ByteReader that also implements the vanilla io.Reader
@ -282,9 +280,9 @@ func (fps *Fingerprints) UnmarshalBinary(buf []byte) error {
return nil return nil
} }
// LabelPair is a metric.LabelPair that implements // LabelPair is a model.LabelPair that implements
// encoding.BinaryMarshaler and encoding.BinaryUnmarshaler. // encoding.BinaryMarshaler and encoding.BinaryUnmarshaler.
type LabelPair metric.LabelPair type LabelPair model.LabelPair
// MarshalBinary implements encoding.BinaryMarshaler. // MarshalBinary implements encoding.BinaryMarshaler.
func (lp LabelPair) MarshalBinary() ([]byte, error) { func (lp LabelPair) MarshalBinary() ([]byte, error) {

View file

@ -23,7 +23,6 @@ import (
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/storage/local/codable" "github.com/prometheus/prometheus/storage/local/codable"
"github.com/prometheus/prometheus/storage/metric"
) )
const ( const (
@ -183,7 +182,7 @@ func DeleteLabelNameLabelValuesIndex(basePath string) error {
// LabelPairFingerprintsMapping is an in-memory map of label pairs to // LabelPairFingerprintsMapping is an in-memory map of label pairs to
// fingerprints. // fingerprints.
type LabelPairFingerprintsMapping map[metric.LabelPair]codable.FingerprintSet type LabelPairFingerprintsMapping map[model.LabelPair]codable.FingerprintSet
// LabelPairFingerprintIndex is a KeyValueStore that maps existing label pairs // LabelPairFingerprintIndex is a KeyValueStore that maps existing label pairs
// to the fingerprints of all metrics containing those label pairs. // to the fingerprints of all metrics containing those label pairs.
@ -216,7 +215,7 @@ func (i *LabelPairFingerprintIndex) IndexBatch(m LabelPairFingerprintsMapping) e
// returned. // returned.
// //
// This method is goroutine-safe. // This method is goroutine-safe.
func (i *LabelPairFingerprintIndex) Lookup(p metric.LabelPair) (fps model.Fingerprints, ok bool, err error) { func (i *LabelPairFingerprintIndex) Lookup(p model.LabelPair) (fps model.Fingerprints, ok bool, err error) {
ok, err = i.Get((codable.LabelPair)(p), (*codable.Fingerprints)(&fps)) ok, err = i.Get((codable.LabelPair)(p), (*codable.Fingerprints)(&fps))
return return
} }
@ -226,7 +225,7 @@ func (i *LabelPairFingerprintIndex) Lookup(p metric.LabelPair) (fps model.Finger
// returned. // returned.
// //
// This method is goroutine-safe. // This method is goroutine-safe.
func (i *LabelPairFingerprintIndex) LookupSet(p metric.LabelPair) (fps map[model.Fingerprint]struct{}, ok bool, err error) { func (i *LabelPairFingerprintIndex) LookupSet(p model.LabelPair) (fps map[model.Fingerprint]struct{}, ok bool, err error) {
ok, err = i.Get((codable.LabelPair)(p), (*codable.FingerprintSet)(&fps)) ok, err = i.Get((codable.LabelPair)(p), (*codable.FingerprintSet)(&fps))
if fps == nil { if fps == nil {
fps = map[model.Fingerprint]struct{}{} fps = map[model.Fingerprint]struct{}{}

View file

@ -34,7 +34,6 @@ import (
"github.com/prometheus/prometheus/storage/local/codable" "github.com/prometheus/prometheus/storage/local/codable"
"github.com/prometheus/prometheus/storage/local/index" "github.com/prometheus/prometheus/storage/local/index"
"github.com/prometheus/prometheus/storage/metric"
"github.com/prometheus/prometheus/util/flock" "github.com/prometheus/prometheus/util/flock"
) )
@ -334,7 +333,7 @@ func (p *persistence) setDirty(dirty bool) {
// pair. This method is goroutine-safe but take into account that metrics queued // pair. This method is goroutine-safe but take into account that metrics queued
// for indexing with IndexMetric might not have made it into the index // for indexing with IndexMetric might not have made it into the index
// yet. (Same applies correspondingly to UnindexMetric.) // yet. (Same applies correspondingly to UnindexMetric.)
func (p *persistence) fingerprintsForLabelPair(lp metric.LabelPair) (model.Fingerprints, error) { func (p *persistence) fingerprintsForLabelPair(lp model.LabelPair) (model.Fingerprints, error) {
fps, _, err := p.labelPairToFingerprints.Lookup(lp) fps, _, err := p.labelPairToFingerprints.Lookup(lp)
if err != nil { if err != nil {
return nil, err return nil, err
@ -1353,7 +1352,7 @@ loop:
batchSize++ batchSize++
for ln, lv := range op.metric { for ln, lv := range op.metric {
lp := metric.LabelPair{Name: ln, Value: lv} lp := model.LabelPair{Name: ln, Value: lv}
baseFPs, ok := pairToFPs[lp] baseFPs, ok := pairToFPs[lp]
if !ok { if !ok {
var err error var err error

View file

@ -603,7 +603,7 @@ func testDropArchivedMetric(t *testing.T, encoding chunkEncoding) {
p.indexMetric(2, m2) p.indexMetric(2, m2)
p.waitForIndexing() p.waitForIndexing()
outFPs, err := p.fingerprintsForLabelPair(metric.LabelPair{Name: "n1", Value: "v1"}) outFPs, err := p.fingerprintsForLabelPair(model.LabelPair{Name: "n1", Value: "v1"})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -611,7 +611,7 @@ func testDropArchivedMetric(t *testing.T, encoding chunkEncoding) {
if !reflect.DeepEqual(outFPs, want) { if !reflect.DeepEqual(outFPs, want) {
t.Errorf("want %#v, got %#v", want, outFPs) t.Errorf("want %#v, got %#v", want, outFPs)
} }
outFPs, err = p.fingerprintsForLabelPair(metric.LabelPair{Name: "n2", Value: "v2"}) outFPs, err = p.fingerprintsForLabelPair(model.LabelPair{Name: "n2", Value: "v2"})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -635,7 +635,7 @@ func testDropArchivedMetric(t *testing.T, encoding chunkEncoding) {
} }
p.waitForIndexing() p.waitForIndexing()
outFPs, err = p.fingerprintsForLabelPair(metric.LabelPair{Name: "n1", Value: "v1"}) outFPs, err = p.fingerprintsForLabelPair(model.LabelPair{Name: "n1", Value: "v1"})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -643,7 +643,7 @@ func testDropArchivedMetric(t *testing.T, encoding chunkEncoding) {
if !reflect.DeepEqual(outFPs, want) { if !reflect.DeepEqual(outFPs, want) {
t.Errorf("want %#v, got %#v", want, outFPs) t.Errorf("want %#v, got %#v", want, outFPs)
} }
outFPs, err = p.fingerprintsForLabelPair(metric.LabelPair{Name: "n2", Value: "v2"}) outFPs, err = p.fingerprintsForLabelPair(model.LabelPair{Name: "n2", Value: "v2"})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -708,27 +708,27 @@ func testIndexing(t *testing.T, encoding chunkEncoding) {
}, },
}, },
expectedLpToFps: index.LabelPairFingerprintsMapping{ expectedLpToFps: index.LabelPairFingerprintsMapping{
metric.LabelPair{ model.LabelPair{
Name: model.MetricNameLabel, Name: model.MetricNameLabel,
Value: "metric_0", Value: "metric_0",
}: codable.FingerprintSet{0: struct{}{}, 1: struct{}{}}, }: codable.FingerprintSet{0: struct{}{}, 1: struct{}{}},
metric.LabelPair{ model.LabelPair{
Name: model.MetricNameLabel, Name: model.MetricNameLabel,
Value: "metric_1", Value: "metric_1",
}: codable.FingerprintSet{2: struct{}{}}, }: codable.FingerprintSet{2: struct{}{}},
metric.LabelPair{ model.LabelPair{
Name: "label_1", Name: "label_1",
Value: "value_1", Value: "value_1",
}: codable.FingerprintSet{0: struct{}{}}, }: codable.FingerprintSet{0: struct{}{}},
metric.LabelPair{ model.LabelPair{
Name: "label_1", Name: "label_1",
Value: "value_2", Value: "value_2",
}: codable.FingerprintSet{2: struct{}{}}, }: codable.FingerprintSet{2: struct{}{}},
metric.LabelPair{ model.LabelPair{
Name: "label_2", Name: "label_2",
Value: "value_2", Value: "value_2",
}: codable.FingerprintSet{1: struct{}{}}, }: codable.FingerprintSet{1: struct{}{}},
metric.LabelPair{ model.LabelPair{
Name: "label_3", Name: "label_3",
Value: "value_3", Value: "value_3",
}: codable.FingerprintSet{1: struct{}{}}, }: codable.FingerprintSet{1: struct{}{}},
@ -769,39 +769,39 @@ func testIndexing(t *testing.T, encoding chunkEncoding) {
}, },
}, },
expectedLpToFps: index.LabelPairFingerprintsMapping{ expectedLpToFps: index.LabelPairFingerprintsMapping{
metric.LabelPair{ model.LabelPair{
Name: model.MetricNameLabel, Name: model.MetricNameLabel,
Value: "metric_0", Value: "metric_0",
}: codable.FingerprintSet{0: struct{}{}, 1: struct{}{}, 3: struct{}{}}, }: codable.FingerprintSet{0: struct{}{}, 1: struct{}{}, 3: struct{}{}},
metric.LabelPair{ model.LabelPair{
Name: model.MetricNameLabel, Name: model.MetricNameLabel,
Value: "metric_1", Value: "metric_1",
}: codable.FingerprintSet{2: struct{}{}, 5: struct{}{}}, }: codable.FingerprintSet{2: struct{}{}, 5: struct{}{}},
metric.LabelPair{ model.LabelPair{
Name: model.MetricNameLabel, Name: model.MetricNameLabel,
Value: "metric_2", Value: "metric_2",
}: codable.FingerprintSet{4: struct{}{}}, }: codable.FingerprintSet{4: struct{}{}},
metric.LabelPair{ model.LabelPair{
Name: "label_1", Name: "label_1",
Value: "value_1", Value: "value_1",
}: codable.FingerprintSet{0: struct{}{}}, }: codable.FingerprintSet{0: struct{}{}},
metric.LabelPair{ model.LabelPair{
Name: "label_1", Name: "label_1",
Value: "value_2", Value: "value_2",
}: codable.FingerprintSet{2: struct{}{}}, }: codable.FingerprintSet{2: struct{}{}},
metric.LabelPair{ model.LabelPair{
Name: "label_1", Name: "label_1",
Value: "value_3", Value: "value_3",
}: codable.FingerprintSet{3: struct{}{}, 5: struct{}{}}, }: codable.FingerprintSet{3: struct{}{}, 5: struct{}{}},
metric.LabelPair{ model.LabelPair{
Name: "label_2", Name: "label_2",
Value: "value_2", Value: "value_2",
}: codable.FingerprintSet{1: struct{}{}, 4: struct{}{}}, }: codable.FingerprintSet{1: struct{}{}, 4: struct{}{}},
metric.LabelPair{ model.LabelPair{
Name: "label_3", Name: "label_3",
Value: "value_1", Value: "value_1",
}: codable.FingerprintSet{4: struct{}{}}, }: codable.FingerprintSet{4: struct{}{}},
metric.LabelPair{ model.LabelPair{
Name: "label_3", Name: "label_3",
Value: "value_3", Value: "value_3",
}: codable.FingerprintSet{1: struct{}{}}, }: codable.FingerprintSet{1: struct{}{}},

View file

@ -385,7 +385,7 @@ func (s *memorySeriesStorage) NewPreloader() Preloader {
// fingerprintsForLabelPairs returns the set of fingerprints that have the given labels. // fingerprintsForLabelPairs returns the set of fingerprints that have the given labels.
// This does not work with empty label values. // This does not work with empty label values.
func (s *memorySeriesStorage) fingerprintsForLabelPairs(pairs ...metric.LabelPair) map[model.Fingerprint]struct{} { func (s *memorySeriesStorage) fingerprintsForLabelPairs(pairs ...model.LabelPair) map[model.Fingerprint]struct{} {
var result map[model.Fingerprint]struct{} var result map[model.Fingerprint]struct{}
for _, pair := range pairs { for _, pair := range pairs {
intersection := map[model.Fingerprint]struct{}{} intersection := map[model.Fingerprint]struct{}{}
@ -412,12 +412,12 @@ func (s *memorySeriesStorage) fingerprintsForLabelPairs(pairs ...metric.LabelPai
// MetricsForLabelMatchers implements Storage. // MetricsForLabelMatchers implements Storage.
func (s *memorySeriesStorage) MetricsForLabelMatchers(matchers ...*metric.LabelMatcher) map[model.Fingerprint]model.COWMetric { func (s *memorySeriesStorage) MetricsForLabelMatchers(matchers ...*metric.LabelMatcher) map[model.Fingerprint]model.COWMetric {
var ( var (
equals []metric.LabelPair equals []model.LabelPair
filters []*metric.LabelMatcher filters []*metric.LabelMatcher
) )
for _, lm := range matchers { for _, lm := range matchers {
if lm.Type == metric.Equal && lm.Value != "" { if lm.Type == metric.Equal && lm.Value != "" {
equals = append(equals, metric.LabelPair{ equals = append(equals, model.LabelPair{
Name: lm.Name, Name: lm.Name,
Value: lm.Value, Value: lm.Value,
}) })
@ -446,7 +446,7 @@ func (s *memorySeriesStorage) MetricsForLabelMatchers(matchers ...*metric.LabelM
return nil return nil
} }
for _, v := range matches { for _, v := range matches {
fps := s.fingerprintsForLabelPairs(metric.LabelPair{ fps := s.fingerprintsForLabelPairs(model.LabelPair{
Name: matcher.Name, Name: matcher.Name,
Value: v, Value: v,
}) })

View file

@ -222,33 +222,33 @@ func TestFingerprintsForLabels(t *testing.T) {
storage.WaitForIndexing() storage.WaitForIndexing()
var matcherTests = []struct { var matcherTests = []struct {
pairs []metric.LabelPair pairs []model.LabelPair
expected model.Fingerprints expected model.Fingerprints
}{ }{
{ {
pairs: []metric.LabelPair{{"label1", "x"}}, pairs: []model.LabelPair{{"label1", "x"}},
expected: fingerprints[:0], expected: fingerprints[:0],
}, },
{ {
pairs: []metric.LabelPair{{"label1", "test_0"}}, pairs: []model.LabelPair{{"label1", "test_0"}},
expected: fingerprints[:10], expected: fingerprints[:10],
}, },
{ {
pairs: []metric.LabelPair{ pairs: []model.LabelPair{
{"label1", "test_0"}, {"label1", "test_0"},
{"label1", "test_1"}, {"label1", "test_1"},
}, },
expected: fingerprints[:0], expected: fingerprints[:0],
}, },
{ {
pairs: []metric.LabelPair{ pairs: []model.LabelPair{
{"label1", "test_0"}, {"label1", "test_0"},
{"label2", "test_1"}, {"label2", "test_1"},
}, },
expected: fingerprints[5:10], expected: fingerprints[5:10],
}, },
{ {
pairs: []metric.LabelPair{ pairs: []model.LabelPair{
{"label1", "test_1"}, {"label1", "test_1"},
{"label2", "test_2"}, {"label2", "test_2"},
}, },
@ -391,7 +391,7 @@ func TestRetentionCutoff(t *testing.T) {
s.WaitForIndexing() s.WaitForIndexing()
var fp model.Fingerprint var fp model.Fingerprint
for f := range s.fingerprintsForLabelPairs(metric.LabelPair{Name: "job", Value: "test"}) { for f := range s.fingerprintsForLabelPairs(model.LabelPair{Name: "job", Value: "test"}) {
fp = f fp = f
break break
} }
@ -455,7 +455,7 @@ func TestDropMetrics(t *testing.T) {
} }
s.WaitForIndexing() s.WaitForIndexing()
fps := s.fingerprintsForLabelPairs(metric.LabelPair{Name: model.MetricNameLabel, Value: "test"}) fps := s.fingerprintsForLabelPairs(model.LabelPair{Name: model.MetricNameLabel, Value: "test"})
if len(fps) != 2 { if len(fps) != 2 {
t.Fatalf("unexpected number of fingerprints: %d", len(fps)) t.Fatalf("unexpected number of fingerprints: %d", len(fps))
} }
@ -472,7 +472,7 @@ func TestDropMetrics(t *testing.T) {
s.DropMetricsForFingerprints(fpList[0]) s.DropMetricsForFingerprints(fpList[0])
s.WaitForIndexing() s.WaitForIndexing()
fps2 := s.fingerprintsForLabelPairs(metric.LabelPair{ fps2 := s.fingerprintsForLabelPairs(model.LabelPair{
Name: model.MetricNameLabel, Value: "test", Name: model.MetricNameLabel, Value: "test",
}) })
if len(fps2) != 1 { if len(fps2) != 1 {
@ -491,7 +491,7 @@ func TestDropMetrics(t *testing.T) {
s.DropMetricsForFingerprints(fpList...) s.DropMetricsForFingerprints(fpList...)
s.WaitForIndexing() s.WaitForIndexing()
fps3 := s.fingerprintsForLabelPairs(metric.LabelPair{ fps3 := s.fingerprintsForLabelPairs(model.LabelPair{
Name: model.MetricNameLabel, Value: "test", Name: model.MetricNameLabel, Value: "test",
}) })
if len(fps3) != 0 { if len(fps3) != 0 {

View file

@ -1,64 +0,0 @@
// 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 metric
import (
"github.com/prometheus/common/model"
)
// LabelPair pairs a name with a value.
type LabelPair struct {
Name model.LabelName
Value model.LabelValue
}
// Equal returns true iff both the Name and the Value of this LabelPair and o
// are equal.
func (l *LabelPair) Equal(o *LabelPair) bool {
switch {
case l.Name != o.Name:
return false
case l.Value != o.Value:
return false
default:
return true
}
}
// LabelPairs is a sortable slice of LabelPair pointers. It implements
// sort.Interface.
type LabelPairs []*LabelPair
func (l LabelPairs) Len() int {
return len(l)
}
func (l LabelPairs) Less(i, j int) bool {
switch {
case l[i].Name > l[j].Name:
return false
case l[i].Name < l[j].Name:
return true
case l[i].Value > l[j].Value:
return false
case l[i].Value < l[j].Value:
return true
default:
return false
}
}
func (l LabelPairs) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}

View file

@ -1,83 +0,0 @@
// 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 metric
import (
"sort"
"testing"
)
func testLabelPairs(t testing.TB) {
var scenarios = []struct {
in LabelPairs
out LabelPairs
}{
{
in: LabelPairs{
{
Name: "AAA",
Value: "aaa",
},
},
out: LabelPairs{
{
Name: "AAA",
Value: "aaa",
},
},
},
{
in: LabelPairs{
{
Name: "aaa",
Value: "aaa",
},
{
Name: "ZZZ",
Value: "aaa",
},
},
out: LabelPairs{
{
Name: "ZZZ",
Value: "aaa",
},
{
Name: "aaa",
Value: "aaa",
},
},
},
}
for i, scenario := range scenarios {
sort.Sort(scenario.in)
for j, expected := range scenario.out {
if !expected.Equal(scenario.in[j]) {
t.Errorf("%d.%d expected %s, got %s", i, j, expected, scenario.in[j])
}
}
}
}
func TestLabelPairs(t *testing.T) {
testLabelPairs(t)
}
func BenchmarkLabelPairs(b *testing.B) {
for i := 0; i < b.N; i++ {
testLabelPairs(b)
}
}