Skip to content

Commit 80901b0

Browse files
Added version 9 of API and new worker metrics (#143)
1 parent 22fc11b commit 80901b0

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ the API was first introduced.
3030
| 6 | R20 |
3131
| 7 | R25 |
3232
| 8 | R27 |
33+
| 9 | R30 |
3334

3435
## Using the Client
3536

client/nginx.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
const (
1717
// APIVersion is the default version of NGINX Plus API supported by the client.
18-
APIVersion = 8
18+
APIVersion = 9
1919

2020
pathNotFoundCode = "PathNotFound"
2121
streamContext = true
@@ -24,7 +24,7 @@ const (
2424
)
2525

2626
var (
27-
supportedAPIVersions = versions{4, 5, 6, 7, 8}
27+
supportedAPIVersions = versions{4, 5, 6, 7, 8, 9}
2828

2929
// Default values for servers in Upstreams.
3030
defaultMaxConns = 0
@@ -132,6 +132,7 @@ type Stats struct {
132132
HTTPLimitRequests HTTPLimitRequests
133133
HTTPLimitConnections HTTPLimitConnections
134134
StreamLimitConnections StreamLimitConnections
135+
Workers []*Workers
135136
}
136137

137138
// NginxInfo contains general information about NGINX Plus.
@@ -494,6 +495,19 @@ type HTTPLimitConnections map[string]LimitConnection
494495
// StreamLimitConnections represents limit connections related stats
495496
type StreamLimitConnections map[string]LimitConnection
496497

498+
// Workers represents worker connections related stats
499+
type Workers struct {
500+
ID int
501+
ProcessID uint64 `json:"pid"`
502+
HTTP WorkersHTTP `json:"http"`
503+
Connections Connections
504+
}
505+
506+
// WorkersHTTP represents HTTP worker connections
507+
type WorkersHTTP struct {
508+
HTTPRequests HTTPRequests `json:"requests"`
509+
}
510+
497511
// NewNginxClient creates an NginxClient with the latest supported version.
498512
func NewNginxClient(httpClient *http.Client, apiEndpoint string) (*NginxClient, error) {
499513
return NewNginxClientWithVersion(httpClient, apiEndpoint, APIVersion)
@@ -1186,6 +1200,11 @@ func (client *NginxClient) GetStats() (*Stats, error) {
11861200
return nil, fmt.Errorf("failed to get stats: %w", err)
11871201
}
11881202

1203+
workers, err := client.GetWorkers()
1204+
if err != nil {
1205+
return nil, fmt.Errorf("failed to get stats: %w", err)
1206+
}
1207+
11891208
return &Stats{
11901209
NginxInfo: *info,
11911210
Caches: *caches,
@@ -1204,6 +1223,7 @@ func (client *NginxClient) GetStats() (*Stats, error) {
12041223
HTTPLimitRequests: *limitReqs,
12051224
HTTPLimitConnections: *limitConnsHTTP,
12061225
StreamLimitConnections: *limitConnsStream,
1226+
Workers: workers,
12071227
}, nil
12081228
}
12091229

@@ -1639,3 +1659,16 @@ func (client *NginxClient) GetStreamConnectionsLimit() (*StreamLimitConnections,
16391659
}
16401660
return &limitConns, nil
16411661
}
1662+
1663+
// GetWorkers returns workers stats.
1664+
func (client *NginxClient) GetWorkers() ([]*Workers, error) {
1665+
var workers []*Workers
1666+
if client.version < 9 {
1667+
return workers, nil
1668+
}
1669+
err := client.get("workers", &workers)
1670+
if err != nil {
1671+
return nil, fmt.Errorf("failed to get workers: %w", err)
1672+
}
1673+
return workers, nil
1674+
}

tests/client_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,10 @@ func TestStats(t *testing.T) {
627627
t.Errorf("Bad connections: %v", stats.Connections)
628628
}
629629

630+
if len(stats.Workers) < 1 {
631+
t.Errorf("Bad workers: %v", stats.Workers)
632+
}
633+
630634
if val, ok := stats.Caches[cacheZone]; ok {
631635
if val.MaxSize != 104857600 { // 100MiB
632636
t.Errorf("Cache max size stats missing: %v", val.Size)

0 commit comments

Comments
 (0)