do not cache history requests
This commit is contained in:
@@ -131,7 +131,7 @@ func newZabbixDatasource(dsInfo *backend.DataSourceInstanceSettings) (*ZabbixDat
|
|||||||
url: zabbixURL,
|
url: zabbixURL,
|
||||||
dsInfo: dsInfo,
|
dsInfo: dsInfo,
|
||||||
Settings: zabbixSettings,
|
Settings: zabbixSettings,
|
||||||
queryCache: NewCache(10*time.Minute, 10*time.Minute),
|
queryCache: NewCache(zabbixSettings.CacheTTL, 10*time.Minute),
|
||||||
httpClient: &http.Client{
|
httpClient: &http.Client{
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
TLSClientConfig: &tls.Config{
|
TLSClientConfig: &tls.Config{
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ func (ds *ZabbixDatasource) zabbixAPIHandler(rw http.ResponseWriter, req *http.R
|
|||||||
pluginCxt := httpadapter.PluginConfigFromContext(req.Context())
|
pluginCxt := httpadapter.PluginConfigFromContext(req.Context())
|
||||||
dsInstance, err := ds.GetDatasource(pluginCxt)
|
dsInstance, err := ds.GetDatasource(pluginCxt)
|
||||||
|
|
||||||
ds.logger.Debug("Invoke Zabbix API call", "ds", pluginCxt.DataSourceInstanceSettings.Name, "method", reqData.Method)
|
|
||||||
apiReq := &ZabbixAPIRequest{Method: reqData.Method, Params: reqData.Params}
|
apiReq := &ZabbixAPIRequest{Method: reqData.Method, Params: reqData.Params}
|
||||||
|
|
||||||
result, err := dsInstance.ZabbixAPIQuery(req.Context(), apiReq)
|
result, err := dsInstance.ZabbixAPIQuery(req.Context(), apiReq)
|
||||||
|
|||||||
@@ -13,16 +13,26 @@ import (
|
|||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var NotCachedMethods = map[string]bool{
|
||||||
|
"history.get": true,
|
||||||
|
"trend.get": true,
|
||||||
|
}
|
||||||
|
|
||||||
// ZabbixAPIQuery handles query requests to Zabbix
|
// ZabbixAPIQuery handles query requests to Zabbix
|
||||||
func (ds *ZabbixDatasourceInstance) ZabbixAPIQuery(ctx context.Context, apiReq *ZabbixAPIRequest) (*ZabbixAPIResourceResponse, error) {
|
func (ds *ZabbixDatasourceInstance) ZabbixAPIQuery(ctx context.Context, apiReq *ZabbixAPIRequest) (*ZabbixAPIResourceResponse, error) {
|
||||||
var result interface{}
|
var result interface{}
|
||||||
var err error
|
var err error
|
||||||
var queryExistInCache bool
|
var queryExistInCache bool
|
||||||
result, queryExistInCache = ds.queryCache.Get(HashString(apiReq.String()))
|
requestHash := HashString(apiReq.String())
|
||||||
|
result, queryExistInCache = ds.queryCache.Get(requestHash)
|
||||||
|
|
||||||
if !queryExistInCache {
|
if !queryExistInCache {
|
||||||
result, err = ds.ZabbixRequest(ctx, apiReq.Method, apiReq.Params)
|
result, err = ds.ZabbixRequest(ctx, apiReq.Method, apiReq.Params)
|
||||||
ds.queryCache.Set(HashString(apiReq.String()), result)
|
|
||||||
|
if _, ok := NotCachedMethods[apiReq.Method]; !ok {
|
||||||
|
ds.logger.Debug("Write result to cache", "method", apiReq.Method)
|
||||||
|
ds.queryCache.Set(HashString(apiReq.String()), result)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import (
|
|||||||
|
|
||||||
// ZabbixRequest checks authentication and makes a request to the Zabbix API
|
// ZabbixRequest checks authentication and makes a request to the Zabbix API
|
||||||
func (ds *ZabbixDatasourceInstance) ZabbixRequest(ctx context.Context, method string, params ZabbixAPIParams) (*simplejson.Json, error) {
|
func (ds *ZabbixDatasourceInstance) ZabbixRequest(ctx context.Context, method string, params ZabbixAPIParams) (*simplejson.Json, error) {
|
||||||
|
ds.logger.Debug("Invoke Zabbix API request", "ds", ds.dsInfo.Name, "method", method)
|
||||||
var result *simplejson.Json
|
var result *simplejson.Json
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user