* 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
+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
}