Able to configure API request timeout, #1046
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/alexanderzobnin/grafana-zabbix/pkg/gtime"
|
"github.com/alexanderzobnin/grafana-zabbix/pkg/gtime"
|
||||||
@@ -49,18 +50,18 @@ func newZabbixDatasourceInstance(settings backend.DataSourceInstanceSettings) (i
|
|||||||
logger := log.New()
|
logger := log.New()
|
||||||
logger.Debug("Initializing new data source instance")
|
logger.Debug("Initializing new data source instance")
|
||||||
|
|
||||||
zabbixAPI, err := zabbixapi.New(settings.URL, &settings)
|
|
||||||
if err != nil {
|
|
||||||
logger.Error("Error initializing Zabbix API", "error", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
zabbixSettings, err := readZabbixSettings(&settings)
|
zabbixSettings, err := readZabbixSettings(&settings)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Error parsing Zabbix settings", "error", err)
|
logger.Error("Error parsing Zabbix settings", "error", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
zabbixAPI, err := zabbixapi.New(&settings, zabbixSettings.Timeout)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Error initializing Zabbix API", "error", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return &ZabbixDatasourceInstance{
|
return &ZabbixDatasourceInstance{
|
||||||
dsInfo: &settings,
|
dsInfo: &settings,
|
||||||
zabbixAPI: zabbixAPI,
|
zabbixAPI: zabbixAPI,
|
||||||
@@ -154,6 +155,10 @@ func readZabbixSettings(dsInstanceSettings *backend.DataSourceInstanceSettings)
|
|||||||
zabbixSettingsDTO.CacheTTL = "1h"
|
zabbixSettingsDTO.CacheTTL = "1h"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if zabbixSettingsDTO.Timeout == "" {
|
||||||
|
zabbixSettingsDTO.Timeout = "30"
|
||||||
|
}
|
||||||
|
|
||||||
trendsFrom, err := gtime.ParseInterval(zabbixSettingsDTO.TrendsFrom)
|
trendsFrom, err := gtime.ParseInterval(zabbixSettingsDTO.TrendsFrom)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -169,11 +174,17 @@ func readZabbixSettings(dsInstanceSettings *backend.DataSourceInstanceSettings)
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
timeout, err := strconv.Atoi(zabbixSettingsDTO.Timeout)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("failed to parse timeout: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
zabbixSettings := &ZabbixDatasourceSettings{
|
zabbixSettings := &ZabbixDatasourceSettings{
|
||||||
Trends: zabbixSettingsDTO.Trends,
|
Trends: zabbixSettingsDTO.Trends,
|
||||||
TrendsFrom: trendsFrom,
|
TrendsFrom: trendsFrom,
|
||||||
TrendsRange: trendsRange,
|
TrendsRange: trendsRange,
|
||||||
CacheTTL: cacheTTL,
|
CacheTTL: cacheTTL,
|
||||||
|
Timeout: time.Duration(timeout) * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
return zabbixSettings, nil
|
return zabbixSettings, nil
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ type ZabbixDatasourceSettingsDTO struct {
|
|||||||
TrendsFrom string `json:"trendsFrom"`
|
TrendsFrom string `json:"trendsFrom"`
|
||||||
TrendsRange string `json:"trendsRange"`
|
TrendsRange string `json:"trendsRange"`
|
||||||
CacheTTL string `json:"cacheTTL"`
|
CacheTTL string `json:"cacheTTL"`
|
||||||
|
Timeout string `json:"timeout"`
|
||||||
|
|
||||||
DisableReadOnlyUsersAck bool `json:"disableReadOnlyUsersAck"`
|
DisableReadOnlyUsersAck bool `json:"disableReadOnlyUsersAck"`
|
||||||
}
|
}
|
||||||
@@ -24,6 +25,7 @@ type ZabbixDatasourceSettings struct {
|
|||||||
TrendsFrom time.Duration
|
TrendsFrom time.Duration
|
||||||
TrendsRange time.Duration
|
TrendsRange time.Duration
|
||||||
CacheTTL time.Duration
|
CacheTTL time.Duration
|
||||||
|
Timeout time.Duration
|
||||||
|
|
||||||
DisableReadOnlyUsersAck bool `json:"disableReadOnlyUsersAck"`
|
DisableReadOnlyUsersAck bool `json:"disableReadOnlyUsersAck"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
|
|
||||||
simplejson "github.com/bitly/go-simplejson"
|
simplejson "github.com/bitly/go-simplejson"
|
||||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type proxyTransportCache struct {
|
type proxyTransportCache struct {
|
||||||
@@ -46,14 +47,16 @@ var ptc = proxyTransportCache{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetHttpClient returns new http.Client. Transport either initialized or got from cache.
|
// GetHttpClient returns new http.Client. Transport either initialized or got from cache.
|
||||||
func GetHttpClient(ds *backend.DataSourceInstanceSettings) (*http.Client, error) {
|
func GetHttpClient(ds *backend.DataSourceInstanceSettings, timeout time.Duration) (*http.Client, error) {
|
||||||
transport, err := getHttpTransport(ds)
|
transport, err := getHttpTransport(ds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.DefaultLogger.Debug("Initializing new HTTP client", "timeout", timeout.Seconds())
|
||||||
|
|
||||||
return &http.Client{
|
return &http.Client{
|
||||||
Timeout: time.Duration(time.Second * 30),
|
Timeout: timeout,
|
||||||
Transport: transport,
|
Transport: transport,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/alexanderzobnin/grafana-zabbix/pkg/httpclient"
|
"github.com/alexanderzobnin/grafana-zabbix/pkg/httpclient"
|
||||||
"github.com/bitly/go-simplejson"
|
"github.com/bitly/go-simplejson"
|
||||||
@@ -31,14 +32,14 @@ type ZabbixAPI struct {
|
|||||||
type ZabbixAPIParams = map[string]interface{}
|
type ZabbixAPIParams = map[string]interface{}
|
||||||
|
|
||||||
// New returns new ZabbixAPI instance initialized with given URL or error.
|
// New returns new ZabbixAPI instance initialized with given URL or error.
|
||||||
func New(api_url string, dsInfo *backend.DataSourceInstanceSettings) (*ZabbixAPI, error) {
|
func New(dsInfo *backend.DataSourceInstanceSettings, timeout time.Duration) (*ZabbixAPI, error) {
|
||||||
apiLogger := log.New()
|
apiLogger := log.New()
|
||||||
zabbixURL, err := url.Parse(api_url)
|
zabbixURL, err := url.Parse(dsInfo.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := httpclient.GetHttpClient(dsInfo)
|
client, err := httpclient.GetHttpClient(dsInfo, timeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -154,6 +154,17 @@ export const ConfigEditor = (props: Props) => {
|
|||||||
tooltip="Zabbix data source caches metric names in memory. Specify how often data will be updated."
|
tooltip="Zabbix data source caches metric names in memory. Specify how often data will be updated."
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
<div className="gf-form">
|
||||||
|
<FormField
|
||||||
|
labelWidth={7}
|
||||||
|
inputWidth={4}
|
||||||
|
label="Timeout"
|
||||||
|
value={options.jsonData.timeout || ''}
|
||||||
|
placeholder="30"
|
||||||
|
onChange={jsonDataChangeHandler('timeout', options, onOptionsChange)}
|
||||||
|
tooltip="Zabbix API connection timeout in seconds. Default is 30."
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="gf-form-group">
|
<div className="gf-form-group">
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ export interface ZabbixDSOptions extends DataSourceJsonData {
|
|||||||
trendsFrom: string;
|
trendsFrom: string;
|
||||||
trendsRange: string;
|
trendsRange: string;
|
||||||
cacheTTL: string;
|
cacheTTL: string;
|
||||||
|
timeout?: string;
|
||||||
dbConnectionEnable: boolean;
|
dbConnectionEnable: boolean;
|
||||||
dbConnectionDatasourceId?: number;
|
dbConnectionDatasourceId?: number;
|
||||||
dbConnectionDatasourceName?: string;
|
dbConnectionDatasourceName?: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user