Skip to content

Added version 9 of API and new worker metrics #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 24, 2023
Merged
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ the API was first introduced.
| 6 | R20 |
| 7 | R25 |
| 8 | R27 |
| 9 | R30 |

## Using the Client

Expand Down
37 changes: 35 additions & 2 deletions client/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

const (
// APIVersion is the default version of NGINX Plus API supported by the client.
APIVersion = 8
APIVersion = 9

pathNotFoundCode = "PathNotFound"
streamContext = true
Expand All @@ -24,7 +24,7 @@ const (
)

var (
supportedAPIVersions = versions{4, 5, 6, 7, 8}
supportedAPIVersions = versions{4, 5, 6, 7, 8, 9}

// Default values for servers in Upstreams.
defaultMaxConns = 0
Expand Down Expand Up @@ -132,6 +132,7 @@ type Stats struct {
HTTPLimitRequests HTTPLimitRequests
HTTPLimitConnections HTTPLimitConnections
StreamLimitConnections StreamLimitConnections
Workers []*Workers
}

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

// Workers represents worker connections related stats
type Workers struct {
ID int
ProcessID uint64 `json:"pid"`
HTTP WorkersHTTP `json:"http"`
Connections Connections
}

// WorkersHTTP represents HTTP worker connections
type WorkersHTTP struct {
HTTPRequests HTTPRequests `json:"requests"`
}

// NewNginxClient creates an NginxClient with the latest supported version.
func NewNginxClient(httpClient *http.Client, apiEndpoint string) (*NginxClient, error) {
return NewNginxClientWithVersion(httpClient, apiEndpoint, APIVersion)
Expand Down Expand Up @@ -1186,6 +1200,11 @@ func (client *NginxClient) GetStats() (*Stats, error) {
return nil, fmt.Errorf("failed to get stats: %w", err)
}

workers, err := client.GetWorkers()
if err != nil {
return nil, fmt.Errorf("failed to get stats: %w", err)
}

return &Stats{
NginxInfo: *info,
Caches: *caches,
Expand All @@ -1204,6 +1223,7 @@ func (client *NginxClient) GetStats() (*Stats, error) {
HTTPLimitRequests: *limitReqs,
HTTPLimitConnections: *limitConnsHTTP,
StreamLimitConnections: *limitConnsStream,
Workers: workers,
}, nil
}

Expand Down Expand Up @@ -1639,3 +1659,16 @@ func (client *NginxClient) GetStreamConnectionsLimit() (*StreamLimitConnections,
}
return &limitConns, nil
}

// GetWorkers returns workers stats.
func (client *NginxClient) GetWorkers() ([]*Workers, error) {
var workers []*Workers
if client.version < 9 {
return workers, nil
}
err := client.get("workers", &workers)
if err != nil {
return nil, fmt.Errorf("failed to get workers: %w", err)
}
return workers, nil
}
4 changes: 4 additions & 0 deletions tests/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,10 @@ func TestStats(t *testing.T) {
t.Errorf("Bad connections: %v", stats.Connections)
}

if len(stats.Workers) < 1 {
t.Errorf("Bad workers: %v", stats.Workers)
}

if val, ok := stats.Caches[cacheZone]; ok {
if val.MaxSize != 104857600 { // 100MiB
t.Errorf("Cache max size stats missing: %v", val.Size)
Expand Down