mirror of
https://github.com/grafana/grafana.git
synced 2024-11-29 12:14:08 -06:00
Alerting: Elide requests to Loki if nothing should be recorded (#65011)
Exit early if no log streams or annotations
This commit is contained in:
parent
a0d440fb03
commit
e39d7f44c9
@ -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 {
|
||||||
|
@ -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)
|
||||||
|
|
||||||
|
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user