From 4b6a015256f5deb9cf5555fefa5652ec23c17cf0 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Tue, 21 Mar 2023 10:50:47 -0400 Subject: [PATCH] [v9.4.x] Alerting: Elide requests to Loki if nothing should be recorded (#65118) Alerting: Elide requests to Loki if nothing should be recorded (#65011) Exit early if no log streams or annotations (cherry picked from commit e39d7f44c94367d157e4e823913c77f06128d21d) Co-authored-by: Alexander Weaver --- pkg/services/ngalert/state/historian/annotation.go | 9 +++++---- pkg/services/ngalert/state/historian/loki.go | 6 ++++++ pkg/services/ngalert/state/historian/loki_test.go | 12 ++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/pkg/services/ngalert/state/historian/annotation.go b/pkg/services/ngalert/state/historian/annotation.go index 4333488c26c..c13de2bf6a1 100644 --- a/pkg/services/ngalert/state/historian/annotation.go +++ b/pkg/services/ngalert/state/historian/annotation.go @@ -62,6 +62,11 @@ func (h *AnnotationBackend) Record(ctx context.Context, rule history_model.RuleM panel := parsePanelKey(rule, logger) errCh := make(chan error, 1) + if len(annotations) == 0 { + close(errCh) + return errCh + } + go func() { defer close(errCh) errCh <- h.recordAnnotations(ctx, panel, annotations, rule.OrgID, logger) @@ -180,10 +185,6 @@ func buildAnnotations(rule history_model.RuleMeta, states []state.StateTransitio } func (h *AnnotationBackend) recordAnnotations(ctx context.Context, panel *panelKey, annotations []annotations.Item, orgID int64, logger log.Logger) error { - if len(annotations) == 0 { - return nil - } - if panel != nil { dashID, err := h.dashboards.getID(ctx, panel.orgID, panel.dashUID) if err != nil { diff --git a/pkg/services/ngalert/state/historian/loki.go b/pkg/services/ngalert/state/historian/loki.go index 6849fdcaa1a..0423a7dc7b3 100644 --- a/pkg/services/ngalert/state/historian/loki.go +++ b/pkg/services/ngalert/state/historian/loki.go @@ -71,7 +71,13 @@ func (h *RemoteLokiBackend) TestConnection(ctx context.Context) error { func (h *RemoteLokiBackend) Record(ctx context.Context, rule history_model.RuleMeta, states []state.StateTransition) <-chan error { logger := h.log.FromContext(ctx) streams := statesToStreams(rule, states, h.externalLabels, logger) + errCh := make(chan error, 1) + if len(streams) == 0 { + close(errCh) + return errCh + } + go func() { defer close(errCh) diff --git a/pkg/services/ngalert/state/historian/loki_test.go b/pkg/services/ngalert/state/historian/loki_test.go index 8d93474089b..3fd4f540b6d 100644 --- a/pkg/services/ngalert/state/historian/loki_test.go +++ b/pkg/services/ngalert/state/historian/loki_test.go @@ -311,6 +311,18 @@ grafana_alerting_state_history_writes_total{org="1"} 2 ) require.NoError(t, err) }) + + t.Run("elides request if nothing to send", func(t *testing.T) { + req := NewFakeRequester() + loki := createTestLokiBackend(req, metrics.NewHistorianMetrics(prometheus.NewRegistry())) + rule := createTestRule() + states := []state.StateTransition{} + + err := <-loki.Record(context.Background(), rule, states) + + require.NoError(t, err) + require.Nil(t, req.lastRequest) + }) } func createTestLokiBackend(req client.Requester, met *metrics.Historian) *RemoteLokiBackend {