|
|
|
|
@ -50,7 +50,8 @@ func NewLogResultCacheMetrics(registerer prometheus.Registerer) *LogResultCacheM |
|
|
|
|
// Log hits are difficult to handle because of the limit query parameter and the size of the response.
|
|
|
|
|
// In the future it could be extended to cache non-empty query results.
|
|
|
|
|
// see https://docs.google.com/document/d/1_mACOpxdWZ5K0cIedaja5gzMbv-m0lUVazqZd2O4mEU/edit
|
|
|
|
|
func NewLogResultCache(logger log.Logger, limits Limits, cache cache.Cache, shouldCache queryrangebase.ShouldCacheFn, metrics *LogResultCacheMetrics) queryrangebase.Middleware { |
|
|
|
|
func NewLogResultCache(logger log.Logger, limits Limits, cache cache.Cache, shouldCache queryrangebase.ShouldCacheFn, |
|
|
|
|
transformer UserIDTransformer, metrics *LogResultCacheMetrics) queryrangebase.Middleware { |
|
|
|
|
if metrics == nil { |
|
|
|
|
metrics = NewLogResultCacheMetrics(nil) |
|
|
|
|
} |
|
|
|
|
@ -61,6 +62,7 @@ func NewLogResultCache(logger log.Logger, limits Limits, cache cache.Cache, shou |
|
|
|
|
cache: cache, |
|
|
|
|
logger: logger, |
|
|
|
|
shouldCache: shouldCache, |
|
|
|
|
transformer: transformer, |
|
|
|
|
metrics: metrics, |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
@ -71,6 +73,7 @@ type logResultCache struct { |
|
|
|
|
limits Limits |
|
|
|
|
cache cache.Cache |
|
|
|
|
shouldCache queryrangebase.ShouldCacheFn |
|
|
|
|
transformer UserIDTransformer |
|
|
|
|
|
|
|
|
|
metrics *LogResultCacheMetrics |
|
|
|
|
logger log.Logger |
|
|
|
|
@ -105,7 +108,17 @@ func (l *logResultCache) Do(ctx context.Context, req queryrangebase.Request) (qu |
|
|
|
|
// The first subquery might not be aligned.
|
|
|
|
|
alignedStart := time.Unix(0, lokiReq.GetStartTs().UnixNano()-(lokiReq.GetStartTs().UnixNano()%interval.Nanoseconds())) |
|
|
|
|
// generate the cache key based on query, tenant and start time.
|
|
|
|
|
cacheKey := fmt.Sprintf("log:%s:%s:%d:%d", tenant.JoinTenantIDs(tenantIDs), req.GetQuery(), interval.Nanoseconds(), alignedStart.UnixNano()/(interval.Nanoseconds())) |
|
|
|
|
|
|
|
|
|
transformedTenantIDs := tenantIDs |
|
|
|
|
if l.transformer != nil { |
|
|
|
|
transformedTenantIDs = make([]string, 0, len(tenantIDs)) |
|
|
|
|
|
|
|
|
|
for _, tenantID := range tenantIDs { |
|
|
|
|
transformedTenantIDs = append(transformedTenantIDs, l.transformer(ctx, tenantID)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cacheKey := fmt.Sprintf("log:%s:%s:%d:%d", tenant.JoinTenantIDs(transformedTenantIDs), req.GetQuery(), interval.Nanoseconds(), alignedStart.UnixNano()/(interval.Nanoseconds())) |
|
|
|
|
|
|
|
|
|
_, buff, _, err := l.cache.Fetch(ctx, []string{cache.HashKey(cacheKey)}) |
|
|
|
|
if err != nil { |
|
|
|
|
|