Skip to content

Commit 89ae290

Browse files
authored
Move health check to the backend (#2120)
This PR moves the health check to backend only leaving in the frontend the functionality to test the dbconnector datasource. Leaving the `dbconnector.testDataSource` should be fine since the datasource types we allow for db connection with Zabbix already are backend datasources, and so their health requests would go through the backend. Verified: Clicking test and seeing a `health` request go out. IMPORTANT: While testing this in the UI, I found a bug with the config editor - whenever a change is made in the UI and tested, the changes don't take effect (i.e. disabling trends, keeps `trends` set to `true`, enabling db connection keep `dbConnectionEnabled` set to `false` and so on.). Created a separate [issue](https://github.com/orgs/grafana/projects/457/views/40?pane=issue&itemId=3627315751&issue=grafana%7Coss-big-tent-squad%7C132) to fix this Fixes grafana/oss-big-tent-squad#124 Fixes #2004
1 parent 631d3bd commit 89ae290

File tree

10 files changed

+521
-55
lines changed

10 files changed

+521
-55
lines changed

.changeset/orange-owls-tie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'grafana-zabbix': minor
3+
---
4+
5+
Moves health check to the backend

pkg/datasource/datasource.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package datasource
33
import (
44
"context"
55
"errors"
6+
"fmt"
67

78
"github.com/alexanderzobnin/grafana-zabbix/pkg/httpclient"
89
"github.com/alexanderzobnin/grafana-zabbix/pkg/metrics"
@@ -99,7 +100,7 @@ func (ds *ZabbixDatasource) CheckHealth(ctx context.Context, req *backend.CheckH
99100
}
100101

101102
res.Status = backend.HealthStatusOk
102-
res.Message = message
103+
res.Message = fmt.Sprintf("Zabbix API version %s", message)
103104
return res, nil
104105
}
105106

pkg/datasource/zabbix.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ func (ds *ZabbixDatasourceInstance) TestConnection(ctx context.Context) (string,
3636
return "", err
3737
}
3838

39-
response, err := ds.zabbix.Request(ctx, &zabbix.ZabbixAPIRequest{Method: "apiinfo.version"})
39+
zabbixVersion, err := ds.zabbix.GetFullVersion(ctx)
4040
if err != nil {
4141
return "", err
4242
}
4343

44-
resultByte, _ := response.MarshalJSON()
45-
return string(resultByte), nil
44+
return zabbixVersion, nil
4645
}
4746

4847
func (ds *ZabbixDatasourceInstance) queryNumericItems(ctx context.Context, query *QueryModel) ([]*data.Frame, error) {

pkg/settings/models.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ type ZabbixDatasourceSettingsDTO struct {
1616
CacheTTL string `json:"cacheTTL"`
1717
Timeout interface{} `json:"timeout"`
1818

19-
DisableDataAlignment bool `json:"disableDataAlignment"`
20-
DisableReadOnlyUsersAck bool `json:"disableReadOnlyUsersAck"`
19+
DisableDataAlignment bool `json:"disableDataAlignment"`
20+
DisableReadOnlyUsersAck bool `json:"disableReadOnlyUsersAck"`
2121
}
2222

2323
// ZabbixDatasourceSettings model
@@ -29,6 +29,6 @@ type ZabbixDatasourceSettings struct {
2929
CacheTTL time.Duration
3030
Timeout time.Duration
3131

32-
DisableDataAlignment bool `json:"disableDataAlignment"`
33-
DisableReadOnlyUsersAck bool `json:"disableReadOnlyUsersAck"`
32+
DisableDataAlignment bool `json:"disableDataAlignment"`
33+
DisableReadOnlyUsersAck bool `json:"disableReadOnlyUsersAck"`
3434
}

pkg/zabbix/methods.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,19 +524,28 @@ func (ds *Zabbix) GetValueMappings(ctx context.Context) ([]ValueMap, error) {
524524
return valuemaps, err
525525
}
526526

527-
func (ds *Zabbix) GetVersion(ctx context.Context) (int, error) {
527+
func (ds *Zabbix) GetFullVersion(ctx context.Context) (string, error) {
528528
result, err := ds.request(ctx, "apiinfo.version", ZabbixAPIParams{})
529529
if err != nil {
530-
return 0, err
530+
return "", err
531531
}
532532

533533
var version string
534534
err = convertTo(result, &version)
535+
if err != nil {
536+
return "", err
537+
}
538+
539+
return version, nil
540+
}
541+
542+
func (ds *Zabbix) GetVersion(ctx context.Context) (int, error) {
543+
fullStringVersion, err := ds.GetFullVersion(ctx)
535544
if err != nil {
536545
return 0, err
537546
}
538547

539-
version = strings.Replace(version[0:3], ".", "", 1)
548+
version := strings.Replace(fullStringVersion[0:3], ".", "", 1)
540549
versionNum, err := strconv.Atoi(version)
541550
return versionNum, err
542551
}

0 commit comments

Comments
 (0)