Skip to content
Draft
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
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Config struct {
WakeOnLanDevices []WakeOnLanDevice `json:"wake_on_lan_devices"`
EdidString string `json:"hdmi_edid_string"`
ActiveExtension string `json:"active_extension"`
MetricsEnabled bool `json:"enable_metrics"`
DisplayMaxBrightness int `json:"display_max_brightness"`
DisplayDimAfterSec int `json:"display_dim_after_sec"`
DisplayOffAfterSec int `json:"display_off_after_sec"`
Expand Down
15 changes: 15 additions & 0 deletions prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package kvm
import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
)

Expand All @@ -14,4 +16,17 @@ func initPrometheus() {
// A Prometheus metrics endpoint.
version.Version = builtAppVersion
prometheus.MustRegister(versioncollector.NewCollector("jetkvm"))

promHandler = promhttp.Handler()
}

func prometheusCheckAuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
if !config.MetricsEnabled {
c.JSON(http.StatusNotFound, gin.H{"error": "Metrics endpoint is disabled"})
return
}

c.Next()
}
}
3 changes: 1 addition & 2 deletions web.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/crypto/bcrypt"
)

//go:embed all:static

Check failure on line 18 in web.go

View workflow job for this annotation

GitHub Actions / lint

pattern all:static: no matching files found (typecheck)
var staticFiles embed.FS

type WebRTCSessionRequest struct {
Expand Down Expand Up @@ -87,7 +86,7 @@
r.POST("/device/setup", handleSetup)

// A Prometheus metrics endpoint.
r.GET("/metrics", gin.WrapH(promhttp.Handler()))
r.GET("/metrics", prometheusCheckAuthMiddleware(), gin.WrapH(promHandler))

// Protected routes (allows both password and noPassword modes)
protected := r.Group("/")
Expand Down
Loading