From 0986010c068f58010149ed42e1b3d993f1d44118 Mon Sep 17 00:00:00 2001 From: Charlie Haley Date: Thu, 16 Jun 2022 21:04:46 +0200 Subject: [PATCH] feat: add configurable request timeout (#46) --- README.md | 2 ++ cmd/exporter.go | 2 ++ pkg/api/api.go | 6 +++--- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ee74f00..484d3f8 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ GLOBAL OPTIONS: --port value Port on which to expose the Prometheus metrics. (default: "9202") [$OMADA_PORT] --site value Omada site to scrape metrics from. (default: "Default") [$OMADA_SITE] --interval value Interval between scrapes, in seconds. (default: 5) [$OMADA_SCRAPE_INTERVAL] + --timeout value Timeout when making requests to the Omada Controller. (default: 15) [$OMADA_REQUEST_TIMEOUT] --insecure Whether to skip verifying the SSL certificate on the controller. (default: false) [$OMADA_INSECURE] --disable-go-collector Disable Go collector metrics. (default: true) [$OMADA_DISABLE_GO_COLLECTOR] --disable-process-collector Disable process collector metrics. (default: true) [$OMADA_DISABLE_PROCESS_COLLECTOR] @@ -93,6 +94,7 @@ OMADA_SITE | Site you'd like to get metrics from. (default: "Defau OMADA_PORT | Port on which to expose the Prometheus metrics. (default: 9202) OMADA_INSECURE | Whether to skip verifying the SSL certificate on the controller. (default: false) OMADA_SCRAPE_INTERVAL | Interval between scrapes, in seconds. (default: 5) +OMADA_REQUEST_TIMEOUT | Timeout when making requests to the Omada Controller. (default: 15) OMADA_DISABLE_GO_COLLECTOR | Disable Go collector metrics. (default: true) OMADA_DISABLE_PROCESS_COLLECTOR | Disable process collector metrics. (default: true) diff --git a/cmd/exporter.go b/cmd/exporter.go index 3971a87..dfdd7e9 100644 --- a/cmd/exporter.go +++ b/cmd/exporter.go @@ -23,6 +23,7 @@ var ( port string site string interval int + timeout int insecure bool goCollectorDisabled bool processCollectorDisabled bool @@ -44,6 +45,7 @@ func Run() { &cli.StringFlag{Destination: &port, Name: "port", Value: "9202", Usage: "Port on which to expose the Prometheus metrics.", EnvVars: []string{"OMADA_PORT"}}, &cli.StringFlag{Destination: &site, Name: "site", Value: "Default", Usage: "Omada site to scrape metrics from.", EnvVars: []string{"OMADA_SITE"}}, &cli.IntFlag{Destination: &interval, Name: "interval", Value: 5, Usage: "Interval between scrapes, in seconds.", EnvVars: []string{"OMADA_SCRAPE_INTERVAL"}}, + &cli.IntFlag{Destination: &timeout, Name: "timeout", Value: 15, Usage: "Timeout when making requests to the Omada Controller.", EnvVars: []string{"OMADA_REQUEST_TIMEOUT"}}, &cli.BoolFlag{Destination: &insecure, Name: "insecure", Value: false, Usage: "Whether to skip verifying the SSL certificate on the controller.", EnvVars: []string{"OMADA_INSECURE"}}, &cli.BoolFlag{Destination: &goCollectorDisabled, Name: "disable-go-collector", Value: true, Usage: "Disable Go collector metrics.", EnvVars: []string{"OMADA_DISABLE_GO_COLLECTOR"}}, &cli.BoolFlag{Destination: &processCollectorDisabled, Name: "disable-process-collector", Value: true, Usage: "Disable process collector metrics.", EnvVars: []string{"OMADA_DISABLE_PROCESS_COLLECTOR"}}, diff --git a/pkg/api/api.go b/pkg/api/api.go index 4a3283e..b6fd70b 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -18,7 +18,7 @@ type Client struct { SiteId string } -func setuphttpClient(insecure bool) (*http.Client, error) { +func setuphttpClient(insecure bool, timeout int) (*http.Client, error) { jar, err := cookiejar.New(nil) if err != nil { return nil, fmt.Errorf("failed to init cookiejar") @@ -28,7 +28,7 @@ func setuphttpClient(insecure bool) (*http.Client, error) { t.MaxConnsPerHost = 100 t.MaxIdleConnsPerHost = 100 - client := &http.Client{Transport: t, Timeout: time.Duration(10) * time.Second, Jar: jar} + client := &http.Client{Transport: t, Timeout: time.Duration(timeout) * time.Second, Jar: jar} if insecure { t.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} @@ -38,7 +38,7 @@ func setuphttpClient(insecure bool) (*http.Client, error) { } func Configure(c *cli.Context) (*Client, error) { - httpClient, err := setuphttpClient(c.Bool("insecure")) + httpClient, err := setuphttpClient(c.Bool("insecure"), c.Int("timeout")) if err != nil { return nil, err }