From ee4c1e7a6513da2040e8b632e7de21c62522bc7f Mon Sep 17 00:00:00 2001 From: spacewander Date: Fri, 25 Oct 2024 17:06:05 +0800 Subject: [PATCH] debugmode: reduce the data we collect Signed-off-by: spacewander --- api/pkg/filtermanager/model/model.go | 2 +- api/pkg/filtermanager/wrapper.go | 4 ++-- api/pkg/filtermanager/wrapper_test.go | 7 ++++--- plugins/plugins/debugmode/filter.go | 11 ++++------- plugins/tests/integration/debug_mode_test.go | 6 +++--- 5 files changed, 14 insertions(+), 16 deletions(-) diff --git a/api/pkg/filtermanager/model/model.go b/api/pkg/filtermanager/model/model.go index 0ae4bf89..008ee258 100644 --- a/api/pkg/filtermanager/model/model.go +++ b/api/pkg/filtermanager/model/model.go @@ -51,5 +51,5 @@ func NewFilterWrapper(name string, f api.Filter) *FilterWrapper { type ExecutionRecord struct { PluginName string - Record map[string]time.Duration + Record time.Duration } diff --git a/api/pkg/filtermanager/wrapper.go b/api/pkg/filtermanager/wrapper.go index 570195c8..0778c56f 100644 --- a/api/pkg/filtermanager/wrapper.go +++ b/api/pkg/filtermanager/wrapper.go @@ -143,13 +143,13 @@ func (f *debugFilter) recordExecution(start time.Time, method string) { } for _, record := range records { if record.PluginName == f.name { - record.Record[method] += duration + record.Record += duration return } } f.callbacks.PluginState().Set("debugMode", "executionRecords", append(records, model.ExecutionRecord{ PluginName: f.name, - Record: map[string]time.Duration{method: duration}, + Record: duration, })) } diff --git a/api/pkg/filtermanager/wrapper_test.go b/api/pkg/filtermanager/wrapper_test.go index 55604d96..5cf8bccc 100644 --- a/api/pkg/filtermanager/wrapper_test.go +++ b/api/pkg/filtermanager/wrapper_test.go @@ -39,9 +39,10 @@ func TestDebugFilter(t *testing.T) { t.Logf("get records %+v\n", records) // for debug when test failed assert.Equal(t, 2, len(records)) assert.Equal(t, "two", records[0].PluginName) - assert.True(t, records[0].Record["DecodeHeaders"] > 0) + assert.True(t, records[0].Record > 0) assert.Equal(t, "one", records[1].PluginName) - assert.True(t, records[1].Record["DecodeHeaders"] > 0) + assert.True(t, records[1].Record > 0) + decodeHeadersCost := records[1].Record patches := gomonkey.ApplyMethodFunc(raw1, "DecodeData", func(data api.BufferInstance, endStream bool) api.ResultAction { time.Sleep(100 * time.Millisecond) @@ -58,6 +59,6 @@ func TestDebugFilter(t *testing.T) { assert.Equal(t, "one", records[1].PluginName) // Should be the sum of multiple calls delta := 10 * time.Millisecond - rec := records[1].Record["DecodeData"] + rec := records[1].Record - decodeHeadersCost assert.True(t, 200*time.Millisecond-delta < rec && rec < 200*time.Millisecond+delta, rec) } diff --git a/plugins/plugins/debugmode/filter.go b/plugins/plugins/debugmode/filter.go index 4e3bfd05..e31c1681 100644 --- a/plugins/plugins/debugmode/filter.go +++ b/plugins/plugins/debugmode/filter.go @@ -38,8 +38,8 @@ type filter struct { } type executionPlugin struct { - Name string `json:"name"` - PerPhaseCostSeconds map[string]float64 `json:"per_phase_cost_seconds"` + Name string `json:"name"` + CostSeconds float64 `json:"cost_seconds"` } type SlowLogReport struct { @@ -107,11 +107,8 @@ func (f *filter) OnLog(reqHeaders api.RequestHeaderMap, reqTrailers api.RequestT executionRecords := r.([]model.ExecutionRecord) for _, record := range executionRecords { p := executionPlugin{ - Name: record.PluginName, - } - p.PerPhaseCostSeconds = make(map[string]float64) - for k, v := range record.Record { - p.PerPhaseCostSeconds[k] = v.Seconds() + Name: record.PluginName, + CostSeconds: record.Record.Seconds(), } report.ExecutedPlugins = append(report.ExecutedPlugins, p) } diff --git a/plugins/tests/integration/debug_mode_test.go b/plugins/tests/integration/debug_mode_test.go index 2bf15e16..dd3b4a49 100644 --- a/plugins/tests/integration/debug_mode_test.go +++ b/plugins/tests/integration/debug_mode_test.go @@ -31,7 +31,7 @@ func TestDebugModeSlowLog(t *testing.T) { dp, err := dataplane.StartDataPlane(t, &dataplane.Option{ NoErrorLogCheck: true, ExpectLogPattern: []string{ - `slow log report:.+"executed_plugins":\[.+"name":"limitReq","per_phase_cost_seconds":\{"DecodeHeaders":.+`, + `slow log report:.+"executed_plugins":\[.+"name":"limitReq","cost_seconds":.+`, }, }) if err != nil { @@ -75,7 +75,7 @@ func TestDebugModeSlowLogNoPlugin(t *testing.T) { dp, err := dataplane.StartDataPlane(t, &dataplane.Option{ NoErrorLogCheck: true, ExpectNoLogPattern: []string{ - `slow log report:.+"executed_plugins":\[.+"name":"limitReq","per_phase_cost_seconds":\{"DecodeHeaders":.+`, + `slow log report:.+"executed_plugins":\[.+"name":"limitReq","cost_seconds":.+`, }, ExpectLogPattern: []string{ `slow log report:.+"server":\["envoy"\]`, @@ -196,7 +196,7 @@ func TestDebugModeSlowLogWithFiltersFromConsumer(t *testing.T) { LogLevel: "debug", NoErrorLogCheck: true, ExpectLogPattern: []string{ - `slow log report:.+"executed_plugins":\[.+"name":"limitReq","per_phase_cost_seconds":\{"DecodeHeaders":.+`, + `slow log report:.+"executed_plugins":\[.+"name":"limitReq","cost_seconds":.+`, }, }) if err != nil {