2017-05-10 09:44:13 +00:00
|
|
|
// Copyright 2017 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
|
2017-10-17 01:26:38 +00:00
|
|
|
// limitations under the License.
|
2017-05-10 09:44:13 +00:00
|
|
|
|
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
2017-11-12 01:15:27 +00:00
|
|
|
"context"
|
2019-12-12 20:47:23 +00:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2017-05-10 09:44:13 +00:00
|
|
|
"sync"
|
2018-05-23 14:03:54 +00:00
|
|
|
"time"
|
2017-05-10 09:44:13 +00:00
|
|
|
|
2021-06-11 16:17:59 +00:00
|
|
|
"github.com/go-kit/log"
|
2018-09-07 21:26:04 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-05-10 09:44:13 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2020-10-22 09:00:08 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
2017-05-10 09:44:13 +00:00
|
|
|
"github.com/prometheus/prometheus/config"
|
2021-11-08 14:23:17 +00:00
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
2020-11-19 15:23:03 +00:00
|
|
|
"github.com/prometheus/prometheus/scrape"
|
2017-11-12 01:15:27 +00:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2021-11-08 14:23:17 +00:00
|
|
|
"github.com/prometheus/prometheus/util/logging"
|
2017-05-10 09:44:13 +00:00
|
|
|
)
|
|
|
|
|
2019-12-12 20:47:23 +00:00
|
|
|
// String constants for instrumentation.
|
|
|
|
const (
|
|
|
|
namespace = "prometheus"
|
|
|
|
subsystem = "remote_storage"
|
|
|
|
remoteName = "remote_name"
|
|
|
|
endpoint = "url"
|
|
|
|
)
|
|
|
|
|
2020-11-19 15:23:03 +00:00
|
|
|
type ReadyScrapeManager interface {
|
|
|
|
Get() (*scrape.Manager, error)
|
|
|
|
}
|
|
|
|
|
2018-12-27 14:02:36 +00:00
|
|
|
// startTimeCallback is a callback func that return the oldest timestamp stored in a storage.
|
2017-10-18 11:08:14 +00:00
|
|
|
type startTimeCallback func() (int64, error)
|
|
|
|
|
2017-08-01 10:19:35 +00:00
|
|
|
// Storage represents all the remote read and write endpoints. It implements
|
|
|
|
// storage.Storage.
|
2017-05-10 09:44:13 +00:00
|
|
|
type Storage struct {
|
2021-10-29 23:39:02 +00:00
|
|
|
logger *logging.Deduper
|
2019-03-05 12:21:11 +00:00
|
|
|
mtx sync.Mutex
|
2017-05-10 09:44:13 +00:00
|
|
|
|
2019-06-01 01:39:40 +00:00
|
|
|
rws *WriteStorage
|
2017-05-10 09:44:13 +00:00
|
|
|
|
2020-06-24 13:41:52 +00:00
|
|
|
// For reads.
|
|
|
|
queryables []storage.SampleAndChunkQueryable
|
2017-10-18 11:08:14 +00:00
|
|
|
localStartTimeCallback startTimeCallback
|
2017-05-10 09:44:13 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 11:08:14 +00:00
|
|
|
// NewStorage returns a remote.Storage.
|
2020-11-19 15:23:03 +00:00
|
|
|
func NewStorage(l log.Logger, reg prometheus.Registerer, stCallback startTimeCallback, walDir string, flushDeadline time.Duration, sm ReadyScrapeManager) *Storage {
|
2017-08-11 18:45:52 +00:00
|
|
|
if l == nil {
|
|
|
|
l = log.NewNopLogger()
|
|
|
|
}
|
2021-10-29 23:39:02 +00:00
|
|
|
logger := logging.Dedupe(l, 1*time.Minute)
|
2020-06-24 13:41:52 +00:00
|
|
|
|
2018-09-07 21:26:04 +00:00
|
|
|
s := &Storage{
|
2021-10-29 23:39:02 +00:00
|
|
|
logger: logger,
|
2018-05-23 14:03:54 +00:00
|
|
|
localStartTimeCallback: stCallback,
|
|
|
|
}
|
2020-11-19 15:23:03 +00:00
|
|
|
s.rws = NewWriteStorage(s.logger, reg, walDir, flushDeadline, sm)
|
2018-09-07 21:26:04 +00:00
|
|
|
return s
|
2017-08-11 18:45:52 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 19:31:49 +00:00
|
|
|
func (s *Storage) Notify() {
|
|
|
|
for _, q := range s.rws.queues {
|
|
|
|
// These should all be non blocking
|
|
|
|
q.watcher.Notify()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-10 09:44:13 +00:00
|
|
|
// ApplyConfig updates the state as the new config requires.
|
|
|
|
func (s *Storage) ApplyConfig(conf *config.Config) error {
|
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
|
|
|
|
2019-06-01 01:39:40 +00:00
|
|
|
if err := s.rws.ApplyConfig(conf); err != nil {
|
2019-05-17 09:29:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update read clients
|
2019-12-12 20:47:23 +00:00
|
|
|
readHashes := make(map[string]struct{})
|
2020-06-24 13:41:52 +00:00
|
|
|
queryables := make([]storage.SampleAndChunkQueryable, 0, len(conf.RemoteReadConfigs))
|
2019-12-12 20:47:23 +00:00
|
|
|
for _, rrConf := range conf.RemoteReadConfigs {
|
|
|
|
hash, err := toHash(rrConf)
|
|
|
|
if err != nil {
|
2020-01-14 08:40:30 +00:00
|
|
|
return err
|
2019-12-12 20:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow duplicate remote read configs.
|
|
|
|
if _, ok := readHashes[hash]; ok {
|
|
|
|
return fmt.Errorf("duplicate remote read configs are not allowed, found duplicate for URL: %s", rrConf.URL)
|
|
|
|
}
|
|
|
|
readHashes[hash] = struct{}{}
|
|
|
|
|
|
|
|
// Set the queue name to the config hash if the user has not set
|
|
|
|
// a name in their remote write config so we can still differentiate
|
|
|
|
// between queues that have the same remote write endpoint.
|
2020-06-24 13:41:52 +00:00
|
|
|
name := hash[:6]
|
2019-12-12 20:47:23 +00:00
|
|
|
if rrConf.Name != "" {
|
|
|
|
name = rrConf.Name
|
|
|
|
}
|
|
|
|
|
2020-09-02 16:15:10 +00:00
|
|
|
c, err := NewReadClient(name, &ClientConfig{
|
2019-05-17 09:29:49 +00:00
|
|
|
URL: rrConf.URL,
|
|
|
|
Timeout: rrConf.RemoteTimeout,
|
|
|
|
HTTPClientConfig: rrConf.HTTPClientConfig,
|
2021-02-18 12:12:21 +00:00
|
|
|
Headers: rrConf.Headers,
|
2019-05-17 09:29:49 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-16 21:12:47 +00:00
|
|
|
externalLabels := conf.GlobalConfig.ExternalLabels
|
|
|
|
if !rrConf.FilterExternalLabels {
|
2022-07-21 16:41:20 +00:00
|
|
|
externalLabels = labels.EmptyLabels()
|
2022-02-16 21:12:47 +00:00
|
|
|
}
|
2020-06-24 13:41:52 +00:00
|
|
|
queryables = append(queryables, NewSampleAndChunkQueryableClient(
|
|
|
|
c,
|
2022-02-16 21:12:47 +00:00
|
|
|
externalLabels,
|
2020-06-24 13:41:52 +00:00
|
|
|
labelsToEqualityMatchers(rrConf.RequiredMatchers),
|
|
|
|
rrConf.ReadRecent,
|
|
|
|
s.localStartTimeCallback,
|
|
|
|
))
|
2019-05-17 09:29:49 +00:00
|
|
|
}
|
|
|
|
s.queryables = queryables
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-18 11:08:14 +00:00
|
|
|
// StartTime implements the Storage interface.
|
|
|
|
func (s *Storage) StartTime() (int64, error) {
|
|
|
|
return int64(model.Latest), nil
|
|
|
|
}
|
|
|
|
|
2017-11-12 01:15:27 +00:00
|
|
|
// Querier returns a storage.MergeQuerier combining the remote client queriers
|
|
|
|
// of each configured remote read endpoint.
|
2020-06-09 16:57:31 +00:00
|
|
|
// Returned querier will never return error as all queryables are assumed best effort.
|
|
|
|
// Additionally all returned queriers ensure that its Select's SeriesSets have ready data after first `Next` invoke.
|
|
|
|
// This is because Prometheus (fanout and secondary queries) can't handle the stream failing half way through by design.
|
2017-11-12 01:15:27 +00:00
|
|
|
func (s *Storage) Querier(ctx context.Context, mint, maxt int64) (storage.Querier, error) {
|
|
|
|
s.mtx.Lock()
|
|
|
|
queryables := s.queryables
|
|
|
|
s.mtx.Unlock()
|
|
|
|
|
|
|
|
queriers := make([]storage.Querier, 0, len(queryables))
|
|
|
|
for _, queryable := range queryables {
|
|
|
|
q, err := queryable.Querier(ctx, mint, maxt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
queriers = append(queriers, q)
|
|
|
|
}
|
2020-07-31 15:03:02 +00:00
|
|
|
return storage.NewMergeQuerier(nil, queriers, storage.ChainedSeriesMerge), nil
|
2020-06-24 13:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChunkQuerier returns a storage.MergeQuerier combining the remote client queriers
|
|
|
|
// of each configured remote read endpoint.
|
|
|
|
func (s *Storage) ChunkQuerier(ctx context.Context, mint, maxt int64) (storage.ChunkQuerier, error) {
|
|
|
|
s.mtx.Lock()
|
|
|
|
queryables := s.queryables
|
|
|
|
s.mtx.Unlock()
|
|
|
|
|
|
|
|
queriers := make([]storage.ChunkQuerier, 0, len(queryables))
|
|
|
|
for _, queryable := range queryables {
|
|
|
|
q, err := queryable.ChunkQuerier(ctx, mint, maxt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
queriers = append(queriers, q)
|
|
|
|
}
|
2020-07-31 15:03:02 +00:00
|
|
|
return storage.NewMergeChunkQuerier(nil, queriers, storage.NewCompactingChunkSeriesMerger(storage.ChainedSeriesMerge)), nil
|
2017-11-12 01:15:27 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 01:39:40 +00:00
|
|
|
// Appender implements storage.Storage.
|
2020-07-30 11:11:13 +00:00
|
|
|
func (s *Storage) Appender(ctx context.Context) storage.Appender {
|
|
|
|
return s.rws.Appender(ctx)
|
2019-06-01 01:39:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 15:25:05 +00:00
|
|
|
// LowestSentTimestamp returns the lowest sent timestamp across all queues.
|
|
|
|
func (s *Storage) LowestSentTimestamp() int64 {
|
|
|
|
return s.rws.LowestSentTimestamp()
|
|
|
|
}
|
|
|
|
|
2017-08-01 10:19:35 +00:00
|
|
|
// Close the background processing of the storage queues.
|
2017-05-10 09:44:13 +00:00
|
|
|
func (s *Storage) Close() error {
|
2021-10-29 23:39:02 +00:00
|
|
|
s.logger.Stop()
|
2017-05-10 09:44:13 +00:00
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
2019-06-01 01:39:40 +00:00
|
|
|
return s.rws.Close()
|
2017-05-10 09:44:13 +00:00
|
|
|
}
|
2017-11-12 01:23:20 +00:00
|
|
|
|
|
|
|
func labelsToEqualityMatchers(ls model.LabelSet) []*labels.Matcher {
|
|
|
|
ms := make([]*labels.Matcher, 0, len(ls))
|
|
|
|
for k, v := range ls {
|
|
|
|
ms = append(ms, &labels.Matcher{
|
|
|
|
Type: labels.MatchEqual,
|
|
|
|
Name: string(k),
|
|
|
|
Value: string(v),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|
2019-12-12 20:47:23 +00:00
|
|
|
|
|
|
|
// Used for hashing configs and diff'ing hashes in ApplyConfig.
|
|
|
|
func toHash(data interface{}) (string, error) {
|
2020-02-05 14:01:28 +00:00
|
|
|
bytes, err := yaml.Marshal(data)
|
2019-12-12 20:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
hash := md5.Sum(bytes)
|
|
|
|
return hex.EncodeToString(hash[:]), nil
|
|
|
|
}
|