Annotations/Alerting: Add Loki historian store stub (#78363)

* Add Loki historian store stub

* Add composite store

* Use composite store if Loki historian enabled

* Split store interface into read/write

* Make composite + historian stores read only

* Use variadic constructor for composite

* Modify Loki store enable logic

* Use dskit.concurrency.ForEachJob for parallelism
This commit is contained in:
William Wernert
2023-12-12 17:43:09 -05:00
committed by GitHub
parent 09cef892a5
commit 62bdbe5b44
9 changed files with 486 additions and 14 deletions
@@ -0,0 +1,75 @@
package loki
import (
"context"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/annotations"
"github.com/grafana/grafana/pkg/services/annotations/accesscontrol"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/ngalert"
ngmetrics "github.com/grafana/grafana/pkg/services/ngalert/metrics"
"github.com/grafana/grafana/pkg/services/ngalert/state/historian"
"github.com/grafana/grafana/pkg/setting"
"github.com/prometheus/client_golang/prometheus"
)
const (
subsystem = "annotations"
)
type lokiQueryClient interface {
RangeQuery(ctx context.Context, query string, start, end, limit int64) (historian.QueryRes, error)
}
// LokiHistorianStore is a read store that queries Loki for alert state history.
type LokiHistorianStore struct {
client lokiQueryClient
db db.DB
log log.Logger
}
func NewLokiHistorianStore(cfg setting.UnifiedAlertingStateHistorySettings, ft featuremgmt.FeatureToggles, db db.DB, log log.Logger) *LokiHistorianStore {
if !useStore(cfg, ft) {
return nil
}
lokiCfg, err := historian.NewLokiConfig(cfg)
if err != nil {
// this config error is already handled elsewhere
return nil
}
return &LokiHistorianStore{
client: historian.NewLokiClient(lokiCfg, historian.NewRequester(), ngmetrics.NewHistorianMetrics(prometheus.DefaultRegisterer, subsystem), log),
db: db,
log: log,
}
}
func (r *LokiHistorianStore) Get(ctx context.Context, query *annotations.ItemQuery, accessResources *accesscontrol.AccessResources) ([]*annotations.ItemDTO, error) {
return []*annotations.ItemDTO{}, nil
}
func (r *LokiHistorianStore) GetTags(ctx context.Context, query *annotations.TagsQuery) (annotations.FindTagsResult, error) {
return annotations.FindTagsResult{}, nil
}
func useStore(cfg setting.UnifiedAlertingStateHistorySettings, ft featuremgmt.FeatureToggles) bool {
if !cfg.Enabled {
return false
}
// Override config based on feature toggles.
// We pass in a no-op logger here since this function is also called during ngalert init,
// and we don't want to log the same problem twice.
ngalert.ApplyStateHistoryFeatureToggles(&cfg, ft, log.NewNopLogger())
backend, err := historian.ParseBackendType(cfg.Backend)
if err != nil {
return false
}
// We should only query Loki if annotations do no exist in the database.
return backend == historian.BackendTypeLoki
}
@@ -0,0 +1,99 @@
package loki
import (
"testing"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/require"
)
func TestUseStore(t *testing.T) {
t.Run("false if state history disabled", func(t *testing.T) {
cfg := setting.UnifiedAlertingStateHistorySettings{
Enabled: false,
}
use := useStore(cfg, featuremgmt.WithFeatures())
require.False(t, use)
})
t.Run("false if any invalid backend", func(t *testing.T) {
t.Run("single", func(t *testing.T) {
cfg := setting.UnifiedAlertingStateHistorySettings{
Enabled: true,
Backend: "invalid-backend",
}
use := useStore(cfg, featuremgmt.WithFeatures())
require.False(t, use)
})
t.Run("primary", func(t *testing.T) {
cfg := setting.UnifiedAlertingStateHistorySettings{
Enabled: true,
Backend: "multiple",
MultiPrimary: "invalid-backend",
}
use := useStore(cfg, featuremgmt.WithFeatures())
require.False(t, use)
})
t.Run("secondary", func(t *testing.T) {
cfg := setting.UnifiedAlertingStateHistorySettings{
Enabled: true,
Backend: "multiple",
MultiPrimary: "annotations",
MultiSecondaries: []string{"annotations", "invalid-backend"},
}
use := useStore(cfg, featuremgmt.WithFeatures())
require.False(t, use)
})
})
t.Run("false if no backend is Loki", func(t *testing.T) {
cfg := setting.UnifiedAlertingStateHistorySettings{
Enabled: true,
Backend: "annotations",
}
use := useStore(cfg, featuremgmt.WithFeatures())
require.False(t, use)
})
t.Run("false if Loki is part of multi backend", func(t *testing.T) {
t.Run("primary", func(t *testing.T) {
cfg := setting.UnifiedAlertingStateHistorySettings{
Enabled: true,
Backend: "multiple",
MultiPrimary: "loki",
}
use := useStore(cfg, featuremgmt.WithFeatures())
require.False(t, use)
})
t.Run("secondary", func(t *testing.T) {
cfg := setting.UnifiedAlertingStateHistorySettings{
Enabled: true,
Backend: "multiple",
MultiPrimary: "annotations",
MultiSecondaries: []string{"loki"},
}
use := useStore(cfg, featuremgmt.WithFeatures())
require.False(t, use)
})
})
t.Run("true if only backend is Loki", func(t *testing.T) {
t.Run("only", func(t *testing.T) {
cfg := setting.UnifiedAlertingStateHistorySettings{
Enabled: true,
Backend: "loki",
}
features := featuremgmt.WithFeatures(
featuremgmt.FlagAlertStateHistoryLokiOnly,
featuremgmt.FlagAlertStateHistoryLokiPrimary,
featuremgmt.FlagAlertStateHistoryLokiSecondary,
)
use := useStore(cfg, features)
require.True(t, use)
})
})
}