Backend: Set content-type on metrics endpoints (#5042)

The prometheus text format requires metrics endpoints respond with the
content-type 'text/plain; version=0.0.4'. Without this, newer versions
of prometheus fail to scrape the metrics endpoint and report an error.
It's possible to work around this by setting the
'fallback_scrape_protocol' setting in the prometheus scrape target
configuration, but this revision sets the content type appropriately to
avoid this in the first place.
This commit is contained in:
Brandon Richardson
2025-06-23 10:32:01 -03:00
committed by GitHub
parent 758d86f903
commit 1d8fa4e3ea
3 changed files with 29 additions and 10 deletions

View File

@@ -14,6 +14,7 @@ import (
"github.com/photoprism/photoprism/internal/auth/acl" "github.com/photoprism/photoprism/internal/auth/acl"
"github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/photoprism/get" "github.com/photoprism/photoprism/internal/photoprism/get"
"github.com/photoprism/photoprism/pkg/media/http/header"
) )
// GetMetrics provides a prometheus-compatible metrics endpoint for monitoring. // GetMetrics provides a prometheus-compatible metrics endpoint for monitoring.
@@ -31,6 +32,8 @@ func GetMetrics(router *gin.RouterGroup) {
conf := get.Config() conf := get.Config()
counts := conf.ClientUser(false).Count counts := conf.ClientUser(false).Count
header.SetContentType(c.Request, header.ContentTypePrometheus)
c.Stream(func(w io.Writer) bool { c.Stream(func(w io.Writer) bool {
reg := prometheus.NewRegistry() reg := prometheus.NewRegistry()
reg.MustRegister(collectors.NewGoCollector()) reg.MustRegister(collectors.NewGoCollector())

View File

@@ -29,6 +29,7 @@ func TestGetMetrics(t *testing.T) {
assert.Regexp(t, regexp.MustCompile(`photoprism_statistics_media_count{stat="folders"} \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) assert.Regexp(t, regexp.MustCompile(`photoprism_statistics_media_count{stat="files"} \d+`), body)
}) })
t.Run("expose build information", func(t *testing.T) { t.Run("expose build information", func(t *testing.T) {
app, router, _ := NewApiTest() app, router, _ := NewApiTest()
@@ -44,4 +45,18 @@ func TestGetMetrics(t *testing.T) {
assert.Regexp(t, regexp.MustCompile(`photoprism_build_info{edition=".+",goversion=".+",version=".+"} 1`), body) assert.Regexp(t, regexp.MustCompile(`photoprism_build_info{edition=".+",goversion=".+",version=".+"} 1`), body)
}) })
t.Run("has prometheus exposition format as content type", 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())
}
if contentType := resp.Result().Header.Get("Content-Type"); contentType != "" {
t.Fatalf("unexpected response content-type: %s", contentType)
}
})
} }

View File

@@ -96,16 +96,17 @@ const (
// Standard ContentType strings for markup and sidecar files. // Standard ContentType strings for markup and sidecar files.
const ( const (
ContentTypeBinary = "application/octet-stream" ContentTypeBinary = "application/octet-stream"
ContentTypeForm = "application/x-www-form-urlencoded" ContentTypeForm = "application/x-www-form-urlencoded"
ContentTypeMultipart = "multipart/form-data" ContentTypeMultipart = "multipart/form-data"
ContentTypeJson = "application/json" ContentTypeJson = "application/json"
ContentTypeJsonUtf8 = "application/json; charset=utf-8" ContentTypeJsonUtf8 = "application/json; charset=utf-8"
ContentTypeXml = "text/xml" ContentTypeXml = "text/xml"
ContentTypeHtml = "text/html; charset=utf-8" ContentTypeHtml = "text/html; charset=utf-8"
ContentTypeText = "text/plain; charset=utf-8" ContentTypeText = "text/plain; charset=utf-8"
ContentTypePDF = "application/pdf" ContentTypePDF = "application/pdf"
ContentTypeZip = "application/zip" ContentTypeZip = "application/zip"
ContentTypePrometheus = "text/plain; version=0.0.4"
) )
// HasContentType checks weather the Content-Type header has the specified type. // HasContentType checks weather the Content-Type header has the specified type.