Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
85 changes: 66 additions & 19 deletions client/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"reflect"
"strings"
"time"

"golang.org/x/exp/slices"
)

const (
Expand Down Expand Up @@ -1149,6 +1151,11 @@ func determineStreamUpdates(updatedServers []StreamUpstreamServer, nginxServers

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

info, err := client.GetNginxInfo()
if err != nil {
return nil, fmt.Errorf("failed to get stats: %w", err)
Expand Down Expand Up @@ -1194,21 +1201,6 @@ func (client *NginxClient) GetStats() (*Stats, error) {
return nil, fmt.Errorf("failed to get stats: %w", err)
}

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

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

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

locationZones, err := client.GetLocationZones()
if err != nil {
return nil, fmt.Errorf("failed to get stats: %w", err)
Expand All @@ -1229,14 +1221,49 @@ func (client *NginxClient) GetStats() (*Stats, error) {
return nil, fmt.Errorf("failed to get stats: %w", err)
}

limitConnsStream, err := client.GetStreamConnectionsLimit()
workers, err := client.GetWorkers()
if err != nil {
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)
streamZones := &StreamServerZones{}
streamUpstreams := &StreamUpstreams{}
limitConnsStream := &StreamLimitConnections{}
streamZoneSync := &StreamZoneSync{}

if slices.Contains(endpoints, "stream") {
streamEndpoints, err := client.GetAvailableStreamEndpoint()
if err != nil {
return nil, fmt.Errorf("failed to get stats: %w", err)
}

if slices.Contains(streamEndpoints, "server_zones") {
streamZones, err = client.GetStreamServerZones()
if err != nil {
return nil, fmt.Errorf("failed to get stats: %w", err)
}
}

if slices.Contains(streamEndpoints, "upstreams") {
streamUpstreams, err = client.GetStreamUpstreams()
if err != nil {
return nil, fmt.Errorf("failed to get stats: %w", err)
}
}

if slices.Contains(streamEndpoints, "limit_conns") {
limitConnsStream, err = client.GetStreamConnectionsLimit()
if err != nil {
return nil, fmt.Errorf("failed to get stats: %w", err)
}
}

if slices.Contains(streamEndpoints, "zone_sync") {
streamZoneSync, err = client.GetStreamZoneSync()
if err != nil {
return nil, fmt.Errorf("failed to get stats: %w", err)
}
}
}

return &Stats{
Expand All @@ -1261,6 +1288,26 @@ func (client *NginxClient) GetStats() (*Stats, error) {
}, nil
}

// GetAvailableEndpoint returns available endpoints in the API.
func (client *NginxClient) GetAvailableEndpoint() ([]string, error) {
var endpoints []string
err := client.get("", &endpoints)
if err != nil {
return nil, fmt.Errorf("failed to get endpoints: %w", err)
}
return endpoints, nil
}

// GetAvailableStreamEndpoint returns available stream endpoints in the API.
func (client *NginxClient) GetAvailableStreamEndpoint() ([]string, error) {
var endpoints []string
err := client.get("stream", &endpoints)
if err != nil {
return nil, fmt.Errorf("failed to get endpoints: %w", err)
}
return endpoints, nil
}

// GetNginxInfo returns Nginx stats.
func (client *NginxClient) GetNginxInfo() (*NginxInfo, error) {
var info NginxInfo
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/nginxinc/nginx-plus-go-client

go 1.19

require golang.org/x/exp v0.0.0-20230905200255-921286631fa9
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=