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
@@ -4,6 +4,7 @@ import (
"context"
"github.com/grafana/grafana/pkg/services/annotations/accesscontrol"
"github.com/grafana/grafana/pkg/services/annotations/annotationsimpl/loki"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
@@ -17,7 +18,8 @@ type RepositoryImpl struct {
db db.DB
authZ *accesscontrol.AuthService
features featuremgmt.FeatureToggles
store store
reader readStore
writer writeStore
}
func ProvideService(
@@ -27,27 +29,42 @@ func ProvideService(
tagService tag.Service,
) *RepositoryImpl {
l := log.New("annotations")
l.Debug("Initializing annotations service")
xormStore := NewXormStore(cfg, log.New("annotations.sql"), db, tagService)
write := xormStore
var read readStore
historianStore := loki.NewLokiHistorianStore(cfg.UnifiedAlerting.StateHistory, features, db, log.New("annotations.loki"))
if historianStore != nil {
l.Debug("Using composite read store")
read = NewCompositeStore(xormStore, historianStore)
} else {
l.Debug("Using xorm read store")
read = write
}
return &RepositoryImpl{
db: db,
features: features,
authZ: accesscontrol.NewAuthService(db, features),
store: NewXormStore(cfg, l, db, tagService),
reader: read,
writer: write,
}
}
func (r *RepositoryImpl) Save(ctx context.Context, item *annotations.Item) error {
return r.store.Add(ctx, item)
return r.writer.Add(ctx, item)
}
// SaveMany inserts multiple annotations at once.
// It does not return IDs associated with created annotations. If you need this functionality, use the single-item Save instead.
func (r *RepositoryImpl) SaveMany(ctx context.Context, items []annotations.Item) error {
return r.store.AddMany(ctx, items)
return r.writer.AddMany(ctx, items)
}
func (r *RepositoryImpl) Update(ctx context.Context, item *annotations.Item) error {
return r.store.Update(ctx, item)
return r.writer.Update(ctx, item)
}
func (r *RepositoryImpl) Find(ctx context.Context, query *annotations.ItemQuery) ([]*annotations.ItemDTO, error) {
@@ -56,13 +73,13 @@ func (r *RepositoryImpl) Find(ctx context.Context, query *annotations.ItemQuery)
return make([]*annotations.ItemDTO, 0), err
}
return r.store.Get(ctx, query, resources)
return r.reader.Get(ctx, query, resources)
}
func (r *RepositoryImpl) Delete(ctx context.Context, params *annotations.DeleteParams) error {
return r.store.Delete(ctx, params)
return r.writer.Delete(ctx, params)
}
func (r *RepositoryImpl) FindTags(ctx context.Context, query *annotations.TagsQuery) (annotations.FindTagsResult, error) {
return r.store.GetTags(ctx, query)
return r.reader.GetTags(ctx, query)
}