From 2143ff22d7021f91c332f9a7e181f186c95fb30a Mon Sep 17 00:00:00 2001 From: Marco Pracucci Date: Tue, 31 Oct 2023 10:05:03 +0100 Subject: [PATCH] Add tracing span to Ruler.getLocalRules() (#6515) * Add tracing span to Ruler.getLocalRules() Signed-off-by: Marco Pracucci * Fixed tests Signed-off-by: Marco Pracucci * Add nolint rule Signed-off-by: Marco Pracucci --------- Signed-off-by: Marco Pracucci --- pkg/ruler/ruler.go | 14 +++++++++++--- pkg/ruler/ruler_test.go | 4 ++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pkg/ruler/ruler.go b/pkg/ruler/ruler.go index 8da2a669f2b..1fb9d210949 100644 --- a/pkg/ruler/ruler.go +++ b/pkg/ruler/ruler.go @@ -41,6 +41,7 @@ import ( "github.com/grafana/mimir/pkg/storage/tsdb/bucketcache" "github.com/grafana/mimir/pkg/util" util_log "github.com/grafana/mimir/pkg/util/log" + "github.com/grafana/mimir/pkg/util/spanlogger" "github.com/grafana/mimir/pkg/util/validation" ) @@ -991,7 +992,7 @@ func (r *Ruler) Rules(ctx context.Context, in *RulesRequest) (*RulesResponse, er return nil, fmt.Errorf("no user id found in context") } - groupDescs, err := r.getLocalRules(userID, *in) + groupDescs, err := r.getLocalRules(ctx, userID, *in) if err != nil { return nil, err } @@ -1019,8 +1020,15 @@ func (fs StringFilterSet) IsFiltered(val string) bool { return !ok } -func (r *Ruler) getLocalRules(userID string, req RulesRequest) ([]*GroupStateDesc, error) { +func (r *Ruler) getLocalRules(ctx context.Context, userID string, req RulesRequest) ([]*GroupStateDesc, error) { + spanLog, _ := spanlogger.NewWithLogger(ctx, r.logger, "Ruler.getLocalRules") + defer spanLog.Finish() + + // Get the rule groups from the manager. We track the time it takes because the manager needs to + // take a lock to run GetRules() and we want to make sure we're not hanging here. + getRulesStart := time.Now() groups := r.manager.GetRules(userID) + spanLog.DebugLog("msg", "fetched rules from manager", "duration", time.Since(getRulesStart)) groupDescs := make([]*GroupStateDesc, 0, len(groups)) prefix := filepath.Join(r.cfg.RulePath, userID) + "/" @@ -1087,7 +1095,7 @@ func (r *Ruler) getLocalRules(userID string, req RulesRequest) ([]*GroupStateDes if !getAlertingRules { continue } - rule.ActiveAlerts() + alerts := []*AlertStateDesc{} for _, a := range rule.ActiveAlerts() { alerts = append(alerts, &AlertStateDesc{ diff --git a/pkg/ruler/ruler_test.go b/pkg/ruler/ruler_test.go index 872fb1b60b7..e10e4f24193 100644 --- a/pkg/ruler/ruler_test.go +++ b/pkg/ruler/ruler_test.go @@ -1218,7 +1218,7 @@ func TestRuler_NotifySyncRulesAsync_ShouldTriggerRulesSyncingAndCorrectlyHandleT var actualRulersWithRuleGroups int for _, ruler := range rulers { - actualRuleGroups, err := ruler.getLocalRules(userID, RulesRequest{Filter: AnyRule}) + actualRuleGroups, err := ruler.getLocalRules(ctx, userID, RulesRequest{Filter: AnyRule}) require.NoError(t, err) actualRuleGroupsCount += len(actualRuleGroups) @@ -1256,7 +1256,7 @@ func TestRuler_NotifySyncRulesAsync_ShouldTriggerRulesSyncingAndCorrectlyHandleT var actualRuleGroupsCountPerRuler []int for _, ruler := range rulers { - actualRuleGroups, err := ruler.getLocalRules(userID, RulesRequest{Filter: AnyRule}) + actualRuleGroups, err := ruler.getLocalRules(ctx, userID, RulesRequest{Filter: AnyRule}) require.NoError(t, err) actualRuleGroupsCountPerRuler = append(actualRuleGroupsCountPerRuler, len(actualRuleGroups)) }