Alerting: Add dashboardUID and panelID query parameters for loki state history (#72119)

* read query parameters

* Generate loki query from params
This commit is contained in:
Alexander Weaver 2023-07-24 23:46:46 -05:00 committed by GitHub
parent bc0bf0ee4b
commit 8c8b3ecb5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 1 deletions

View File

@ -29,6 +29,8 @@ func (srv *HistorySrv) RouteQueryStateHistory(c *contextmodel.ReqContext) respon
to := c.QueryInt64("to")
limit := c.QueryInt("limit")
ruleUID := c.Query("ruleUID")
dashUID := c.Query("dashboardUID")
panelID := c.QueryInt64("panelID")
labels := make(map[string]string)
for k, v := range c.Req.URL.Query() {
@ -40,6 +42,8 @@ func (srv *HistorySrv) RouteQueryStateHistory(c *contextmodel.ReqContext) respon
query := models.HistoryQuery{
RuleUID: ruleUID,
OrgID: c.OrgID,
DashboardUID: dashUID,
PanelID: panelID,
SignedInUser: c.SignedInUser,
From: time.Unix(from, 0),
To: time.Unix(to, 0),

View File

@ -10,6 +10,8 @@ import (
type HistoryQuery struct {
RuleUID string
OrgID int64
DashboardUID string
PanelID int64
Labels map[string]string
From time.Time
To time.Time

View File

@ -377,6 +377,12 @@ func buildLogQuery(query models.HistoryQuery) (string, error) {
if query.RuleUID != "" {
logQL = fmt.Sprintf("%s | ruleUID=%q", logQL, query.RuleUID)
}
if query.DashboardUID != "" {
logQL = fmt.Sprintf("%s | dashboardUID=%q", logQL, query.DashboardUID)
}
if query.PanelID != 0 {
logQL = fmt.Sprintf("%s | panelID=%d", logQL, query.PanelID)
}
labelFilters := ""
labelKeys := make([]string, 0, len(query.Labels))
@ -394,5 +400,8 @@ func buildLogQuery(query models.HistoryQuery) (string, error) {
}
func queryHasLogFilters(query models.HistoryQuery) bool {
return query.RuleUID != "" || len(query.Labels) > 0
return query.RuleUID != "" ||
query.DashboardUID != "" ||
query.PanelID != 0 ||
len(query.Labels) > 0
}

View File

@ -237,6 +237,22 @@ func TestRemoteLokiBackend(t *testing.T) {
},
exp: `{orgID="123",from="state-history"} | json | ruleUID="rule-uid"`,
},
{
name: "filters dashboardUID in log line",
query: models.HistoryQuery{
OrgID: 123,
DashboardUID: "dash-uid",
},
exp: `{orgID="123",from="state-history"} | json | dashboardUID="dash-uid"`,
},
{
name: "filters panelID in log line",
query: models.HistoryQuery{
OrgID: 123,
PanelID: 456,
},
exp: `{orgID="123",from="state-history"} | json | panelID=456`,
},
{
name: "filters instance labels in log line",
query: models.HistoryQuery{