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
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 < 8 {
return workers, nil
}
err := client.get("workers", &workers)
if err != nil {
return nil, fmt.Errorf("failed to get workers: %w", err)
}
return workers, nil
}
7 changes: 1 addition & 6 deletions docker/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@

user nginx;
worker_processes auto;
worker_processes 4;

error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
Expand All @@ -22,12 +20,9 @@ http {
access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

keyval_zone zone=zone_one:32k;
keyval $arg_text $text zone=zone_one;

Expand Down
6 changes: 1 addition & 5 deletions docker/nginx_no_stream.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

user nginx;
worker_processes auto;
worker_processes 4;

error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
Expand All @@ -10,7 +10,6 @@ events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
Expand All @@ -22,11 +21,8 @@ http {
access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/*.conf;
}
1 change: 0 additions & 1 deletion docker/test.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ server {
health_check interval=10 fails=3 passes=1;
}
status_zone test;

}

upstream test-drain {
Expand Down
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