Skip to content

Commit

Permalink
GH-255: Added ws_requests_total unsubscribe metric.
Browse files Browse the repository at this point in the history
  • Loading branch information
jirenius committed Jul 2, 2024
1 parent 33a6986 commit c3afaa7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
10 changes: 6 additions & 4 deletions server/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ type MetricSet struct {
WSConnections openmetrics.Gauge
WSConnectionCount openmetrics.Counter
// WebSocket requests
WSRequestsGet openmetrics.Counter
WSRequestsSubscribe openmetrics.Counter
WSRequestsCall openmetrics.Counter
WSRequestsAuth openmetrics.Counter
WSRequestsGet openmetrics.Counter
WSRequestsSubscribe openmetrics.Counter
WSRequestsUnsubscribe openmetrics.Counter
WSRequestsCall openmetrics.Counter
WSRequestsAuth openmetrics.Counter
// Cache
CacheResources openmetrics.Gauge
CacheSubscriptions openmetrics.Gauge
Expand Down Expand Up @@ -74,6 +75,7 @@ func (m *MetricSet) Register(reg *openmetrics.Registry, version string, protocol
})
m.WSRequestsGet = wsRequests.With("get")
m.WSRequestsSubscribe = wsRequests.With("subscribe")
m.WSRequestsUnsubscribe = wsRequests.With("unsubscribe")
m.WSRequestsCall = wsRequests.With("call")
m.WSRequestsAuth = wsRequests.With("auth")

Expand Down
5 changes: 5 additions & 0 deletions server/wsConn.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,11 @@ func (c *wsConn) handleResourceResult(refRID string, cb func(result interface{},
}

func (c *wsConn) UnsubscribeResource(rid string, count int, cb func(ok bool)) {
// Metrics
if c.serv.metrics != nil {
c.serv.metrics.WSRequestsUnsubscribe.Add(1)
}

cb(c.UnsubscribeByRID(rid, count))
}

Expand Down
16 changes: 16 additions & 0 deletions test/33metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestMetrics_DefaultResponse_ContainsExpectedValues(t *testing.T) {
`resgate_ws_connections_total 0`,
`# TYPE resgate_ws_requests counter`,
`resgate_ws_requests_total{method="get"} 0`,
`resgate_ws_requests_total{method="unsubscribe"} 0`,
`resgate_ws_requests_total{method="auth"} 0`,
`resgate_ws_requests_total{method="subscribe"} 0`,
`resgate_ws_requests_total{method="call"} 0`,
Expand Down Expand Up @@ -133,6 +134,21 @@ func TestMetrics_WSRequestsGet_IncreasesCounter(t *testing.T) {
})
}

func TestMetrics_WSRequestsUnsubscribe_IncreasesCounter(t *testing.T) {
runTest(t, func(s *Session) {
c := s.Connect()
// Send subscribe request
subscribeToTestModel(t, s, c)
c.Request("unsubscribe.test.model", nil).GetResponse(t)

AssertResponseContainsMetrics(t, s.MetricsHTTPRequest(), []string{
`resgate_ws_requests_total{method="unsubscribe"} 1`,
})
}, func(cfg *server.Config) {
cfg.MetricsPort = 8090
})
}

func TestMetrics_WSRequestsAuth_IncreasesCounter(t *testing.T) {
runTest(t, func(s *Session) {
c := s.Connect()
Expand Down

0 comments on commit c3afaa7

Please sign in to comment.