Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a limit to queue draining logic #1441

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions collect/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func (i *InMemCollector) collect() {
span.End()
return
}
count := drainSpanQueue(ctx, sp, i.fromPeer, "peer", i.processSpan)
count := drainSpanQueue(ctx, sp, i.fromPeer, "peer", 1000, i.processSpan)
i.Metrics.Gauge("num_span_drained_from_peer", count)

case sp, ok := <-i.incoming:
Expand All @@ -428,7 +428,7 @@ func (i *InMemCollector) collect() {
span.End()
return
}
count := drainSpanQueue(ctx, sp, i.incoming, "incoming", i.processSpan)
count := drainSpanQueue(ctx, sp, i.incoming, "incoming", 1000, i.processSpan)
i.Metrics.Gauge("num_span_drained_from_incoming", count)
case msg, ok := <-i.dropDecisionMessages:
if !ok {
Expand Down Expand Up @@ -464,15 +464,23 @@ func (i *InMemCollector) collect() {
}
}

func drainSpanQueue(ctx context.Context, span *types.Span, ch <-chan *types.Span, queueName string, processSpanFunc func(context.Context, *types.Span, string)) int {
func drainSpanQueue(ctx context.Context, span *types.Span, ch <-chan *types.Span, queueName string, limit int, processSpanFunc func(context.Context, *types.Span, string)) int {
// process the original span
processSpanFunc(ctx, span, queueName)
count := 1

// let't try to process as many spans as we can in the next 100ms
// TODO: make timer configurable?
timer := time.NewTimer(time.Millisecond * 100)
defer timer.Stop()

for {

// if we've processed enough spans, we should return
if limit != 0 && count >= limit {
return count
}

select {
case <-timer.C:
// we've spent enough time processing spans
Expand Down Expand Up @@ -1568,6 +1576,7 @@ func (i *InMemCollector) publishTraceDecision(ctx context.Context, td TraceDecis

if td.Kept {
select {
case <-i.done:
case i.keptDecisionBuffer <- td:
default:
i.Metrics.Increment("collector_kept_decisions_queue_full")
Expand All @@ -1576,6 +1585,7 @@ func (i *InMemCollector) publishTraceDecision(ctx context.Context, td TraceDecis
return
} else {
select {
case <-i.done:
case i.dropDecisionBuffer <- td:
default:
i.Metrics.Increment("collector_drop_decisions_queue_full")
Expand Down