Skip to content

Commit da5dbf7

Browse files
authored
Add cache stats (#69)
1 parent f4e8c62 commit da5dbf7

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

client/nginx.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func (internalError *internalError) Wrap(err string) *internalError {
115115
// https://nginx.org/en/docs/http/ngx_http_api_module.html
116116
type Stats struct {
117117
NginxInfo NginxInfo
118+
Caches Caches
118119
Processes Processes
119120
Connections Connections
120121
Slabs Slabs
@@ -141,6 +142,36 @@ type NginxInfo struct {
141142
ParentProcessID uint64 `json:"ppid"`
142143
}
143144

145+
// Caches is a map of cache stats by cache zone
146+
type Caches = map[string]HTTPCache
147+
148+
// HTTPCache represents a zone's HTTP Cache
149+
type HTTPCache struct {
150+
Size uint64
151+
MaxSize uint64 `json:"max_size"`
152+
Cold bool
153+
Hit CacheStats
154+
Stale CacheStats
155+
Updating CacheStats
156+
Revalidated CacheStats
157+
Miss CacheStats
158+
Expired ExtendedCacheStats
159+
Bypass ExtendedCacheStats
160+
}
161+
162+
// CacheStats are basic cache stats.
163+
type CacheStats struct {
164+
Responses uint64
165+
Bytes uint64
166+
}
167+
168+
// ExtendedCacheStats are extended cache stats.
169+
type ExtendedCacheStats struct {
170+
CacheStats
171+
ResponsesWritten uint64 `json:"responses_written"`
172+
BytesWritten uint64 `json:"bytes_written"`
173+
}
174+
144175
// Connections represents connection related stats.
145176
type Connections struct {
146177
Accepted uint64
@@ -965,6 +996,11 @@ func (client *NginxClient) GetStats() (*Stats, error) {
965996
return nil, fmt.Errorf("failed to get stats: %v", err)
966997
}
967998

999+
caches, err := client.GetCaches()
1000+
if err != nil {
1001+
return nil, fmt.Errorf("failed to get stats: %v", err)
1002+
}
1003+
9681004
processes, err := client.GetProcesses()
9691005
if err != nil {
9701006
return nil, fmt.Errorf("failed to get stats: %v", err)
@@ -1027,6 +1063,7 @@ func (client *NginxClient) GetStats() (*Stats, error) {
10271063

10281064
return &Stats{
10291065
NginxInfo: *info,
1066+
Caches: *caches,
10301067
Processes: *processes,
10311068
Slabs: *slabs,
10321069
Connections: *cons,
@@ -1052,6 +1089,16 @@ func (client *NginxClient) GetNginxInfo() (*NginxInfo, error) {
10521089
return &info, nil
10531090
}
10541091

1092+
// GetCaches returns Cache stats
1093+
func (client *NginxClient) GetCaches() (*Caches, error) {
1094+
var caches Caches
1095+
err := client.get("http/caches", &caches)
1096+
if err != nil {
1097+
return nil, fmt.Errorf("failed to get caches: %v", err)
1098+
}
1099+
return &caches, nil
1100+
}
1101+
10551102
// GetSlabs returns Slabs stats.
10561103
func (client *NginxClient) GetSlabs() (*Slabs, error) {
10571104
var slabs Slabs

docker/test.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ upstream test {
22
zone test 64k;
33
}
44

5+
proxy_cache_path /var/cache/nginx keys_zone=http_cache:10m max_size=100m;
6+
57
server {
68
listen 8080;
79

@@ -16,6 +18,7 @@ server {
1618

1719
location /test {
1820
proxy_pass http://test;
21+
proxy_cache http_cache;
1922
health_check interval=10 fails=3 passes=1;
2023
}
2124
status_zone test;

tests/client_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
)
1313

1414
const (
15+
cacheZone = "http_cache"
1516
upstream = "test"
1617
streamUpstream = "stream_test"
1718
streamZoneSync = "zone_test_sync"
@@ -627,6 +628,14 @@ func TestStats(t *testing.T) {
627628
t.Errorf("Bad connections: %v", stats.Connections)
628629
}
629630

631+
if val, ok := stats.Caches[cacheZone]; ok {
632+
if val.MaxSize != 104857600 { // 100MiB
633+
t.Errorf("Cache max size stats missing: %v", val.Size)
634+
}
635+
} else {
636+
t.Errorf("Cache stats for cache zone '%v' not found", cacheZone)
637+
}
638+
630639
if val, ok := stats.Slabs[upstream]; ok {
631640
if val.Pages.Used < 1 {
632641
t.Errorf("Slabs pages stats missing: %v", val.Pages)

0 commit comments

Comments
 (0)