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