mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
78d7b6c0c9
commit
35342a3c76
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user