2013-02-07 10:49:04 +00:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// 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.
|
|
|
|
|
2013-01-07 22:24:26 +00:00
|
|
|
package ast
|
|
|
|
|
|
|
|
import (
|
2013-01-25 11:21:44 +00:00
|
|
|
"flag"
|
2013-01-27 17:49:45 +00:00
|
|
|
"github.com/prometheus/prometheus/model"
|
|
|
|
"github.com/prometheus/prometheus/storage/metric"
|
2013-01-07 22:24:26 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-01-25 11:21:44 +00:00
|
|
|
var defaultStalenessDelta = flag.Int("defaultStalenessDelta", 300, "Default staleness delta allowance in seconds during expression evaluations.")
|
2013-01-07 22:24:26 +00:00
|
|
|
|
2013-01-25 11:21:44 +00:00
|
|
|
type PersistenceAdapter struct {
|
|
|
|
persistence metric.MetricPersistence
|
|
|
|
stalenessPolicy *metric.StalenessPolicy
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AST-global persistence to use.
|
2013-01-25 11:21:44 +00:00
|
|
|
var persistenceAdapter *PersistenceAdapter = nil
|
2013-01-07 22:24:26 +00:00
|
|
|
|
2013-03-18 15:46:52 +00:00
|
|
|
func (p *PersistenceAdapter) getMetricsWithLabels(labels model.LabelSet) (fingerprintToMetric map[model.Fingerprint]model.Metric, err error) {
|
2013-02-14 01:13:41 +00:00
|
|
|
fingerprints, err := p.persistence.GetFingerprintsForLabelSet(labels)
|
2013-01-07 22:24:26 +00:00
|
|
|
if err != nil {
|
2013-02-14 16:43:02 +00:00
|
|
|
return
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
2013-03-18 15:46:52 +00:00
|
|
|
fingerprintToMetric = make(map[model.Fingerprint]model.Metric)
|
2013-01-07 22:24:26 +00:00
|
|
|
for _, fingerprint := range fingerprints {
|
2013-03-18 15:46:52 +00:00
|
|
|
var metric *model.Metric // Don't shadow err.
|
|
|
|
metric, err = p.persistence.GetMetricForFingerprint(fingerprint)
|
2013-01-07 22:24:26 +00:00
|
|
|
if err != nil {
|
2013-03-18 15:46:52 +00:00
|
|
|
return
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
2013-02-08 17:03:26 +00:00
|
|
|
if metric == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-03-18 15:46:52 +00:00
|
|
|
fingerprintToMetric[fingerprint] = *metric
|
2013-01-11 00:19:27 +00:00
|
|
|
}
|
2013-02-14 16:43:02 +00:00
|
|
|
|
|
|
|
return
|
2013-01-11 00:19:27 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 11:21:44 +00:00
|
|
|
func (p *PersistenceAdapter) GetValueAtTime(labels model.LabelSet, timestamp *time.Time) ([]*model.Sample, error) {
|
2013-01-12 20:22:59 +00:00
|
|
|
metrics, err := p.getMetricsWithLabels(labels)
|
2013-01-11 00:19:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
samples := []*model.Sample{}
|
2013-03-18 15:46:52 +00:00
|
|
|
for fingerprint := range metrics {
|
|
|
|
sample, err := p.persistence.GetValueAtTime(fingerprint, *timestamp, *p.stalenessPolicy)
|
2013-01-07 22:24:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if sample == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
samples = append(samples, sample)
|
|
|
|
}
|
|
|
|
return samples, nil
|
|
|
|
}
|
|
|
|
|
2013-01-25 11:21:44 +00:00
|
|
|
func (p *PersistenceAdapter) GetBoundaryValues(labels model.LabelSet, interval *model.Interval) ([]*model.SampleSet, error) {
|
2013-01-12 20:22:59 +00:00
|
|
|
metrics, err := p.getMetricsWithLabels(labels)
|
2013-01-11 00:19:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sampleSets := []*model.SampleSet{}
|
2013-03-18 15:46:52 +00:00
|
|
|
for fingerprint, metric := range metrics {
|
2013-01-12 20:22:59 +00:00
|
|
|
// TODO: change to GetBoundaryValues() once it has the right return type.
|
2013-03-18 15:46:52 +00:00
|
|
|
sampleSet, err := p.persistence.GetRangeValues(fingerprint, *interval)
|
2013-01-11 00:19:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if sampleSet == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-01-12 20:22:59 +00:00
|
|
|
// TODO remove when persistence return value is fixed.
|
2013-02-14 16:43:02 +00:00
|
|
|
sampleSet.Metric = metric
|
2013-01-11 00:19:27 +00:00
|
|
|
sampleSets = append(sampleSets, sampleSet)
|
|
|
|
}
|
|
|
|
return sampleSets, nil
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 11:21:44 +00:00
|
|
|
func (p *PersistenceAdapter) GetRangeValues(labels model.LabelSet, interval *model.Interval) ([]*model.SampleSet, error) {
|
2013-01-12 20:22:59 +00:00
|
|
|
metrics, err := p.getMetricsWithLabels(labels)
|
2013-01-11 00:19:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sampleSets := []*model.SampleSet{}
|
2013-03-18 15:46:52 +00:00
|
|
|
for fingerprint, metric := range metrics {
|
|
|
|
sampleSet, err := p.persistence.GetRangeValues(fingerprint, *interval)
|
2013-01-11 00:19:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if sampleSet == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-01-12 20:22:59 +00:00
|
|
|
// TODO remove when persistence return value is fixed.
|
2013-02-14 16:43:02 +00:00
|
|
|
sampleSet.Metric = metric
|
2013-01-11 00:19:27 +00:00
|
|
|
sampleSets = append(sampleSets, sampleSet)
|
|
|
|
}
|
|
|
|
return sampleSets, nil
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 11:21:44 +00:00
|
|
|
func SetPersistence(persistence metric.MetricPersistence, policy *metric.StalenessPolicy) {
|
|
|
|
if policy == nil {
|
|
|
|
policy = &metric.StalenessPolicy{
|
|
|
|
DeltaAllowance: time.Duration(*defaultStalenessDelta) * time.Second,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
persistenceAdapter = &PersistenceAdapter{
|
|
|
|
persistence: persistence,
|
|
|
|
stalenessPolicy: policy,
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
}
|