Skip to content

Commit

Permalink
api: remove websocket conn close test (vechain#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
libotony authored Aug 1, 2024
1 parent 8a04014 commit 58bfe18
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
10 changes: 5 additions & 5 deletions api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
)

var (
metricHttpReqCounter = metrics.LazyLoadCounterVec("api_request_count", []string{"name", "code", "method"})
metricHttpReqDuration = metrics.LazyLoadHistogramVec("api_duration_ms", []string{"name", "code", "method"}, metrics.BucketHTTPReqs)
metricsActiveWebsocketCount = metrics.LazyLoadGaugeVec("api_active_websocket_count", []string{"subject"})
metricHttpReqCounter = metrics.LazyLoadCounterVec("api_request_count", []string{"name", "code", "method"})
metricHttpReqDuration = metrics.LazyLoadHistogramVec("api_duration_ms", []string{"name", "code", "method"}, metrics.BucketHTTPReqs)
metricActiveWebsocketCount = metrics.LazyLoadGaugeVec("api_active_websocket_count", []string{"subject"})
)

// metricsResponseWriter is a wrapper around http.ResponseWriter that captures the status code.
Expand Down Expand Up @@ -81,13 +81,13 @@ func metricsMiddleware(next http.Handler) http.Handler {
now := time.Now()
mrw := newMetricsResponseWriter(w)
if subscription != "" {
metricsActiveWebsocketCount().AddWithLabel(1, map[string]string{"subject": subscription})
metricActiveWebsocketCount().AddWithLabel(1, map[string]string{"subject": subscription})
}

next.ServeHTTP(mrw, r)

if subscription != "" {
metricsActiveWebsocketCount().AddWithLabel(-1, map[string]string{"subject": subscription})
metricActiveWebsocketCount().AddWithLabel(-1, map[string]string{"subject": subscription})
} else if enabled {
metricHttpReqCounter().AddWithLabel(1, map[string]string{"name": name, "code": strconv.Itoa(mrw.statusCode), "method": r.Method})
metricHttpReqDuration().ObserveWithLabels(time.Since(now).Milliseconds(), map[string]string{"name": name, "code": strconv.Itoa(mrw.statusCode), "method": r.Method})
Expand Down
27 changes: 8 additions & 19 deletions api/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"net/url"
"strings"
"testing"
"time"

"github.com/gorilla/mux"
"github.com/gorilla/websocket"
Expand Down Expand Up @@ -129,8 +128,9 @@ func TestWebsocketMetrics(t *testing.T) {

// initiate 1 beat subscription, active websocket should be 1
u := url.URL{Scheme: "ws", Host: strings.TrimPrefix(ts.URL, "http://"), Path: "/subscriptions/beat"}
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
conn1, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
assert.Nil(t, err)
defer conn1.Close()

body, _ := httpGet(t, ts.URL+"/metrics")
parser := expfmt.TextParser{}
Expand All @@ -146,8 +146,9 @@ func TestWebsocketMetrics(t *testing.T) {
assert.Equal(t, "beat", labels[0].GetValue())

// initiate 1 beat subscription, active websocket should be 2
_, _, err = websocket.DefaultDialer.Dial(u.String(), nil)
conn2, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
assert.Nil(t, err)
defer conn2.Close()

body, _ = httpGet(t, ts.URL+"/metrics")
metrics, err = parser.TextToMetricFamilies(bytes.NewReader(body))
Expand All @@ -157,23 +158,11 @@ func TestWebsocketMetrics(t *testing.T) {
assert.Equal(t, 1, len(m), "should be 1 metric entries")
assert.Equal(t, float64(2), m[0].GetGauge().GetValue())

// close 1 beat subscription, active websocket should be 1
conn.Close()
// ensure close is done
<-time.After(100 * time.Millisecond)

body, _ = httpGet(t, ts.URL+"/metrics")
metrics, err = parser.TextToMetricFamilies(bytes.NewReader(body))
assert.Nil(t, err)

m = metrics["thor_metrics_api_active_websocket_count"].GetMetric()
assert.Equal(t, 1, len(m), "should be 1 metric entries")
assert.Equal(t, float64(1), m[0].GetGauge().GetValue())

// initiate 1 block subscription, active websocket should be 2
// initiate 1 block subscription, active websocket should be 3
u = url.URL{Scheme: "ws", Host: strings.TrimPrefix(ts.URL, "http://"), Path: "/subscriptions/block"}
_, _, err = websocket.DefaultDialer.Dial(u.String(), nil)
conn3, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
assert.Nil(t, err)
defer conn3.Close()

body, _ = httpGet(t, ts.URL+"/metrics")
metrics, err = parser.TextToMetricFamilies(bytes.NewReader(body))
Expand All @@ -182,7 +171,7 @@ func TestWebsocketMetrics(t *testing.T) {
m = metrics["thor_metrics_api_active_websocket_count"].GetMetric()
assert.Equal(t, 2, len(m), "should be 2 metric entries")
// both m[0] and m[1] should have the value of 1
assert.Equal(t, float64(1), m[0].GetGauge().GetValue())
assert.Equal(t, float64(2), m[0].GetGauge().GetValue())
assert.Equal(t, float64(1), m[1].GetGauge().GetValue())

// m[1] should have the subject of block
Expand Down

0 comments on commit 58bfe18

Please sign in to comment.