Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 20f2e43

Browse files
committedJun 29, 2017
Create context inside lazy iterator
1 parent 4e290bb commit 20f2e43

File tree

4 files changed

+93
-33
lines changed

4 files changed

+93
-33
lines changed
 

‎pkg/chunk/chunk_store.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,11 @@ outer:
262262
}
263263
}
264264

265-
newIterator, err := NewLazySeriesIterator(c, metric, from, through)
265+
orgID, err := user.ExtractOrgID(ctx)
266+
if err != nil {
267+
return nil, err
268+
}
269+
newIterator, err := NewLazySeriesIterator(c, metric, from, through, orgID)
266270
if err != nil {
267271
return nil, err
268272
}

‎pkg/chunk/chunk_store_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ func TestChunkStore_Get_lazy(t *testing.T) {
299299
// Create expected iterators with current schema store
300300
var expectedIterators []local.SeriesIterator
301301
for _, expectedMetric := range tc.expectedIteratorMetrics {
302-
newIterator, err := NewLazySeriesIterator(store, expectedMetric, from, now)
302+
newIterator, err := NewLazySeriesIterator(store, expectedMetric, from, now, userID)
303303
require.NoError(t, err)
304304
expectedIterators = append(expectedIterators, newIterator)
305305
}

‎pkg/chunk/iterator.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/prometheus/common/model"
1010
"github.com/prometheus/prometheus/storage/local"
1111
"github.com/prometheus/prometheus/storage/metric"
12+
"github.com/weaveworks/common/user"
1213
)
1314

1415
// LazySeriesIterator is a struct and not just a renamed type because otherwise the Metric
@@ -27,6 +28,7 @@ type LazySeriesIterator struct {
2728
// be fetched. Use sync.Once to ensure the iterator is only created once.
2829
sampleSeriesIterator *local.SeriesIterator
2930
onceCreateIterator sync.Once
31+
orgID string
3032
}
3133

3234
type byMatcherLabel metric.LabelMatchers
@@ -36,14 +38,18 @@ func (lms byMatcherLabel) Swap(i, j int) { lms[i], lms[j] = lms[j], lms[i]
3638
func (lms byMatcherLabel) Less(i, j int) bool { return lms[i].Name < lms[j].Name }
3739

3840
// NewLazySeriesIterator creates a LazySeriesIterator.
39-
func NewLazySeriesIterator(chunkStore *Store, seriesMetric model.Metric, from model.Time, through model.Time) (*LazySeriesIterator, error) {
41+
func NewLazySeriesIterator(chunkStore *Store, seriesMetric model.Metric, from model.Time, through model.Time, orgID string) (*LazySeriesIterator, error) {
4042
_, ok := seriesMetric[model.MetricNameLabel]
4143
if !ok {
4244
return nil, fmt.Errorf("series does not have a metric name")
4345
}
4446

4547
var matchers metric.LabelMatchers
4648
for labelName, labelValue := range seriesMetric {
49+
if labelName == "__name__" {
50+
continue
51+
}
52+
4753
matcher, err := metric.NewLabelMatcher(metric.Equal, labelName, labelValue)
4854
if err != nil {
4955
return nil, err
@@ -58,6 +64,7 @@ func NewLazySeriesIterator(chunkStore *Store, seriesMetric model.Metric, from mo
5864
from: from,
5965
through: through,
6066
matchers: matchers,
67+
orgID: orgID,
6168
}, nil
6269
}
6370

@@ -87,6 +94,7 @@ func (it *LazySeriesIterator) RangeValues(in metric.Interval) []model.SamplePair
8794
})
8895
if err != nil {
8996
// TODO: Handle error.
97+
fmt.Printf("ERROR %+v", err)
9098
return nil
9199
}
92100
return (*it.sampleSeriesIterator).RangeValues(in)
@@ -101,7 +109,7 @@ func (it *LazySeriesIterator) createSampleSeriesIterator() error {
101109
return fmt.Errorf("series does not have a metric name")
102110
}
103111

104-
ctx := context.Background()
112+
ctx := user.InjectOrgID(context.Background(), it.orgID)
105113
sampleSeriesIterators, err := it.chunkStore.getMetricNameIterators(ctx, it.from, it.through, it.matchers, metricName)
106114
if err != nil {
107115
return err

‎pkg/chunk/iterator_test.go

+77-29
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package chunk
22

33
import (
4+
"context"
45
"testing"
56

67
"github.com/prometheus/common/model"
78
"github.com/prometheus/prometheus/storage/metric"
89
"github.com/stretchr/testify/require"
10+
"github.com/weaveworks/common/user"
911
)
1012

1113
func TestLazySeriesIterator_Metric(t *testing.T) {
1214
store := newTestChunkStore(t, StoreConfig{})
1315
now := model.Now()
1416
sampleMetric := model.Metric{model.MetricNameLabel: "foo"}
15-
iterator, err := NewLazySeriesIterator(store, sampleMetric, now, now)
17+
iterator, err := NewLazySeriesIterator(store, sampleMetric, now, now, userID)
1618
require.NoError(t, err)
1719
for _, c := range []struct {
1820
iterator *LazySeriesIterator
@@ -29,45 +31,91 @@ func TestLazySeriesIterator_Metric(t *testing.T) {
2931
}
3032

3133
func TestLazySeriesIterator_ValueAtOrBeforeTime(t *testing.T) {
32-
store := newTestChunkStore(t, StoreConfig{})
3334
now := model.Now()
35+
ctx := user.InjectOrgID(context.Background(), userID)
36+
3437
sampleMetric := model.Metric{model.MetricNameLabel: "foo"}
35-
iterator, err := NewLazySeriesIterator(store, sampleMetric, now, now)
38+
dummyChunk := dummyChunkFor(sampleMetric)
39+
dummySamples, err := dummyChunk.Samples()
3640
require.NoError(t, err)
37-
for _, c := range []struct {
38-
iterator *LazySeriesIterator
39-
timestamp model.Time
40-
expectedSample model.SamplePair
41+
42+
schemas := []struct {
43+
name string
44+
fn func(cfg SchemaConfig) Schema
4145
}{
42-
{
43-
iterator: iterator,
44-
timestamp: now,
45-
expectedSample: model.ZeroSamplePair,
46-
},
47-
} {
48-
sample := c.iterator.ValueAtOrBeforeTime(c.timestamp)
49-
require.Equal(t, c.expectedSample, sample)
46+
{"v8 schema", v8Schema},
47+
}
48+
49+
for _, schema := range schemas {
50+
// Create store with the dummy chunk.
51+
store := newTestChunkStore(t, StoreConfig{schemaFactory: schema.fn})
52+
store.Put(ctx, []Chunk{dummyChunk})
53+
54+
// Create the lazy series iterator
55+
iterator, err := NewLazySeriesIterator(store, sampleMetric, now, now, userID)
56+
require.NoError(t, err)
57+
for _, tc := range []struct {
58+
iterator *LazySeriesIterator
59+
timestamp model.Time
60+
expectedSample model.SamplePair
61+
}{
62+
{
63+
iterator: iterator,
64+
timestamp: now,
65+
expectedSample: dummySamples[0],
66+
},
67+
} {
68+
// sampleSeriesIterator should be created lazily only when RangeValues is called.
69+
require.Nil(t, tc.iterator.sampleSeriesIterator)
70+
sample := tc.iterator.ValueAtOrBeforeTime(tc.timestamp)
71+
require.NotNil(t, tc.iterator.sampleSeriesIterator)
72+
73+
require.Equal(t, tc.expectedSample, sample)
74+
}
5075
}
5176
}
5277

5378
func TestLazySeriesIterator_RangeValues(t *testing.T) {
54-
store := newTestChunkStore(t, StoreConfig{})
5579
now := model.Now()
80+
ctx := user.InjectOrgID(context.Background(), userID)
81+
5682
sampleMetric := model.Metric{model.MetricNameLabel: "foo"}
57-
iterator, err := NewLazySeriesIterator(store, sampleMetric, now, now)
83+
dummyChunk := dummyChunkFor(sampleMetric)
84+
dummySamples, err := dummyChunk.Samples()
5885
require.NoError(t, err)
59-
for _, c := range []struct {
60-
iterator *LazySeriesIterator
61-
interval metric.Interval
62-
expectedSamples []model.SamplePair
86+
87+
schemas := []struct {
88+
name string
89+
fn func(cfg SchemaConfig) Schema
6390
}{
64-
{
65-
iterator: iterator,
66-
interval: metric.Interval{OldestInclusive: now, NewestInclusive: now},
67-
expectedSamples: nil,
68-
},
69-
} {
70-
samples := c.iterator.RangeValues(c.interval)
71-
require.Equal(t, c.expectedSamples, samples)
91+
{"v8 schema", v8Schema},
92+
}
93+
94+
for _, schema := range schemas {
95+
// Create store with the dummy chunk.
96+
store := newTestChunkStore(t, StoreConfig{schemaFactory: schema.fn})
97+
store.Put(ctx, []Chunk{dummyChunk})
98+
99+
// Create the lazy series iterator
100+
iterator, err := NewLazySeriesIterator(store, sampleMetric, now, now, userID)
101+
require.NoError(t, err)
102+
for _, tc := range []struct {
103+
iterator *LazySeriesIterator
104+
interval metric.Interval
105+
expectedSamples []model.SamplePair
106+
}{
107+
{
108+
iterator: iterator,
109+
interval: metric.Interval{OldestInclusive: now, NewestInclusive: now},
110+
expectedSamples: dummySamples,
111+
},
112+
} {
113+
// sampleSeriesIterator should be created lazily only when RangeValues is called.
114+
require.Nil(t, tc.iterator.sampleSeriesIterator)
115+
samples := tc.iterator.RangeValues(tc.interval)
116+
require.NotNil(t, tc.iterator.sampleSeriesIterator)
117+
118+
require.Equal(t, tc.expectedSamples, samples)
119+
}
72120
}
73121
}

0 commit comments

Comments
 (0)
Please sign in to comment.