Просмотр исходного кода

feat(prometheus): make prometheus support optional

Prometheus support doesn't make much sense for small-scale deployments.
Removing it from builds reduces the binary size by around 15%, significantly
lower the amount of dependencies, and reduces the attacks surface.
jvoisin 1 день назад
Родитель
Сommit
608e765dda

+ 7 - 0
CONTRIBUTING.md

@@ -100,6 +100,13 @@ You can also use an existing PostgreSQL instance. Make sure to set the `DATABASE
 - **Specific platforms:** `make linux-amd64`, `make darwin-arm64`, etc.
 - **Docker image:** `make docker-image`
 
+Prometheus support is included by default. To exclude it and its dependencies
+from the binary, build with `go build -tags noprometheus` or
+`GOFLAGS="-tags=noprometheus" make miniflux`. In these builds, the metrics endpoint
+and collector are unavailable, and setting `METRICS_COLLECTOR=1` causes a
+configuration error. Both variants can be tested with `go test ./...` and
+`go test -tags noprometheus ./...`.
+
 ### Cross-Platform Support
 
 Miniflux supports multiple architectures. When making changes, ensure compatibility across:

+ 8 - 0
internal/config/metrics.go

@@ -0,0 +1,8 @@
+// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+//go:build !noprometheus
+
+package config // import "miniflux.app/v2/internal/config"
+
+const metricsSupported = true

+ 8 - 0
internal/config/metrics_disabled.go

@@ -0,0 +1,8 @@
+// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+//go:build noprometheus
+
+package config // import "miniflux.app/v2/internal/config"
+
+const metricsSupported = false

+ 1 - 1
internal/config/options.go

@@ -789,7 +789,7 @@ func (c *configOptions) HasMaintenanceMode() bool {
 }
 
 func (c *configOptions) HasMetricsCollector() bool {
-	return c.options["METRICS_COLLECTOR"].parsedBoolValue
+	return metricsSupported && c.MetricsCollector()
 }
 
 func (c *configOptions) HasSchedulerService() bool {

+ 14 - 2
internal/config/options_parsing_test.go

@@ -871,8 +871,17 @@ func TestMetricsCollectorOptionParsing(t *testing.T) {
 		t.Fatal("Expected METRICS_COLLECTOR to be enabled")
 	}
 
-	if !configParser.options.HasMetricsCollector() {
-		t.Fatal("Expected HAS_METRICS_COLLECTOR to be enabled")
+	if configParser.options.HasMetricsCollector() != metricsSupported {
+		t.Fatal("Expected HAS_METRICS_COLLECTOR to reflect build-time Prometheus support")
+	}
+
+	err := configParser.options.Validate()
+	if metricsSupported {
+		if err != nil {
+			t.Fatalf("Unexpected validation error: %v", err)
+		}
+	} else if err == nil {
+		t.Fatal("Expected validation error when enabling metrics without Prometheus support")
 	}
 
 	if err := configParser.parseLines([]string{"METRICS_COLLECTOR=0"}); err != nil {
@@ -886,6 +895,9 @@ func TestMetricsCollectorOptionParsing(t *testing.T) {
 	if configParser.options.HasMetricsCollector() {
 		t.Fatal("Expected HAS_METRICS_COLLECTOR to be disabled")
 	}
+	if err := configParser.options.Validate(); err != nil {
+		t.Fatalf("Unexpected validation error with metrics disabled: %v", err)
+	}
 }
 
 func TestMetricsPasswordOptionParsing(t *testing.T) {

+ 4 - 0
internal/config/parser.go

@@ -78,6 +78,10 @@ func (c *configOptions) Validate() error {
 		return errors.New("METRICS_USERNAME and METRICS_PASSWORD must both be provided")
 	}
 
+	if c.MetricsCollector() && !metricsSupported {
+		return errors.New("METRICS_COLLECTOR cannot be enabled: Prometheus support was disabled at build time with noprometheus")
+	}
+
 	if c.DatabaseMinConns() > c.DatabaseMaxConns() {
 		return errors.New("DATABASE_MIN_CONNS must be less than or equal to DATABASE_MAX_CONNS")
 	}

+ 2 - 3
internal/http/server/metrics.go

@@ -10,12 +10,11 @@ import (
 	"miniflux.app/v2/internal/config"
 	"miniflux.app/v2/internal/crypto"
 	"miniflux.app/v2/internal/http/request"
-
-	"github.com/prometheus/client_golang/prometheus/promhttp"
+	"miniflux.app/v2/internal/metric"
 )
 
 func metricsHandler() http.Handler {
-	handler := promhttp.Handler()
+	handler := metric.Handler()
 	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 		if !isAllowedToAccessMetricsEndpoint(r) {
 			slog.Warn("Authentication failed while accessing the metrics endpoint",

+ 9 - 0
internal/metric/metric.go

@@ -1,18 +1,27 @@
 // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
 // SPDX-License-Identifier: Apache-2.0
 
+//go:build !noprometheus
+
 package metric // import "miniflux.app/v2/internal/metric"
 
 import (
 	"context"
 	"log/slog"
+	"net/http"
 	"time"
 
 	"miniflux.app/v2/internal/storage"
 
 	"github.com/prometheus/client_golang/prometheus"
+	"github.com/prometheus/client_golang/prometheus/promhttp"
 )
 
+// Handler returns the Prometheus metrics HTTP handler.
+func Handler() http.Handler {
+	return promhttp.Handler()
+}
+
 // Status label values for histogram metrics.
 const (
 	StatusSuccess = "success"

+ 39 - 0
internal/metric/metric_disabled.go

@@ -0,0 +1,39 @@
+// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+//go:build noprometheus
+
+package metric // import "miniflux.app/v2/internal/metric"
+
+import (
+	"context"
+	"net/http"
+	"time"
+
+	"miniflux.app/v2/internal/storage"
+)
+
+const (
+	StatusSuccess = "success"
+	StatusError   = "error"
+)
+
+var (
+	BackgroundFeedRefreshDuration histogram
+	ScraperRequestDuration        histogram
+	ArchiveEntriesDuration        histogram
+)
+
+type histogram struct{}
+
+func (h histogram) WithLabelValues(...string) histogram { return h }
+func (histogram) Observe(float64)                       {}
+
+type collector struct{}
+
+func NewCollector(*storage.Storage, time.Duration) *collector { return &collector{} }
+func (*collector) GatherStorageMetrics(context.Context)       {}
+
+func Handler() http.Handler {
+	return http.NotFoundHandler()
+}