Skip to content

Commit 487aac4

Browse files
committed
Use timeout flag for context cancelation
1 parent 9cd3941 commit 487aac4

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

client/nginx.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ func NewNginxClient(httpClient *http.Client, apiEndpoint string) *NginxClient {
4747
}
4848

4949
// GetStubStats fetches the stub_status metrics.
50-
func (client *NginxClient) GetStubStats() (*StubStats, error) {
51-
ctx, cancel := context.WithCancel(context.Background())
52-
defer cancel()
53-
50+
func (client *NginxClient) GetStubStats(ctx context.Context) (*StubStats, error) {
5451
req, err := http.NewRequestWithContext(ctx, http.MethodGet, client.apiEndpoint, nil)
5552
if err != nil {
5653
return nil, fmt.Errorf("failed to create a get request: %w", err)

collector/nginx.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package collector
22

33
import (
4+
"context"
45
"log/slog"
56
"sync"
7+
"time"
68

79
"github.com/nginxinc/nginx-prometheus-exporter/client"
810
"github.com/prometheus/client_golang/prometheus"
@@ -14,14 +16,16 @@ type NginxCollector struct {
1416
logger *slog.Logger
1517
nginxClient *client.NginxClient
1618
metrics map[string]*prometheus.Desc
19+
timeout time.Duration
1720
mutex sync.Mutex
1821
}
1922

2023
// NewNginxCollector creates an NginxCollector.
21-
func NewNginxCollector(nginxClient *client.NginxClient, namespace string, constLabels map[string]string, logger *slog.Logger) *NginxCollector {
24+
func NewNginxCollector(nginxClient *client.NginxClient, namespace string, constLabels map[string]string, logger *slog.Logger, timeout time.Duration) *NginxCollector {
2225
return &NginxCollector{
2326
nginxClient: nginxClient,
2427
logger: logger,
28+
timeout: timeout,
2529
metrics: map[string]*prometheus.Desc{
2630
"connections_active": newGlobalMetric(namespace, "connections_active", "Active client connections", constLabels),
2731
"connections_accepted": newGlobalMetric(namespace, "connections_accepted", "Accepted client connections", constLabels),
@@ -50,7 +54,10 @@ func (c *NginxCollector) Collect(ch chan<- prometheus.Metric) {
5054
c.mutex.Lock() // To protect metrics from concurrent collects
5155
defer c.mutex.Unlock()
5256

53-
stats, err := c.nginxClient.GetStubStats()
57+
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
58+
defer cancel()
59+
60+
stats, err := c.nginxClient.GetStubStats(ctx)
5461
if err != nil {
5562
c.upMetric.Set(nginxDown)
5663
ch <- c.upMetric

collector/nginx_plus.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log/slog"
77
"strconv"
88
"sync"
9+
"time"
910

1011
plusclient "github.com/nginxinc/nginx-plus-go-client/v2/client"
1112
"github.com/prometheus/client_golang/prometheus"
@@ -31,11 +32,11 @@ type LabelUpdater interface {
3132

3233
// NginxPlusCollector collects NGINX Plus metrics. It implements prometheus.Collector interface.
3334
type NginxPlusCollector struct {
34-
upMetric prometheus.Gauge
35+
nginxClient *plusclient.NginxClient
3536
logger *slog.Logger
37+
upMetric prometheus.Gauge
3638
cacheZoneMetrics map[string]*prometheus.Desc
3739
workerMetrics map[string]*prometheus.Desc
38-
nginxClient *plusclient.NginxClient
3940
streamServerZoneMetrics map[string]*prometheus.Desc
4041
streamZoneSyncMetrics map[string]*prometheus.Desc
4142
streamUpstreamMetrics map[string]*prometheus.Desc
@@ -47,16 +48,17 @@ type NginxPlusCollector struct {
4748
streamLimitConnectionMetrics map[string]*prometheus.Desc
4849
upstreamServerMetrics map[string]*prometheus.Desc
4950
upstreamMetrics map[string]*prometheus.Desc
50-
streamUpstreamServerPeerLabels map[string][]string
5151
serverZoneMetrics map[string]*prometheus.Desc
52+
totalMetrics map[string]*prometheus.Desc
53+
streamUpstreamServerPeerLabels map[string][]string
5254
upstreamServerLabels map[string][]string
5355
streamUpstreamServerLabels map[string][]string
5456
serverZoneLabels map[string][]string
5557
streamServerZoneLabels map[string][]string
5658
upstreamServerPeerLabels map[string][]string
5759
cacheZoneLabels map[string][]string
58-
totalMetrics map[string]*prometheus.Desc
5960
variableLabelNames VariableLabelNames
61+
timeout time.Duration
6062
variableLabelsMutex sync.RWMutex
6163
mutex sync.Mutex
6264
}
@@ -256,7 +258,7 @@ func NewVariableLabelNames(upstreamServerVariableLabelNames []string, serverZone
256258
}
257259

258260
// NewNginxPlusCollector creates an NginxPlusCollector.
259-
func NewNginxPlusCollector(nginxClient *plusclient.NginxClient, namespace string, variableLabelNames VariableLabelNames, constLabels map[string]string, logger *slog.Logger) *NginxPlusCollector {
261+
func NewNginxPlusCollector(nginxClient *plusclient.NginxClient, namespace string, variableLabelNames VariableLabelNames, constLabels map[string]string, logger *slog.Logger, timeout time.Duration) *NginxPlusCollector {
260262
upstreamServerVariableLabelNames := variableLabelNames.UpstreamServerVariableLabelNames
261263
streamUpstreamServerVariableLabelNames := variableLabelNames.StreamUpstreamServerVariableLabelNames
262264

@@ -273,6 +275,7 @@ func NewNginxPlusCollector(nginxClient *plusclient.NginxClient, namespace string
273275
cacheZoneLabels: make(map[string][]string),
274276
nginxClient: nginxClient,
275277
logger: logger,
278+
timeout: timeout,
276279
totalMetrics: map[string]*prometheus.Desc{
277280
"connections_accepted": newGlobalMetric(namespace, "connections_accepted", "Accepted client connections", constLabels),
278281
"connections_dropped": newGlobalMetric(namespace, "connections_dropped", "Dropped client connections", constLabels),
@@ -623,8 +626,10 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
623626
c.mutex.Lock() // To protect metrics from concurrent collects
624627
defer c.mutex.Unlock()
625628

626-
// FIXME: https://github.com/nginxinc/nginx-prometheus-exporter/issues/858
627-
stats, err := c.nginxClient.GetStats(context.TODO())
629+
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
630+
defer cancel()
631+
632+
stats, err := c.nginxClient.GetStats(ctx)
628633
if err != nil {
629634
c.upMetric.Set(nginxDown)
630635
ch <- c.upMetric

exporter.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,7 @@ func main() {
220220
_ = srv.Shutdown(srvCtx)
221221
}
222222

223-
func registerCollector(logger *slog.Logger, transport *http.Transport,
224-
addr string, labels map[string]string,
225-
) {
223+
func registerCollector(logger *slog.Logger, transport *http.Transport, addr string, labels map[string]string) {
226224
if strings.HasPrefix(addr, "unix:") {
227225
socketPath, requestPath, err := parseUnixSocketAddress(addr)
228226
if err != nil {
@@ -239,7 +237,6 @@ func registerCollector(logger *slog.Logger, transport *http.Transport,
239237
userAgent := fmt.Sprintf("NGINX-Prometheus-Exporter/v%v", common_version.Version)
240238

241239
httpClient := &http.Client{
242-
Timeout: *timeout,
243240
Transport: &userAgentRoundTripper{
244241
agent: userAgent,
245242
rt: transport,
@@ -253,10 +250,10 @@ func registerCollector(logger *slog.Logger, transport *http.Transport,
253250
os.Exit(1)
254251
}
255252
variableLabelNames := collector.NewVariableLabelNames(nil, nil, nil, nil, nil, nil, nil)
256-
prometheus.MustRegister(collector.NewNginxPlusCollector(plusClient, "nginxplus", variableLabelNames, labels, logger))
253+
prometheus.MustRegister(collector.NewNginxPlusCollector(plusClient, "nginxplus", variableLabelNames, labels, logger, *timeout))
257254
} else {
258255
ossClient := client.NewNginxClient(httpClient, addr)
259-
prometheus.MustRegister(collector.NewNginxCollector(ossClient, "nginx", labels, logger))
256+
prometheus.MustRegister(collector.NewNginxCollector(ossClient, "nginx", labels, logger, *timeout))
260257
}
261258
}
262259

0 commit comments

Comments
 (0)