2017-05-10 02:44:13 -07: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-16 18:26:38 -07:00
|
|
|
// limitations under the License.
|
2017-05-10 02:44:13 -07:00
|
|
|
|
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
2017-11-11 17:15:27 -08:00
|
|
|
"context"
|
2019-12-12 12:47:23 -08:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2017-05-10 02:44:13 -07:00
|
|
|
"sync"
|
2018-05-23 07:03:54 -07:00
|
|
|
"time"
|
2017-05-10 02:44:13 -07:00
|
|
|
|
2021-06-11 09:17:59 -07:00
|
|
|
"github.com/go-kit/log"
|
2018-09-07 14:26:04 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-05-10 02:44:13 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2020-10-22 02:00:08 -07:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
2017-05-10 02:44:13 -07:00
|
|
|
"github.com/prometheus/prometheus/config"
|
2021-11-08 06:23:17 -08:00
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
2020-11-19 07:23:03 -08:00
|
|
|
"github.com/prometheus/prometheus/scrape"
|
2017-11-11 17:15:27 -08:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2021-11-08 06:23:17 -08:00
|
|
|
"github.com/prometheus/prometheus/util/logging"
|
2017-05-10 02:44:13 -07:00
|
|
|
)
|
|
|
|
|
2019-12-12 12:47:23 -08:00
|
|
|
// String constants for instrumentation.
|
|
|
|
const (
|
|
|
|
namespace = "prometheus"
|
|
|
|
subsystem = "remote_storage"
|
|
|
|
remoteName = "remote_name"
|
|
|
|
endpoint = "url"
|
|
|
|
)
|
|
|
|
|
2020-11-19 07:23:03 -08:00
|
|
|
type ReadyScrapeManager interface {
|
|
|
|
Get() (*scrape.Manager, error)
|
|
|
|
}
|
|
|
|
|
2018-12-27 06:02:36 -08:00
|
|
|
// startTimeCallback is a callback func that return the oldest timestamp stored in a storage.
|
2017-10-18 04:08:14 -07:00
|
|
|
type startTimeCallback func() (int64, error)
|
|
|
|
|
2017-08-01 03:19:35 -07:00
|
|
|
// Storage represents all the remote read and write endpoints. It implements
|
|
|
|
// storage.Storage.
|
2017-05-10 02:44:13 -07:00
|
|
|
type Storage struct {
|
2021-10-29 16:39:02 -07:00
|
|
|
logger *logging.Deduper
|
2019-03-05 04:21:11 -08:00
|
|
|
mtx sync.Mutex
|
2017-05-10 02:44:13 -07:00
|
|
|
|
2019-05-31 18:39:40 -07:00
|
|
|
rws *WriteStorage
|
2017-05-10 02:44:13 -07:00
|
|
|
|
2020-06-24 06:41:52 -07:00
|
|
|
// For reads.
|
|
|
|
queryables []storage.SampleAndChunkQueryable
|
2017-10-18 04:08:14 -07:00
|
|
|
localStartTimeCallback startTimeCallback
|
2017-05-10 02:44:13 -07:00
|
|
|
}
|
|
|
|
|
2017-10-18 04:08:14 -07:00
|
|
|
// NewStorage returns a remote.Storage.
|
2020-11-19 07:23:03 -08:00
|
|
|
func NewStorage(l log.Logger, reg prometheus.Registerer, stCallback startTimeCallback, walDir string, flushDeadline time.Duration, sm ReadyScrapeManager) *Storage {
|
2017-08-11 11:45:52 -07:00
|
|
|
if l == nil {
|
|
|
|
l = log.NewNopLogger()
|
|
|
|
}
|
2021-10-29 16:39:02 -07:00
|
|
|
logger := logging.Dedupe(l, 1*time.Minute)
|
2020-06-24 06:41:52 -07:00
|
|
|
|
2018-09-07 14:26:04 -07:00
|
|
|
s := &Storage{
|
2021-10-29 16:39:02 -07:00
|
|
|
logger: logger,
|
2018-05-23 07:03:54 -07:00
|
|
|
localStartTimeCallback: stCallback,
|
|
|
|
}
|
2020-11-19 07:23:03 -08:00
|
|
|
s.rws = NewWriteStorage(s.logger, reg, walDir, flushDeadline, sm)
|
2018-09-07 14:26:04 -07:00
|
|
|
return s
|
2017-08-11 11:45:52 -07:00
|
|
|
}
|
|
|
|
|
2023-05-15 12:31:49 -07:00
|
|
|
func (s *Storage) Notify() {
|
|
|
|
for _, q := range s.rws.queues {
|
|
|
|
// These should all be non blocking
|
|
|
|
q.watcher.Notify()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-10 02:44:13 -07: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-05-31 18:39:40 -07:00
|
|
|
if err := s.rws.ApplyConfig(conf); err != nil {
|
2019-05-17 02:29:49 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update read clients
|
2019-12-12 12:47:23 -08:00
|
|
|
readHashes := make(map[string]struct{})
|
2020-06-24 06:41:52 -07:00
|
|
|
queryables := make([]storage.SampleAndChunkQueryable, 0, len(conf.RemoteReadConfigs))
|
2019-12-12 12:47:23 -08:00
|
|
|
for _, rrConf := range conf.RemoteReadConfigs {
|
|
|
|
hash, err := toHash(rrConf)
|
|
|
|
if err != nil {
|
2020-01-14 00:40:30 -08:00
|
|
|
return err
|
2019-12-12 12:47:23 -08: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 06:41:52 -07:00
|
|
|
name := hash[:6]
|
2019-12-12 12:47:23 -08:00
|
|
|
if rrConf.Name != "" {
|
|
|
|
name = rrConf.Name
|
|
|
|
}
|
|
|
|
|
2020-09-02 09:15:10 -07:00
|
|
|
c, err := NewReadClient(name, &ClientConfig{
|
2019-05-17 02:29:49 -07:00
|
|
|
URL: rrConf.URL,
|
|
|
|
Timeout: rrConf.RemoteTimeout,
|
|
|
|
HTTPClientConfig: rrConf.HTTPClientConfig,
|
2021-02-18 04:12:21 -08:00
|
|
|
Headers: rrConf.Headers,
|
2019-05-17 02:29:49 -07:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-16 13:12:47 -08:00
|
|
|
externalLabels := conf.GlobalConfig.ExternalLabels
|
|
|
|
if !rrConf.FilterExternalLabels {
|
2022-07-21 09:41:20 -07:00
|
|
|
externalLabels = labels.EmptyLabels()
|
2022-02-16 13:12:47 -08:00
|
|
|
}
|
2020-06-24 06:41:52 -07:00
|
|
|
queryables = append(queryables, NewSampleAndChunkQueryableClient(
|
|
|
|
c,
|
2022-02-16 13:12:47 -08:00
|
|
|
externalLabels,
|
2020-06-24 06:41:52 -07:00
|
|
|
labelsToEqualityMatchers(rrConf.RequiredMatchers),
|
|
|
|
rrConf.ReadRecent,
|
|
|
|
s.localStartTimeCallback,
|
|
|
|
))
|
2019-05-17 02:29:49 -07:00
|
|
|
}
|
|
|
|
s.queryables = queryables
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-18 04:08:14 -07:00
|
|
|
// StartTime implements the Storage interface.
|
|
|
|
func (s *Storage) StartTime() (int64, error) {
|
|
|
|
return int64(model.Latest), nil
|
|
|
|
}
|
|
|
|
|
2017-11-11 17:15:27 -08:00
|
|
|
// Querier returns a storage.MergeQuerier combining the remote client queriers
|
|
|
|
// of each configured remote read endpoint.
|
2020-06-09 09:57:31 -07: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-11 17:15:27 -08: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 08:03:02 -07:00
|
|
|
return storage.NewMergeQuerier(nil, queriers, storage.ChainedSeriesMerge), nil
|
2020-06-24 06:41:52 -07: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 08:03:02 -07:00
|
|
|
return storage.NewMergeChunkQuerier(nil, queriers, storage.NewCompactingChunkSeriesMerger(storage.ChainedSeriesMerge)), nil
|
2017-11-11 17:15:27 -08:00
|
|
|
}
|
|
|
|
|
2019-05-31 18:39:40 -07:00
|
|
|
// Appender implements storage.Storage.
|
2020-07-30 04:11:13 -07:00
|
|
|
func (s *Storage) Appender(ctx context.Context) storage.Appender {
|
|
|
|
return s.rws.Appender(ctx)
|
2019-05-31 18:39:40 -07:00
|
|
|
}
|
|
|
|
|
2021-10-29 08:25:05 -07:00
|
|
|
// LowestSentTimestamp returns the lowest sent timestamp across all queues.
|
|
|
|
func (s *Storage) LowestSentTimestamp() int64 {
|
|
|
|
return s.rws.LowestSentTimestamp()
|
|
|
|
}
|
|
|
|
|
2017-08-01 03:19:35 -07:00
|
|
|
// Close the background processing of the storage queues.
|
2017-05-10 02:44:13 -07:00
|
|
|
func (s *Storage) Close() error {
|
2021-10-29 16:39:02 -07:00
|
|
|
s.logger.Stop()
|
2017-05-10 02:44:13 -07:00
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
2019-05-31 18:39:40 -07:00
|
|
|
return s.rws.Close()
|
2017-05-10 02:44:13 -07:00
|
|
|
}
|
2017-11-11 17:23:20 -08: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 12:47:23 -08:00
|
|
|
|
|
|
|
// Used for hashing configs and diff'ing hashes in ApplyConfig.
|
|
|
|
func toHash(data interface{}) (string, error) {
|
2020-02-05 06:01:28 -08:00
|
|
|
bytes, err := yaml.Marshal(data)
|
2019-12-12 12:47:23 -08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
hash := md5.Sum(bytes)
|
|
|
|
return hex.EncodeToString(hash[:]), nil
|
|
|
|
}
|