[v9.5.x] Alerting: Fix DatasourceUID and RefID missing for DatasourceNoData alerts (#66962)
Alerting: Fix DatasourceUID and RefID missing for DatasourceNoData alerts (#66733)
This commit fixes a bug where DatasourceUID and RefID annotations are
missing for DatasourceNoData alerts in Grafana 9.5. This bug affects
datasource plugins that have moved to using the data plane contract.
(cherry picked from commit 35342a3c76)
Co-authored-by: George Robinson <george.robinson@grafana.com>
This commit is contained in:
co-authored by
George Robinson
parent
2ed30e9831
commit
32869c96df
@@ -321,14 +321,27 @@ func queryDataResponseToExecutionResults(c models.Condition, execResp *backend.Q
|
||||
|
||||
result := ExecutionResults{Results: make(map[string]data.Frames)}
|
||||
for refID, res := range execResp.Responses {
|
||||
if len(res.Frames) == 0 {
|
||||
// to ensure that NoData is consistent with Results we do not initialize NoData
|
||||
// unless there is at least one RefID that returned no data
|
||||
// There are two possible frame formats for No Data:
|
||||
//
|
||||
// 1. A response with no frames
|
||||
// 2. A response with 1 frame but no fields
|
||||
//
|
||||
// The first format is not documented in the data plane contract but needs to be
|
||||
// supported for older datasource plugins. The second format is documented in
|
||||
// https://github.com/grafana/grafana-plugin-sdk-go/blob/main/data/contract_docs/contract.md
|
||||
// and is what datasource plugins should use going forward.
|
||||
if len(res.Frames) <= 1 {
|
||||
// To make sure NoData is nil when Results are also nil we wait to initialize
|
||||
// NoData until there is at least one query or expression that returned no data
|
||||
if result.NoData == nil {
|
||||
result.NoData = make(map[string]string)
|
||||
}
|
||||
if s, ok := datasourceUIDsForRefIDs[refID]; ok && s != datasourceExprUID {
|
||||
result.NoData[refID] = s
|
||||
hasNoFrames := len(res.Frames) == 0
|
||||
hasNoFields := len(res.Frames) == 1 && len(res.Frames[0].Fields) == 0
|
||||
if hasNoFrames || hasNoFields {
|
||||
if s, ok := datasourceUIDsForRefIDs[refID]; ok && s != datasourceExprUID {
|
||||
result.NoData[refID] = s
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -573,6 +573,26 @@ func TestEvaluate(t *testing.T) {
|
||||
"ref_id": "A",
|
||||
},
|
||||
}},
|
||||
}, {
|
||||
name: "is no data for one frame with no fields",
|
||||
cond: models.Condition{
|
||||
Data: []models.AlertQuery{{
|
||||
RefID: "A",
|
||||
DatasourceUID: "test",
|
||||
}},
|
||||
},
|
||||
resp: backend.QueryDataResponse{
|
||||
Responses: backend.Responses{
|
||||
"A": {Frames: []*data.Frame{{Fields: nil}}},
|
||||
},
|
||||
},
|
||||
expected: Results{{
|
||||
State: NoData,
|
||||
Instance: data.Labels{
|
||||
"datasource_uid": "test",
|
||||
"ref_id": "A",
|
||||
},
|
||||
}},
|
||||
}}
|
||||
|
||||
for _, tc := range cases {
|
||||
|
||||
Reference in New Issue
Block a user