Skip to content

Commit

Permalink
filtermanager: enrich log for handleAction
Browse files Browse the repository at this point in the history
Signed-off-by: spacewander <[email protected]>
  • Loading branch information
spacewander committed Dec 16, 2024
1 parent a065daf commit 66221de
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
42 changes: 42 additions & 0 deletions api/pkg/filtermanager/api/phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

package api

import (
"fmt"
"strings"
)

type Phase int

const (
Expand All @@ -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":
Expand Down
10 changes: 7 additions & 3 deletions api/pkg/filtermanager/filtermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
}
Expand Down

0 comments on commit 66221de

Please sign in to comment.