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-02-08 12:05:35 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
2013-01-17 23:07:00 +00:00
|
|
|
"fmt"
|
2013-02-08 12:05:35 +00:00
|
|
|
"sort"
|
2012-11-24 11:33:34 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-02-08 12:05:35 +00:00
|
|
|
const (
|
|
|
|
// XXX: Re-evaluate down the road.
|
|
|
|
reservedDelimiter = '"'
|
|
|
|
)
|
|
|
|
|
2012-12-07 10:55:43 +00:00
|
|
|
// A Fingerprint is a simplified representation of an entity---e.g., a hash of
|
|
|
|
// an entire Metric.
|
2012-11-24 11:33:34 +00:00
|
|
|
type Fingerprint string
|
|
|
|
|
2012-12-07 10:55:43 +00:00
|
|
|
// A LabelName is a key for a LabelSet or Metric. It has a value associated
|
|
|
|
// therewith.
|
|
|
|
type LabelName string
|
2012-11-24 11:33:34 +00:00
|
|
|
|
2012-12-07 10:55:43 +00:00
|
|
|
// A LabelValue is an associated value for a LabelName.
|
|
|
|
type LabelValue string
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2013-02-08 12:05:35 +00:00
|
|
|
// Fingerprint generates a fingerprint for this given Metric.
|
|
|
|
func (m Metric) Fingerprint() string {
|
|
|
|
labelLength := len(m)
|
|
|
|
labelNames := make([]string, 0, labelLength)
|
|
|
|
|
|
|
|
for labelName := range m {
|
|
|
|
labelNames = append(labelNames, string(labelName))
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(labelNames)
|
|
|
|
|
|
|
|
summer := md5.New()
|
|
|
|
|
|
|
|
buffer := bytes.Buffer{}
|
|
|
|
for _, labelName := range labelNames {
|
|
|
|
buffer.WriteString(labelName)
|
|
|
|
buffer.WriteRune(reservedDelimiter)
|
|
|
|
buffer.WriteString(string(m[LabelName(labelName)]))
|
|
|
|
}
|
|
|
|
summer.Write(buffer.Bytes())
|
|
|
|
|
|
|
|
return hex.EncodeToString(summer.Sum(nil))
|
|
|
|
}
|
|
|
|
|
2012-12-07 10:55:43 +00:00
|
|
|
// A SampleValue is a representation of a value for a given sample at a given
|
|
|
|
// time. It is presently float32 due to that being the representation that
|
|
|
|
// Protocol Buffers provide of floats in Go. This is a smell and should be
|
|
|
|
// remedied down the road.
|
2012-11-24 11:33:34 +00:00
|
|
|
type SampleValue float32
|
|
|
|
|
2013-01-13 18:56:48 +00:00
|
|
|
func (v SampleValue) MarshalJSON() ([]byte, error) {
|
2013-01-17 23:07:00 +00:00
|
|
|
return []byte(fmt.Sprintf("\"%f\"", v)), nil
|
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-11-24 11:33:34 +00:00
|
|
|
type Sample struct {
|
2012-12-11 19:46:16 +00:00
|
|
|
Metric Metric
|
2012-11-24 11:33:34 +00:00
|
|
|
Value SampleValue
|
|
|
|
Timestamp time.Time
|
|
|
|
}
|
|
|
|
|
2012-12-12 11:13:27 +00:00
|
|
|
type SamplePair struct {
|
2012-11-24 11:33:34 +00:00
|
|
|
Value SampleValue
|
|
|
|
Timestamp time.Time
|
|
|
|
}
|
|
|
|
|
2012-12-12 11:13:27 +00:00
|
|
|
type SampleSet struct {
|
|
|
|
Metric Metric
|
|
|
|
Values []SamplePair
|
|
|
|
}
|
|
|
|
|
2012-11-24 11:33:34 +00:00
|
|
|
type Interval struct {
|
|
|
|
OldestInclusive time.Time
|
|
|
|
NewestInclusive time.Time
|
|
|
|
}
|
2012-12-12 11:13:27 +00:00
|
|
|
|
|
|
|
// PENDING DELETION BELOW THIS LINE
|
|
|
|
|
|
|
|
type Samples struct {
|
|
|
|
Value SampleValue
|
|
|
|
Timestamp time.Time
|
|
|
|
}
|