feat: implement CancelOpenSpansByName method and related tests

This commit introduces the CancelOpenSpansByName method in the KnowledgeSpanRepository, allowing for the cancellation of open spans by their name for a specific knowledge ID and attempt. This functionality is crucial for managing spans during retries or server restarts, preventing duplicate entries in the trace tree. Additionally, a new test case, TestKnowledgeSpanRepo_CancelOpenSpansByName, has been added to ensure the correct behavior of this method, verifying that only the intended spans are cancelled while others remain unaffected. This enhancement improves the robustness of span management in the application.
This commit is contained in:
wizardchen
2026-05-28 20:09:49 +08:00
committed by lyingbug
parent e3525f884b
commit f1a27e0e18
4 changed files with 91 additions and 0 deletions
@@ -369,6 +369,14 @@ func (t *spanTracker) BeginSubSpan(ctx context.Context, parent *Span, name, kind
if kind != types.SpanKindGeneration && kind != types.SpanKindSubSpan {
kind = types.SpanKindSubSpan
}
// Asynq retry / server restart can re-run the same handler while the
// previous invocation's span is still status=running (worker died
// without EndSpan). Cancel same-name open rows so the UI shows one
// logical subspan per (attempt, name) instead of duplicate stripes.
if _, err := t.repo.CancelOpenSpansByName(ctx, parent.KnowledgeID, parent.Attempt, name,
"TASK_SUPERSEDED", "superseded by a new run of the same subtask"); err != nil {
logger.Warnf(ctx, "[SpanTracker] supersede %s before BeginSubSpan failed: %v", name, err)
}
now := time.Now()
id := newSpanID()
row := &types.KnowledgeProcessingSpan{