Skip to content

Commit

Permalink
chore: standardise naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenvechain committed Nov 26, 2024
1 parent 5265ec3 commit 3af97f9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
var (
metricHTTPReqCounter = metrics.LazyLoadCounterVec("api_request_count", []string{"name", "code", "method"})
metricHTTPReqDuration = metrics.LazyLoadHistogramVec("api_duration_ms", []string{"name", "code", "method"}, metrics.BucketHTTPReqs)
metricActiveWebsocketGauge = metrics.LazyLoadGaugeVec("api_active_websocket_gauge", []string{"subject"})
metricWebsocketCounter = metrics.LazyLoadCounterVec("api_websocket_counter", []string{"subject"})
metricActiveWebsocketGauge = metrics.LazyLoadGaugeVec("api_active_websocket_gauge", []string{"name"})
metricWebsocketCounter = metrics.LazyLoadCounterVec("api_websocket_counter", []string{"name"})
)

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

next.ServeHTTP(mrw, r)

if subscription {
metricActiveWebsocketGauge().AddWithLabel(-1, map[string]string{"subject": name})
metricActiveWebsocketGauge().AddWithLabel(-1, map[string]string{"name": name})
} 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
6 changes: 3 additions & 3 deletions api/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestWebsocketMetrics(t *testing.T) {
assert.Equal(t, float64(1), m[0].GetGauge().GetValue())

labels := m[0].GetLabel()
assert.Equal(t, "subject", labels[0].GetName())
assert.Equal(t, "name", labels[0].GetName())
assert.Equal(t, "WS /subscriptions/beat", labels[0].GetValue())

// initiate 1 beat subscription, active websocket should be 2
Expand Down Expand Up @@ -157,9 +157,9 @@ func TestWebsocketMetrics(t *testing.T) {
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
// m[1] should have the name of block
labels = m[1].GetLabel()
assert.Equal(t, "subject", labels[0].GetName())
assert.Equal(t, "name", labels[0].GetName())
assert.Equal(t, "WS /subscriptions/block", labels[0].GetValue())
}

Expand Down

0 comments on commit 3af97f9

Please sign in to comment.