Skip to content

Commit

Permalink
Merge pull request #1256 from alexanderzobnin/backend-functions
Browse files Browse the repository at this point in the history
Implement data processing functions on the backend
  • Loading branch information
alexanderzobnin authored Aug 6, 2021
2 parents 61565c1 + 4855cd0 commit c6441cc
Show file tree
Hide file tree
Showing 76 changed files with 7,935 additions and 6,416 deletions.
27 changes: 17 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@ install:
# Frontend
yarn install --pure-lockfile
# Backend
go mod vendor
go install -v ./pkg/
GO111MODULE=off go get -u golang.org/x/lint/golint

deps-go:
go install -v ./pkg/

build: build-frontend build-backend
build-frontend:
yarn dev-build
build-backend:
env GOOS=linux go build -mod=vendor -o ./dist/zabbix-plugin_linux_amd64 ./pkg
env GOOS=linux go build -o ./dist/zabbix-plugin_linux_amd64 ./pkg
build-debug:
env GOOS=linux go build -mod=vendor -gcflags=all="-N -l" -o ./dist/zabbix-plugin_linux_amd64 ./pkg
env GOOS=linux go build -gcflags="all=-N -l" -o ./dist/zabbix-plugin_linux_amd64 ./pkg

# Build for specific platform
build-backend-windows: extension = .exe
build-backend-darwin-arm64:
env GOOS=darwin GOARCH=arm64 go build -o ./dist/zabbix-plugin_darwin_arm64 ./pkg
build-backend-%:
$(eval filename = zabbix-plugin_$*_amd64$(extension))
env GOOS=$* GOARCH=amd64 go build -mod=vendor -o ./dist/$(filename) ./pkg
env GOOS=$* GOARCH=amd64 go build -o ./dist/$(filename) ./pkg

run-frontend:
yarn install --pure-lockfile
Expand All @@ -40,27 +45,29 @@ dist-backend: dist-backend-linux dist-backend-darwin dist-backend-windows dist-a
dist-backend-windows: extension = .exe
dist-backend-%:
$(eval filename = zabbix-plugin_$*_amd64$(extension))
env GOOS=$* GOARCH=amd64 go build -ldflags="-s -w" -mod=vendor -o ./dist/$(filename) ./pkg
env GOOS=$* GOARCH=amd64 go build -ldflags="-s -w" -o ./dist/$(filename) ./pkg

# ARM
dist-arm: dist-arm-linux-arm-v6 dist-arm-linux-arm64
dist-arm-linux-arm-v6:
env GOOS=linux GOARCH=arm GOARM=6 go build -ldflags="-s -w" -mod=vendor -o ./dist/zabbix-plugin_linux_arm ./pkg
env GOOS=linux GOARCH=arm GOARM=6 go build -ldflags="-s -w" -o ./dist/zabbix-plugin_linux_arm ./pkg
dist-arm-linux-arm-v7:
env GOOS=linux GOARCH=arm GOARM=7 go build -ldflags="-s -w" -mod=vendor -o ./dist/zabbix-plugin_linux_arm ./pkg
env GOOS=linux GOARCH=arm GOARM=7 go build -ldflags="-s -w" -o ./dist/zabbix-plugin_linux_arm ./pkg
dist-arm-linux-arm64:
env GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o ./dist/zabbix-plugin_linux_arm64 ./pkg
dist-arm-linux-arm64:
env GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -mod=vendor -o ./dist/zabbix-plugin_linux_arm64 ./pkg
env GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -o ./dist/zabbix-plugin_darwin_arm64 ./pkg

.PHONY: test
test: test-frontend test-backend
test-frontend:
yarn test
test-backend:
go test -mod=vendor ./pkg/...
go test ./pkg/...
test-ci:
yarn ci-test
mkdir -p tmp/coverage/golang/
go test -race -coverprofile=tmp/coverage/golang/coverage.txt -covermode=atomic -mod=vendor ./pkg/...
go test -race -coverprofile=tmp/coverage/golang/coverage.txt -covermode=atomic ./pkg/...

.PHONY: clean
clean:
Expand Down
7 changes: 7 additions & 0 deletions debug-backend.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ fi
PORT="${2:-3222}"
PLUGIN_NAME="${1:-zabbix-plugin_}"

# Build optimized for debug
make build-debug

# Reload plugin
pkill ${PLUGIN_NAME}
sleep 2

if [ "$OSTYPE" == "linux-gnu" ]; then
ptrace_scope=`cat /proc/sys/kernel/yama/ptrace_scope`
if [ "$ptrace_scope" != 0 ]; then
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module github.com/alexanderzobnin/grafana-zabbix

go 1.12
go 1.15

require (
github.com/bitly/go-simplejson v0.5.0
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/grafana/grafana-plugin-sdk-go v0.65.0
github.com/hashicorp/go-hclog v0.9.2 // indirect
github.com/grafana/grafana-plugin-sdk-go v0.98.1
github.com/hashicorp/go-hclog v0.16.1 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/stretchr/testify v1.5.1
golang.org/x/net v0.0.0-20190923162816-aa69164e4478
github.com/stretchr/testify v1.7.0
golang.org/x/net v0.0.0-20210510120150-4163338589ed
gotest.tools v2.2.0+incompatible
)
416 changes: 369 additions & 47 deletions go.sum

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
"@babel/core": "7.7.7",
"@babel/preset-env": "7.7.7",
"@babel/preset-react": "7.6.3",
"@emotion/core": "10.0.27",
"@grafana/data": "^7.3.2",
"@grafana/runtime": "^7.3.2",
"@grafana/toolkit": "^7.3.2",
"@grafana/ui": "7.0.1",
"@emotion/css": "11.1.3",
"@emotion/react": "11.1.5",
"@grafana/data": "^8.0.6",
"@grafana/runtime": "^8.0.6",
"@grafana/toolkit": "^8.0.6",
"@grafana/ui": "^8.0.6",
"@popperjs/core": "2.4.0",
"@types/classnames": "2.2.9",
"@types/grafana": "github:CorpGlory/types-grafana",
Expand Down Expand Up @@ -81,14 +82,15 @@
"react-test-renderer": "^16.7.0",
"react-transition-group": "4.3.0",
"rst2html": "github:thoward/rst2html#990cb89",
"rxjs": "6.6.3",
"sass-loader": "8.0.2",
"semver": "^7.3.2",
"style-loader": "1.1.3",
"tether-drop": "^1.4.2",
"ts-jest": "24.1.0",
"ts-loader": "4.4.1",
"tslint": "5.20.1",
"typescript": "3.9.2",
"tslint": "^6.1.3",
"typescript": "^4.1.2",
"webpack": "4.41.5",
"webpack-cli": "3.3.10"
},
Expand Down
67 changes: 43 additions & 24 deletions pkg/datasource/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"time"

"github.com/alexanderzobnin/grafana-zabbix/pkg/gtime"
"github.com/alexanderzobnin/grafana-zabbix/pkg/httpclient"
"github.com/alexanderzobnin/grafana-zabbix/pkg/zabbix"
"github.com/alexanderzobnin/grafana-zabbix/pkg/zabbixapi"

"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana-plugin-sdk-go/data"
)

var (
Expand All @@ -30,11 +31,10 @@ type ZabbixDatasource struct {
// ZabbixDatasourceInstance stores state about a specific datasource
// and provides methods to make requests to the Zabbix API
type ZabbixDatasourceInstance struct {
zabbixAPI *zabbixapi.ZabbixAPI
dsInfo *backend.DataSourceInstanceSettings
Settings *ZabbixDatasourceSettings
queryCache *DatasourceCache
logger log.Logger
zabbix *zabbix.Zabbix
dsInfo *backend.DataSourceInstanceSettings
Settings *ZabbixDatasourceSettings
logger log.Logger
}

func NewZabbixDatasource() *ZabbixDatasource {
Expand All @@ -56,18 +56,29 @@ func newZabbixDatasourceInstance(settings backend.DataSourceInstanceSettings) (i
return nil, err
}

zabbixAPI, err := zabbixapi.New(&settings, zabbixSettings.Timeout)
client, err := httpclient.New(&settings, zabbixSettings.Timeout)
if err != nil {
logger.Error("Error initializing HTTP client", "error", err)
return nil, err
}

zabbixAPI, err := zabbixapi.New(settings.URL, client)
if err != nil {
logger.Error("Error initializing Zabbix API", "error", err)
return nil, err
}

zabbixClient, err := zabbix.New(&settings, zabbixAPI)
if err != nil {
logger.Error("Error initializing Zabbix client", "error", err)
return nil, err
}

return &ZabbixDatasourceInstance{
dsInfo: &settings,
zabbixAPI: zabbixAPI,
Settings: zabbixSettings,
queryCache: NewDatasourceCache(zabbixSettings.CacheTTL, 10*time.Minute),
logger: logger,
dsInfo: &settings,
zabbix: zabbixClient,
Settings: zabbixSettings,
logger: logger,
}, nil
}

Expand Down Expand Up @@ -97,6 +108,7 @@ func (ds *ZabbixDatasource) CheckHealth(ctx context.Context, req *backend.CheckH
}

func (ds *ZabbixDatasource) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
ds.logger.Debug("QueryData()")
qdr := backend.NewQueryDataResponse()

zabbixDS, err := ds.getDSInstance(req.PluginContext)
Expand All @@ -110,17 +122,22 @@ func (ds *ZabbixDatasource) QueryData(ctx context.Context, req *backend.QueryDat
ds.logger.Debug("DS query", "query", q)
if err != nil {
res.Error = err
} else if len(query.Functions) > 0 {
res.Error = ErrFunctionsNotSupported
} else if query.Mode != 0 {
res.Error = ErrNonMetricQueryNotSupported
} else {
frame, err := zabbixDS.queryNumericItems(ctx, &query)
} else if query.QueryType == MODE_METRICS {
frames, err := zabbixDS.queryNumericItems(ctx, &query)
if err != nil {
res.Error = err
} else {
res.Frames = append(res.Frames, frames...)
}
} else if query.QueryType == MODE_ITEMID {
frames, err := zabbixDS.queryItemIdData(ctx, &query)
if err != nil {
res.Error = err
} else {
res.Frames = []*data.Frame{frame}
res.Frames = append(res.Frames, frames...)
}
} else {
res.Error = ErrNonMetricQueryNotSupported
}
qdr.Responses[q.RefID] = res
}
Expand Down Expand Up @@ -180,11 +197,13 @@ func readZabbixSettings(dsInstanceSettings *backend.DataSourceInstanceSettings)
}

zabbixSettings := &ZabbixDatasourceSettings{
Trends: zabbixSettingsDTO.Trends,
TrendsFrom: trendsFrom,
TrendsRange: trendsRange,
CacheTTL: cacheTTL,
Timeout: time.Duration(timeout) * time.Second,
Trends: zabbixSettingsDTO.Trends,
TrendsFrom: trendsFrom,
TrendsRange: trendsRange,
CacheTTL: cacheTTL,
Timeout: time.Duration(timeout) * time.Second,
DisableDataAlignment: zabbixSettingsDTO.DisableDataAlignment,
DisableReadOnlyUsersAck: zabbixSettingsDTO.DisableReadOnlyUsersAck,
}

return zabbixSettings, nil
Expand Down
40 changes: 0 additions & 40 deletions pkg/datasource/datasource_cache.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/datasource/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestZabbixBackend_getCachedDatasource(t *testing.T) {
got, _ := ds.getDSInstance(tt.pluginContext)

// Only checking the URL, being the easiest value to, and guarantee equality for
assert.Equal(t, tt.want.zabbixAPI.GetUrl().String(), got.zabbixAPI.GetUrl().String())
assert.Equal(t, tt.want.zabbix.GetAPI().GetUrl().String(), got.zabbix.GetAPI().GetUrl().String())
})
}
}
Loading

0 comments on commit c6441cc

Please sign in to comment.