2013-02-07 10:38:01 +00:00
|
|
|
// Copyright 2013 Prometheus Team
|
2012-11-29 19:55:30 +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.
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.google.com/p/goprotobuf/proto"
|
2013-01-27 17:49:45 +00:00
|
|
|
dto "github.com/prometheus/prometheus/model/generated"
|
2012-11-29 19:55:30 +00:00
|
|
|
"sort"
|
2012-12-19 19:34:54 +00:00
|
|
|
"time"
|
2012-11-29 19:55:30 +00:00
|
|
|
)
|
|
|
|
|
2012-12-09 15:27:12 +00:00
|
|
|
func SampleToMetricDTO(s *Sample) *dto.Metric {
|
2012-12-11 19:46:16 +00:00
|
|
|
labelLength := len(s.Metric)
|
2012-11-29 19:55:30 +00:00
|
|
|
labelNames := make([]string, 0, labelLength)
|
|
|
|
|
2012-12-11 19:46:16 +00:00
|
|
|
for labelName := range s.Metric {
|
2012-11-29 19:55:30 +00:00
|
|
|
labelNames = append(labelNames, string(labelName))
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(labelNames)
|
|
|
|
|
2012-12-09 15:27:12 +00:00
|
|
|
labelSets := make([]*dto.LabelPair, 0, labelLength)
|
2012-11-29 19:55:30 +00:00
|
|
|
|
|
|
|
for _, labelName := range labelNames {
|
2012-12-11 19:46:16 +00:00
|
|
|
labelValue := s.Metric[LabelName(labelName)]
|
2012-12-09 15:27:12 +00:00
|
|
|
labelPair := &dto.LabelPair{
|
2012-11-29 19:55:30 +00:00
|
|
|
Name: proto.String(string(labelName)),
|
|
|
|
Value: proto.String(string(labelValue)),
|
|
|
|
}
|
|
|
|
|
2012-12-07 10:55:43 +00:00
|
|
|
labelSets = append(labelSets, labelPair)
|
2012-11-29 19:55:30 +00:00
|
|
|
}
|
|
|
|
|
2012-12-09 15:27:12 +00:00
|
|
|
return &dto.Metric{
|
2012-12-07 10:55:43 +00:00
|
|
|
LabelPair: labelSets,
|
2012-11-29 19:55:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-14 23:55:50 +00:00
|
|
|
func MetricToDTO(m Metric) *dto.Metric {
|
|
|
|
metricLength := len(m)
|
2012-11-29 19:55:30 +00:00
|
|
|
labelNames := make([]string, 0, metricLength)
|
|
|
|
|
2013-03-14 23:55:50 +00:00
|
|
|
for labelName := range m {
|
2012-11-29 19:55:30 +00:00
|
|
|
labelNames = append(labelNames, string(labelName))
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(labelNames)
|
|
|
|
|
2012-12-09 15:27:12 +00:00
|
|
|
labelSets := make([]*dto.LabelPair, 0, metricLength)
|
2012-11-29 19:55:30 +00:00
|
|
|
|
|
|
|
for _, labelName := range labelNames {
|
2012-12-07 10:55:43 +00:00
|
|
|
l := LabelName(labelName)
|
2013-03-14 23:55:50 +00:00
|
|
|
labelValue := m[l]
|
2012-12-09 15:27:12 +00:00
|
|
|
labelPair := &dto.LabelPair{
|
2012-11-29 19:55:30 +00:00
|
|
|
Name: proto.String(string(labelName)),
|
|
|
|
Value: proto.String(string(labelValue)),
|
|
|
|
}
|
|
|
|
|
2012-12-07 10:55:43 +00:00
|
|
|
labelSets = append(labelSets, labelPair)
|
2012-11-29 19:55:30 +00:00
|
|
|
}
|
|
|
|
|
2012-12-09 15:27:12 +00:00
|
|
|
return &dto.Metric{
|
2012-12-07 10:55:43 +00:00
|
|
|
LabelPair: labelSets,
|
2012-11-29 19:55:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-09 15:27:12 +00:00
|
|
|
func LabelSetToDTOs(s *LabelSet) []*dto.LabelPair {
|
2012-12-07 10:55:43 +00:00
|
|
|
metricLength := len(*s)
|
|
|
|
labelNames := make([]string, 0, metricLength)
|
|
|
|
|
|
|
|
for labelName := range *s {
|
|
|
|
labelNames = append(labelNames, string(labelName))
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(labelNames)
|
|
|
|
|
2012-12-09 15:27:12 +00:00
|
|
|
labelSets := make([]*dto.LabelPair, 0, metricLength)
|
2012-12-07 10:55:43 +00:00
|
|
|
|
|
|
|
for _, labelName := range labelNames {
|
|
|
|
l := LabelName(labelName)
|
|
|
|
labelValue := (*s)[l]
|
2012-12-09 15:27:12 +00:00
|
|
|
labelPair := &dto.LabelPair{
|
2012-12-07 10:55:43 +00:00
|
|
|
Name: proto.String(string(labelName)),
|
|
|
|
Value: proto.String(string(labelValue)),
|
|
|
|
}
|
|
|
|
|
|
|
|
labelSets = append(labelSets, labelPair)
|
|
|
|
}
|
|
|
|
|
|
|
|
return labelSets
|
|
|
|
}
|
|
|
|
|
2012-12-09 15:27:12 +00:00
|
|
|
func LabelSetToDTO(s *LabelSet) *dto.LabelSet {
|
|
|
|
return &dto.LabelSet{
|
2012-12-07 10:55:43 +00:00
|
|
|
Member: LabelSetToDTOs(s),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-09 15:27:12 +00:00
|
|
|
func LabelNameToDTO(l *LabelName) *dto.LabelName {
|
|
|
|
return &dto.LabelName{
|
2012-12-07 10:55:43 +00:00
|
|
|
Name: proto.String(string(*l)),
|
|
|
|
}
|
|
|
|
}
|
2012-12-09 15:27:12 +00:00
|
|
|
|
2013-02-08 17:03:26 +00:00
|
|
|
func FingerprintToDTO(f Fingerprint) *dto.Fingerprint {
|
2012-12-09 15:27:12 +00:00
|
|
|
return &dto.Fingerprint{
|
2013-02-08 17:03:26 +00:00
|
|
|
Signature: proto.String(f.ToRowKey()),
|
2012-12-09 15:27:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 17:03:26 +00:00
|
|
|
func SampleFromDTO(m *Metric, t *time.Time, v *dto.SampleValueSeries) *Sample {
|
2012-12-19 19:34:54 +00:00
|
|
|
s := &Sample{
|
2013-02-08 17:03:26 +00:00
|
|
|
Value: SampleValue(*v.Value[0].Value),
|
2012-12-19 19:34:54 +00:00
|
|
|
Timestamp: *t,
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Metric = *m
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|