@@ -16,7 +16,7 @@ import (
1616
1717const (
1818 // APIVersion is the default version of NGINX Plus API supported by the client.
19- APIVersion = 5
19+ APIVersion = 6
2020
2121 pathNotFoundCode = "PathNotFound"
2222 streamContext = true
@@ -25,7 +25,7 @@ const (
2525)
2626
2727var (
28- supportedAPIVersions = versions {4 , 5 }
28+ supportedAPIVersions = versions {4 , 5 , 6 }
2929
3030 // Default values for servers in Upstreams.
3131 defaultMaxConns = 0
@@ -116,20 +116,23 @@ func (internalError *internalError) Wrap(err string) *internalError {
116116// Stats represents NGINX Plus stats fetched from the NGINX Plus API.
117117// https://nginx.org/en/docs/http/ngx_http_api_module.html
118118type Stats struct {
119- NginxInfo NginxInfo
120- Caches Caches
121- Processes Processes
122- Connections Connections
123- Slabs Slabs
124- HTTPRequests HTTPRequests
125- SSL SSL
126- ServerZones ServerZones
127- Upstreams Upstreams
128- StreamServerZones StreamServerZones
129- StreamUpstreams StreamUpstreams
130- StreamZoneSync * StreamZoneSync
131- LocationZones LocationZones
132- Resolvers Resolvers
119+ NginxInfo NginxInfo
120+ Caches Caches
121+ Processes Processes
122+ Connections Connections
123+ Slabs Slabs
124+ HTTPRequests HTTPRequests
125+ SSL SSL
126+ ServerZones ServerZones
127+ Upstreams Upstreams
128+ StreamServerZones StreamServerZones
129+ StreamUpstreams StreamUpstreams
130+ StreamZoneSync * StreamZoneSync
131+ LocationZones LocationZones
132+ Resolvers Resolvers
133+ HTTPLimitRequests HTTPLimitRequests
134+ HTTPLimitConnections HTTPLimitConnections
135+ StreamLimitConnections StreamLimitConnections
133136}
134137
135138// NginxInfo contains general information about NGINX Plus.
@@ -418,6 +421,31 @@ type Processes struct {
418421 Respawned int64
419422}
420423
424+ // HTTPLimitRequest represents HTTP Requests Rate Limiting
425+ type HTTPLimitRequest struct {
426+ Passed uint64
427+ Delayed uint64
428+ Rejected uint64
429+ DelayedDryRun uint64 `json:"delayed_dry_run"`
430+ RejectedDryRun uint64 `json:"rejected_dry_run"`
431+ }
432+
433+ // HTTPLimitRequests represents limit requests related stats
434+ type HTTPLimitRequests map [string ]HTTPLimitRequest
435+
436+ // LimitConnection represents Connections Limiting
437+ type LimitConnection struct {
438+ Passed uint64
439+ Rejected uint64
440+ RejectedDryRun uint64 `json:"rejected_dry_run"`
441+ }
442+
443+ // HTTPLimitConnections represents limit connections related stats
444+ type HTTPLimitConnections map [string ]LimitConnection
445+
446+ // StreamLimitConnections represents limit connections related stats
447+ type StreamLimitConnections map [string ]LimitConnection
448+
421449// NewNginxClient creates an NginxClient with the latest supported version.
422450func NewNginxClient (httpClient * http.Client , apiEndpoint string ) (* NginxClient , error ) {
423451 return NewNginxClientWithVersion (httpClient , apiEndpoint , APIVersion )
@@ -1095,21 +1123,39 @@ func (client *NginxClient) GetStats() (*Stats, error) {
10951123 return nil , fmt .Errorf ("failed to get stats: %w" , err )
10961124 }
10971125
1126+ limitReqs , err := client .GetHTTPLimitReqs ()
1127+ if err != nil {
1128+ return nil , fmt .Errorf ("failed to get stats: %w" , err )
1129+ }
1130+
1131+ limitConnsHTTP , err := client .GetHTTPConnectionsLimit ()
1132+ if err != nil {
1133+ return nil , fmt .Errorf ("failed to get stats: %w" , err )
1134+ }
1135+
1136+ limitConnsStream , err := client .GetStreamConnectionsLimit ()
1137+ if err != nil {
1138+ return nil , fmt .Errorf ("failed to get stats: %w" , err )
1139+ }
1140+
10981141 return & Stats {
1099- NginxInfo : * info ,
1100- Caches : * caches ,
1101- Processes : * processes ,
1102- Slabs : * slabs ,
1103- Connections : * cons ,
1104- HTTPRequests : * requests ,
1105- SSL : * ssl ,
1106- ServerZones : * zones ,
1107- StreamServerZones : * streamZones ,
1108- Upstreams : * upstreams ,
1109- StreamUpstreams : * streamUpstreams ,
1110- StreamZoneSync : streamZoneSync ,
1111- LocationZones : * locationZones ,
1112- Resolvers : * resolvers ,
1142+ NginxInfo : * info ,
1143+ Caches : * caches ,
1144+ Processes : * processes ,
1145+ Slabs : * slabs ,
1146+ Connections : * cons ,
1147+ HTTPRequests : * requests ,
1148+ SSL : * ssl ,
1149+ ServerZones : * zones ,
1150+ StreamServerZones : * streamZones ,
1151+ Upstreams : * upstreams ,
1152+ StreamUpstreams : * streamUpstreams ,
1153+ StreamZoneSync : streamZoneSync ,
1154+ LocationZones : * locationZones ,
1155+ Resolvers : * resolvers ,
1156+ HTTPLimitRequests : * limitReqs ,
1157+ HTTPLimitConnections : * limitConnsHTTP ,
1158+ StreamLimitConnections : * limitConnsStream ,
11131159 }, nil
11141160}
11151161
@@ -1500,3 +1546,48 @@ func addPortToServer(server string) string {
15001546
15011547 return fmt .Sprintf ("%v:%v" , server , defaultServerPort )
15021548}
1549+
1550+ // GetHTTPLimitReqs returns http/limit_reqs stats.
1551+ func (client * NginxClient ) GetHTTPLimitReqs () (* HTTPLimitRequests , error ) {
1552+ var limitReqs HTTPLimitRequests
1553+ if client .version < 6 {
1554+ return & limitReqs , nil
1555+ }
1556+ err := client .get ("http/limit_reqs" , & limitReqs )
1557+ if err != nil {
1558+ return nil , fmt .Errorf ("failed to get http limit requests: %w" , err )
1559+ }
1560+ return & limitReqs , nil
1561+ }
1562+
1563+ // GetHTTPConnectionsLimit returns http/limit_conns stats.
1564+ func (client * NginxClient ) GetHTTPConnectionsLimit () (* HTTPLimitConnections , error ) {
1565+ var limitConns HTTPLimitConnections
1566+ if client .version < 6 {
1567+ return & limitConns , nil
1568+ }
1569+ err := client .get ("http/limit_conns" , & limitConns )
1570+ if err != nil {
1571+ return nil , fmt .Errorf ("failed to get http connections limit: %w" , err )
1572+ }
1573+ return & limitConns , nil
1574+ }
1575+
1576+ // GetStreamConnectionsLimit returns stream/limit_conns stats.
1577+ func (client * NginxClient ) GetStreamConnectionsLimit () (* StreamLimitConnections , error ) {
1578+ var limitConns StreamLimitConnections
1579+ if client .version < 6 {
1580+ return & limitConns , nil
1581+ }
1582+ err := client .get ("stream/limit_conns" , & limitConns )
1583+ if err != nil {
1584+ var ie * internalError
1585+ if errors .As (err , & ie ) {
1586+ if ie .Code == pathNotFoundCode {
1587+ return & limitConns , nil
1588+ }
1589+ }
1590+ return nil , fmt .Errorf ("failed to get stream connections limit: %w" , err )
1591+ }
1592+ return & limitConns , nil
1593+ }
0 commit comments