Skip to content

Commit

Permalink
fix: txs endpoints names & ws subject
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenvechain committed Nov 26, 2024
1 parent 24b7f90 commit bb320ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
23 changes: 10 additions & 13 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)
metricActiveWebsocketCount = metrics.LazyLoadGaugeVec("api_active_websocket_count", []string{"subject"})
metricWebsocketCounter = metrics.LazyLoadCounterVec("api_websocket_count", []string{"subject"})
metricActiveWebsocketGauge = metrics.LazyLoadGaugeVec("api_active_websocket_gauge", []string{"subject"})
metricWebsocketCounter = metrics.LazyLoadCounterVec("api_websocket_counter", []string{"subject"})
)

// metricsResponseWriter is a wrapper around http.ResponseWriter that captures the status code.
Expand Down Expand Up @@ -63,33 +63,30 @@ func metricsMiddleware(next http.Handler) http.Handler {
var (
enabled = false
name = ""
subscription = ""
subscription = false
)

// all named route will be recorded
if rt != nil && rt.GetName() != "" {
enabled = true
name = rt.GetName()
if strings.HasPrefix(name, "subscriptions") {
// example path: /subscriptions/txpool -> subject = txpool
paths := strings.Split(r.URL.Path, "/")
if len(paths) > 2 {
subscription = paths[2]
}
subscription = true
name = "WS " + r.URL.Path
}
}

now := time.Now()
mrw := newMetricsResponseWriter(w)
if subscription != "" {
metricActiveWebsocketCount().AddWithLabel(1, map[string]string{"subject": subscription})
metricWebsocketCounter().AddWithLabel(1, map[string]string{"subject": subscription})
if subscription {
metricActiveWebsocketGauge().AddWithLabel(1, map[string]string{"subject": name})
metricWebsocketCounter().AddWithLabel(1, map[string]string{"subject": name})
}

next.ServeHTTP(mrw, r)

if subscription != "" {
metricActiveWebsocketCount().AddWithLabel(-1, map[string]string{"subject": subscription})
if subscription {
metricActiveWebsocketGauge().AddWithLabel(-1, map[string]string{"subject": 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
4 changes: 2 additions & 2 deletions api/transactions/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,10 @@ func (t *Transactions) Mount(root *mux.Router, pathPrefix string) {
HandlerFunc(utils.WrapHandlerFunc(t.handleSendTransaction))
sub.Path("/{id}").
Methods(http.MethodGet).
Name("GET /transactions/${id}").
Name("GET /transactions/{id}").
HandlerFunc(utils.WrapHandlerFunc(t.handleGetTransactionByID))
sub.Path("/{id}/receipt").
Methods(http.MethodGet).
Name("GET /transactions/${id}/receipt").
Name("GET /transactions/{id}/receipt").
HandlerFunc(utils.WrapHandlerFunc(t.handleGetTransactionReceiptByID))
}

0 comments on commit bb320ec

Please sign in to comment.