caching the Zabbix API Queries (#807)

This commit is contained in:
vignesh-reddy
2019-10-04 11:24:56 -05:00
committed by Alexander Zobnin
parent 6e80b49120
commit e8c5c0c3b9
4 changed files with 88 additions and 40 deletions

38
pkg/cache.go Normal file
View File

@@ -0,0 +1,38 @@
package main
import (
"crypto/sha1"
"encoding/hex"
"time"
cache "github.com/patrickmn/go-cache"
)
// Cache is a abstraction over go-cache.
type Cache struct {
cache *cache.Cache
}
// NewCache creates a go-cache with expiration(ttl) time and cleanupInterval.
func NewCache(ttl time.Duration, cleanupInterval time.Duration) *Cache {
return &Cache{
cache.New(ttl, cleanupInterval),
}
}
// Set the value of the key "request" to "rersponse" with default expiration time.
func (c *Cache) Set(request string, response interface{}) {
c.cache.SetDefault(request, response)
}
// Get the value associated with request from the cache
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 {
hash := sha1.New()
hash.Write([]byte(text))
return hex.EncodeToString(hash.Sum(nil))
}

View File

@@ -38,59 +38,66 @@ var httpClient = &http.Client{
Timeout: time.Duration(time.Second * 30),
}
var queryCache = NewCache(10*time.Minute, 10*time.Minute)
var zabbixAuth string = ""
func (ds *ZabbixDatasource) ZabbixAPIQuery(ctx context.Context, tsdbReq *datasource.DatasourceRequest) (*datasource.DatasourceResponse, error) {
dsInfo := tsdbReq.GetDatasource()
zabbixUrlStr := dsInfo.GetUrl()
zabbixUrl, err := url.Parse(zabbixUrlStr)
if err != nil {
return nil, err
}
jsonDataStr := dsInfo.GetJsonData()
jsonData, err := simplejson.NewJson([]byte(jsonDataStr))
if err != nil {
return nil, err
}
zabbixLogin := jsonData.Get("username").MustString()
// zabbixPassword := jsonData.Get("password").MustString()
ds.logger.Debug("ZabbixAPIQuery", "url", zabbixUrl, "user", zabbixLogin)
jsonQueries := make([]*simplejson.Json, 0)
for _, query := range tsdbReq.Queries {
json, err := simplejson.NewJson([]byte(query.ModelJson))
apiMethod := json.GetPath("target", "method").MustString()
apiParams := json.GetPath("target", "params")
result, queryExistInCache := queryCache.Get(Hash(tsdbReq.String()))
if !queryExistInCache {
dsInfo := tsdbReq.GetDatasource()
zabbixUrlStr := dsInfo.GetUrl()
zabbixUrl, err := url.Parse(zabbixUrlStr)
if err != nil {
return nil, err
}
ds.logger.Debug("ZabbixAPIQuery", "method", apiMethod, "params", apiParams)
jsonDataStr := dsInfo.GetJsonData()
jsonData, err := simplejson.NewJson([]byte(jsonDataStr))
if err != nil {
return nil, err
}
jsonQueries = append(jsonQueries, json)
zabbixLogin := jsonData.Get("username").MustString()
// zabbixPassword := jsonData.Get("password").MustString()
ds.logger.Debug("ZabbixAPIQuery", "url", zabbixUrl, "user", zabbixLogin)
jsonQueries := make([]*simplejson.Json, 0)
for _, query := range tsdbReq.Queries {
json, err := simplejson.NewJson([]byte(query.ModelJson))
apiMethod := json.GetPath("target", "method").MustString()
apiParams := json.GetPath("target", "params")
if err != nil {
return nil, err
}
ds.logger.Debug("ZabbixAPIQuery", "method", apiMethod, "params", apiParams)
jsonQueries = append(jsonQueries, json)
}
if len(jsonQueries) == 0 {
return nil, errors.New("At least one query should be provided")
}
jsonQuery := jsonQueries[0].Get("target")
apiMethod := jsonQuery.Get("method").MustString()
apiParams := jsonQuery.Get("params")
result, err = ds.ZabbixRequest(ctx, dsInfo, apiMethod, apiParams)
queryCache.Set(Hash(tsdbReq.String()), result)
if err != nil {
ds.logger.Debug("ZabbixAPIQuery", "error", err)
return nil, errors.New("ZabbixAPIQuery is not implemented yet")
}
}
if len(jsonQueries) == 0 {
return nil, errors.New("At least one query should be provided")
}
jsonQuery := jsonQueries[0].Get("target")
apiMethod := jsonQuery.Get("method").MustString()
apiParams := jsonQuery.Get("params")
result, err := ds.ZabbixRequest(ctx, dsInfo, apiMethod, apiParams)
if err != nil {
ds.logger.Debug("ZabbixAPIQuery", "error", err)
return nil, errors.New("ZabbixAPIQuery is not implemented yet")
}
resultByte, err := result.MarshalJSON()
resultByte, _ := result.(*simplejson.Json).MarshalJSON()
ds.logger.Debug("ZabbixAPIQuery", "result", string(resultByte))
return ds.BuildResponse(result)
return ds.BuildResponse(result.(*simplejson.Json))
}
func (ds *ZabbixDatasource) BuildResponse(result *simplejson.Json) (*datasource.DatasourceResponse, error) {