85c794d932
Alerting: Create new state history "fanout" backend that dispatches to multiple other backends at once (#64774)
* Rename RecordStatesAsync to Record
* Rename QueryStates to Query
* Implement fanout writes
* Implement primary queries
* Simplify error joining
* Add test for query path
* Add tests for writes and error propagation
* Allow fanout backend to be configured
* Touch up log messages and config validation
* Consistent documentation for all backend structs
* Parse and normalize backend names more consistently against an enum
* Touch-ups to documentation
* Improve clarity around multi-record blocking
* Keep primary and secondaries more distinct
* Rename fanout backend to multiple backend
* Simplify config keys for multi backend mode
(cherry picked from commit a31672fa40)
Co-authored-by: Alexander Weaver <weaver.alex.d@gmail.com>
53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
)
|
|
|
|
type Historian interface {
|
|
Query(ctx context.Context, query models.HistoryQuery) (*data.Frame, error)
|
|
}
|
|
|
|
type HistorySrv struct {
|
|
logger log.Logger
|
|
hist Historian
|
|
}
|
|
|
|
const labelQueryPrefix = "labels_"
|
|
|
|
func (srv *HistorySrv) RouteQueryStateHistory(c *contextmodel.ReqContext) response.Response {
|
|
from := c.QueryInt64("from")
|
|
to := c.QueryInt64("to")
|
|
ruleUID := c.Query("ruleUID")
|
|
|
|
labels := make(map[string]string)
|
|
for k, v := range c.Req.URL.Query() {
|
|
if strings.HasPrefix(k, labelQueryPrefix) {
|
|
labels[k[len(labelQueryPrefix):]] = v[0]
|
|
}
|
|
}
|
|
|
|
query := models.HistoryQuery{
|
|
RuleUID: ruleUID,
|
|
OrgID: c.OrgID,
|
|
SignedInUser: c.SignedInUser,
|
|
From: time.Unix(from, 0),
|
|
To: time.Unix(to, 0),
|
|
Labels: labels,
|
|
}
|
|
frame, err := srv.hist.Query(c.Req.Context(), query)
|
|
if err != nil {
|
|
return ErrResp(http.StatusInternalServerError, err, "")
|
|
}
|
|
return response.JSON(http.StatusOK, frame)
|
|
}
|