Alerting: Elide requests to Loki if nothing should be recorded (#65011)

Exit early if no log streams or annotations
This commit is contained in:
Alexander Weaver 2023-03-21 09:30:56 -05:00 committed by GitHub
parent a0d440fb03
commit e39d7f44c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 4 deletions

View File

@ -62,6 +62,11 @@ func (h *AnnotationBackend) Record(ctx context.Context, rule history_model.RuleM
panel := parsePanelKey(rule, logger) panel := parsePanelKey(rule, logger)
errCh := make(chan error, 1) errCh := make(chan error, 1)
if len(annotations) == 0 {
close(errCh)
return errCh
}
go func() { go func() {
defer close(errCh) defer close(errCh)
errCh <- h.recordAnnotations(ctx, panel, annotations, rule.OrgID, logger) 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 { 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 { if panel != nil {
dashID, err := h.dashboards.getID(ctx, panel.orgID, panel.dashUID) dashID, err := h.dashboards.getID(ctx, panel.orgID, panel.dashUID)
if err != nil { if err != nil {

View File

@ -72,7 +72,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 { func (h *RemoteLokiBackend) Record(ctx context.Context, rule history_model.RuleMeta, states []state.StateTransition) <-chan error {
logger := h.log.FromContext(ctx) logger := h.log.FromContext(ctx)
streams := statesToStreams(rule, states, h.externalLabels, logger) streams := statesToStreams(rule, states, h.externalLabels, logger)
errCh := make(chan error, 1) errCh := make(chan error, 1)
if len(streams) == 0 {
close(errCh)
return errCh
}
go func() { go func() {
defer close(errCh) defer close(errCh)

View File

@ -311,6 +311,18 @@ grafana_alerting_state_history_writes_total{org="1"} 2
) )
require.NoError(t, err) 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 { func createTestLokiBackend(req client.Requester, met *metrics.Historian) *RemoteLokiBackend {