Skip to content

Commit f4e29f1

Browse files
committed
feat: make metrics endpoint opt-in
1 parent ec5226e commit f4e29f1

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Config struct {
2828
WakeOnLanDevices []WakeOnLanDevice `json:"wake_on_lan_devices"`
2929
EdidString string `json:"hdmi_edid_string"`
3030
ActiveExtension string `json:"active_extension"`
31+
MetricsEnabled bool `json:"enable_metrics"`
3132
DisplayMaxBrightness int `json:"display_max_brightness"`
3233
DisplayDimAfterSec int `json:"display_dim_after_sec"`
3334
DisplayOffAfterSec int `json:"display_off_after_sec"`

prometheus.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package kvm
33
import (
44
"net/http"
55

6+
"github.com/gin-gonic/gin"
67
"github.com/prometheus/client_golang/prometheus"
78
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
9+
"github.com/prometheus/client_golang/prometheus/promhttp"
810
"github.com/prometheus/common/version"
911
)
1012

@@ -14,4 +16,17 @@ func initPrometheus() {
1416
// A Prometheus metrics endpoint.
1517
version.Version = builtAppVersion
1618
prometheus.MustRegister(versioncollector.NewCollector("jetkvm"))
19+
20+
promHandler = promhttp.Handler()
21+
}
22+
23+
func prometheusCheckAuthMiddleware() gin.HandlerFunc {
24+
return func(c *gin.Context) {
25+
if !config.MetricsEnabled {
26+
c.JSON(http.StatusNotFound, gin.H{"error": "Metrics endpoint is disabled"})
27+
return
28+
}
29+
30+
c.Next()
31+
}
1732
}

web.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/gin-gonic/gin"
1414
"github.com/google/uuid"
15-
"github.com/prometheus/client_golang/prometheus/promhttp"
1615
"golang.org/x/crypto/bcrypt"
1716
)
1817

@@ -87,7 +86,7 @@ func setupRouter() *gin.Engine {
8786
r.POST("/device/setup", handleSetup)
8887

8988
// A Prometheus metrics endpoint.
90-
r.GET("/metrics", gin.WrapH(promhttp.Handler()))
89+
r.GET("/metrics", prometheusCheckAuthMiddleware(), gin.WrapH(promHandler))
9190

9291
// Protected routes (allows both password and noPassword modes)
9392
protected := r.Group("/")

0 commit comments

Comments
 (0)