Skip to content

Add Feature: Stats contain /nginx status info #12

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 1 commit into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions client/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (internalError *internalError) Wrap(err string) *internalError {
// Stats represents NGINX Plus stats fetched from the NGINX Plus API.
// https://nginx.org/en/docs/http/ngx_http_api_module.html
type Stats struct {
NginxInfo NginxInfo
Connections Connections
HTTPRequests HTTPRequests
SSL SSL
Expand All @@ -88,6 +89,18 @@ type Stats struct {
StreamUpstreams StreamUpstreams
}

// NginxInfo contains general information about NGINX Plus.
type NginxInfo struct {
Version string
Build string
Address string
Generation uint64
LoadTimestamp string `json:"load_timestamp"`
Timestamp string
ProcessID uint64 `json:"pid"`
ParentProcessID uint64 `json:"ppid"`
}

// Connections represents connection related stats.
type Connections struct {
Accepted uint64
Expand Down Expand Up @@ -658,6 +671,11 @@ func determineStreamUpdates(updatedServers []StreamUpstreamServer, nginxServers

// GetStats gets connection, request, ssl, zone, stream zone, upstream and stream upstream related stats from the NGINX Plus API.
func (client *NginxClient) GetStats() (*Stats, error) {
info, err := client.getNginxInfo()
if err != nil {
return nil, fmt.Errorf("failed to get stats %v", err)
}

cons, err := client.getConnections()
if err != nil {
return nil, fmt.Errorf("failed to get stats: %v", err)
Expand Down Expand Up @@ -694,6 +712,7 @@ func (client *NginxClient) GetStats() (*Stats, error) {
}

return &Stats{
NginxInfo: *info,
Connections: *cons,
HTTPRequests: *requests,
SSL: *ssl,
Expand All @@ -704,6 +723,15 @@ func (client *NginxClient) GetStats() (*Stats, error) {
}, nil
}

func (client *NginxClient) getNginxInfo() (*NginxInfo, error) {
var info NginxInfo
err := client.get("nginx", &info)
if err != nil {
return nil, fmt.Errorf("failed to get info: %v", err)
}
return &info, nil
}

func (client *NginxClient) getConnections() (*Connections, error) {
var cons Connections
err := client.get("connections", &cons)
Expand Down
6 changes: 1 addition & 5 deletions docker/test.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ server {
}

location /api {
limit_except GET {
allow 172.0.0.0/8;
deny all;
}
api write=on;
}

Expand All @@ -22,4 +18,4 @@ server {
health_check interval=10 fails=3 passes=1;
}
status_zone test;
}
}
26 changes: 26 additions & 0 deletions tests/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,32 @@ func TestStats(t *testing.T) {
t.Errorf("Error getting stats: %v", err)
}

// NginxInfo
if stats.NginxInfo.Version == "" {
t.Error("Missing version string")
}
if stats.NginxInfo.Build == "" {
t.Error("Missing build string")
}
if stats.NginxInfo.Address == "" {
t.Errorf("Missing server address")
}
if stats.NginxInfo.Generation < 1 {
t.Errorf("Bad config generation: %v", stats.NginxInfo.Generation)
}
if stats.NginxInfo.LoadTimestamp == "" {
t.Error("Missing load timestamp")
}
if stats.NginxInfo.Timestamp == "" {
t.Error("Missing timestamp")
}
if stats.NginxInfo.ProcessID < 1 {
t.Errorf("Bad process id: %v", stats.NginxInfo.ProcessID)
}
if stats.NginxInfo.ParentProcessID < 1 {
t.Errorf("Bad parent process id: %v", stats.NginxInfo.ParentProcessID)
}

if stats.Connections.Accepted < 1 {
t.Errorf("Bad connections: %v", stats.Connections)
}
Expand Down