Skip to content

Commit 54859ed

Browse files
committed
Add prometheus endpoint
1 parent d231e65 commit 54859ed

8 files changed

Lines changed: 154 additions & 5 deletions

File tree

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Config struct {
4848
AuthMode string `default:"turn" split_words:"true"`
4949
CorsAllowedOrigins []string `split_words:"true"`
5050
UsersFile string `split_words:"true"`
51+
Prometheus bool `split_words:"true"`
5152

5253
CheckOrigin func(string) bool `ignored:"true" json:"-"`
5354
}

docs/config.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,8 @@ SCREEGO_USERS_FILE=
6969

7070
# The loglevel (one of: debug, info, warn, error)
7171
SCREEGO_LOG_LEVEL=info
72-
```
72+
73+
# If screego should expose a prometheus endpoint at /metrics. The endpoint
74+
# requires basic authentication from a user in the users file.
75+
SCREEGO_PROMETHEUS=false
76+
```

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
1616
github.com/pion/transport v0.10.1 // indirect
1717
github.com/pion/turn/v2 v2.0.4
18+
github.com/prometheus/client_golang v1.7.1
1819
github.com/rs/xid v1.2.1
1920
github.com/rs/zerolog v1.19.0
2021
github.com/stretchr/testify v1.6.1

go.sum

Lines changed: 81 additions & 0 deletions
Large diffs are not rendered by default.

router/router.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package router
33
import (
44
"encoding/json"
55
"github.com/rs/zerolog/hlog"
6-
"github.com/rs/zerolog/log"
76
"net/http"
87
"time"
98

109
"github.com/gorilla/handlers"
1110
"github.com/gorilla/mux"
11+
"github.com/prometheus/client_golang/prometheus/promhttp"
12+
"github.com/rs/zerolog/log"
1213
"github.com/screego/server/auth"
1314
"github.com/screego/server/config"
1415
"github.com/screego/server/ui"
@@ -42,6 +43,10 @@ func Router(conf config.Config, rooms *ws.Rooms, users *auth.Users, version stri
4243
Version: version,
4344
})
4445
})
46+
if conf.Prometheus {
47+
log.Info().Msg("Prometheus enabled")
48+
router.Methods("GET").Path("/metrics").Handler(basicAuth(promhttp.Handler(), users))
49+
}
4550

4651
ui.Register(router)
4752

@@ -57,3 +62,19 @@ func accessLogger(r *http.Request, status, size int, dur time.Duration) {
5762
Str("duration", dur.String()).
5863
Msg("HTTP")
5964
}
65+
66+
func basicAuth(handler http.Handler, users *auth.Users) http.HandlerFunc {
67+
return func(w http.ResponseWriter, r *http.Request) {
68+
69+
user, pass, ok := r.BasicAuth()
70+
71+
if !ok || !users.Validate(user, pass) {
72+
w.Header().Set("WWW-Authenticate", `Basic realm="screego"`)
73+
w.WriteHeader(401)
74+
_, _ = w.Write([]byte("Unauthorised.\n"))
75+
return
76+
}
77+
78+
handler.ServeHTTP(w, r)
79+
}
80+
}

screego.config.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ SCREEGO_USERS_FILE=
5050

5151
# The loglevel (one of: debug, info, warn, error)
5252
SCREEGO_LOG_LEVEL=info
53+
54+
# If screego should expose a prometheus endpoint at /metrics. The endpoint
55+
# requires basic authentication from a user in the users file.
56+
SCREEGO_PROMETHEUS=false

ws/prometheus.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ws
2+
3+
import (
4+
"github.com/prometheus/client_golang/prometheus"
5+
"github.com/prometheus/client_golang/prometheus/promauto"
6+
)
7+
8+
var (
9+
roomGauge = promauto.NewGauge(prometheus.GaugeOpts{
10+
Name: "screego_room_total",
11+
Help: "The total number of rooms",
12+
})
13+
userGauge = promauto.NewGauge(prometheus.GaugeOpts{
14+
Name: "screego_user_total",
15+
Help: "The total number of users",
16+
})
17+
sessionGauge = promauto.NewGauge(prometheus.GaugeOpts{
18+
Name: "screego_session_total",
19+
Help: "The total number of sessions",
20+
})
21+
)

ws/rooms.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,26 @@ func (r *Rooms) Upgrade(w http.ResponseWriter, req *http.Request) {
6565
}
6666

6767
func (r *Rooms) Start() {
68+
prometheusTicker := time.NewTicker(time.Second * 5)
69+
if !r.config.Prometheus {
70+
prometheusTicker.Stop()
71+
}
6872
for {
69-
msg := <-r.Incoming
70-
if err := msg.Incoming.Execute(r, msg.Info); err != nil {
71-
msg.Info.Close <- err.Error()
73+
select {
74+
case msg := <-r.Incoming:
75+
if err := msg.Incoming.Execute(r, msg.Info); err != nil {
76+
msg.Info.Close <- err.Error()
77+
}
78+
case <-prometheusTicker.C:
79+
roomGauge.Set(float64(len(r.Rooms)))
80+
users := 0
81+
sessions := 0
82+
for _, room := range r.Rooms {
83+
users += len(room.Users)
84+
sessions += len(room.Sessions)
85+
}
86+
userGauge.Set(float64(users))
87+
sessionGauge.Set(float64(sessions))
7288
}
7389
}
7490
}

0 commit comments

Comments
 (0)