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:
parent
946a8e1419
commit
70eaf37d4b
2
api.go
2
api.go
@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/franela/play-with-docker/services"
|
"github.com/franela/play-with-docker/services"
|
||||||
"github.com/franela/play-with-docker/templates"
|
"github.com/franela/play-with-docker/templates"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
"github.com/urfave/negroni"
|
"github.com/urfave/negroni"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -71,6 +72,7 @@ func main() {
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
r.Handle("/sessions/{sessionId}/ws/", server)
|
r.Handle("/sessions/{sessionId}/ws/", server)
|
||||||
|
r.Handle("/metrics", promhttp.Handler())
|
||||||
|
|
||||||
n := negroni.Classic()
|
n := negroni.Classic()
|
||||||
n.UseHandler(r)
|
n.UseHandler(r)
|
||||||
|
@ -4,8 +4,20 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/googollee/go-socket.io"
|
"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 {
|
type ViewPort struct {
|
||||||
Rows uint
|
Rows uint
|
||||||
Cols uint
|
Cols uint
|
||||||
@ -23,6 +35,7 @@ func (c *Client) ResizeViewPort(cols, rows uint) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(so socketio.Socket, session *Session) *Client {
|
func NewClient(so socketio.Socket, session *Session) *Client {
|
||||||
|
clientsGauge.Inc()
|
||||||
so.Join(session.Id)
|
so.Join(session.Id)
|
||||||
|
|
||||||
c := &Client{so: so, Id: so.Id()}
|
c := &Client{so: so, Id: so.Id()}
|
||||||
@ -52,7 +65,9 @@ func NewClient(so socketio.Socket, session *Session) *Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
so.On("disconnection", func() {
|
so.On("disconnection", func() {
|
||||||
|
clientsGauge.Dec()
|
||||||
// Client has disconnected. Remove from session and recheck terminal sizes.
|
// Client has disconnected. Remove from session and recheck terminal sizes.
|
||||||
for i, cl := range session.clients {
|
for i, cl := range session.clients {
|
||||||
if cl.Id == c.Id {
|
if cl.Id == c.Id {
|
||||||
@ -69,6 +84,5 @@ func NewClient(so socketio.Socket, session *Session) *Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
@ -11,10 +11,22 @@ import (
|
|||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
var rw sync.Mutex
|
var rw sync.Mutex
|
||||||
|
|
||||||
|
var (
|
||||||
|
instancesGauge = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||||
|
Name: "instances",
|
||||||
|
Help: "Instances",
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
prometheus.MustRegister(instancesGauge)
|
||||||
|
}
|
||||||
|
|
||||||
type Instance struct {
|
type Instance struct {
|
||||||
session *Session `json:"-"`
|
session *Session `json:"-"`
|
||||||
Name string `json:"name"`
|
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)
|
wsServer.BroadcastTo(session.Id, "new instance", instance.Name, instance.IP, instance.Hostname)
|
||||||
|
|
||||||
|
instancesGauge.Inc()
|
||||||
|
|
||||||
return instance, nil
|
return instance, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,5 +156,7 @@ func DeleteInstance(session *Session, instance *Instance) error {
|
|||||||
|
|
||||||
wsServer.BroadcastTo(session.Id, "delete instance", instance.Name)
|
wsServer.BroadcastTo(session.Id, "delete instance", instance.Name)
|
||||||
|
|
||||||
|
instancesGauge.Dec()
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -14,9 +14,21 @@ import (
|
|||||||
|
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
"github.com/googollee/go-socket.io"
|
"github.com/googollee/go-socket.io"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/twinj/uuid"
|
"github.com/twinj/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
sessionsGauge = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||||
|
Name: "sessions",
|
||||||
|
Help: "Sessions",
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
prometheus.MustRegister(sessionsGauge)
|
||||||
|
}
|
||||||
|
|
||||||
var wsServer *socketio.Server
|
var wsServer *socketio.Server
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
@ -129,10 +141,15 @@ func CloseSession(s *Session) error {
|
|||||||
s.rw.Lock()
|
s.rw.Lock()
|
||||||
defer s.rw.Unlock()
|
defer s.rw.Unlock()
|
||||||
|
|
||||||
|
sessionsGauge.Dec()
|
||||||
if s.ticker != nil {
|
if s.ticker != nil {
|
||||||
s.ticker.Stop()
|
s.ticker.Stop()
|
||||||
}
|
}
|
||||||
wsServer.BroadcastTo(s.Id, "session end")
|
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)
|
log.Printf("Starting clean up of session [%s]\n", s.Id)
|
||||||
for _, i := range s.Instances {
|
for _, i := range s.Instances {
|
||||||
if i.conn != nil {
|
if i.conn != nil {
|
||||||
@ -207,6 +224,8 @@ func NewSession() (*Session, error) {
|
|||||||
if err := saveSessionsToDisk(); err != nil {
|
if err := saveSessionsToDisk(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sessionsGauge.Inc()
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,6 +255,7 @@ func LoadSessionsFromDisk() error {
|
|||||||
|
|
||||||
// schedule session expiration
|
// schedule session expiration
|
||||||
for _, s := range sessions {
|
for _, s := range sessions {
|
||||||
|
sessionsGauge.Inc()
|
||||||
timeLeft := s.ExpiresAt.Sub(time.Now())
|
timeLeft := s.ExpiresAt.Sub(time.Now())
|
||||||
CloseSessionAfter(s, timeLeft)
|
CloseSessionAfter(s, timeLeft)
|
||||||
|
|
||||||
@ -243,7 +263,7 @@ func LoadSessionsFromDisk() error {
|
|||||||
for _, i := range s.Instances {
|
for _, i := range s.Instances {
|
||||||
// wire the session back to the instance
|
// wire the session back to the instance
|
||||||
i.session = s
|
i.session = s
|
||||||
|
instancesGauge.Inc()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect PWD daemon to the new network
|
// Connect PWD daemon to the new network
|
||||||
|
Loading…
x
Reference in New Issue
Block a user