@@ -16,7 +16,7 @@ import (
16
16
17
17
const (
18
18
// APIVersion is the default version of NGINX Plus API supported by the client.
19
- APIVersion = 5
19
+ APIVersion = 6
20
20
21
21
pathNotFoundCode = "PathNotFound"
22
22
streamContext = true
@@ -25,7 +25,7 @@ const (
25
25
)
26
26
27
27
var (
28
- supportedAPIVersions = versions {4 , 5 }
28
+ supportedAPIVersions = versions {4 , 5 , 6 }
29
29
30
30
// Default values for servers in Upstreams.
31
31
defaultMaxConns = 0
@@ -116,20 +116,23 @@ func (internalError *internalError) Wrap(err string) *internalError {
116
116
// Stats represents NGINX Plus stats fetched from the NGINX Plus API.
117
117
// https://nginx.org/en/docs/http/ngx_http_api_module.html
118
118
type 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
133
136
}
134
137
135
138
// NginxInfo contains general information about NGINX Plus.
@@ -418,6 +421,31 @@ type Processes struct {
418
421
Respawned int64
419
422
}
420
423
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
+
421
449
// NewNginxClient creates an NginxClient with the latest supported version.
422
450
func NewNginxClient (httpClient * http.Client , apiEndpoint string ) (* NginxClient , error ) {
423
451
return NewNginxClientWithVersion (httpClient , apiEndpoint , APIVersion )
@@ -1095,21 +1123,39 @@ func (client *NginxClient) GetStats() (*Stats, error) {
1095
1123
return nil , fmt .Errorf ("failed to get stats: %w" , err )
1096
1124
}
1097
1125
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
+
1098
1141
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 ,
1113
1159
}, nil
1114
1160
}
1115
1161
@@ -1500,3 +1546,48 @@ func addPortToServer(server string) string {
1500
1546
1501
1547
return fmt .Sprintf ("%v:%v" , server , defaultServerPort )
1502
1548
}
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