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.
This commit is contained in:
George Robinson 2023-04-20 14:38:20 +01:00 committed by GitHub
parent 78d7b6c0c9
commit 35342a3c76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 5 deletions

View File

@ -321,16 +321,29 @@ 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)
}
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
}
}
}
// for each frame within each response, the response can contain several data types including time-series data.
// For now, we favour simplicity and only care about single scalar values.

View File

@ -575,6 +575,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 {