-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Sent Cache to an interface for future expansion (#561)
## Which problem is this PR solving? This is a prep PR for further work on the sent trace cache. For improved scalability, we want to be able to store trace decision records for a longer time. The best way to implement this in a backwards-compatible way is to pull the mechanisms for managing decision records into an interface, and then implement the interface with the legacy logic. That's what this does. There are no expected changes in behavior, and all tests still pass. ## Short description of the changes - Define interfaces for a TraceSentCache and a TraceSentRecord - Implement code for those that duplicates the existing legacy logic - Refactor the places the code is used to use the new interfaces - Tweak span count data type so that it's not an int64 any more - Rename SpanCount to DescendantCount because now that we have links and events, they're not all Spans anymore, and future PRs will add additional counting functions. I haven't renamed the corresponding Get and Add functions because that's pretty messy.
- Loading branch information
Showing
8 changed files
with
133 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cache | ||
|
||
import ( | ||
lru "github.com/hashicorp/golang-lru" | ||
"github.com/honeycombio/refinery/types" | ||
) | ||
|
||
// legacySentRecord is Refinery's original traceSent cache. It keeps the same records | ||
// for both kept and dropped traces and the size of the sent cache is set based on the size | ||
// of the live trace cache. | ||
|
||
// legacySentRecord is an internal record we leave behind when sending a trace to remember | ||
// our decision for the future, so any delinquent spans that show up later can | ||
// be dropped or passed along. | ||
type legacySentRecord struct { | ||
keep bool // true if the trace was kept, false if it was dropped | ||
rate uint // sample rate used when sending the trace | ||
spanCount uint // number of spans in the trace (we decorate the root span with this) | ||
} | ||
|
||
func (t *legacySentRecord) Kept() bool { | ||
return t.keep | ||
} | ||
|
||
func (t *legacySentRecord) Rate() uint { | ||
return t.rate | ||
} | ||
|
||
func (t *legacySentRecord) DescendantCount() uint { | ||
return uint(t.spanCount) | ||
} | ||
|
||
func (t *legacySentRecord) Count(*types.Span) { | ||
t.spanCount++ | ||
} | ||
|
||
// Make sure it implements TraceSentRecord | ||
var _ TraceSentRecord = (*legacySentRecord)(nil) | ||
|
||
type legacySentCache struct { | ||
sentTraceCache *lru.Cache | ||
} | ||
|
||
// Make sure it implements TraceSentCache | ||
var _ TraceSentCache = (*legacySentCache)(nil) | ||
|
||
func NewLegacySentCache(capacity int) (TraceSentCache, error) { | ||
stc, err := lru.New(capacity) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &legacySentCache{sentTraceCache: stc}, nil | ||
} | ||
|
||
func (c *legacySentCache) Record(trace *types.Trace, keep bool) { | ||
// record this decision in the sent record LRU for future spans | ||
sentRecord := legacySentRecord{ | ||
keep: keep, | ||
rate: trace.SampleRate, | ||
spanCount: trace.DescendantCount(), | ||
} | ||
c.sentTraceCache.Add(trace.TraceID, &sentRecord) | ||
} | ||
|
||
func (c *legacySentCache) Check(span *types.Span) (TraceSentRecord, bool) { | ||
if sentRecord, found := c.sentTraceCache.Get(span.TraceID); found { | ||
if sr, ok := sentRecord.(*legacySentRecord); ok { | ||
sr.Count(span) | ||
return sr, true | ||
} | ||
} | ||
return nil, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cache | ||
|
||
import ( | ||
"github.com/honeycombio/refinery/types" | ||
) | ||
|
||
type TraceSentRecord interface { | ||
// Kept returns whether the trace was kept (sampled and sent to honeycomb) or dropped. | ||
Kept() bool | ||
// Rate() returns the sample rate for the trace | ||
Rate() uint | ||
// DescendantCount returns the count of items associated with the trace, including all types of children like span links and span events. | ||
DescendantCount() uint | ||
// Count records additional spans in the totals | ||
Count(*types.Span) | ||
} | ||
|
||
type TraceSentCache interface { | ||
// Record preserves the record of a trace being sent or not. | ||
Record(trace *types.Trace, keep bool) | ||
// Check tests if a trace corresponding to the span is in the cache; if found, it returns the appropriate TraceSentRecord and true, | ||
// else nil and false. | ||
Check(span *types.Span) (TraceSentRecord, bool) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.direnv | ||
.tool-versions | ||
__* | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters