diff --git a/internal/cortex/chunk/cache/fifo_cache.go b/internal/cortex/chunk/cache/fifo_cache.go index 3e58fa1c32..e95e40219a 100644 --- a/internal/cortex/chunk/cache/fifo_cache.go +++ b/internal/cortex/chunk/cache/fifo_cache.go @@ -17,9 +17,6 @@ import ( "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" - - "github.com/thanos-io/thanos/internal/cortex/util/flagext" - util_log "github.com/thanos-io/thanos/internal/cortex/util/log" ) const ( @@ -96,10 +93,7 @@ type cacheEntry struct { // NewFifoCache returns a new initialised FifoCache of size. func NewFifoCache(name string, cfg FifoCacheConfig, reg prometheus.Registerer, logger log.Logger) *FifoCache { - util_log.WarnExperimentalUse("In-memory (FIFO) cache") - if cfg.DeprecatedSize > 0 { - flagext.DeprecatedFlagsUsed.Inc() level.Warn(logger).Log("msg", "running with DEPRECATED flag fifocache.size, use fifocache.max-size-items or fifocache.max-size-bytes instead", "cache", name) cfg.MaxSizeItems = cfg.DeprecatedSize } diff --git a/internal/cortex/chunk/cache/memcached_client.go b/internal/cortex/chunk/cache/memcached_client.go index bcae408bac..a2f7b6b88b 100644 --- a/internal/cortex/chunk/cache/memcached_client.go +++ b/internal/cortex/chunk/cache/memcached_client.go @@ -19,7 +19,6 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/sony/gobreaker" - util_log "github.com/thanos-io/thanos/internal/cortex/util/log" "github.com/thanos-io/thanos/pkg/discovery/dns" ) @@ -146,7 +145,6 @@ func NewMemcachedClient(cfg MemcachedClientConfig, name string, r prometheus.Reg } if len(cfg.Addresses) > 0 { - util_log.WarnExperimentalUse("DNS-based memcached service discovery") newClient.addresses = strings.Split(cfg.Addresses, ",") } diff --git a/internal/cortex/chunk/cache/redis_cache.go b/internal/cortex/chunk/cache/redis_cache.go index 16cf15b2a7..2fd30b7e80 100644 --- a/internal/cortex/chunk/cache/redis_cache.go +++ b/internal/cortex/chunk/cache/redis_cache.go @@ -13,7 +13,6 @@ import ( "github.com/prometheus/client_golang/prometheus/promauto" instr "github.com/weaveworks/common/instrument" - util_log "github.com/thanos-io/thanos/internal/cortex/util/log" "github.com/thanos-io/thanos/internal/cortex/util/spanlogger" ) @@ -27,7 +26,6 @@ type RedisCache struct { // NewRedisCache creates a new RedisCache func NewRedisCache(name string, redisClient *RedisClient, reg prometheus.Registerer, logger log.Logger) *RedisCache { - util_log.WarnExperimentalUse("Redis cache") cache := &RedisCache{ name: name, redis: redisClient, diff --git a/internal/cortex/cortexpb/compat.go b/internal/cortex/cortexpb/compat.go index 4925ee33b1..84f80861c8 100644 --- a/internal/cortex/cortexpb/compat.go +++ b/internal/cortex/cortexpb/compat.go @@ -15,10 +15,7 @@ import ( jsoniter "github.com/json-iterator/go" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/model/exemplar" "github.com/prometheus/prometheus/model/labels" - "github.com/prometheus/prometheus/model/textparse" - "github.com/thanos-io/thanos/internal/cortex/util" ) @@ -32,47 +29,6 @@ func FromLabelAdaptersToLabels(ls []LabelAdapter) labels.Labels { return *(*labels.Labels)(unsafe.Pointer(&ls)) } -// FromLabelAdaptersToLabelsWithCopy converts []LabelAdapter to labels.Labels. -// Do NOT use unsafe to convert between data types because this function may -// get in input labels whose data structure is reused. -func FromLabelAdaptersToLabelsWithCopy(input []LabelAdapter) labels.Labels { - return CopyLabels(FromLabelAdaptersToLabels(input)) -} - -// Efficiently copies labels input slice. To be used in cases where input slice -// can be reused, but long-term copy is needed. -func CopyLabels(input []labels.Label) labels.Labels { - result := make(labels.Labels, len(input)) - - size := 0 - for _, l := range input { - size += len(l.Name) - size += len(l.Value) - } - - // Copy all strings into the buffer, and use 'yoloString' to convert buffer - // slices to strings. - buf := make([]byte, size) - - for i, l := range input { - result[i].Name, buf = copyStringToBuffer(l.Name, buf) - result[i].Value, buf = copyStringToBuffer(l.Value, buf) - } - return result -} - -// Copies string to buffer (which must be big enough), and converts buffer slice containing -// the string copy into new string. -func copyStringToBuffer(in string, buf []byte) (string, []byte) { - l := len(in) - c := copy(buf, in) - if c != l { - panic("not copied full string") - } - - return yoloString(buf[0:l]), buf[l:] -} - // FromLabelsToLabelAdapters casts labels.Labels to []LabelAdapter. // It uses unsafe, but as LabelAdapter == labels.Label this should be safe. // This allows us to use labels.Labels directly in protos. @@ -86,12 +42,6 @@ func FromLabelAdaptersToMetric(ls []LabelAdapter) model.Metric { return util.LabelsToMetric(FromLabelAdaptersToLabels(ls)) } -// FromLabelAdaptersToMetric converts []LabelAdapter to a model.Metric with copy. -// Don't do this on any performance sensitive paths. -func FromLabelAdaptersToMetricWithCopy(ls []LabelAdapter) model.Metric { - return util.LabelsToMetric(FromLabelAdaptersToLabelsWithCopy(ls)) -} - // FromMetricsToLabelAdapters converts model.Metric to []LabelAdapter. // Don't do this on any performance sensitive paths. // The result is sorted. @@ -107,61 +57,12 @@ func FromMetricsToLabelAdapters(metric model.Metric) []LabelAdapter { return result } -func FromExemplarsToExemplarProtos(es []exemplar.Exemplar) []Exemplar { - result := make([]Exemplar, 0, len(es)) - for _, e := range es { - result = append(result, Exemplar{ - Labels: FromLabelsToLabelAdapters(e.Labels), - Value: e.Value, - TimestampMs: e.Ts, - }) - } - return result -} - -func FromExemplarProtosToExemplars(es []Exemplar) []exemplar.Exemplar { - result := make([]exemplar.Exemplar, 0, len(es)) - for _, e := range es { - result = append(result, exemplar.Exemplar{ - Labels: FromLabelAdaptersToLabels(e.Labels), - Value: e.Value, - Ts: e.TimestampMs, - }) - } - return result -} - type byLabel []LabelAdapter func (s byLabel) Len() int { return len(s) } func (s byLabel) Less(i, j int) bool { return strings.Compare(s[i].Name, s[j].Name) < 0 } func (s byLabel) Swap(i, j int) { s[i], s[j] = s[j], s[i] } -// MetricMetadataMetricTypeToMetricType converts a metric type from our internal client -// to a Prometheus one. -func MetricMetadataMetricTypeToMetricType(mt MetricMetadata_MetricType) textparse.MetricType { - switch mt { - case UNKNOWN: - return textparse.MetricTypeUnknown - case COUNTER: - return textparse.MetricTypeCounter - case GAUGE: - return textparse.MetricTypeGauge - case HISTOGRAM: - return textparse.MetricTypeHistogram - case GAUGEHISTOGRAM: - return textparse.MetricTypeGaugeHistogram - case SUMMARY: - return textparse.MetricTypeSummary - case INFO: - return textparse.MetricTypeInfo - case STATESET: - return textparse.MetricTypeStateset - default: - return textparse.MetricTypeUnknown - } -} - // isTesting is only set from tests to get special behaviour to verify that custom sample encode and decode is used, // both when using jsonitor or standard json package. var isTesting = false diff --git a/internal/cortex/cortexpb/cortex.pb.go b/internal/cortex/cortexpb/cortex.pb.go index 4a88a7e7c3..ba5b0a2419 100644 --- a/internal/cortex/cortexpb/cortex.pb.go +++ b/internal/cortex/cortexpb/cortex.pb.go @@ -495,10 +495,6 @@ func (m *Exemplar) GetTimestampMs() int64 { } func init() { - proto.RegisterEnum("cortexpb.WriteRequest_SourceEnum", WriteRequest_SourceEnum_name, WriteRequest_SourceEnum_value) - proto.RegisterEnum("cortexpb.MetricMetadata_MetricType", MetricMetadata_MetricType_name, MetricMetadata_MetricType_value) - proto.RegisterType((*WriteRequest)(nil), "cortexpb.WriteRequest") - proto.RegisterType((*WriteResponse)(nil), "cortexpb.WriteResponse") proto.RegisterType((*TimeSeries)(nil), "cortexpb.TimeSeries") proto.RegisterType((*LabelPair)(nil), "cortexpb.LabelPair") proto.RegisterType((*Sample)(nil), "cortexpb.Sample") diff --git a/internal/cortex/cortexpb/timeseries.go b/internal/cortex/cortexpb/timeseries.go index 3fc0a17f7c..58afee0899 100644 --- a/internal/cortex/cortexpb/timeseries.go +++ b/internal/cortex/cortexpb/timeseries.go @@ -276,37 +276,8 @@ func PreallocTimeseriesSliceFromPool() []PreallocTimeseries { return slicePool.Get().([]PreallocTimeseries) } -// ReuseSlice puts the slice back into a sync.Pool for reuse. -func ReuseSlice(ts []PreallocTimeseries) { - for i := range ts { - ReuseTimeseries(ts[i].TimeSeries) - } - - slicePool.Put(ts[:0]) //nolint:staticcheck //see comment on slicePool for more details -} - // TimeseriesFromPool retrieves a pointer to a TimeSeries from a sync.Pool. // ReuseTimeseries should be called once done, unless ReuseSlice was called on the slice that contains this TimeSeries. func TimeseriesFromPool() *TimeSeries { return timeSeriesPool.Get().(*TimeSeries) } - -// ReuseTimeseries puts the timeseries back into a sync.Pool for reuse. -func ReuseTimeseries(ts *TimeSeries) { - // Name and Value may point into a large gRPC buffer, so clear the reference to allow GC - for i := 0; i < len(ts.Labels); i++ { - ts.Labels[i].Name = "" - ts.Labels[i].Value = "" - } - ts.Labels = ts.Labels[:0] - ts.Samples = ts.Samples[:0] - // Name and Value may point into a large gRPC buffer, so clear the reference in each exemplar to allow GC - for i := range ts.Exemplars { - for j := range ts.Exemplars[i].Labels { - ts.Exemplars[i].Labels[j].Name = "" - ts.Exemplars[i].Labels[j].Value = "" - } - } - ts.Exemplars = ts.Exemplars[:0] - timeSeriesPool.Put(ts) -} diff --git a/internal/cortex/querier/queryrange/results_cache.go b/internal/cortex/querier/queryrange/results_cache.go index 2df79b744b..dd63ddcfdc 100644 --- a/internal/cortex/querier/queryrange/results_cache.go +++ b/internal/cortex/querier/queryrange/results_cache.go @@ -31,8 +31,6 @@ import ( "github.com/thanos-io/thanos/internal/cortex/cortexpb" "github.com/thanos-io/thanos/internal/cortex/querier" "github.com/thanos-io/thanos/internal/cortex/tenant" - "github.com/thanos-io/thanos/internal/cortex/util/flagext" - util_log "github.com/thanos-io/thanos/internal/cortex/util/log" "github.com/thanos-io/thanos/internal/cortex/util/spanlogger" "github.com/thanos-io/thanos/internal/cortex/util/validation" ) @@ -62,8 +60,6 @@ func (cfg *ResultsCacheConfig) RegisterFlags(f *flag.FlagSet) { f.StringVar(&cfg.Compression, "frontend.compression", "", "Use compression in results cache. Supported values are: 'snappy' and '' (disable compression).") f.BoolVar(&cfg.CacheQueryableSamplesStats, "frontend.cache-queryable-samples-stats", false, "Cache Statistics queryable samples on results cache.") - //lint:ignore faillint Need to pass the global logger like this for warning on deprecated methods - flagext.DeprecatedFlag(f, "frontend.cache-split-interval", "Deprecated: The maximum interval expected for each request, results will be cached per single interval. This behavior is now determined by querier.split-queries-by-interval.", util_log.Logger) } func (cfg *ResultsCacheConfig) Validate(qCfg querier.Config) error { diff --git a/internal/cortex/util/flagext/deprecated.go b/internal/cortex/util/flagext/deprecated.go deleted file mode 100644 index fca0d77742..0000000000 --- a/internal/cortex/util/flagext/deprecated.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) The Cortex Authors. -// Licensed under the Apache License 2.0. - -package flagext - -import ( - "flag" - - "github.com/go-kit/log" - "github.com/go-kit/log/level" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promauto" -) - -// DeprecatedFlagsUsed is the metric that counts deprecated flags set. -var DeprecatedFlagsUsed = promauto.NewCounter( - prometheus.CounterOpts{ - Name: "deprecated_flags_inuse_total", - Help: "The number of deprecated flags currently set.", - }) - -type deprecatedFlag struct { - name string - logger log.Logger -} - -func (deprecatedFlag) String() string { - return "deprecated" -} - -func (d deprecatedFlag) Set(string) error { - level.Warn(d.logger).Log("msg", "flag disabled", "flag", d.name) - DeprecatedFlagsUsed.Inc() - return nil -} - -// DeprecatedFlag logs a warning when you try to use it. -func DeprecatedFlag(f *flag.FlagSet, name, message string, logger log.Logger) { - f.Var(deprecatedFlag{name: name, logger: logger}, name, message) -} diff --git a/internal/cortex/util/flagext/register.go b/internal/cortex/util/flagext/register.go index 24b3bf2f46..7df2d8050f 100644 --- a/internal/cortex/util/flagext/register.go +++ b/internal/cortex/util/flagext/register.go @@ -10,13 +10,6 @@ type Registerer interface { RegisterFlags(*flag.FlagSet) } -// RegisterFlags registers flags with the provided Registerers -func RegisterFlags(rs ...Registerer) { - for _, r := range rs { - r.RegisterFlags(flag.CommandLine) - } -} - // DefaultValues initiates a set of configs (Registerers) with their defaults. func DefaultValues(rs ...Registerer) { fs := flag.NewFlagSet("", flag.PanicOnError) diff --git a/internal/cortex/util/log/experimental.go b/internal/cortex/util/log/experimental.go deleted file mode 100644 index d81b91b29a..0000000000 --- a/internal/cortex/util/log/experimental.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) The Cortex Authors. -// Licensed under the Apache License 2.0. - -package log - -import ( - "github.com/go-kit/log/level" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promauto" -) - -var experimentalFeaturesInUse = promauto.NewCounter( - prometheus.CounterOpts{ - Namespace: "cortex", - Name: "experimental_features_in_use_total", - Help: "The number of experimental features in use.", - }, -) - -// WarnExperimentalUse logs a warning and increments the experimental features metric. -func WarnExperimentalUse(feature string) { - level.Warn(Logger).Log("msg", "experimental feature in use", "feature", feature) - experimentalFeaturesInUse.Inc() -} diff --git a/internal/cortex/util/log/log.go b/internal/cortex/util/log/log.go index 1260f19aad..d20da85a75 100644 --- a/internal/cortex/util/log/log.go +++ b/internal/cortex/util/log/log.go @@ -4,15 +4,7 @@ package log import ( - "fmt" - "os" - - "github.com/go-kit/log" kitlog "github.com/go-kit/log" - "github.com/go-kit/log/level" - "github.com/prometheus/client_golang/prometheus" - "github.com/weaveworks/common/logging" - "github.com/weaveworks/common/server" ) var ( @@ -20,111 +12,4 @@ var ( // TODO: Change all components to take a non-global logger via their constructors. // Prefer accepting a non-global logger as an argument. Logger = kitlog.NewNopLogger() - - logMessages = prometheus.NewCounterVec(prometheus.CounterOpts{ - Name: "log_messages_total", - Help: "Total number of log messages.", - }, []string{"level"}) - - supportedLevels = []level.Value{ - level.DebugValue(), - level.InfoValue(), - level.WarnValue(), - level.ErrorValue(), - } ) - -func init() { - prometheus.MustRegister(logMessages) -} - -// InitLogger initialises the global gokit logger (util_log.Logger) and overrides the -// default logger for the server. -func InitLogger(cfg *server.Config) { - l, err := NewPrometheusLogger(cfg.LogLevel, cfg.LogFormat) - if err != nil { - panic(err) - } - - // when use util_log.Logger, skip 3 stack frames. - Logger = log.With(l, "caller", log.Caller(3)) - - // cfg.Log wraps log function, skip 4 stack frames to get caller information. - // this works in go 1.12, but doesn't work in versions earlier. - // it will always shows the wrapper function generated by compiler - // marked in old versions. - cfg.Log = logging.GoKit(log.With(l, "caller", log.Caller(4))) -} - -// PrometheusLogger exposes Prometheus counters for each of go-kit's log levels. -type PrometheusLogger struct { - logger log.Logger -} - -// NewPrometheusLogger creates a new instance of PrometheusLogger which exposes -// Prometheus counters for various log levels. -func NewPrometheusLogger(l logging.Level, format logging.Format) (log.Logger, error) { - logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)) - if format.String() == "json" { - logger = log.NewJSONLogger(log.NewSyncWriter(os.Stderr)) - } - logger = level.NewFilter(logger, LevelFilter(l.String())) - - // Initialise counters for all supported levels: - for _, level := range supportedLevels { - logMessages.WithLabelValues(level.String()) - } - - logger = &PrometheusLogger{ - logger: logger, - } - - // return a Logger without caller information, shouldn't use directly - logger = log.With(logger, "ts", log.DefaultTimestampUTC) - return logger, nil -} - -// Log increments the appropriate Prometheus counter depending on the log level. -func (pl *PrometheusLogger) Log(kv ...interface{}) error { - pl.logger.Log(kv...) - l := "unknown" - for i := 1; i < len(kv); i += 2 { - if v, ok := kv[i].(level.Value); ok { - l = v.String() - break - } - } - logMessages.WithLabelValues(l).Inc() - return nil -} - -// CheckFatal prints an error and exits with error code 1 if err is non-nil -func CheckFatal(location string, err error) { - if err != nil { - logger := level.Error(Logger) - if location != "" { - logger = log.With(logger, "msg", "error "+location) - } - // %+v gets the stack trace from errors using github.com/pkg/errors - logger.Log("err", fmt.Sprintf("%+v", err)) - os.Exit(1) - } -} - -// TODO(dannyk): remove once weaveworks/common updates to go-kit/log -// -// -> we can then revert to using Level.Gokit -func LevelFilter(l string) level.Option { - switch l { - case "debug": - return level.AllowDebug() - case "info": - return level.AllowInfo() - case "warn": - return level.AllowWarn() - case "error": - return level.AllowError() - default: - return level.AllowAll() - } -}