@@ -275,24 +275,56 @@ func displayPQData(result *LRQResult, response backend.DataResponse) backend.Dat
275275 return response
276276}
277277
278- // CheckHealth handles health checks sent from Grafana to the plugin.
279- // The main use case for these health checks is the test button on the
280- // datasource configuration page which allows users to verify that
281- // a datasource is working as expected.
278+ // CheckHealth handles health checks sent from the Grafana server to the plugin.
279+ // Used by the test button on the config page to validate the DataSet url and api key.
282280func (d * DataSetDatasource ) CheckHealth (ctx context.Context , req * backend.CheckHealthRequest ) (* backend.CheckHealthResult , error ) {
283- statusCode , err := d .dataSetClient .DoFacetRequest (ctx , FacetRequest {
284- QueryType : "facet" ,
285- MaxCount : 1 ,
286- Field : "test" ,
287- })
281+ const refId = "A"
282+ const powerQuery = "grafana_plugin_checkhealth=1 | limit 1"
283+
284+ resp , err := d .QueryData (
285+ ctx ,
286+ & backend.QueryDataRequest {
287+ Queries : []backend.DataQuery {
288+ {
289+ RefID : refId ,
290+ TimeRange : backend.TimeRange {
291+ From : time .Now ().Add (- 1 * time .Minute ),
292+ To : time .Now (),
293+ },
294+ JSON : []byte (fmt .Sprintf (`{"expression":"%s","queryType":"Power Query"}` , powerQuery )),
295+ },
296+ },
297+ },
298+ )
288299 if err != nil {
289- return nil , err
300+ return & backend.CheckHealthResult {
301+ Status : backend .HealthStatusError ,
302+ Message : "Failed to connect to DataSet: QueryData failed: " + err .Error (),
303+ }, nil
304+ }
305+
306+ dataResp , ok := resp .Responses [refId ]
307+ if ! ok {
308+ return & backend.CheckHealthResult {
309+ Status : backend .HealthStatusError ,
310+ Message : "Failed to connect to DataSet: No response for RefID " + refId ,
311+ }, nil
312+ }
313+
314+ if dataResp .Error != nil {
315+ return & backend.CheckHealthResult {
316+ Status : backend .HealthStatusError ,
317+ Message : "Failed to connect to DataSet: DataResponse.Error: " + dataResp .Error .Error (),
318+ }, nil
290319 }
291320
292- if statusCode != 200 {
321+ // FIXME Look into upgrading to a later version and add a liveDataSource test
322+ // NB At least for grafana-plugin-sdk-go v0.250.0, backend.DataResponse.Status is not set.
323+ // Thankfully backend.DataResponse.Error is set when the http status code is not 200.
324+ if dataResp .Status != backend .StatusUnknown && dataResp .Status != 0 && dataResp .Status != backend .StatusOK {
293325 return & backend.CheckHealthResult {
294326 Status : backend .HealthStatusError ,
295- Message : "Failed to connect to DataSet, please inspect the Grafana server log for details" ,
327+ Message : fmt . Sprintf ( "Failed to connect to DataSet: Received HTTP status code %d" , dataResp . Status ) ,
296328 }, nil
297329 }
298330
0 commit comments