|
|
|
@ -16,97 +16,89 @@ package tsdb
|
|
|
|
|
import ( |
|
|
|
|
"context" |
|
|
|
|
"fmt" |
|
|
|
|
"io/ioutil" |
|
|
|
|
"math" |
|
|
|
|
"testing" |
|
|
|
|
"os" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require" |
|
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/model/labels" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func TestExample(t *testing.T) { |
|
|
|
|
func Example() { |
|
|
|
|
// Create a random dir to work in. Open() doesn't require a pre-existing dir, but
|
|
|
|
|
// we want to make sure not to make a mess where we shouldn't.
|
|
|
|
|
dir := t.TempDir() |
|
|
|
|
dir, err := ioutil.TempDir("", "tsdb-test") |
|
|
|
|
noErr(err) |
|
|
|
|
|
|
|
|
|
// Open a TSDB for reading and/or writing.
|
|
|
|
|
db, err := Open(dir, nil, nil, DefaultOptions(), nil) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
noErr(err) |
|
|
|
|
|
|
|
|
|
// Open an appender for writing.
|
|
|
|
|
app := db.Appender(context.Background()) |
|
|
|
|
|
|
|
|
|
lbls := labels.FromStrings("foo", "bar") |
|
|
|
|
var appendedSamples []sample |
|
|
|
|
series := labels.FromStrings("foo", "bar") |
|
|
|
|
|
|
|
|
|
// Ref is 0 for the first append since we don't know the reference for the series.
|
|
|
|
|
ts, v := time.Now().Unix(), 123.0 |
|
|
|
|
ref, err := app.Append(0, lbls, ts, v) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
appendedSamples = append(appendedSamples, sample{ts, v}) |
|
|
|
|
ref, err := app.Append(0, series, time.Now().Unix(), 123) |
|
|
|
|
noErr(err) |
|
|
|
|
|
|
|
|
|
// Another append for a second later.
|
|
|
|
|
// Re-using the ref from above since it's the same series, makes append faster.
|
|
|
|
|
time.Sleep(time.Second) |
|
|
|
|
ts, v = time.Now().Unix(), 124 |
|
|
|
|
_, err = app.Append(ref, lbls, ts, v) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
appendedSamples = append(appendedSamples, sample{ts, v}) |
|
|
|
|
_, err = app.Append(ref, series, time.Now().Unix(), 124) |
|
|
|
|
noErr(err) |
|
|
|
|
|
|
|
|
|
// Commit to storage.
|
|
|
|
|
err = app.Commit() |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
noErr(err) |
|
|
|
|
|
|
|
|
|
// In case you want to do more appends after app.Commit(),
|
|
|
|
|
// you need a new appender.
|
|
|
|
|
// app = db.Appender(context.Background())
|
|
|
|
|
//
|
|
|
|
|
app = db.Appender(context.Background()) |
|
|
|
|
// ... adding more samples.
|
|
|
|
|
//
|
|
|
|
|
// Commit to storage.
|
|
|
|
|
// err = app.Commit()
|
|
|
|
|
// require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Open a querier for reading.
|
|
|
|
|
querier, err := db.Querier(context.Background(), math.MinInt64, math.MaxInt64) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
noErr(err) |
|
|
|
|
ss := querier.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")) |
|
|
|
|
var queriedSamples []sample |
|
|
|
|
|
|
|
|
|
for ss.Next() { |
|
|
|
|
series := ss.At() |
|
|
|
|
fmt.Println("series:", series.Labels().String()) |
|
|
|
|
|
|
|
|
|
it := series.Iterator() |
|
|
|
|
for it.Next() { |
|
|
|
|
ts, v := it.At() |
|
|
|
|
fmt.Println("sample", ts, v) |
|
|
|
|
queriedSamples = append(queriedSamples, sample{ts, v}) |
|
|
|
|
_, v := it.At() // We ignore the timestamp here, only to have a predictable output we can test against (below)
|
|
|
|
|
fmt.Println("sample", v) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
require.NoError(t, it.Err()) |
|
|
|
|
fmt.Println("it.Err():", it.Err()) |
|
|
|
|
} |
|
|
|
|
require.NoError(t, ss.Err()) |
|
|
|
|
fmt.Println("ss.Err():", ss.Err()) |
|
|
|
|
ws := ss.Warnings() |
|
|
|
|
if len(ws) > 0 { |
|
|
|
|
fmt.Println("warnings:", ws) |
|
|
|
|
} |
|
|
|
|
err = querier.Close() |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
noErr(err) |
|
|
|
|
|
|
|
|
|
// Clean up any last resources when done.
|
|
|
|
|
err = db.Close() |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
require.Equal(t, appendedSamples, queriedSamples) |
|
|
|
|
noErr(err) |
|
|
|
|
err = os.RemoveAll(dir) |
|
|
|
|
noErr(err) |
|
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
|
// series: {foo="bar"}
|
|
|
|
|
// sample <ts1> 123
|
|
|
|
|
// sample <ts2> 124
|
|
|
|
|
// sample 123
|
|
|
|
|
// sample 124
|
|
|
|
|
// it.Err(): <nil>
|
|
|
|
|
// ss.Err(): <nil>
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func noErr(err error) { |
|
|
|
|
if err != nil { |
|
|
|
|
panic(err) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|