mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 00:34:13 +01:00
AI: Enhance "GET /api/v1/metrics" endpoint with additional stats #213
Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
@@ -6,6 +6,11 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/photoprism/photoprism/internal/service/cluster"
|
||||
reg "github.com/photoprism/photoprism/internal/service/cluster/registry"
|
||||
"github.com/photoprism/photoprism/pkg/http/header"
|
||||
"github.com/photoprism/photoprism/pkg/rnd"
|
||||
)
|
||||
|
||||
func TestGetMetrics(t *testing.T) {
|
||||
@@ -21,13 +26,34 @@ func TestGetMetrics(t *testing.T) {
|
||||
}
|
||||
|
||||
body := resp.Body.String()
|
||||
floatPattern := `[-+]?\d+(?:\.\d+)?(?:e[-+]?\d+)?`
|
||||
stats := []string{
|
||||
"all",
|
||||
"photos",
|
||||
"media",
|
||||
"animated",
|
||||
"live",
|
||||
"videos",
|
||||
"audio",
|
||||
"documents",
|
||||
"albums",
|
||||
"private_albums",
|
||||
"folders",
|
||||
"private_folders",
|
||||
"files",
|
||||
"hidden",
|
||||
"favorites",
|
||||
"private",
|
||||
"people",
|
||||
"labels",
|
||||
"label_max_photos",
|
||||
"users",
|
||||
"guests",
|
||||
}
|
||||
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_statistics_media_count{stat="all"} \d+`), body)
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_statistics_media_count{stat="photos"} \d+`), body)
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_statistics_media_count{stat="videos"} \d+`), body)
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_statistics_media_count{stat="albums"} \d+`), body)
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_statistics_media_count{stat="folders"} \d+`), body)
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_statistics_media_count{stat="files"} \d+`), body)
|
||||
for _, stat := range stats {
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_statistics_media_count{stat="`+stat+`"} `+floatPattern), body)
|
||||
}
|
||||
})
|
||||
t.Run("ExposeBuildInformation", func(t *testing.T) {
|
||||
app, router, _ := NewApiTest()
|
||||
@@ -44,6 +70,72 @@ func TestGetMetrics(t *testing.T) {
|
||||
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_build_info{edition=".+",goversion=".+",version=".+"} 1`), body)
|
||||
})
|
||||
t.Run("ExposeUsageMetrics", func(t *testing.T) {
|
||||
app, router, _ := NewApiTest()
|
||||
|
||||
GetMetrics(router)
|
||||
|
||||
resp := PerformRequestWithStream(app, "GET", "/api/v1/metrics")
|
||||
|
||||
if resp.Code != http.StatusOK {
|
||||
t.Fatal(resp.Body.String())
|
||||
}
|
||||
|
||||
body := resp.Body.String()
|
||||
floatPattern := `[-+]?\d+(?:\.\d+)?(?:e[-+]?\d+)?`
|
||||
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_usage_files_bytes{state="used"} `+floatPattern), body)
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_usage_files_bytes{state="free"} `+floatPattern), body)
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_usage_files_bytes{state="total"} `+floatPattern), body)
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_usage_files_percent{state="used"} `+floatPattern), body)
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_usage_files_percent{state="free"} `+floatPattern), body)
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_usage_accounts_percent{state="used"} `+floatPattern), body)
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_usage_accounts_percent{state="free"} `+floatPattern), body)
|
||||
})
|
||||
t.Run("ExposeClusterMetricsForPortal", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
conf.Options().NodeRole = cluster.RolePortal
|
||||
|
||||
GetMetrics(router)
|
||||
|
||||
regy, err := reg.NewClientRegistryWithConfig(conf)
|
||||
assert.NoError(t, err)
|
||||
|
||||
nodeDefs := []struct {
|
||||
name string
|
||||
role string
|
||||
}{
|
||||
{"metrics-instance-1", string(cluster.RoleInstance)},
|
||||
{"metrics-service-1", string(cluster.RoleService)},
|
||||
}
|
||||
|
||||
var cleanupIDs []string
|
||||
for _, def := range nodeDefs {
|
||||
n := ®.Node{Node: cluster.Node{Name: def.name, Role: def.role, UUID: rnd.UUIDv7()}}
|
||||
assert.NoError(t, regy.Put(n))
|
||||
cleanupIDs = append(cleanupIDs, n.UUID)
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
for _, uuid := range cleanupIDs {
|
||||
_ = regy.Delete(uuid)
|
||||
}
|
||||
})
|
||||
|
||||
resp := PerformRequestWithStream(app, "GET", "/api/v1/metrics")
|
||||
|
||||
if resp.Code != http.StatusOK {
|
||||
t.Fatal(resp.Body.String())
|
||||
}
|
||||
|
||||
body := resp.Body.String()
|
||||
floatPattern := `[-+]?\d+(?:\.\d+)?(?:e[-+]?\d+)?`
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_cluster_nodes{role="total"} `+floatPattern), body)
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_cluster_nodes{role="instance"} `+floatPattern), body)
|
||||
assert.Regexp(t, regexp.MustCompile(`photoprism_cluster_nodes{role="service"} `+floatPattern), body)
|
||||
infoPattern := `photoprism_cluster_info\{(?:cidr="[^"]*",[^}]*uuid="[^"]*"|uuid="[^"]*",[^}]*cidr="[^"]*")\} 1`
|
||||
assert.Regexp(t, regexp.MustCompile(infoPattern), body)
|
||||
})
|
||||
t.Run("HasPrometheusExpositionFormatAsContentType", func(t *testing.T) {
|
||||
app, router, _ := NewApiTest()
|
||||
|
||||
@@ -53,8 +145,6 @@ func TestGetMetrics(t *testing.T) {
|
||||
if resp.Code != http.StatusOK {
|
||||
t.Fatal(resp.Body.String())
|
||||
}
|
||||
if contentType := resp.Result().Header.Get("Content-Type"); contentType != "" {
|
||||
t.Fatalf("unexpected response content-type: %s", contentType)
|
||||
}
|
||||
assert.Equal(t, header.ContentTypePrometheus, resp.Result().Header.Get("Content-Type"))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user