Skip to content
This repository has been archived by the owner on Jul 31, 2022. It is now read-only.

Commit

Permalink
chore: collect metrics :>
Browse files Browse the repository at this point in the history
  • Loading branch information
auguwu committed Mar 5, 2022
1 parent 4ebb1b1 commit c25e8be
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
14 changes: 13 additions & 1 deletion pkg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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:
{
Expand Down Expand Up @@ -155,6 +159,10 @@ func (c *Client) HandleMessage(msg Message) {

case Request:
{
if MetricsEnabled {
TimeoutMetric.Inc()
}

c.HandleTimeout(toTimeout(msg.Data.(map[string]interface{})))
}

Expand All @@ -171,4 +179,8 @@ func (c *Client) HandleMessage(msg Message) {
})
}
}

if MetricsEnabled {
TimeoutLatencyMetric.Observe(float64(time.Since(t).Nanoseconds() / 1000000))
}
}
3 changes: 3 additions & 0 deletions pkg/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -51,6 +53,7 @@ func SetupMetrics() bool {
return false
}

MetricsEnabled = true
logrus.Infof("Now setting up collector registry...")
prometheus.MustRegister(TimeoutMetric, TimeoutLatencyMetric)

Expand Down
4 changes: 3 additions & 1 deletion pkg/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"net/http"
"os"
"sync"
"time"
)

type WebSocketServer struct {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}()
}

0 comments on commit c25e8be

Please sign in to comment.