Skip to content
Open
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
61 changes: 61 additions & 0 deletions admin/api/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2026 CloudBlue LLC
// SPDX-License-Identifier: Apache-2.0

package api

import (
"log/slog"
"net/http"

"github.com/cloudblue/chaperone/admin/metrics"
"github.com/cloudblue/chaperone/admin/store"
)

// MetricsHandler serves computed metrics via the REST API.
type MetricsHandler struct {
store *store.Store
collector *metrics.Collector
}

// NewMetricsHandler creates a handler backed by the given store and collector.
func NewMetricsHandler(st *store.Store, c *metrics.Collector) *MetricsHandler {
return &MetricsHandler{store: st, collector: c}
}

// Register mounts metrics routes on the given mux.
func (h *MetricsHandler) Register(mux *http.ServeMux) {
mux.HandleFunc("GET /api/metrics/fleet", h.fleet)
mux.HandleFunc("GET /api/metrics/{id}", h.instance)
}

func (h *MetricsHandler) fleet(w http.ResponseWriter, r *http.Request) {
instances, err := h.store.ListInstances(r.Context())
if err != nil {
slog.Error("listing instances for fleet metrics", "error", err)
respondError(w, http.StatusInternalServerError, "INTERNAL_ERROR", "Failed to list instances")
return
}

ids := make([]int64, len(instances))
for i := range instances {
ids[i] = instances[i].ID
}

fm := h.collector.GetFleetMetrics(ids)
respondJSON(w, http.StatusOK, fm)
}

func (h *MetricsHandler) instance(w http.ResponseWriter, r *http.Request) {
id, ok := parseID(w, r)
if !ok {
return
}

im := h.collector.GetInstanceMetrics(id)
if im == nil {
respondError(w, http.StatusNotFound, "NO_METRICS", "No metric data available for this instance")
return
}

respondJSON(w, http.StatusOK, im)
}
177 changes: 177 additions & 0 deletions admin/api/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// Copyright 2026 CloudBlue LLC
// SPDX-License-Identifier: Apache-2.0

package api

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/cloudblue/chaperone/admin/metrics"
)

func makeSnapshot(t time.Time, totalReq, errReq, active, panics float64) metrics.Snapshot {
return metrics.Snapshot{
Time: t,
Vendors: map[string]*metrics.VendorSnapshot{
"acme": {
RequestsTotal: totalReq,
RequestsErrors: errReq,
Duration: metrics.Histogram{
Count: totalReq,
Buckets: []metrics.Bucket{
{UpperBound: 0.1, Count: totalReq * 0.5},
{UpperBound: 0.5, Count: totalReq * 0.9},
{UpperBound: 1.0, Count: totalReq},
},
},
},
},
ActiveConnections: active,
PanicsTotal: panics,
}
}

func TestMetricsHandler_Fleet_ReturnsAggregated(t *testing.T) {
t.Parallel()
st := openTestStore(t)
c := metrics.NewCollector(10)

ctx := context.Background()
inst, err := st.CreateInstance(ctx, "proxy-1", "10.0.0.1:9090")
if err != nil {
t.Fatalf("CreateInstance() error = %v", err)
}

t0 := time.Date(2026, 3, 7, 12, 0, 0, 0, time.UTC)
c.Record(inst.ID, makeSnapshot(t0, 1000, 50, 10, 1))
c.Record(inst.ID, makeSnapshot(t0.Add(10*time.Second), 1100, 55, 12, 2))

h := NewMetricsHandler(st, c)
mux := http.NewServeMux()
h.Register(mux)

req := httptest.NewRequest(http.MethodGet, "/api/metrics/fleet", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)

if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
}

var fm metrics.FleetMetrics
if err := json.NewDecoder(rec.Body).Decode(&fm); err != nil {
t.Fatalf("decode error: %v", err)
}
if fm.TotalRPS <= 0 {
t.Errorf("TotalRPS = %v, want > 0", fm.TotalRPS)
}
if len(fm.Instances) != 1 {
t.Errorf("Instances = %d, want 1", len(fm.Instances))
}
}

func TestMetricsHandler_Instance_ReturnsMetrics(t *testing.T) {
t.Parallel()
st := openTestStore(t)
c := metrics.NewCollector(10)

ctx := context.Background()
inst, err := st.CreateInstance(ctx, "proxy-1", "10.0.0.1:9090")
if err != nil {
t.Fatalf("CreateInstance() error = %v", err)
}

t0 := time.Date(2026, 3, 7, 12, 0, 0, 0, time.UTC)
c.Record(inst.ID, makeSnapshot(t0, 1000, 50, 10, 1))
c.Record(inst.ID, makeSnapshot(t0.Add(10*time.Second), 1100, 55, 12, 2))

h := NewMetricsHandler(st, c)
mux := http.NewServeMux()
h.Register(mux)

req := httptest.NewRequest(http.MethodGet, "/api/metrics/1", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)

if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
}

var im metrics.InstanceMetrics
if err := json.NewDecoder(rec.Body).Decode(&im); err != nil {
t.Fatalf("decode error: %v", err)
}
if im.RPS <= 0 {
t.Errorf("RPS = %v, want > 0", im.RPS)
}
if im.DataPoints != 2 {
t.Errorf("DataPoints = %d, want 2", im.DataPoints)
}
}

func TestMetricsHandler_Instance_NoData_Returns404(t *testing.T) {
t.Parallel()
st := openTestStore(t)
c := metrics.NewCollector(10)

h := NewMetricsHandler(st, c)
mux := http.NewServeMux()
h.Register(mux)

req := httptest.NewRequest(http.MethodGet, "/api/metrics/99", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)

if rec.Code != http.StatusNotFound {
t.Errorf("status = %d, want %d", rec.Code, http.StatusNotFound)
}
}

func TestMetricsHandler_Instance_InvalidID_Returns400(t *testing.T) {
t.Parallel()
st := openTestStore(t)
c := metrics.NewCollector(10)

h := NewMetricsHandler(st, c)
mux := http.NewServeMux()
h.Register(mux)

req := httptest.NewRequest(http.MethodGet, "/api/metrics/abc", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)

if rec.Code != http.StatusBadRequest {
t.Errorf("status = %d, want %d", rec.Code, http.StatusBadRequest)
}
}

func TestMetricsHandler_Fleet_EmptyFleet_ReturnsEmptyInstances(t *testing.T) {
t.Parallel()
st := openTestStore(t)
c := metrics.NewCollector(10)

h := NewMetricsHandler(st, c)
mux := http.NewServeMux()
h.Register(mux)

req := httptest.NewRequest(http.MethodGet, "/api/metrics/fleet", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)

if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
}

var fm metrics.FleetMetrics
if err := json.NewDecoder(rec.Body).Decode(&fm); err != nil {
t.Fatalf("decode error: %v", err)
}
if len(fm.Instances) != 0 {
t.Errorf("Instances = %d, want 0", len(fm.Instances))
}
}
15 changes: 11 additions & 4 deletions admin/cmd/chaperone-admin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/cloudblue/chaperone/admin"
"github.com/cloudblue/chaperone/admin/config"
"github.com/cloudblue/chaperone/admin/metrics"
"github.com/cloudblue/chaperone/admin/poller"
"github.com/cloudblue/chaperone/admin/store"
)
Expand Down Expand Up @@ -59,24 +60,30 @@ func run() error {
}
defer st.Close()

srv, err := admin.NewServer(cfg, st)
collector := metrics.NewCollector(metrics.DefaultCapacity)

srv, err := admin.NewServer(cfg, st, collector)
if err != nil {
return fmt.Errorf("creating server: %w", err)
}

// Start the background health poller.
// Start the background health + metrics poller.
pollerCtx, pollerCancel := context.WithCancel(context.Background())
defer pollerCancel()

p := poller.New(st, cfg.Scraper.Interval.Unwrap(), cfg.Scraper.Timeout.Unwrap())
p := poller.New(st, collector, cfg.Scraper.Interval.Unwrap(), cfg.Scraper.Timeout.Unwrap())
go p.Run(pollerCtx)

return serve(cfg.Server.Addr, srv)
}

func serve(addr string, srv *admin.Server) error {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

errCh := make(chan error, 1)
go func() {
slog.Info("listening", "addr", cfg.Server.Addr)
slog.Info("listening", "addr", addr)
errCh <- srv.ListenAndServe()
}()

Expand Down
8 changes: 7 additions & 1 deletion admin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ module github.com/cloudblue/chaperone/admin
go 1.26.1

require (
github.com/prometheus/client_model v0.6.2
github.com/prometheus/common v0.67.5
gopkg.in/yaml.v3 v3.0.1
modernc.org/sqlite v1.46.1
)

require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/ncruces/go-strftime v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 // indirect
golang.org/x/sys v0.37.0 // indirect
golang.org/x/sys v0.39.0 // indirect
google.golang.org/protobuf v1.36.11 // indirect
modernc.org/libc v1.67.6 // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.11.0 // indirect
Expand Down
34 changes: 31 additions & 3 deletions admin/go.sum
Original file line number Diff line number Diff line change
@@ -1,30 +1,58 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
github.com/prometheus/common v0.67.5 h1:pIgK94WWlQt1WLwAC5j2ynLaBRDiinoAb86HZHTUGI4=
github.com/prometheus/common v0.67.5/go.mod h1:SjE/0MzDEEAyrdr5Gqc6G+sXI67maCxzaT3A2+HqjUw=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
go.yaml.in/yaml/v2 v2.4.3 h1:6gvOSjQoTB3vt1l+CU+tSyi/HOjfOjRLJ4YwYZGwRO0=
go.yaml.in/yaml/v2 v2.4.3/go.mod h1:zSxWcmIDjOzPXpjlTTbAsKokqkDNAVtZO0WOMiT90s8=
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 h1:mgKeJMpvi0yx/sU5GsxQ7p6s2wtOnGAHZWCHUM4KGzY=
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546/go.mod h1:j/pmGrbnkbPtQfxEe5D0VQhZC6qKbfKifgD0oM7sR70=
golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA=
golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w=
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ=
golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
modernc.org/cc/v4 v4.27.1 h1:9W30zRlYrefrDV2JE2O8VDtJ1yPGownxciz5rrbQZis=
Expand Down
Loading
Loading