mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
228 lines
5.7 KiB
228 lines
5.7 KiB
7 years ago
|
// Copyright 2018 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.
|
||
|
|
||
5 years ago
|
package record
|
||
7 years ago
|
|
||
|
import (
|
||
|
"math"
|
||
|
"sort"
|
||
|
|
||
|
"github.com/pkg/errors"
|
||
5 years ago
|
"github.com/prometheus/prometheus/tsdb/encoding"
|
||
|
"github.com/prometheus/prometheus/tsdb/labels"
|
||
5 years ago
|
"github.com/prometheus/prometheus/tsdb/tombstones"
|
||
7 years ago
|
)
|
||
|
|
||
5 years ago
|
// Type represents the data type of a record.
|
||
|
type Type uint8
|
||
7 years ago
|
|
||
|
const (
|
||
5 years ago
|
// Invalid is returned for unrecognised WAL record types.
|
||
|
Invalid Type = 255
|
||
|
// Series is used to match WAL records of type Series.
|
||
|
Series Type = 1
|
||
|
// Samples is used to match WAL records of type Samples.
|
||
|
Samples Type = 2
|
||
|
// Tombstones is used to match WAL records of type Tombstones.
|
||
|
Tombstones Type = 3
|
||
7 years ago
|
)
|
||
|
|
||
5 years ago
|
var (
|
||
|
// ErrNotFound is returned if a looked up resource was not found. Duplicate ErrNotFound from head.go.
|
||
|
ErrNotFound = errors.New("not found")
|
||
|
)
|
||
|
|
||
|
// RefSeries is the series labels with the series ID.
|
||
|
type RefSeries struct {
|
||
|
Ref uint64
|
||
|
Labels labels.Labels
|
||
|
}
|
||
|
|
||
|
// RefSample is a timestamp/value pair associated with a reference to a series.
|
||
|
type RefSample struct {
|
||
|
Ref uint64
|
||
|
T int64
|
||
|
V float64
|
||
|
}
|
||
|
|
||
|
// Decoder decodes series, sample, and tombstone records.
|
||
7 years ago
|
// The zero value is ready to use.
|
||
5 years ago
|
type Decoder struct {
|
||
7 years ago
|
}
|
||
|
|
||
|
// Type returns the type of the record.
|
||
5 years ago
|
// Returns RecordInvalid if no valid record type is found.
|
||
|
func (d *Decoder) Type(rec []byte) Type {
|
||
7 years ago
|
if len(rec) < 1 {
|
||
5 years ago
|
return Invalid
|
||
7 years ago
|
}
|
||
5 years ago
|
switch t := Type(rec[0]); t {
|
||
|
case Series, Samples, Tombstones:
|
||
7 years ago
|
return t
|
||
|
}
|
||
5 years ago
|
return Invalid
|
||
7 years ago
|
}
|
||
|
|
||
|
// Series appends series in rec to the given slice.
|
||
5 years ago
|
func (d *Decoder) Series(rec []byte, series []RefSeries) ([]RefSeries, error) {
|
||
6 years ago
|
dec := encoding.Decbuf{B: rec}
|
||
7 years ago
|
|
||
5 years ago
|
if Type(dec.Byte()) != Series {
|
||
7 years ago
|
return nil, errors.New("invalid record type")
|
||
|
}
|
||
6 years ago
|
for len(dec.B) > 0 && dec.Err() == nil {
|
||
|
ref := dec.Be64()
|
||
7 years ago
|
|
||
6 years ago
|
lset := make(labels.Labels, dec.Uvarint())
|
||
7 years ago
|
|
||
|
for i := range lset {
|
||
6 years ago
|
lset[i].Name = dec.UvarintStr()
|
||
|
lset[i].Value = dec.UvarintStr()
|
||
7 years ago
|
}
|
||
|
sort.Sort(lset)
|
||
|
|
||
|
series = append(series, RefSeries{
|
||
|
Ref: ref,
|
||
|
Labels: lset,
|
||
|
})
|
||
|
}
|
||
6 years ago
|
if dec.Err() != nil {
|
||
|
return nil, dec.Err()
|
||
7 years ago
|
}
|
||
6 years ago
|
if len(dec.B) > 0 {
|
||
|
return nil, errors.Errorf("unexpected %d bytes left in entry", len(dec.B))
|
||
7 years ago
|
}
|
||
|
return series, nil
|
||
|
}
|
||
|
|
||
|
// Samples appends samples in rec to the given slice.
|
||
5 years ago
|
func (d *Decoder) Samples(rec []byte, samples []RefSample) ([]RefSample, error) {
|
||
6 years ago
|
dec := encoding.Decbuf{B: rec}
|
||
7 years ago
|
|
||
5 years ago
|
if Type(dec.Byte()) != Samples {
|
||
7 years ago
|
return nil, errors.New("invalid record type")
|
||
|
}
|
||
6 years ago
|
if dec.Len() == 0 {
|
||
7 years ago
|
return samples, nil
|
||
|
}
|
||
|
var (
|
||
6 years ago
|
baseRef = dec.Be64()
|
||
|
baseTime = dec.Be64int64()
|
||
7 years ago
|
)
|
||
6 years ago
|
for len(dec.B) > 0 && dec.Err() == nil {
|
||
|
dref := dec.Varint64()
|
||
|
dtime := dec.Varint64()
|
||
|
val := dec.Be64()
|
||
7 years ago
|
|
||
|
samples = append(samples, RefSample{
|
||
|
Ref: uint64(int64(baseRef) + dref),
|
||
|
T: baseTime + dtime,
|
||
|
V: math.Float64frombits(val),
|
||
|
})
|
||
|
}
|
||
|
|
||
6 years ago
|
if dec.Err() != nil {
|
||
|
return nil, errors.Wrapf(dec.Err(), "decode error after %d samples", len(samples))
|
||
7 years ago
|
}
|
||
6 years ago
|
if len(dec.B) > 0 {
|
||
|
return nil, errors.Errorf("unexpected %d bytes left in entry", len(dec.B))
|
||
7 years ago
|
}
|
||
|
return samples, nil
|
||
|
}
|
||
|
|
||
|
// Tombstones appends tombstones in rec to the given slice.
|
||
5 years ago
|
func (d *Decoder) Tombstones(rec []byte, tstones []tombstones.Stone) ([]tombstones.Stone, error) {
|
||
6 years ago
|
dec := encoding.Decbuf{B: rec}
|
||
7 years ago
|
|
||
5 years ago
|
if Type(dec.Byte()) != Tombstones {
|
||
7 years ago
|
return nil, errors.New("invalid record type")
|
||
|
}
|
||
6 years ago
|
for dec.Len() > 0 && dec.Err() == nil {
|
||
5 years ago
|
tstones = append(tstones, tombstones.Stone{
|
||
|
Ref: dec.Be64(),
|
||
|
Intervals: tombstones.Intervals{
|
||
6 years ago
|
{Mint: dec.Varint64(), Maxt: dec.Varint64()},
|
||
7 years ago
|
},
|
||
|
})
|
||
|
}
|
||
6 years ago
|
if dec.Err() != nil {
|
||
|
return nil, dec.Err()
|
||
7 years ago
|
}
|
||
6 years ago
|
if len(dec.B) > 0 {
|
||
|
return nil, errors.Errorf("unexpected %d bytes left in entry", len(dec.B))
|
||
7 years ago
|
}
|
||
|
return tstones, nil
|
||
|
}
|
||
|
|
||
5 years ago
|
// Encoder encodes series, sample, and tombstones records.
|
||
7 years ago
|
// The zero value is ready to use.
|
||
5 years ago
|
type Encoder struct {
|
||
7 years ago
|
}
|
||
|
|
||
|
// Series appends the encoded series to b and returns the resulting slice.
|
||
5 years ago
|
func (e *Encoder) Series(series []RefSeries, b []byte) []byte {
|
||
6 years ago
|
buf := encoding.Encbuf{B: b}
|
||
5 years ago
|
buf.PutByte(byte(Series))
|
||
7 years ago
|
|
||
|
for _, s := range series {
|
||
6 years ago
|
buf.PutBE64(s.Ref)
|
||
|
buf.PutUvarint(len(s.Labels))
|
||
7 years ago
|
|
||
|
for _, l := range s.Labels {
|
||
6 years ago
|
buf.PutUvarintStr(l.Name)
|
||
|
buf.PutUvarintStr(l.Value)
|
||
7 years ago
|
}
|
||
|
}
|
||
6 years ago
|
return buf.Get()
|
||
7 years ago
|
}
|
||
|
|
||
|
// Samples appends the encoded samples to b and returns the resulting slice.
|
||
5 years ago
|
func (e *Encoder) Samples(samples []RefSample, b []byte) []byte {
|
||
6 years ago
|
buf := encoding.Encbuf{B: b}
|
||
5 years ago
|
buf.PutByte(byte(Samples))
|
||
7 years ago
|
|
||
|
if len(samples) == 0 {
|
||
6 years ago
|
return buf.Get()
|
||
7 years ago
|
}
|
||
|
|
||
|
// Store base timestamp and base reference number of first sample.
|
||
|
// All samples encode their timestamp and ref as delta to those.
|
||
|
first := samples[0]
|
||
|
|
||
6 years ago
|
buf.PutBE64(first.Ref)
|
||
|
buf.PutBE64int64(first.T)
|
||
7 years ago
|
|
||
|
for _, s := range samples {
|
||
6 years ago
|
buf.PutVarint64(int64(s.Ref) - int64(first.Ref))
|
||
|
buf.PutVarint64(s.T - first.T)
|
||
|
buf.PutBE64(math.Float64bits(s.V))
|
||
7 years ago
|
}
|
||
6 years ago
|
return buf.Get()
|
||
7 years ago
|
}
|
||
|
|
||
|
// Tombstones appends the encoded tombstones to b and returns the resulting slice.
|
||
5 years ago
|
func (e *Encoder) Tombstones(tstones []tombstones.Stone, b []byte) []byte {
|
||
6 years ago
|
buf := encoding.Encbuf{B: b}
|
||
5 years ago
|
buf.PutByte(byte(Tombstones))
|
||
7 years ago
|
|
||
|
for _, s := range tstones {
|
||
5 years ago
|
for _, iv := range s.Intervals {
|
||
|
buf.PutBE64(s.Ref)
|
||
6 years ago
|
buf.PutVarint64(iv.Mint)
|
||
|
buf.PutVarint64(iv.Maxt)
|
||
7 years ago
|
}
|
||
|
}
|
||
6 years ago
|
return buf.Get()
|
||
7 years ago
|
}
|