Skip to content

Add cache stats #69

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
Jun 7, 2021
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
47 changes: 47 additions & 0 deletions client/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1027,6 +1063,7 @@ func (client *NginxClient) GetStats() (*Stats, error) {

return &Stats{
NginxInfo: *info,
Caches: *caches,
Processes: *processes,
Slabs: *slabs,
Connections: *cons,
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docker/test.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions tests/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

const (
cacheZone = "http_cache"
upstream = "test"
streamUpstream = "stream_test"
streamZoneSync = "zone_test_sync"
Expand Down Expand Up @@ -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)
Expand Down