From 288c00ffe31b44b2db1490f10399938263bf2668 Mon Sep 17 00:00:00 2001 From: saurav-malani Date: Thu, 8 Jun 2023 13:40:26 +0530 Subject: [PATCH] removed gorillaware pkg --- gorillaware/stats.go | 81 --------------------------------------- gorillaware/stats_test.go | 57 --------------------------- 2 files changed, 138 deletions(-) delete mode 100644 gorillaware/stats.go delete mode 100644 gorillaware/stats_test.go diff --git a/gorillaware/stats.go b/gorillaware/stats.go deleted file mode 100644 index 3767a432..00000000 --- a/gorillaware/stats.go +++ /dev/null @@ -1,81 +0,0 @@ -package gorillaware - -import ( - "context" - "fmt" - "net/http" - "strconv" - "sync/atomic" - "time" - - "github.com/gorilla/mux" - "github.com/rudderlabs/rudder-go-kit/stats" -) - -func StatMiddleware(ctx context.Context, router *mux.Router, s stats.Stats, component string) func(http.Handler) http.Handler { - var concurrentRequests int32 - activeClientCount := s.NewStat(fmt.Sprintf("%s.concurrent_requests_count", component), stats.GaugeType) - go func() { - for { - select { - case <-ctx.Done(): - return - case <-time.After(10 * time.Second): - activeClientCount.Gauge(atomic.LoadInt32(&concurrentRequests)) - } - } - }() - - // getPath retrieves the path from the request. - // The matched route's template is used if a match is found, - // otherwise the request's URL path is used instead. - getPath := func(r *http.Request) string { - var match mux.RouteMatch - if router.Match(r, &match) { - if path, err := match.Route.GetPathTemplate(); err == nil { - return path - } - } - return r.URL.Path - } - return func(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - sw := newStatusCapturingWriter(w) - path := getPath(r) - start := time.Now() - atomic.AddInt32(&concurrentRequests, 1) - defer atomic.AddInt32(&concurrentRequests, -1) - - next.ServeHTTP(sw, r) - - s.NewSampledTaggedStat( - fmt.Sprintf("%s.response_time", component), - stats.TimerType, - map[string]string{ - "reqType": path, - "method": r.Method, - "code": strconv.Itoa(sw.status), - }).Since(start) - }) - } -} - -// newStatusCapturingWriter returns a new, properly initialized statusCapturingWriter -func newStatusCapturingWriter(w http.ResponseWriter) *statusCapturingWriter { - return &statusCapturingWriter{ - ResponseWriter: w, - status: http.StatusOK, - } -} - -// statusCapturingWriter is a response writer decorator that captures the status code. -type statusCapturingWriter struct { - http.ResponseWriter - status int -} - -// WriteHeader override the http.ResponseWriter's `WriteHeader` method -func (w *statusCapturingWriter) WriteHeader(status int) { - w.status = status - w.ResponseWriter.WriteHeader(status) -} diff --git a/gorillaware/stats_test.go b/gorillaware/stats_test.go deleted file mode 100644 index 2cb9f29c..00000000 --- a/gorillaware/stats_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package gorillaware_test - -import ( - "context" - "fmt" - "net/http" - "net/http/httptest" - "strconv" - "testing" - - "github.com/golang/mock/gomock" - "github.com/gorilla/mux" - "github.com/rudderlabs/rudder-go-kit/gorillaware" - "github.com/rudderlabs/rudder-go-kit/stats" - "github.com/rudderlabs/rudder-go-kit/stats/mock_stats" - "github.com/stretchr/testify/require" -) - -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) - mockStats := mock_stats.NewMockStats(ctrl) - handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(expectedStatusCode) - }) - - measurement := mock_stats.NewMockMeasurement(ctrl) - 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, - "code": strconv.Itoa(expectedStatusCode), - }).Return(measurement).Times(1) - measurement.EXPECT().Since(gomock.Any()).Times(1) - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - router := mux.NewRouter() - router.Use( - gorillaware.StatMiddleware(ctx, router, mockStats, component), - ) - router.HandleFunc(pathTemplate, handler).Methods(expectedMethod) - - response := httptest.NewRecorder() - request := httptest.NewRequest("GET", "http://example.com"+requestPath, http.NoBody) - router.ServeHTTP(response, request) - require.Equal(t, expectedStatusCode, response.Code) - } - } - - t.Run("template with param in path", testCase(http.StatusNotFound, "/v1/{param}", "/v1/abc", "/v1/{param}", "GET")) - - t.Run("template without param in path", testCase(http.StatusNotFound, "/v1/some-other/key", "/v1/some-other/key", "/v1/some-other/key", "GET")) -}