diff --git a/pkg/client.go b/pkg/client.go index 7ad0aa1..7b5aecd 100644 --- a/pkg/client.go +++ b/pkg/client.go @@ -94,6 +94,10 @@ func (c *Client) HandleTimeout(t Timeout) { select { case <-time.After(time.Duration(t.ExpiresAt-t.IssuedAt) * time.Millisecond): { + if MetricsEnabled { + TimeoutMetric.Dec() + } + if !Server.HasClient() { Server.QueueIn(t) logrus.Warnf("Client has been disconnected, added pending timeout to replay soon.") @@ -120,7 +124,7 @@ func (c *Client) HandleTimeout(t Timeout) { }() } -func (c *Client) HandleMessage(msg Message) { +func (c *Client) HandleMessage(msg Message, t time.Time) { switch msg.OP { case RequestAll: { @@ -155,6 +159,10 @@ func (c *Client) HandleMessage(msg Message) { case Request: { + if MetricsEnabled { + TimeoutMetric.Inc() + } + c.HandleTimeout(toTimeout(msg.Data.(map[string]interface{}))) } @@ -171,4 +179,8 @@ func (c *Client) HandleMessage(msg Message) { }) } } + + if MetricsEnabled { + TimeoutLatencyMetric.Observe(float64(time.Since(t).Nanoseconds() / 1000000)) + } } diff --git a/pkg/metrics.go b/pkg/metrics.go index e1315d1..2cddd59 100644 --- a/pkg/metrics.go +++ b/pkg/metrics.go @@ -27,6 +27,8 @@ import ( ) var ( + MetricsEnabled = false + TimeoutMetric = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "nino_timeouts_timeouts", Help: "How many timeouts the service is handling", @@ -51,6 +53,7 @@ func SetupMetrics() bool { return false } + MetricsEnabled = true logrus.Infof("Now setting up collector registry...") prometheus.MustRegister(TimeoutMetric, TimeoutLatencyMetric) diff --git a/pkg/socket.go b/pkg/socket.go index 4e0a896..3d9e489 100644 --- a/pkg/socket.go +++ b/pkg/socket.go @@ -29,6 +29,7 @@ import ( "net/http" "os" "sync" + "time" ) type WebSocketServer struct { @@ -122,6 +123,7 @@ func HandleRequest(w http.ResponseWriter, req *http.Request) { break } + s := time.Now() var message Message err := conn.ReadJSON(&message) if err != nil { @@ -145,7 +147,7 @@ func HandleRequest(w http.ResponseWriter, req *http.Request) { Server.Queue = []Timeout{} } - go Server.client.HandleMessage(message) + go Server.client.HandleMessage(message, s) } }() }