Fixed pagination on profiler hits
This commit is contained in:
@@ -166,15 +166,21 @@ func Test_profilerHandle_profilerGlobal(t *testing.T) {
|
||||
tcc = []tfp{
|
||||
{
|
||||
name: "skip profiler hit on profiler global = true",
|
||||
cfg: types.Config{ProfilerGlobal: true},
|
||||
r: rr,
|
||||
exp: nil,
|
||||
cfg: types.Config{Profiler: struct {
|
||||
Enabled bool
|
||||
Global bool
|
||||
}{Global: true}},
|
||||
r: rr,
|
||||
exp: nil,
|
||||
},
|
||||
{
|
||||
name: "add profiler hit on profiler global = false",
|
||||
cfg: types.Config{ProfilerGlobal: false},
|
||||
r: rr,
|
||||
exp: createRequest(rr),
|
||||
cfg: types.Config{Profiler: struct {
|
||||
Enabled bool
|
||||
Global bool
|
||||
}{Global: false}},
|
||||
r: rr,
|
||||
exp: createRequest(rr),
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -4,46 +4,27 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/pkg/apigw/profiler"
|
||||
"github.com/cortezaproject/corteza/server/pkg/apigw/types"
|
||||
h "github.com/cortezaproject/corteza/server/pkg/http"
|
||||
"github.com/cortezaproject/corteza/server/pkg/options"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
const (
|
||||
devHelperResponseBody string = `Hey developer!`
|
||||
)
|
||||
|
||||
func helperDefaultResponse(opt options.ApigwOpt, pr *profiler.Profiler, log *zap.Logger) http.HandlerFunc {
|
||||
func helperDefaultResponse(cfg types.Config, pr *profiler.Profiler, log *zap.Logger) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
addToProfiler(opt, pr, log, r, http.StatusNotFound)
|
||||
|
||||
responseBody := ""
|
||||
|
||||
if opt.LogEnabled {
|
||||
// Say something friendly when logging is enabled
|
||||
responseBody = devHelperResponseBody
|
||||
}
|
||||
|
||||
http.Error(w, responseBody, http.StatusNotFound)
|
||||
addToProfiler(cfg, pr, log, r, http.StatusNotFound)
|
||||
http.Error(w, "", http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
func helperMethodNotAllowed(opt options.ApigwOpt, pr *profiler.Profiler, log *zap.Logger) http.HandlerFunc {
|
||||
func helperMethodNotAllowed(cfg types.Config, pr *profiler.Profiler, log *zap.Logger) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
addToProfiler(opt, pr, log, r, http.StatusMethodNotAllowed)
|
||||
|
||||
if opt.LogEnabled {
|
||||
// Say something friendly when logging is enabled
|
||||
http.Error(w, devHelperResponseBody, http.StatusTeapot)
|
||||
} else {
|
||||
// Default 405 response
|
||||
http.Error(w, "", http.StatusMethodNotAllowed)
|
||||
}
|
||||
addToProfiler(cfg, pr, log, r, http.StatusMethodNotAllowed)
|
||||
http.Error(w, "", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func addToProfiler(opt options.ApigwOpt, pr *profiler.Profiler, log *zap.Logger, r *http.Request, status int) {
|
||||
if !(opt.ProfilerEnabled && opt.ProfilerGlobal) {
|
||||
func addToProfiler(cfg types.Config, pr *profiler.Profiler, log *zap.Logger, r *http.Request, status int) {
|
||||
if !(cfg.Profiler.Enabled && cfg.Profiler.Global) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"github.com/cortezaproject/corteza/server/pkg/apigw/pipeline"
|
||||
"github.com/cortezaproject/corteza/server/pkg/apigw/pipeline/chain"
|
||||
"github.com/cortezaproject/corteza/server/pkg/apigw/types"
|
||||
"github.com/cortezaproject/corteza/server/pkg/options"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
@@ -95,7 +94,6 @@ func Test_pl(t *testing.T) {
|
||||
method: tc.method,
|
||||
endpoint: tc.endpoint,
|
||||
log: zap.NewNop(),
|
||||
opts: *options.Apigw(),
|
||||
handler: pipe.Handler(),
|
||||
errHandler: pipe.Error(),
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@ import (
|
||||
"github.com/cortezaproject/corteza/server/pkg/apigw/registry"
|
||||
"github.com/cortezaproject/corteza/server/pkg/apigw/types"
|
||||
f "github.com/cortezaproject/corteza/server/pkg/filter"
|
||||
"github.com/cortezaproject/corteza/server/pkg/options"
|
||||
st "github.com/cortezaproject/corteza/server/system/types"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"go.uber.org/zap"
|
||||
@@ -28,7 +27,6 @@ type (
|
||||
}
|
||||
|
||||
apigw struct {
|
||||
opts options.ApigwOpt
|
||||
log *zap.Logger
|
||||
reg *registry.Registry
|
||||
routes []*route
|
||||
@@ -86,7 +84,7 @@ func (s *apigw) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if len(s.routes) == 0 {
|
||||
helperDefaultResponse(s.opts, s.pr, s.log)(w, r)
|
||||
helperDefaultResponse(s.cfg, s.pr, s.log)(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -125,8 +123,8 @@ func (s *apigw) Reload(ctx context.Context) (err error) {
|
||||
// profiler gets the missed hit info also
|
||||
{
|
||||
var (
|
||||
defaultMethodResponse = helperMethodNotAllowed(s.opts, s.pr, s.log)
|
||||
defaultResponse = helperDefaultResponse(s.opts, s.pr, s.log)
|
||||
defaultMethodResponse = helperMethodNotAllowed(s.cfg, s.pr, s.log)
|
||||
defaultResponse = helperDefaultResponse(s.cfg, s.pr, s.log)
|
||||
)
|
||||
|
||||
s.mx.NotFound(defaultResponse)
|
||||
@@ -171,8 +169,8 @@ func (s *apigw) ReloadEndpoint(ctx context.Context, method, endpoint string) (er
|
||||
// profiler gets the missed hit info also
|
||||
{
|
||||
var (
|
||||
defaultMethodResponse = helperMethodNotAllowed(s.opts, s.pr, s.log)
|
||||
defaultResponse = helperDefaultResponse(s.opts, s.pr, s.log)
|
||||
defaultMethodResponse = helperMethodNotAllowed(s.cfg, s.pr, s.log)
|
||||
defaultResponse = helperDefaultResponse(s.cfg, s.pr, s.log)
|
||||
)
|
||||
|
||||
s.mx.NotFound(defaultResponse)
|
||||
@@ -310,7 +308,7 @@ func (s *apigw) NotFound(_ context.Context, method, endpoint string) {
|
||||
}
|
||||
|
||||
var (
|
||||
defaultResponse = helperDefaultResponse(s.opts, s.pr, s.log)
|
||||
defaultResponse = helperDefaultResponse(s.cfg, s.pr, s.log)
|
||||
)
|
||||
|
||||
// Attach 404 handler
|
||||
@@ -448,26 +446,17 @@ func (s *apigw) loadFilters(ctx context.Context, route uint64) (ff []*st.ApigwFi
|
||||
}
|
||||
|
||||
func (s *apigw) loadInfo() {
|
||||
s.log.Info("loading Integration Gateway", zap.Bool("debug", s.opts.Debug), zap.Bool("log", s.opts.LogEnabled))
|
||||
s.log.Info("loading Integration Gateway")
|
||||
|
||||
if s.opts.ProfilerEnabled {
|
||||
if s.opts.LogRequestBody {
|
||||
s.log.Info("profiler and request body logging is enabled, profiler use is prefered",
|
||||
zap.Bool("APIGW_PROFILER_ENABLED", s.opts.ProfilerEnabled),
|
||||
zap.Bool("APIGW_LOG_REQUEST_BODY", s.opts.LogRequestBody))
|
||||
} else {
|
||||
s.log.Info("request body logging is enabled, profiler use is prefered (APIGW_PROFILER_ENABLED)",
|
||||
zap.Bool("APIGW_LOG_REQUEST_BODY", s.opts.LogRequestBody))
|
||||
}
|
||||
|
||||
if !s.opts.ProfilerGlobal {
|
||||
if s.cfg.Profiler.Enabled {
|
||||
if !s.cfg.Profiler.Global {
|
||||
s.log.Warn("profiler enabled only for routes with a profiler prefilter, use global setting to enable for all (APIGW_PROFILER_GLOBAL)")
|
||||
}
|
||||
} else {
|
||||
if s.opts.ProfilerGlobal {
|
||||
if s.cfg.Profiler.Global {
|
||||
s.log.Warn("profiler global is enabled, but profiler disabled, no routes will be profiled",
|
||||
zap.Bool("APIGW_PROFILER_ENABLED", s.opts.ProfilerEnabled),
|
||||
zap.Bool("APIGW_PROFILER_GLOBAL", s.opts.ProfilerGlobal))
|
||||
zap.Bool("APIGW_PROFILER_ENABLED", s.cfg.Profiler.Enabled),
|
||||
zap.Bool("APIGW_PROFILER_GLOBAL", s.cfg.Profiler.Global))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,7 +194,6 @@ func (svc *apigwProfiler) HitsAggregated(ctx context.Context, filter types.Apigw
|
||||
|
||||
func (svc *apigwProfiler) Purge(ctx context.Context, f *profiler.PurgeFilter) {
|
||||
apigw.Service().Profiler().Purge(f)
|
||||
return
|
||||
}
|
||||
|
||||
func sortAggregation(list *types.ApigwProfilerAggregationSet, filter *types.ApigwProfilerFilter) {
|
||||
@@ -235,9 +234,9 @@ func sortHits(list *types.ApigwProfilerHitSet, filter *types.ApigwProfilerFilter
|
||||
|
||||
func filterAggregation(list *types.ApigwProfilerAggregationSet, filter *types.ApigwProfilerFilter) {
|
||||
var (
|
||||
dec string = ""
|
||||
i uint = 0
|
||||
b = filter.Before == ""
|
||||
dec string = ""
|
||||
i uint = 0
|
||||
start = filter.Before == ""
|
||||
)
|
||||
|
||||
if filter.Limit == 0 {
|
||||
@@ -247,28 +246,36 @@ func filterAggregation(list *types.ApigwProfilerAggregationSet, filter *types.Ap
|
||||
dec, _ = decodeRoutePath(filter.Before)
|
||||
|
||||
*list, _ = list.Filter(func(apa *types.ApigwProfilerAggregation) (bool, error) {
|
||||
// after a specific hit and inside the limits
|
||||
if b && i < filter.Limit {
|
||||
i++
|
||||
filter.Next = encodeRoutePath(apa.Path)
|
||||
return true, nil
|
||||
if start {
|
||||
if i < filter.Limit {
|
||||
i++
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if i == filter.Limit {
|
||||
filter.Next = encodeRoutePath(apa.Path)
|
||||
i++
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
// after the specific hit check
|
||||
if dec != "" && b == false {
|
||||
b = apa.Path == dec
|
||||
if !start {
|
||||
start = apa.Path == dec
|
||||
|
||||
if start {
|
||||
i = 1
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func filterHits(list *types.ApigwProfilerHitSet, filter *types.ApigwProfilerFilter) {
|
||||
var (
|
||||
i uint = 0
|
||||
b = filter.Before == ""
|
||||
i uint = 0
|
||||
start = filter.Before == ""
|
||||
)
|
||||
|
||||
if filter.Limit == 0 {
|
||||
@@ -276,22 +283,31 @@ func filterHits(list *types.ApigwProfilerHitSet, filter *types.ApigwProfilerFilt
|
||||
}
|
||||
|
||||
*list, _ = list.Filter(func(aph *types.ApigwProfilerHit) (bool, error) {
|
||||
// after a specific hit and inside the limits
|
||||
if b && i < filter.Limit {
|
||||
i++
|
||||
filter.Next = aph.ID
|
||||
return true, nil
|
||||
|
||||
if start {
|
||||
if i < filter.Limit {
|
||||
i++
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if i == filter.Limit {
|
||||
filter.Next = aph.ID
|
||||
i++
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
// after the specific hit check
|
||||
if filter.Before != "" && b == false {
|
||||
b = aph.ID == filter.Before
|
||||
if !start {
|
||||
start = aph.ID == filter.Before
|
||||
|
||||
if start {
|
||||
i = 1
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func encodeRoutePath(p string) string {
|
||||
|
||||
Reference in New Issue
Block a user