@@ -20,6 +20,7 @@ import (
2020	"fmt" 
2121	"regexp" 
2222	"strconv" 
23+ 	"strings" 
2324	"time" 
2425
2526	"go.uber.org/zap" 
@@ -204,12 +205,26 @@ func (r *redisCounterDeleter) scan(environmentId, kind, key string) ([]string, e
204205	return  keys , nil 
205206}
206207
208+ func  (r  * redisCounterDeleter ) isTemporaryKey (key  string ) bool  {
209+ 	// Check if the key contains patterns for temporary keys 
210+ 	// pfmerge-key: temporary key used for HyperLogLog PFMERGE operations 
211+ 	return  strings .Contains (key , "pfmerge-key" )
212+ }
213+ 
207214func  (r  * redisCounterDeleter ) filterKeysOlderThanThirtyOneDays (
208215	environmentId , kind  string ,
209216	keys  []string ,
210217) ([]string , error ) {
211218	filteredKeys  :=  make ([]string , 0 , len (keys ))
219+ 	skippedKeys  :=  make ([]string , 0 )
212220	for  _ , key  :=  range  keys  {
221+ 		// Skip temporary keys like pfmerge-key 
222+ 		// These keys have TTL (10 minutes) and will be automatically deleted 
223+ 		if  r .isTemporaryKey (key ) {
224+ 			skippedKeys  =  append (skippedKeys , key )
225+ 			continue 
226+ 		}
227+ 
213228		// E.g. environment_id:uc:1689835532:feature_id:variation_id 
214229		var  regex  string 
215230		if  environmentId  ==  ""  {
@@ -243,6 +258,21 @@ func (r *redisCounterDeleter) filterKeysOlderThanThirtyOneDays(
243258		}
244259		filteredKeys  =  append (filteredKeys , key )
245260	}
261+ 
262+ 	if  len (skippedKeys ) >  0  {
263+ 		// Log only a sample of skipped keys to avoid log flooding 
264+ 		sampleSize  :=  len (skippedKeys )
265+ 		if  sampleSize  >  5  {
266+ 			sampleSize  =  5 
267+ 		}
268+ 		r .logger .Warn ("Skipped temporary keys" ,
269+ 			zap .String ("environmentId" , environmentId ),
270+ 			zap .String ("kind" , kind ),
271+ 			zap .Int ("skippedCount" , len (skippedKeys )),
272+ 			zap .Strings ("examples" , skippedKeys [:sampleSize ]),
273+ 		)
274+ 	}
275+ 
246276	return  filteredKeys , nil 
247277}
248278
0 commit comments