1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-07-14 01:57:32 +08:00

Add metrics (#70)

* Add prometheus support to count sessions, instances and clientes over
time

* Track counters on server reload

* Change to gauges
This commit is contained in:
Jonathan Leibiusky 2016-12-15 17:12:17 -08:00 committed by GitHub
parent 946a8e1419
commit 70eaf37d4b
4 changed files with 54 additions and 2 deletions

2
api.go
View File

@ -12,6 +12,7 @@ import (
"github.com/franela/play-with-docker/services"
"github.com/franela/play-with-docker/templates"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/negroni"
)
@ -71,6 +72,7 @@ func main() {
}))
r.Handle("/sessions/{sessionId}/ws/", server)
r.Handle("/metrics", promhttp.Handler())
n := negroni.Classic()
n.UseHandler(r)

View File

@ -4,8 +4,20 @@ import (
"log"
"github.com/googollee/go-socket.io"
"github.com/prometheus/client_golang/prometheus"
)
var (
clientsGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "clients",
Help: "Clients",
})
)
func init() {
prometheus.MustRegister(clientsGauge)
}
type ViewPort struct {
Rows uint
Cols uint
@ -23,6 +35,7 @@ func (c *Client) ResizeViewPort(cols, rows uint) {
}
func NewClient(so socketio.Socket, session *Session) *Client {
clientsGauge.Inc()
so.Join(session.Id)
c := &Client{so: so, Id: so.Id()}
@ -52,7 +65,9 @@ func NewClient(so socketio.Socket, session *Session) *Client {
}
}
})
so.On("disconnection", func() {
clientsGauge.Dec()
// Client has disconnected. Remove from session and recheck terminal sizes.
for i, cl := range session.clients {
if cl.Id == c.Id {
@ -69,6 +84,5 @@ func NewClient(so socketio.Socket, session *Session) *Client {
}
}
})
return c
}

View File

@ -11,10 +11,22 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/prometheus/client_golang/prometheus"
)
var rw sync.Mutex
var (
instancesGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "instances",
Help: "Instances",
})
)
func init() {
prometheus.MustRegister(instancesGauge)
}
type Instance struct {
session *Session `json:"-"`
Name string `json:"name"`
@ -90,6 +102,8 @@ func NewInstance(session *Session) (*Instance, error) {
wsServer.BroadcastTo(session.Id, "new instance", instance.Name, instance.IP, instance.Hostname)
instancesGauge.Inc()
return instance, nil
}
@ -142,5 +156,7 @@ func DeleteInstance(session *Session, instance *Instance) error {
wsServer.BroadcastTo(session.Id, "delete instance", instance.Name)
instancesGauge.Dec()
return err
}

View File

@ -14,9 +14,21 @@ import (
"github.com/docker/docker/client"
"github.com/googollee/go-socket.io"
"github.com/prometheus/client_golang/prometheus"
"github.com/twinj/uuid"
)
var (
sessionsGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "sessions",
Help: "Sessions",
})
)
func init() {
prometheus.MustRegister(sessionsGauge)
}
var wsServer *socketio.Server
type Session struct {
@ -129,10 +141,15 @@ func CloseSession(s *Session) error {
s.rw.Lock()
defer s.rw.Unlock()
sessionsGauge.Dec()
if s.ticker != nil {
s.ticker.Stop()
}
wsServer.BroadcastTo(s.Id, "session end")
for _, c := range s.clients {
c.so.Emit("disconnect")
clientsGauge.Dec()
}
log.Printf("Starting clean up of session [%s]\n", s.Id)
for _, i := range s.Instances {
if i.conn != nil {
@ -207,6 +224,8 @@ func NewSession() (*Session, error) {
if err := saveSessionsToDisk(); err != nil {
return nil, err
}
sessionsGauge.Inc()
return s, nil
}
@ -236,6 +255,7 @@ func LoadSessionsFromDisk() error {
// schedule session expiration
for _, s := range sessions {
sessionsGauge.Inc()
timeLeft := s.ExpiresAt.Sub(time.Now())
CloseSessionAfter(s, timeLeft)
@ -243,7 +263,7 @@ func LoadSessionsFromDisk() error {
for _, i := range s.Instances {
// wire the session back to the instance
i.session = s
instancesGauge.Inc()
}
// Connect PWD daemon to the new network