2013-02-07 10:38:01 +00:00
|
|
|
// Copyright 2013 Prometheus Team
|
2012-11-26 19:11:34 +00:00
|
|
|
// 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.
|
|
|
|
|
2012-11-26 18:56:51 +00:00
|
|
|
package model
|
2012-11-24 11:33:34 +00:00
|
|
|
|
|
|
|
import (
|
2013-03-21 11:07:13 +00:00
|
|
|
"bytes"
|
2013-04-21 16:15:56 +00:00
|
|
|
"code.google.com/p/goprotobuf/proto"
|
2013-01-17 23:07:00 +00:00
|
|
|
"fmt"
|
2013-04-22 11:30:16 +00:00
|
|
|
dto "github.com/prometheus/prometheus/model/generated"
|
2013-02-08 12:05:35 +00:00
|
|
|
"sort"
|
2013-06-06 13:12:37 +00:00
|
|
|
"strings"
|
2012-11-24 11:33:34 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-02-08 12:05:35 +00:00
|
|
|
const (
|
|
|
|
// XXX: Re-evaluate down the road.
|
2013-02-08 17:03:26 +00:00
|
|
|
reservedDelimiter = `"`
|
2013-02-08 12:05:35 +00:00
|
|
|
)
|
|
|
|
|
2012-12-07 10:55:43 +00:00
|
|
|
// A LabelSet is a collection of LabelName and LabelValue pairs. The LabelSet
|
|
|
|
// may be fully-qualified down to the point where it may resolve to a single
|
|
|
|
// Metric in the data store or not. All operations that occur within the realm
|
|
|
|
// of a LabelSet can emit a vector of Metric entities to which the LabelSet may
|
|
|
|
// match.
|
|
|
|
type LabelSet map[LabelName]LabelValue
|
|
|
|
|
2013-04-26 09:52:26 +00:00
|
|
|
// Helper function to non-destructively merge two label sets.
|
|
|
|
func (l LabelSet) Merge(other LabelSet) LabelSet {
|
|
|
|
result := make(LabelSet, len(l))
|
|
|
|
|
|
|
|
for k, v := range l {
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range other {
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2013-03-21 11:07:13 +00:00
|
|
|
func (l LabelSet) String() string {
|
2013-06-06 13:12:37 +00:00
|
|
|
labelStrings := make([]string, 0, len(l))
|
|
|
|
for label, value := range l {
|
2013-06-11 09:01:36 +00:00
|
|
|
labelStrings = append(labelStrings, fmt.Sprintf("%s=%q", label, value))
|
2013-03-21 11:07:13 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 09:01:36 +00:00
|
|
|
switch len(labelStrings) {
|
|
|
|
case 0:
|
|
|
|
return ""
|
|
|
|
default:
|
|
|
|
sort.Strings(labelStrings)
|
|
|
|
return fmt.Sprintf("{%s}", strings.Join(labelStrings, ", "))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Metric) String() string {
|
|
|
|
metricName, ok := m[MetricNameLabel]
|
|
|
|
if !ok {
|
|
|
|
panic("Tried to print metric without name")
|
|
|
|
}
|
|
|
|
labelStrings := make([]string, 0, len(m)-1)
|
|
|
|
for label, value := range m {
|
|
|
|
if label != MetricNameLabel {
|
|
|
|
labelStrings = append(labelStrings, fmt.Sprintf("%s=%q", label, value))
|
|
|
|
}
|
|
|
|
}
|
2013-03-21 11:07:13 +00:00
|
|
|
|
2013-06-11 09:01:36 +00:00
|
|
|
switch len(labelStrings) {
|
|
|
|
case 0:
|
|
|
|
return string(metricName)
|
|
|
|
default:
|
|
|
|
sort.Strings(labelStrings)
|
|
|
|
return fmt.Sprintf("%s{%s}", metricName, strings.Join(labelStrings, ", "))
|
|
|
|
}
|
2013-03-21 11:07:13 +00:00
|
|
|
}
|
|
|
|
|
2013-05-16 05:38:31 +00:00
|
|
|
func (l LabelSet) ToMetric() Metric {
|
|
|
|
metric := Metric{}
|
2013-04-24 09:51:40 +00:00
|
|
|
for label, value := range l {
|
|
|
|
metric[label] = value
|
|
|
|
}
|
2013-05-16 05:38:31 +00:00
|
|
|
return metric
|
2013-04-24 09:51:40 +00:00
|
|
|
}
|
|
|
|
|
2013-06-13 14:10:05 +00:00
|
|
|
func (m Metric) ToLabelSet() LabelSet {
|
|
|
|
labels := LabelSet{}
|
|
|
|
for label, value := range m {
|
|
|
|
labels[label] = value
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
2012-12-07 10:55:43 +00:00
|
|
|
// A Metric is similar to a LabelSet, but the key difference is that a Metric is
|
|
|
|
// a singleton and refers to one and only one stream of samples.
|
|
|
|
type Metric map[LabelName]LabelValue
|
|
|
|
|
|
|
|
// A SampleValue is a representation of a value for a given sample at a given
|
2013-04-21 16:15:56 +00:00
|
|
|
// time.
|
|
|
|
type SampleValue float64
|
|
|
|
|
2013-04-24 09:02:51 +00:00
|
|
|
func (s SampleValue) Equal(o SampleValue) bool {
|
|
|
|
return s == o
|
|
|
|
}
|
|
|
|
|
2013-04-21 16:15:56 +00:00
|
|
|
func (s SampleValue) ToDTO() *float64 {
|
|
|
|
return proto.Float64(float64(s))
|
|
|
|
}
|
2012-11-24 11:33:34 +00:00
|
|
|
|
2013-01-13 18:56:48 +00:00
|
|
|
func (v SampleValue) MarshalJSON() ([]byte, error) {
|
2013-04-05 16:03:45 +00:00
|
|
|
return []byte(fmt.Sprintf(`"%f"`, v)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v SampleValue) String() string {
|
|
|
|
return fmt.Sprint(float64(v))
|
2013-01-13 18:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s SamplePair) MarshalJSON() ([]byte, error) {
|
2013-01-17 23:07:00 +00:00
|
|
|
return []byte(fmt.Sprintf("{\"Value\": \"%f\", \"Timestamp\": %d}", s.Value, s.Timestamp.Unix())), nil
|
2013-01-13 18:56:48 +00:00
|
|
|
}
|
|
|
|
|
2012-12-12 11:13:27 +00:00
|
|
|
type SamplePair struct {
|
2012-11-24 11:33:34 +00:00
|
|
|
Value SampleValue
|
|
|
|
Timestamp time.Time
|
|
|
|
}
|
|
|
|
|
2013-04-24 10:42:58 +00:00
|
|
|
func (s SamplePair) Equal(o SamplePair) bool {
|
|
|
|
return s.Value.Equal(o.Value) && s.Timestamp.Equal(o.Timestamp)
|
2013-04-24 09:02:51 +00:00
|
|
|
}
|
|
|
|
|
2013-04-05 16:03:45 +00:00
|
|
|
func (s SamplePair) ToDTO() (out *dto.SampleValueSeries_Value) {
|
|
|
|
out = &dto.SampleValueSeries_Value{
|
|
|
|
Timestamp: proto.Int64(s.Timestamp.Unix()),
|
|
|
|
Value: s.Value.ToDTO(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s SamplePair) String() string {
|
|
|
|
return fmt.Sprintf("SamplePair at %s of %s", s.Timestamp, s.Value)
|
|
|
|
}
|
|
|
|
|
2013-02-08 17:03:26 +00:00
|
|
|
type Values []SamplePair
|
|
|
|
|
|
|
|
func (v Values) Len() int {
|
|
|
|
return len(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Values) Less(i, j int) bool {
|
|
|
|
return v[i].Timestamp.Before(v[j].Timestamp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Values) Swap(i, j int) {
|
|
|
|
v[i], v[j] = v[j], v[i]
|
|
|
|
}
|
|
|
|
|
2013-04-18 23:00:57 +00:00
|
|
|
// FirstTimeAfter indicates whether the first sample of a set is after a given
|
|
|
|
// timestamp.
|
|
|
|
func (v Values) FirstTimeAfter(t time.Time) bool {
|
|
|
|
return v[0].Timestamp.After(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LastTimeBefore indicates whether the last sample of a set is before a given
|
|
|
|
// timestamp.
|
|
|
|
func (v Values) LastTimeBefore(t time.Time) bool {
|
|
|
|
return v[len(v)-1].Timestamp.Before(t)
|
|
|
|
}
|
|
|
|
|
2013-03-19 02:04:25 +00:00
|
|
|
// InsideInterval indicates whether a given range of sorted values could contain
|
|
|
|
// a value for a given time.
|
2013-05-16 05:38:31 +00:00
|
|
|
func (v Values) InsideInterval(t time.Time) bool {
|
|
|
|
switch {
|
|
|
|
case v.Len() == 0:
|
|
|
|
return false
|
|
|
|
case t.Before(v[0].Timestamp):
|
|
|
|
return false
|
|
|
|
case !v[v.Len()-1].Timestamp.Before(t):
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return true
|
2013-03-19 02:04:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-24 09:02:51 +00:00
|
|
|
// TruncateBefore returns a subslice of the original such that extraneous
|
|
|
|
// samples in the collection that occur before the provided time are
|
2013-05-08 18:39:59 +00:00
|
|
|
// dropped. The original slice is not mutated
|
|
|
|
func (v Values) TruncateBefore(t time.Time) Values {
|
2013-04-24 09:02:51 +00:00
|
|
|
index := sort.Search(len(v), func(i int) bool {
|
|
|
|
timestamp := v[i].Timestamp
|
|
|
|
|
2013-04-24 10:42:58 +00:00
|
|
|
return !timestamp.Before(t)
|
2013-04-24 09:02:51 +00:00
|
|
|
})
|
|
|
|
|
2013-05-08 18:39:59 +00:00
|
|
|
return v[index:]
|
2013-04-24 09:02:51 +00:00
|
|
|
}
|
|
|
|
|
2013-04-05 16:03:45 +00:00
|
|
|
func (v Values) ToDTO() (out *dto.SampleValueSeries) {
|
|
|
|
out = &dto.SampleValueSeries{}
|
|
|
|
|
|
|
|
for _, value := range v {
|
|
|
|
out.Value = append(out.Value, value.ToDTO())
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-17 10:58:15 +00:00
|
|
|
func (v Values) ToSampleKey(f *Fingerprint) SampleKey {
|
2013-04-05 16:03:45 +00:00
|
|
|
return SampleKey{
|
|
|
|
Fingerprint: f,
|
|
|
|
FirstTimestamp: v[0].Timestamp,
|
|
|
|
LastTimestamp: v[len(v)-1].Timestamp,
|
|
|
|
SampleCount: uint32(len(v)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Values) String() string {
|
|
|
|
buffer := bytes.Buffer{}
|
|
|
|
|
|
|
|
fmt.Fprintf(&buffer, "[")
|
|
|
|
for i, value := range v {
|
|
|
|
fmt.Fprintf(&buffer, "%d. %s", i, value)
|
|
|
|
if i != len(v)-1 {
|
|
|
|
fmt.Fprintf(&buffer, "\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&buffer, "]")
|
|
|
|
|
|
|
|
return buffer.String()
|
|
|
|
}
|
|
|
|
|
2013-04-22 11:30:16 +00:00
|
|
|
func NewValuesFromDTO(dto *dto.SampleValueSeries) (v Values) {
|
|
|
|
for _, value := range dto.Value {
|
|
|
|
v = append(v, SamplePair{
|
2013-04-24 10:05:52 +00:00
|
|
|
Timestamp: time.Unix(*value.Timestamp, 0).UTC(),
|
2013-04-22 11:30:16 +00:00
|
|
|
Value: SampleValue(*value.Value),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-05-16 05:38:31 +00:00
|
|
|
return v
|
2013-04-22 11:30:16 +00:00
|
|
|
}
|
|
|
|
|
2012-12-12 11:13:27 +00:00
|
|
|
type SampleSet struct {
|
|
|
|
Metric Metric
|
2013-02-08 17:03:26 +00:00
|
|
|
Values Values
|
2012-12-12 11:13:27 +00:00
|
|
|
}
|
|
|
|
|
2012-11-24 11:33:34 +00:00
|
|
|
type Interval struct {
|
|
|
|
OldestInclusive time.Time
|
|
|
|
NewestInclusive time.Time
|
|
|
|
}
|