2021-11-17 02:21:27 -08:00
|
|
|
// Copyright 2021 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 tsdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
2022-01-11 03:54:03 -08:00
|
|
|
"os"
|
2021-11-17 02:21:27 -08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
2021-11-28 23:54:23 -08:00
|
|
|
"github.com/prometheus/prometheus/tsdb/chunkenc"
|
2021-11-17 02:21:27 -08:00
|
|
|
)
|
|
|
|
|
2022-01-11 03:54:03 -08:00
|
|
|
func Example() {
|
2021-11-17 02:21:27 -08:00
|
|
|
// 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.
|
2022-04-27 02:24:36 -07:00
|
|
|
dir, err := os.MkdirTemp("", "tsdb-test")
|
2022-01-11 03:54:03 -08:00
|
|
|
noErr(err)
|
2021-11-17 02:21:27 -08:00
|
|
|
|
|
|
|
// Open a TSDB for reading and/or writing.
|
|
|
|
db, err := Open(dir, nil, nil, DefaultOptions(), nil)
|
2022-01-11 03:54:03 -08:00
|
|
|
noErr(err)
|
2021-11-17 02:21:27 -08:00
|
|
|
|
|
|
|
// Open an appender for writing.
|
|
|
|
app := db.Appender(context.Background())
|
|
|
|
|
2022-01-11 03:54:03 -08:00
|
|
|
series := labels.FromStrings("foo", "bar")
|
2021-11-17 02:21:27 -08:00
|
|
|
|
|
|
|
// Ref is 0 for the first append since we don't know the reference for the series.
|
2022-01-11 03:54:03 -08:00
|
|
|
ref, err := app.Append(0, series, time.Now().Unix(), 123)
|
|
|
|
noErr(err)
|
2021-11-17 02:21:27 -08:00
|
|
|
|
|
|
|
// 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)
|
2022-01-11 03:54:03 -08:00
|
|
|
_, err = app.Append(ref, series, time.Now().Unix(), 124)
|
|
|
|
noErr(err)
|
2021-11-17 02:21:27 -08:00
|
|
|
|
|
|
|
// Commit to storage.
|
|
|
|
err = app.Commit()
|
2022-01-11 03:54:03 -08:00
|
|
|
noErr(err)
|
2021-11-17 02:21:27 -08:00
|
|
|
|
|
|
|
// In case you want to do more appends after app.Commit(),
|
|
|
|
// you need a new appender.
|
2022-01-11 03:54:03 -08:00
|
|
|
app = db.Appender(context.Background())
|
2021-11-17 02:21:27 -08:00
|
|
|
// ... adding more samples.
|
|
|
|
|
|
|
|
// Open a querier for reading.
|
|
|
|
querier, err := db.Querier(context.Background(), math.MinInt64, math.MaxInt64)
|
2022-01-11 03:54:03 -08:00
|
|
|
noErr(err)
|
2021-11-17 02:21:27 -08:00
|
|
|
ss := querier.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar"))
|
2022-01-11 03:54:03 -08:00
|
|
|
|
2021-11-17 02:21:27 -08:00
|
|
|
for ss.Next() {
|
|
|
|
series := ss.At()
|
|
|
|
fmt.Println("series:", series.Labels().String())
|
|
|
|
|
2022-09-20 10:16:45 -07:00
|
|
|
it := series.Iterator(nil)
|
2021-11-28 23:54:23 -08:00
|
|
|
for it.Next() == chunkenc.ValFloat {
|
2022-01-11 03:54:03 -08:00
|
|
|
_, v := it.At() // We ignore the timestamp here, only to have a predictable output we can test against (below)
|
|
|
|
fmt.Println("sample", v)
|
2021-11-17 02:21:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("it.Err():", it.Err())
|
|
|
|
}
|
|
|
|
fmt.Println("ss.Err():", ss.Err())
|
|
|
|
ws := ss.Warnings()
|
|
|
|
if len(ws) > 0 {
|
|
|
|
fmt.Println("warnings:", ws)
|
|
|
|
}
|
|
|
|
err = querier.Close()
|
2022-01-11 03:54:03 -08:00
|
|
|
noErr(err)
|
2021-11-17 02:21:27 -08:00
|
|
|
|
|
|
|
// Clean up any last resources when done.
|
|
|
|
err = db.Close()
|
2022-01-11 03:54:03 -08:00
|
|
|
noErr(err)
|
|
|
|
err = os.RemoveAll(dir)
|
|
|
|
noErr(err)
|
2021-11-17 02:21:27 -08:00
|
|
|
|
|
|
|
// Output:
|
|
|
|
// series: {foo="bar"}
|
2022-01-11 03:54:03 -08:00
|
|
|
// sample 123
|
|
|
|
// sample 124
|
2021-11-17 02:21:27 -08:00
|
|
|
// it.Err(): <nil>
|
|
|
|
// ss.Err(): <nil>
|
|
|
|
}
|
2022-01-11 03:54:03 -08:00
|
|
|
|
|
|
|
func noErr(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|