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.
3334type 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
0 commit comments