Initial Commit
This commit is contained in:
15
pkg/cache.go
15
pkg/cache.go
@@ -3,8 +3,10 @@ package main
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana_plugin_model/go/datasource"
|
||||
cache "github.com/patrickmn/go-cache"
|
||||
)
|
||||
|
||||
@@ -30,9 +32,18 @@ func (c *Cache) Get(request string) (interface{}, bool) {
|
||||
return c.cache.Get(request)
|
||||
}
|
||||
|
||||
// Hash converts the given text string to hash string
|
||||
func Hash(text string) string {
|
||||
// HashString converts the given text string to hash string
|
||||
func HashString(text string) string {
|
||||
hash := sha1.New()
|
||||
hash.Write([]byte(text))
|
||||
return hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
|
||||
// HashDatasourceInfo converts the given datasource info to hash string
|
||||
func HashDatasourceInfo(dsInfo *datasource.DatasourceInfo) string {
|
||||
digester := sha1.New()
|
||||
if err := json.NewEncoder(digester).Encode(dsInfo); err != nil {
|
||||
panic(err) // This shouldn't be possible but just in case DatasourceInfo changes
|
||||
}
|
||||
return hex.EncodeToString(digester.Sum(nil))
|
||||
}
|
||||
|
||||
68
pkg/cache_test.go
Normal file
68
pkg/cache_test.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana_plugin_model/go/datasource"
|
||||
"gotest.tools/assert"
|
||||
)
|
||||
|
||||
func TestHashDatasourceInfo(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
dsInfo *datasource.DatasourceInfo
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "Normal Datasource Info",
|
||||
dsInfo: &datasource.DatasourceInfo{
|
||||
Id: 1,
|
||||
OrgId: 1,
|
||||
Name: "Zabbix",
|
||||
Type: "alexanderzobnin-zabbix-datasource",
|
||||
Url: "https://localhost:3306/zabbix/api_jsonrpc.php",
|
||||
JsonData: "{}",
|
||||
DecryptedSecureJsonData: map[string]string{
|
||||
"username": "grafanaZabbixUser",
|
||||
"password": "$uper$ecr3t!!!",
|
||||
},
|
||||
},
|
||||
want: "ed161f89179c46d9a578e4d7e92ff95444222e0a",
|
||||
},
|
||||
// Can't find a case where the input causes the encoder to fail
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := HashDatasourceInfo(tt.dsInfo)
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkHashDatasourceInfo(b *testing.B) {
|
||||
benches := []struct {
|
||||
name string
|
||||
dsInfo *datasource.DatasourceInfo
|
||||
}{
|
||||
{
|
||||
"Normal Datasource Info",
|
||||
&datasource.DatasourceInfo{
|
||||
Id: 4,
|
||||
OrgId: 6,
|
||||
Name: "MyZabbixDatasource",
|
||||
Type: "alexanderzobnin-zabbix-datasource",
|
||||
Url: "https://localhost:3306/zabbix/api_jsonrpc.php",
|
||||
JsonData: `{ "addThresholds": true, "disableReadOnlyUsersAck": true }`,
|
||||
DecryptedSecureJsonData: map[string]string{
|
||||
"username": "grafanaZabbixUser",
|
||||
"password": "$uper$ecr3t!!!",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, bt := range benches {
|
||||
b.Run(bt.name, func(b *testing.B) {
|
||||
HashDatasourceInfo(bt.dsInfo)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
simplejson "github.com/bitly/go-simplejson"
|
||||
"github.com/grafana/grafana_plugin_model/go/datasource"
|
||||
@@ -10,39 +11,63 @@ import (
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type ZabbixDatasource struct {
|
||||
// ZabbixBackend implements the Grafana backend interface and forwards queries to the ZabbixDatasource
|
||||
type ZabbixBackend struct {
|
||||
plugin.NetRPCUnsupportedPlugin
|
||||
logger hclog.Logger
|
||||
logger hclog.Logger
|
||||
datasourceCache *Cache
|
||||
}
|
||||
|
||||
func (ds *ZabbixDatasource) Query(ctx context.Context, tsdbReq *datasource.DatasourceRequest) (*datasource.DatasourceResponse, error) {
|
||||
func (b *ZabbixBackend) newZabbixDatasource() *ZabbixDatasource {
|
||||
ds := NewZabbixDatasource()
|
||||
ds.logger = b.logger
|
||||
return ds
|
||||
}
|
||||
|
||||
// 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) {
|
||||
zabbixDs := b.getCachedDatasource(tsdbReq)
|
||||
|
||||
queryType, err := GetQueryType(tsdbReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dsInfo := tsdbReq.GetDatasource()
|
||||
ds.logger.Debug("createRequest", "dsInfo", dsInfo)
|
||||
|
||||
ds.logger.Debug("createRequest", "queryType", queryType)
|
||||
|
||||
switch queryType {
|
||||
case "zabbixAPI":
|
||||
return ds.ZabbixAPIQuery(ctx, tsdbReq)
|
||||
return zabbixDs.ZabbixAPIQuery(ctx, tsdbReq)
|
||||
default:
|
||||
return nil, errors.New("Query is not implemented yet")
|
||||
}
|
||||
}
|
||||
|
||||
func (b *ZabbixBackend) getCachedDatasource(tsdbReq *datasource.DatasourceRequest) *ZabbixDatasource {
|
||||
dsInfoHash := HashDatasourceInfo(tsdbReq.GetDatasource())
|
||||
|
||||
if cachedData, ok := b.datasourceCache.Get(dsInfoHash); ok {
|
||||
if cachedDS, ok := cachedData.(*ZabbixDatasource); ok {
|
||||
return cachedDS
|
||||
}
|
||||
}
|
||||
|
||||
if b.logger.IsDebug() {
|
||||
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()
|
||||
}
|
||||
|
||||
// GetQueryType determines the query type from a query or list of queries
|
||||
func GetQueryType(tsdbReq *datasource.DatasourceRequest) (string, error) {
|
||||
queryType := "query"
|
||||
if len(tsdbReq.Queries) > 0 {
|
||||
firstQuery := tsdbReq.Queries[0]
|
||||
queryJson, err := simplejson.NewJson([]byte(firstQuery.ModelJson))
|
||||
queryJSON, err := simplejson.NewJson([]byte(firstQuery.ModelJson))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
queryType = queryJson.Get("queryType").MustString("query")
|
||||
queryType = queryJSON.Get("queryType").MustString("query")
|
||||
}
|
||||
return queryType, nil
|
||||
}
|
||||
|
||||
81
pkg/datasource_test.go
Normal file
81
pkg/datasource_test.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana_plugin_model/go/datasource"
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
cache "github.com/patrickmn/go-cache"
|
||||
"gotest.tools/assert"
|
||||
)
|
||||
|
||||
func TestZabbixBackend_getCachedDatasource(t *testing.T) {
|
||||
basicDatasourceInfo := &datasource.DatasourceInfo{
|
||||
Id: 1,
|
||||
Name: "TestDatasource",
|
||||
}
|
||||
basicDatasourceInfoHash := HashDatasourceInfo(basicDatasourceInfo)
|
||||
|
||||
modifiedDatasource := NewZabbixDatasource()
|
||||
modifiedDatasource.authToken = "AB404F1234"
|
||||
|
||||
altDatasourceInfo := &datasource.DatasourceInfo{
|
||||
Id: 2,
|
||||
Name: "AnotherDatasource",
|
||||
}
|
||||
altDatasourceInfoHash := HashDatasourceInfo(altDatasourceInfo)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cache *cache.Cache
|
||||
request *datasource.DatasourceRequest
|
||||
want *ZabbixDatasource
|
||||
}{
|
||||
{
|
||||
name: "Uncached Datasource (nothing in cache)",
|
||||
request: &datasource.DatasourceRequest{
|
||||
Datasource: basicDatasourceInfo,
|
||||
},
|
||||
want: NewZabbixDatasource(),
|
||||
},
|
||||
{
|
||||
name: "Uncached Datasource (cache miss)",
|
||||
cache: cache.NewFrom(cache.NoExpiration, cache.NoExpiration, map[string]cache.Item{
|
||||
basicDatasourceInfoHash: cache.Item{Object: modifiedDatasource},
|
||||
}),
|
||||
request: &datasource.DatasourceRequest{
|
||||
Datasource: altDatasourceInfo,
|
||||
},
|
||||
want: NewZabbixDatasource(),
|
||||
},
|
||||
{
|
||||
name: "Cached Datasource",
|
||||
cache: cache.NewFrom(cache.NoExpiration, cache.NoExpiration, map[string]cache.Item{
|
||||
altDatasourceInfoHash: cache.Item{Object: NewZabbixDatasource()},
|
||||
basicDatasourceInfoHash: cache.Item{Object: modifiedDatasource},
|
||||
}),
|
||||
request: &datasource.DatasourceRequest{
|
||||
Datasource: basicDatasourceInfo,
|
||||
},
|
||||
want: modifiedDatasource,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.cache == nil {
|
||||
tt.cache = cache.New(cache.NoExpiration, cache.NoExpiration)
|
||||
}
|
||||
b := &ZabbixBackend{
|
||||
logger: hclog.New(&hclog.LoggerOptions{
|
||||
Name: "TestZabbixBackend_getCachedDatasource",
|
||||
Level: hclog.LevelFromString("DEBUG"),
|
||||
}),
|
||||
datasourceCache: &Cache{cache: tt.cache},
|
||||
}
|
||||
got := b.getCachedDatasource(tt.request)
|
||||
|
||||
// Only checking the authToken, being the easiest value to, and guarantee equality for
|
||||
assert.Equal(t, tt.want.authToken, got.authToken)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana_plugin_model/go/datasource"
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
plugin "github.com/hashicorp/go-plugin"
|
||||
@@ -22,8 +24,9 @@ func main() {
|
||||
MagicCookieValue: "datasource",
|
||||
},
|
||||
Plugins: map[string]plugin.Plugin{
|
||||
"zabbix-backend-datasource": &datasource.DatasourcePluginImpl{Plugin: &ZabbixDatasource{
|
||||
logger: pluginLogger,
|
||||
"zabbix-backend-datasource": &datasource.DatasourcePluginImpl{Plugin: &ZabbixBackend{
|
||||
datasourceCache: NewCache(10*time.Minute, 10*time.Minute),
|
||||
logger: pluginLogger,
|
||||
}},
|
||||
},
|
||||
|
||||
|
||||
@@ -15,35 +15,47 @@ import (
|
||||
|
||||
simplejson "github.com/bitly/go-simplejson"
|
||||
"github.com/grafana/grafana_plugin_model/go/datasource"
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
"golang.org/x/net/context"
|
||||
"golang.org/x/net/context/ctxhttp"
|
||||
)
|
||||
|
||||
var httpClient = &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
Renegotiation: tls.RenegotiateFreelyAsClient,
|
||||
},
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
Dial: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
DualStack: true,
|
||||
}).Dial,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
},
|
||||
Timeout: time.Duration(time.Second * 30),
|
||||
// ZabbixDatasource stores state about a specific datasource and provides methods to make
|
||||
// requests to the Zabbix API
|
||||
type ZabbixDatasource struct {
|
||||
queryCache *Cache
|
||||
logger hclog.Logger
|
||||
httpClient *http.Client
|
||||
authToken string
|
||||
}
|
||||
|
||||
var queryCache = NewCache(10*time.Minute, 10*time.Minute)
|
||||
|
||||
var zabbixAuth string = ""
|
||||
// NewZabbixDatasource returns an initialized ZabbixDatasource
|
||||
func NewZabbixDatasource() *ZabbixDatasource {
|
||||
return &ZabbixDatasource{
|
||||
queryCache: NewCache(10*time.Minute, 10*time.Minute),
|
||||
httpClient: &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
Renegotiation: tls.RenegotiateFreelyAsClient,
|
||||
},
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
Dial: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).Dial,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
},
|
||||
Timeout: time.Duration(time.Second * 30),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// ZabbixAPIQuery handles query requests to Zabbix
|
||||
func (ds *ZabbixDatasource) ZabbixAPIQuery(ctx context.Context, tsdbReq *datasource.DatasourceRequest) (*datasource.DatasourceResponse, error) {
|
||||
result, queryExistInCache := queryCache.Get(Hash(tsdbReq.String()))
|
||||
result, queryExistInCache := ds.queryCache.Get(HashString(tsdbReq.String()))
|
||||
|
||||
if !queryExistInCache {
|
||||
dsInfo := tsdbReq.GetDatasource()
|
||||
@@ -86,8 +98,8 @@ func (ds *ZabbixDatasource) ZabbixAPIQuery(ctx context.Context, tsdbReq *datasou
|
||||
apiMethod := jsonQuery.Get("method").MustString()
|
||||
apiParams := jsonQuery.Get("params")
|
||||
|
||||
result, err = ds.ZabbixRequest(ctx, dsInfo, apiMethod, apiParams)
|
||||
queryCache.Set(Hash(tsdbReq.String()), result)
|
||||
result, err := ds.ZabbixRequest(ctx, dsInfo, apiMethod, apiParams)
|
||||
ds.queryCache.Set(HashString(tsdbReq.String()), result)
|
||||
if err != nil {
|
||||
ds.logger.Debug("ZabbixAPIQuery", "error", err)
|
||||
return nil, errors.New("ZabbixAPIQuery is not implemented yet")
|
||||
@@ -100,6 +112,7 @@ func (ds *ZabbixDatasource) ZabbixAPIQuery(ctx context.Context, tsdbReq *datasou
|
||||
return ds.BuildResponse(result.(*simplejson.Json))
|
||||
}
|
||||
|
||||
// BuildResponse transforms a Zabbix API response to a DatasourceResponse
|
||||
func (ds *ZabbixDatasource) BuildResponse(result *simplejson.Json) (*datasource.DatasourceResponse, error) {
|
||||
resultByte, err := result.MarshalJSON()
|
||||
if err != nil {
|
||||
@@ -116,19 +129,20 @@ func (ds *ZabbixDatasource) BuildResponse(result *simplejson.Json) (*datasource.
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ZabbixRequest checks authentication and makes a request to the Zabbix API
|
||||
func (ds *ZabbixDatasource) ZabbixRequest(ctx context.Context, dsInfo *datasource.DatasourceInfo, method string, params *simplejson.Json) (*simplejson.Json, error) {
|
||||
zabbixUrl := dsInfo.GetUrl()
|
||||
zabbixURL := dsInfo.GetUrl()
|
||||
|
||||
// Authenticate first
|
||||
if zabbixAuth == "" {
|
||||
if ds.authToken == "" {
|
||||
auth, err := ds.loginWithDs(ctx, dsInfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
zabbixAuth = auth
|
||||
ds.authToken = auth
|
||||
}
|
||||
|
||||
return ds.zabbixAPIRequest(ctx, zabbixUrl, method, params, zabbixAuth)
|
||||
return ds.zabbixAPIRequest(ctx, zabbixURL, method, params, ds.authToken)
|
||||
}
|
||||
|
||||
func (ds *ZabbixDatasource) loginWithDs(ctx context.Context, dsInfo *datasource.DatasourceInfo) (string, error) {
|
||||
@@ -210,7 +224,7 @@ func (ds *ZabbixDatasource) zabbixAPIRequest(ctx context.Context, apiUrl string,
|
||||
Body: rc,
|
||||
}
|
||||
|
||||
response, err := makeHttpRequest(ctx, req)
|
||||
response, err := makeHTTPRequest(ctx, ds.httpClient, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -233,7 +247,7 @@ func handleApiResult(response []byte) (*simplejson.Json, error) {
|
||||
return jsonResult, nil
|
||||
}
|
||||
|
||||
func makeHttpRequest(ctx context.Context, req *http.Request) ([]byte, error) {
|
||||
func makeHTTPRequest(ctx context.Context, httpClient *http.Client, req *http.Request) ([]byte, error) {
|
||||
res, err := ctxhttp.Do(ctx, httpClient, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user