From 38e71d62bc34b9db2c29669477cfcfed70927ffa Mon Sep 17 00:00:00 2001 From: Kaviraj Date: Tue, 20 Feb 2024 08:38:07 +0100 Subject: [PATCH] Add cache hit log lines for instant metric query Signed-off-by: Kaviraj --- cmd/loki/loki-local-with-memcached.yaml | 11 +++++++++++ pkg/logql/metrics.go | 17 ++++++++++++----- pkg/querier/queryrange/instant_metric_cache.go | 4 +++- pkg/querier/queryrange/split_by_range.go | 2 +- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/cmd/loki/loki-local-with-memcached.yaml b/cmd/loki/loki-local-with-memcached.yaml index d1b0ae1c2493c..a2f4336cdd484 100644 --- a/cmd/loki/loki-local-with-memcached.yaml +++ b/cmd/loki/loki-local-with-memcached.yaml @@ -22,6 +22,17 @@ query_range: cache_results: true cache_volume_results: true cache_series_results: true + cache_instant_metric_results: true + instant_metric_query_split_align: true + instant_metric_results_cache: + cache: + default_validity: 12h + memcached_client: + consistent_hash: true + addresses: "dns+localhost:11211" + max_idle_conns: 16 + timeout: 500ms + update_interval: 1m series_results_cache: cache: default_validity: 12h diff --git a/pkg/logql/metrics.go b/pkg/logql/metrics.go index 40fbece82d87d..b55e9840a4758 100644 --- a/pkg/logql/metrics.go +++ b/pkg/logql/metrics.go @@ -94,7 +94,8 @@ func RecordRangeAndInstantQueryMetrics( ) { var ( logger = fixLogger(ctx, log) - rt = string(GetRangeType(p)) + rangeType = GetRangeType(p) + rt = string(rangeType) latencyType = latencyTypeFast returnedLines = 0 ) @@ -103,6 +104,12 @@ func RecordRangeAndInstantQueryMetrics( level.Warn(logger).Log("msg", "error parsing query type", "err", err) } + resultCache := stats.Caches.Result + + if queryType == QueryTypeMetric && rangeType == InstantType { + resultCache = stats.Caches.InstantMetricResult + } + // Tag throughput metric by latency type based on a threshold. // Latency below the threshold is fast, above is slow. if stats.Summary.ExecTime > slowQueryThresholdSecond { @@ -162,10 +169,10 @@ func RecordRangeAndInstantQueryMetrics( "cache_volume_results_req", stats.Caches.VolumeResult.EntriesRequested, "cache_volume_results_hit", stats.Caches.VolumeResult.EntriesFound, "cache_volume_results_download_time", stats.Caches.VolumeResult.CacheDownloadTime(), - "cache_result_req", stats.Caches.Result.EntriesRequested, - "cache_result_hit", stats.Caches.Result.EntriesFound, - "cache_result_download_time", stats.Caches.Result.CacheDownloadTime(), - "cache_result_query_length_served", stats.Caches.Result.CacheQueryLengthServed(), + "cache_result_req", resultCache.EntriesRequested, + "cache_result_hit", resultCache.EntriesFound, + "cache_result_download_time", resultCache.CacheDownloadTime(), + "cache_result_query_length_served", resultCache.CacheQueryLengthServed(), }...) logValues = append(logValues, tagsToKeyValues(queryTags)...) diff --git a/pkg/querier/queryrange/instant_metric_cache.go b/pkg/querier/queryrange/instant_metric_cache.go index cf4d4639b4c19..ef1083e6cd229 100644 --- a/pkg/querier/queryrange/instant_metric_cache.go +++ b/pkg/querier/queryrange/instant_metric_cache.go @@ -49,6 +49,8 @@ func (cfg *InstantMetricCacheConfig) Validate() error { return cfg.ResultsCacheConfig.Validate() } +type instantMetricExtractor struct{} + func NewInstantMetricCacheMiddleware( log log.Logger, limits Limits, @@ -67,7 +69,7 @@ func NewInstantMetricCacheMiddleware( InstantMetricSplitter{limits, transformer}, limits, merger, - queryrangebase.PrometheusResponseExtractor{}, + PrometheusExtractor{}, cacheGenNumberLoader, func(ctx context.Context, r queryrangebase.Request) bool { if shouldCache != nil && !shouldCache(ctx, r) { diff --git a/pkg/querier/queryrange/split_by_range.go b/pkg/querier/queryrange/split_by_range.go index 3e570350303ef..16076cd948596 100644 --- a/pkg/querier/queryrange/split_by_range.go +++ b/pkg/querier/queryrange/split_by_range.go @@ -62,7 +62,7 @@ func (s *splitByRange) Do(ctx context.Context, request queryrangebase.Request) ( return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) } - interval := validation.SmallestPositiveNonZeroDurationPerTenant(tenants, s.limits.QuerySplitDuration) + interval := validation.SmallestPositiveNonZeroDurationPerTenant(tenants, s.limits.InstantMetricQuerySplitDuration) // if no interval configured, continue to the next middleware if interval == 0 { return s.next.Do(ctx, request)