* metrics add totalRooms and runningRooms.

* load initial metrics.

* fix.
This commit is contained in:
Miroslav Šedivý
2024-03-03 17:31:18 +01:00
committed by GitHub
parent 74fe2d7eab
commit 44fa2e06e4
5 changed files with 95 additions and 11 deletions
+13 -6
View File
@@ -16,12 +16,13 @@ type Admin struct {
}
type Server struct {
Cert string
Key string
Bind string
Proxy bool
CORS bool
PProf bool
Cert string
Key string
Bind string
Proxy bool
CORS bool
PProf bool
Metrics bool
Admin Admin
}
@@ -57,6 +58,11 @@ func (Server) Init(cmd *cobra.Command) error {
return err
}
cmd.PersistentFlags().Bool("metrics", false, "enable metrics endpoint available at /metrics")
if err := viper.BindPFlag("metrics", cmd.PersistentFlags().Lookup("metrics")); err != nil {
return err
}
// Admin
cmd.PersistentFlags().String("admin.static", "", "path to neko_rooms admin client files to serve")
@@ -94,6 +100,7 @@ func (s *Server) Set() {
s.Proxy = viper.GetBool("proxy")
s.CORS = viper.GetBool("cors")
s.PProf = viper.GetBool("pprof")
s.Metrics = viper.GetBool("metrics")
s.Admin.Static = viper.GetString("admin.static")
s.Admin.PathPrefix = path.Join("/", path.Clean(viper.GetString("admin.path_prefix")))
+37
View File
@@ -9,6 +9,8 @@ import (
"github.com/m1k1o/neko-rooms/internal/config"
"github.com/m1k1o/neko-rooms/internal/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
@@ -38,6 +40,9 @@ type events struct {
listeners []chan types.RoomEvent
listenersMu sync.Mutex
runningRooms prometheus.Gauge
totalRooms prometheus.Counter
}
func newEvents(config *config.Room, client *dockerClient.Client) *events {
@@ -48,12 +53,41 @@ func newEvents(config *config.Room, client *dockerClient.Client) *events {
roomsReadyCh: make(chan roomReady),
roomsReady: make(map[string]struct{}),
// metrics
runningRooms: promauto.NewGauge(prometheus.GaugeOpts{
Name: "running_rooms",
Namespace: "neko_rooms",
Help: "Number of currently running rooms.",
}),
totalRooms: promauto.NewCounter(prometheus.CounterOpts{
Name: "total_rooms",
Namespace: "neko_rooms",
Help: "Total number of rooms created since start.",
}),
}
}
func (e *events) Start() {
e.ctx, e.cancel = context.WithCancel(context.Background())
// load initial metrics
containers, err := e.client.ContainerList(e.ctx, dockerTypes.ContainerListOptions{
Filters: filters.NewArgs(
filters.Arg("label", fmt.Sprintf("m1k1o.neko_rooms.instance=%s", e.config.InstanceName)),
),
})
if err != nil {
e.logger.Err(err).Msg("failed to list containers")
return
}
for _, container := range containers {
e.totalRooms.Inc()
if container.State == "running" {
e.runningRooms.Inc()
}
}
msgs, errs := e.client.Events(e.ctx, dockerTypes.EventsOptions{
Filters: filters.NewArgs(
filters.Arg("type", "container"),
@@ -108,9 +142,11 @@ func (e *events) Start() {
switch msg.Action {
case "create":
action = types.RoomEventCreated
e.totalRooms.Inc()
case "start":
action = types.RoomEventStarted
e.waitForRoomReady(roomId, labels)
e.runningRooms.Inc()
case "health_status: healthy":
action = types.RoomEventReady
// ignore if room was already ready
@@ -120,6 +156,7 @@ func (e *events) Start() {
case "stop":
action = types.RoomEventStopped
e.setRoomNotReady(roomId)
e.runningRooms.Dec()
case "destroy":
action = types.RoomEventDestroyed
}
+8
View File
@@ -15,6 +15,8 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
@@ -240,6 +242,12 @@ func New(ApiManager types.ApiManager, roomConfig *config.Room, config *config.Se
logger.Info().Msgf("with pprof endpoint")
}
// mount metrics prometheus endpoint
if config.Metrics {
router.Mount("/metrics", promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}))
logger.Info().Msgf("with metrics endpoint")
}
// handle all remaining paths with proxy
router.Handle("/*", proxyHandler)