From 66221de8c225f87fe85f148257da6d00c04a8e92 Mon Sep 17 00:00:00 2001 From: spacewander Date: Mon, 16 Dec 2024 19:33:55 +0800 Subject: [PATCH] filtermanager: enrich log for handleAction Signed-off-by: spacewander --- api/pkg/filtermanager/api/phase.go | 42 ++++++++++++++++++++++++++ api/pkg/filtermanager/filtermanager.go | 10 ++++-- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/api/pkg/filtermanager/api/phase.go b/api/pkg/filtermanager/api/phase.go index 2fdcaad57..f5a51d1d3 100644 --- a/api/pkg/filtermanager/api/phase.go +++ b/api/pkg/filtermanager/api/phase.go @@ -14,6 +14,11 @@ package api +import ( + "fmt" + "strings" +) + type Phase int const ( @@ -37,6 +42,43 @@ func (p Phase) Contains(phases Phase) bool { return p&phases == phases } +func (p Phase) String() string { + var names []string + + if p&PhaseDecodeHeaders != 0 { + names = append(names, "PhaseDecodeHeaders") + } + if p&PhaseDecodeData != 0 { + names = append(names, "PhaseDecodeData") + } + if p&PhaseDecodeTrailers != 0 { + names = append(names, "PhaseDecodeTrailers") + } + if p&PhaseDecodeRequest != 0 { + names = append(names, "PhaseDecodeRequest") + } + if p&PhaseEncodeHeaders != 0 { + names = append(names, "PhaseEncodeHeaders") + } + if p&PhaseEncodeData != 0 { + names = append(names, "PhaseEncodeData") + } + if p&PhaseEncodeTrailers != 0 { + names = append(names, "PhaseEncodeTrailers") + } + if p&PhaseEncodeResponse != 0 { + names = append(names, "PhaseEncodeResponse") + } + if p&PhaseOnLog != 0 { + names = append(names, "PhaseOnLog") + } + + if len(names) == 0 { + return fmt.Sprintf("Phase(%d)", p) + } + return strings.Join(names, " | ") +} + func MethodToPhase(meth string) Phase { switch meth { case "DecodeHeaders": diff --git a/api/pkg/filtermanager/filtermanager.go b/api/pkg/filtermanager/filtermanager.go index 5701e20f1..d4d2c6612 100644 --- a/api/pkg/filtermanager/filtermanager.go +++ b/api/pkg/filtermanager/filtermanager.go @@ -247,13 +247,17 @@ func FilterManagerFactory(c interface{}, cb capi.FilterCallbackHandler) (streamF return wrapFilterManager(fm) } -func (m *filterManager) recordLocalReplyPluginName(name string) { +func (m *filterManager) recordLocalReplyPluginName(name string, code int) { // We can get the plugin name which returns the local response from the dynamic metadata. // For example, use %DYNAMIC_METADATA(htnn:local_reply_plugin_name)% in the access log format. m.callbacks.StreamInfo().DynamicMetadata().Set("htnn", "local_reply_plugin_name", name) // For now, we don't record when the local reply is caused by panic. As we can't always get // the name of plugin which is the root of the panic correctly. For example, consider a plugin kicks // off a goroutine and the goroutine panics. + + // Also log it in the application log. In some situation, multiple plugins may send local reply. + // Via the application log, we can know all the calls. + api.LogInfof("local reply from plugin: %s, code: %d", name, code) } func (m *filterManager) handleAction(res api.ResultAction, phase api.Phase, filter *model.FilterWrapper) (needReturn bool) { @@ -274,11 +278,11 @@ func (m *filterManager) handleAction(res api.ResultAction, phase api.Phase, filt switch v := res.(type) { case *api.LocalResponse: - m.recordLocalReplyPluginName(filter.Name) + m.recordLocalReplyPluginName(filter.Name, v.Code) m.localReply(v, phase < api.PhaseEncodeHeaders) return true default: - api.LogErrorf("unknown result action: %+v", v) + api.LogErrorf("unknown result action: %+v returned from %s in phase %s", v, filter.Name, phase) return false } }