Skip to content

Commit

Permalink
fix: periodically clean up recent_dropped_traces cache (#1312)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?

The recent_dropped_traces cache wasn't getting cleaned up

## Short description of the changes

- update documentation on the `SetWithTTL`
- make sure the cache is cleaned up periodically

Co-authored-by: Kent Quirk <kentquirk@honeycomb.io>
  • Loading branch information
VinozzZ and kentquirk authored Sep 4, 2024
1 parent 290152a commit 8cb02d6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions collect/cache/cuckooSentCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (t *cuckooDroppedRecord) Reason() uint {
var _ TraceSentRecord = (*cuckooDroppedRecord)(nil)

type cuckooSentCache struct {
met metrics.Metrics
kept *lru.Cache[string, *keptTraceCacheEntry]
dropped *CuckooTraceChecker
recentDroppedIDs *generics.SetWithTTL[string]
Expand Down Expand Up @@ -179,7 +180,10 @@ func NewCuckooSentCache(cfg config.SampleCacheConfig, met metrics.Metrics) (Trac
// request.
recentDroppedIDs := generics.NewSetWithTTL[string](3 * time.Second)

met.Register("cache_recent_dropped_traces", "gauge")

cache := &cuckooSentCache{
met: met,
kept: stc,
dropped: dropped,
recentDroppedIDs: recentDroppedIDs,
Expand All @@ -198,6 +202,10 @@ func (c *cuckooSentCache) monitor() {
select {
case <-ticker.C:
c.dropped.Maintain()
// Length() returns the number of items in the cache and it will
// clean up any expired items.
numOfDroppedIDs := c.recentDroppedIDs.Length()
c.met.Gauge("cache_recent_dropped_traces", numOfDroppedIDs)
case <-c.done:
return
}
Expand Down
2 changes: 1 addition & 1 deletion generics/setttl.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// SetWithTTL is a unique set of items with a TTL (time to live) for each item.
// After the TTL expires, the item is automatically removed from the set.
// After the TTL expires, the item is automatically removed from the set when either Members or Length is called.
// It is safe for concurrent use.
type SetWithTTL[T cmp.Ordered] struct {
Items map[T]time.Time
Expand Down

0 comments on commit 8cb02d6

Please sign in to comment.