Backend: implement alerting query (#847)

* Unit tests for backend

* minor change

* Implemented querNumericItems in backend

* Added query type

* Set alerting to true

* Return TimeSeries from History

* Updated alerting feature

* Fix params marshal error

* Fix params more

* Update zabbixAPIConnector.js

* Numbers, I guess

* Params Output Type

* Output marshaling

* Unmarshal and decoder error catch

* HistoryPoint and Unmarshal fixes

* Unmarshal to History

* Revert "Update zabbixAPIConnector.js"

This reverts commit e0ffdff859b6f920893a47a709493f8076e38ef4.

* Fix unmarshaling for real

* Time range integer

* Use more zabbix.Items

* Update response_models.go

* Update zabbix_api.go

* Update models.go

* Update zabbix_api.go

* Tests

* Adding more unit tests and cleaning up additional logging

* Make history request param a pointer

* Actually store datasource in cache

* Debug logs and timings

* Handle panics gracefully

* Updated Regex filter parsing

* Removed must compile

* Clean up regex filter
This commit is contained in:
vignesh-reddy
2020-01-13 01:38:24 -06:00
committed by Alexander Zobnin
parent c300debe35
commit 3f5719794a
9 changed files with 1106 additions and 33 deletions

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"runtime/debug"
simplejson "github.com/bitly/go-simplejson"
"github.com/grafana/grafana_plugin_model/go/datasource"
@@ -27,7 +28,16 @@ func (b *ZabbixBackend) newZabbixDatasource() *ZabbixDatasource {
// Query receives requests from the Grafana backend. Requests are filtered by query type and sent to the
// applicable ZabbixDatasource.
func (b *ZabbixBackend) Query(ctx context.Context, tsdbReq *datasource.DatasourceRequest) (*datasource.DatasourceResponse, error) {
func (b *ZabbixBackend) Query(ctx context.Context, tsdbReq *datasource.DatasourceRequest) (resp *datasource.DatasourceResponse, _e error) {
defer func() {
if r := recover(); r != nil {
pErr, _ := r.(error)
b.logger.Error("Fatal error in Zabbix Plugin Backend", "Error", pErr)
b.logger.Error(string(debug.Stack()))
resp = BuildErrorResponse(fmt.Errorf("Unrecoverable error in grafana-zabbix plugin backend"))
}
}()
zabbixDs := b.getCachedDatasource(tsdbReq)
queryType, err := GetQueryType(tsdbReq)
@@ -37,13 +47,17 @@ func (b *ZabbixBackend) Query(ctx context.Context, tsdbReq *datasource.Datasourc
switch queryType {
case "zabbixAPI":
return zabbixDs.ZabbixAPIQuery(ctx, tsdbReq)
resp, err = zabbixDs.ZabbixAPIQuery(ctx, tsdbReq)
case "query":
resp, err = zabbixDs.queryNumericItems(ctx, tsdbReq)
case "connectionTest":
return zabbixDs.TestConnection(ctx, tsdbReq)
resp, err = zabbixDs.TestConnection(ctx, tsdbReq)
default:
err = errors.New("Query not implemented")
return BuildErrorResponse(err), nil
}
return
}
func (b *ZabbixBackend) getCachedDatasource(tsdbReq *datasource.DatasourceRequest) *ZabbixDatasource {
@@ -59,7 +73,10 @@ func (b *ZabbixBackend) getCachedDatasource(tsdbReq *datasource.DatasourceReques
dsInfo := tsdbReq.GetDatasource()
b.logger.Debug(fmt.Sprintf("Datasource cache miss (Org %d Id %d '%s' %s)", dsInfo.GetOrgId(), dsInfo.GetId(), dsInfo.GetName(), dsInfoHash))
}
return b.newZabbixDatasource()
ds := b.newZabbixDatasource()
b.datasourceCache.Set(dsInfoHash, ds)
return ds
}
// GetQueryType determines the query type from a query or list of queries
@@ -104,3 +121,15 @@ func BuildErrorResponse(err error) *datasource.DatasourceResponse {
},
}
}
// BuildMetricsResponse builds a response object using a given TimeSeries array
func BuildMetricsResponse(metrics []*datasource.TimeSeries) (*datasource.DatasourceResponse, error) {
return &datasource.DatasourceResponse{
Results: []*datasource.QueryResult{
&datasource.QueryResult{
RefId: "zabbixMetrics",
Series: metrics,
},
},
}, nil
}