2016-09-19 13:47:51 -07:00
|
|
|
// Copyright 2016 The Prometheus Authors
|
|
|
|
// 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 remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/config"
|
2016-10-01 08:42:43 -07:00
|
|
|
"github.com/prometheus/prometheus/relabel"
|
2016-09-19 13:47:51 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Storage collects multiple remote storage queues.
|
|
|
|
type ReloadableStorage struct {
|
|
|
|
mtx sync.RWMutex
|
|
|
|
externalLabels model.LabelSet
|
2016-10-01 08:42:43 -07:00
|
|
|
conf config.RemoteWriteConfig
|
2016-09-19 13:47:51 -07:00
|
|
|
|
2016-10-01 08:42:43 -07:00
|
|
|
queue *StorageQueueManager
|
2016-09-19 13:47:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new remote Storage.
|
|
|
|
func NewConfigurable() *ReloadableStorage {
|
|
|
|
return &ReloadableStorage{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyConfig updates the state as the new config requires.
|
|
|
|
func (s *ReloadableStorage) ApplyConfig(conf *config.Config) error {
|
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
|
|
|
|
|
|
|
// TODO: we should only stop & recreate queues which have changes,
|
|
|
|
// as this can be quite disruptive.
|
2016-10-01 08:42:43 -07:00
|
|
|
var newQueue *StorageQueueManager
|
|
|
|
|
|
|
|
if conf.RemoteWriteConfig.URL != nil {
|
|
|
|
c, err := NewClient(conf.RemoteWriteConfig)
|
2016-09-19 13:47:51 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-01 08:42:43 -07:00
|
|
|
newQueue = NewStorageQueueManager(c, nil)
|
2016-09-19 13:47:51 -07:00
|
|
|
}
|
|
|
|
|
2016-10-01 08:42:43 -07:00
|
|
|
if s.queue != nil {
|
|
|
|
s.queue.Stop()
|
2016-09-19 13:47:51 -07:00
|
|
|
}
|
2016-10-01 08:42:43 -07:00
|
|
|
s.queue = newQueue
|
2016-09-19 13:47:51 -07:00
|
|
|
s.conf = conf.RemoteWriteConfig
|
2016-10-01 08:42:43 -07:00
|
|
|
s.externalLabels = conf.GlobalConfig.ExternalLabels
|
|
|
|
if s.queue != nil {
|
|
|
|
s.queue.Start()
|
2016-09-19 13:47:51 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the background processing of the storage queues.
|
|
|
|
func (s *ReloadableStorage) Stop() {
|
2016-10-01 08:42:43 -07:00
|
|
|
if s.queue != nil {
|
|
|
|
s.queue.Stop()
|
2016-09-19 13:47:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append implements storage.SampleAppender. Always returns nil.
|
|
|
|
func (s *ReloadableStorage) Append(smpl *model.Sample) error {
|
|
|
|
s.mtx.RLock()
|
|
|
|
defer s.mtx.RUnlock()
|
|
|
|
|
2016-12-21 03:33:28 -08:00
|
|
|
if s.queue == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-19 13:47:51 -07:00
|
|
|
var snew model.Sample
|
|
|
|
snew = *smpl
|
|
|
|
snew.Metric = smpl.Metric.Clone()
|
|
|
|
|
|
|
|
for ln, lv := range s.externalLabels {
|
|
|
|
if _, ok := smpl.Metric[ln]; !ok {
|
|
|
|
snew.Metric[ln] = lv
|
|
|
|
}
|
|
|
|
}
|
2016-10-01 08:42:43 -07:00
|
|
|
snew.Metric = model.Metric(
|
|
|
|
relabel.Process(model.LabelSet(snew.Metric), s.conf.WriteRelabelConfigs...))
|
2016-09-19 13:47:51 -07:00
|
|
|
|
2016-10-01 08:42:43 -07:00
|
|
|
if snew.Metric == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-12-21 03:33:28 -08:00
|
|
|
s.queue.Append(&snew)
|
2016-09-19 13:47:51 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NeedsThrottling implements storage.SampleAppender. It will always return
|
|
|
|
// false as a remote storage drops samples on the floor if backlogging instead
|
|
|
|
// of asking for throttling.
|
|
|
|
func (s *ReloadableStorage) NeedsThrottling() bool {
|
|
|
|
return false
|
|
|
|
}
|