Skip to content

Commit

Permalink
chore: modified statsMiddleware to be generic (#2626)
Browse files Browse the repository at this point in the history
* chore: modified statsMiddleware to be generic

* minor fix

* Update gateway/gateway.go

Co-authored-by: Francesco Casula <fracasula@users.noreply.github.com>

* Update gateway/gateway.go

Co-authored-by: Francesco Casula <fracasula@users.noreply.github.com>

* lint fix

* test fix

Co-authored-by: chandumlg <54652834+chandumlg@users.noreply.github.com>
Co-authored-by: Francesco Casula <fracasula@users.noreply.github.com>
  • Loading branch information
3 people committed Oct 28, 2022
1 parent 46b5b3f commit b6ce9b9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
8 changes: 4 additions & 4 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -1423,10 +1423,10 @@ func (gateway *HandleT) StartWebHandler(ctx context.Context) error {
gateway.logger.Infof("WebHandler waiting for BackendConfig before starting on %d", webPort)
gateway.backendConfig.WaitForConfig(ctx)
gateway.logger.Infof("WebHandler Starting on %d", webPort)

component := "gateway"
srvMux := mux.NewRouter()
srvMux.Use(
middleware.StatMiddleware(ctx, srvMux, stats.Default),
middleware.StatMiddleware(ctx, srvMux, stats.Default, component),
middleware.LimitConcurrentRequests(maxConcurrentRequests),
)
srvMux.HandleFunc("/v1/batch", gateway.webBatchHandler).Methods("POST")
Expand Down Expand Up @@ -1499,10 +1499,10 @@ func (gateway *HandleT) StartAdminHandler(ctx context.Context) error {
gateway.logger.Infof("AdminHandler waiting for BackendConfig before starting on %d", adminWebPort)
gateway.backendConfig.WaitForConfig(ctx)
gateway.logger.Infof("AdminHandler starting on %d", adminWebPort)

component := "gateway"
srvMux := mux.NewRouter()
srvMux.Use(
middleware.StatMiddleware(ctx, srvMux, stats.Default),
middleware.StatMiddleware(ctx, srvMux, stats.Default, component),
middleware.LimitConcurrentRequests(maxConcurrentRequests),
)
srvMux.HandleFunc("/v1/pending-events", gateway.pendingEventsHandler).Methods("POST")
Expand Down
7 changes: 4 additions & 3 deletions middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package middleware

import (
"context"
"fmt"
"net/http"
"strconv"
"sync/atomic"
Expand Down Expand Up @@ -33,9 +34,9 @@ func LimitConcurrentRequests(maxRequests int) func(http.Handler) http.Handler {
}
}

func StatMiddleware(ctx context.Context, router *mux.Router, s stats.Stats) func(http.Handler) http.Handler {
func StatMiddleware(ctx context.Context, router *mux.Router, s stats.Stats, component string) func(http.Handler) http.Handler {
var concurrentRequests int32
activeClientCount := s.NewStat("gateway.concurrent_requests_count", stats.GaugeType)
activeClientCount := s.NewStat(fmt.Sprintf("%s.concurrent_requests_count", component), stats.GaugeType)
go func() {
for {
select {
Expand Down Expand Up @@ -70,7 +71,7 @@ func StatMiddleware(ctx context.Context, router *mux.Router, s stats.Stats) func
next.ServeHTTP(sw, r)

s.NewSampledTaggedStat(
"gateway.response_time",
fmt.Sprintf("%s.response_time", component),
stats.TimerType,
map[string]string{
"reqType": path,
Expand Down
8 changes: 5 additions & 3 deletions middleware/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package middleware_test

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
Expand Down Expand Up @@ -81,6 +82,7 @@ func TestMaxConcurrentRequestsMiddleware(t *testing.T) {
}

func TestStatsMiddleware(t *testing.T) {
component := "test"
testCase := func(expectedStatusCode int, pathTemplate, requestPath, expectedReqType, expectedMethod string) func(t *testing.T) {
return func(t *testing.T) {
ctrl := gomock.NewController(t)
Expand All @@ -90,8 +92,8 @@ func TestStatsMiddleware(t *testing.T) {
})

measurement := mock_stats.NewMockMeasurement(ctrl)
mockStats.EXPECT().NewStat("gateway.concurrent_requests_count", stats.GaugeType).Return(measurement).Times(1)
mockStats.EXPECT().NewSampledTaggedStat("gateway.response_time", stats.TimerType,
mockStats.EXPECT().NewStat(fmt.Sprintf("%s.concurrent_requests_count", component), stats.GaugeType).Return(measurement).Times(1)
mockStats.EXPECT().NewSampledTaggedStat(fmt.Sprintf("%s.response_time", component), stats.TimerType,
map[string]string{
"reqType": expectedReqType,
"method": expectedMethod,
Expand All @@ -103,7 +105,7 @@ func TestStatsMiddleware(t *testing.T) {
defer cancel()
router := mux.NewRouter()
router.Use(
middleware.StatMiddleware(ctx, router, mockStats),
middleware.StatMiddleware(ctx, router, mockStats, component),
)
router.HandleFunc(pathTemplate, handler).Methods(expectedMethod)

Expand Down

0 comments on commit b6ce9b9

Please sign in to comment.