Skip to content

Commit

Permalink
feat: redact unknown paths by default in chiware StatMiddleware (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
saurav-malani authored Jun 8, 2023
1 parent f43c5fb commit bb7d78d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 146 deletions.
27 changes: 25 additions & 2 deletions chiware/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,28 @@ import (
"github.com/rudderlabs/rudder-go-kit/stats"
)

func StatMiddleware(ctx context.Context, router chi.Router, s stats.Stats, component string) func(http.Handler) http.Handler {
type config struct {
redactUnknownPaths bool
}

type Option func(*config)

// RedactUnknownPaths sets the redactUnknownPaths flag.
// If set to true, the path will be redacted if the route is not found.
// If set to false, the path will be used as is.
func RedactUnknownPaths(redactUnknownPaths bool) Option {
return func(c *config) {
c.redactUnknownPaths = redactUnknownPaths
}
}

func StatMiddleware(ctx context.Context, router chi.Router, s stats.Stats, component string, options ...Option) func(http.Handler) http.Handler {
conf := config{
redactUnknownPaths: true,
}
for _, option := range options {
option(&conf)
}
var concurrentRequests int32
activeClientCount := s.NewStat(fmt.Sprintf("%s.concurrent_requests_count", component), stats.GaugeType)
go func() {
Expand All @@ -33,6 +54,9 @@ func StatMiddleware(ctx context.Context, router chi.Router, s stats.Stats, compo
if path := chi.RouteContext(r.Context()).RoutePattern(); path != "" {
return path
}
if conf.redactUnknownPaths {
return "/redacted"
}
return r.URL.Path
}
return func(next http.Handler) http.Handler {
Expand All @@ -43,7 +67,6 @@ func StatMiddleware(ctx context.Context, router chi.Router, s stats.Stats, compo
defer atomic.AddInt32(&concurrentRequests, -1)

next.ServeHTTP(sw, r)

s.NewSampledTaggedStat(
fmt.Sprintf("%s.response_time", component),
stats.TimerType,
Expand Down
8 changes: 5 additions & 3 deletions chiware/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

func TestStatsMiddleware(t *testing.T) {
component := "test"
testCase := func(expectedStatusCode int, pathTemplate, requestPath, expectedMethod string) func(t *testing.T) {
testCase := func(expectedStatusCode int, pathTemplate, requestPath, expectedMethod string, options ...chiware.Option) func(t *testing.T) {
return func(t *testing.T) {
ctrl := gomock.NewController(t)
mockStats := mock_stats.NewMockStats(ctrl)
Expand All @@ -40,7 +40,7 @@ func TestStatsMiddleware(t *testing.T) {
defer cancel()
router := chi.NewRouter()
router.Use(
chiware.StatMiddleware(ctx, router, mockStats, component),
chiware.StatMiddleware(ctx, router, mockStats, component, options...),
)
router.MethodFunc(expectedMethod, pathTemplate, handler)

Expand All @@ -52,6 +52,8 @@ func TestStatsMiddleware(t *testing.T) {
}

t.Run("template with param in path", testCase(http.StatusNotFound, "/v1/{param}", "/v1/abc", "GET"))

t.Run("template without param in path", testCase(http.StatusNotFound, "/v1/some-other/key", "/v1/some-other/key", "GET"))
t.Run("template with unknown path ", testCase(http.StatusNotFound, "/a/b/c", "/a/b/c", "GET", chiware.RedactUnknownPaths(false)))
t.Run("template with unknown path ", testCase(http.StatusNotFound, "/redacted", "/a/b/c", "GET", chiware.RedactUnknownPaths(true)))
t.Run("template with unknown path ", testCase(http.StatusNotFound, "/redacted", "/a/b/c", "GET"))
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/go-chi/chi/v5 v5.0.8
github.com/go-redis/redis/v8 v8.11.5
github.com/golang/mock v1.6.0
github.com/gorilla/mux v1.8.0
github.com/joho/godotenv v1.5.1
github.com/lib/pq v1.10.9
github.com/ory/dockertest/v3 v3.10.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,6 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 h1:BZHcxBETFHIdVyhyEfOvn/RdU/QGdLI4y34qQGjGWO0=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks=
Expand Down
81 changes: 0 additions & 81 deletions gorillaware/stats.go

This file was deleted.

57 changes: 0 additions & 57 deletions gorillaware/stats_test.go

This file was deleted.

0 comments on commit bb7d78d

Please sign in to comment.