From d1309d47d39d4b7c1a07863f5b5e0303e54c0cea Mon Sep 17 00:00:00 2001 From: jacotter Date: Thu, 3 Jun 2021 17:50:25 +0100 Subject: [PATCH] Add cache stats --- client/nginx.go | 47 ++++++++++++++++++++++++++++++++++++++++++++ docker/test.conf | 3 +++ tests/client_test.go | 9 +++++++++ 3 files changed, 59 insertions(+) diff --git a/client/nginx.go b/client/nginx.go index 9b4221bd..fb737e90 100644 --- a/client/nginx.go +++ b/client/nginx.go @@ -115,6 +115,7 @@ func (internalError *internalError) Wrap(err string) *internalError { // https://nginx.org/en/docs/http/ngx_http_api_module.html type Stats struct { NginxInfo NginxInfo + Caches Caches Processes Processes Connections Connections Slabs Slabs @@ -141,6 +142,36 @@ type NginxInfo struct { ParentProcessID uint64 `json:"ppid"` } +// Caches is a map of cache stats by cache zone +type Caches = map[string]HTTPCache + +// HTTPCache represents a zone's HTTP Cache +type HTTPCache struct { + Size uint64 + MaxSize uint64 `json:"max_size"` + Cold bool + Hit CacheStats + Stale CacheStats + Updating CacheStats + Revalidated CacheStats + Miss CacheStats + Expired ExtendedCacheStats + Bypass ExtendedCacheStats +} + +// CacheStats are basic cache stats. +type CacheStats struct { + Responses uint64 + Bytes uint64 +} + +// ExtendedCacheStats are extended cache stats. +type ExtendedCacheStats struct { + CacheStats + ResponsesWritten uint64 `json:"responses_written"` + BytesWritten uint64 `json:"bytes_written"` +} + // Connections represents connection related stats. type Connections struct { Accepted uint64 @@ -965,6 +996,11 @@ func (client *NginxClient) GetStats() (*Stats, error) { return nil, fmt.Errorf("failed to get stats: %v", err) } + caches, err := client.GetCaches() + if err != nil { + return nil, fmt.Errorf("failed to get stats: %v", err) + } + processes, err := client.GetProcesses() if err != nil { return nil, fmt.Errorf("failed to get stats: %v", err) @@ -1027,6 +1063,7 @@ func (client *NginxClient) GetStats() (*Stats, error) { return &Stats{ NginxInfo: *info, + Caches: *caches, Processes: *processes, Slabs: *slabs, Connections: *cons, @@ -1052,6 +1089,16 @@ func (client *NginxClient) GetNginxInfo() (*NginxInfo, error) { return &info, nil } +// GetCaches returns Cache stats +func (client *NginxClient) GetCaches() (*Caches, error) { + var caches Caches + err := client.get("http/caches", &caches) + if err != nil { + return nil, fmt.Errorf("failed to get caches: %v", err) + } + return &caches, nil +} + // GetSlabs returns Slabs stats. func (client *NginxClient) GetSlabs() (*Slabs, error) { var slabs Slabs diff --git a/docker/test.conf b/docker/test.conf index 4d985053..ee4def0e 100644 --- a/docker/test.conf +++ b/docker/test.conf @@ -2,6 +2,8 @@ upstream test { zone test 64k; } +proxy_cache_path /var/cache/nginx keys_zone=http_cache:10m max_size=100m; + server { listen 8080; @@ -16,6 +18,7 @@ server { location /test { proxy_pass http://test; + proxy_cache http_cache; health_check interval=10 fails=3 passes=1; } status_zone test; diff --git a/tests/client_test.go b/tests/client_test.go index 25c60a45..59775a85 100644 --- a/tests/client_test.go +++ b/tests/client_test.go @@ -12,6 +12,7 @@ import ( ) const ( + cacheZone = "http_cache" upstream = "test" streamUpstream = "stream_test" streamZoneSync = "zone_test_sync" @@ -627,6 +628,14 @@ func TestStats(t *testing.T) { t.Errorf("Bad connections: %v", stats.Connections) } + if val, ok := stats.Caches[cacheZone]; ok { + if val.MaxSize != 104857600 { // 100MiB + t.Errorf("Cache max size stats missing: %v", val.Size) + } + } else { + t.Errorf("Cache stats for cache zone '%v' not found", cacheZone) + } + if val, ok := stats.Slabs[upstream]; ok { if val.Pages.Used < 1 { t.Errorf("Slabs pages stats missing: %v", val.Pages)