Skip to content

Commit

Permalink
support expose metrics for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
wonderflow committed Dec 18, 2024
1 parent 982b45c commit d581790
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
6 changes: 6 additions & 0 deletions api/pkg/filtermanager/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ func (p *FilterManagerConfigParser) Parse(any *anypb.Any, callbacks capi.ConfigC

for _, proto := range plugins {
name := proto.Name

if registerMetrics := pkgPlugins.LoadMetricsCallback(name); registerMetrics != nil {
api.LogInfof("register metrics for plugin %s", name)
registerMetrics(callbacks)
}

if plugin := pkgPlugins.LoadHTTPFilterFactoryAndParser(name); plugin != nil {
config, err := plugin.ConfigParser.Parse(proto.Config)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions api/pkg/plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"errors"
"runtime/debug"

capi "github.com/envoyproxy/envoy/contrib/golang/common/go/api"

"mosn.io/htnn/api/internal/proto"
"mosn.io/htnn/api/pkg/filtermanager/api"
"mosn.io/htnn/api/pkg/log"
Expand All @@ -31,6 +33,7 @@ var (
pluginTypes = map[string]Plugin{}
plugins = map[string]Plugin{}
httpFilterFactoryAndParser = map[string]*FilterFactoryAndParser{}
metricsRegister = map[string]func(capi.ConfigCallbacks){}
)

// Here we introduce extra struct to avoid cyclic import between pkg/filtermanager and pkg/plugins
Expand Down Expand Up @@ -188,6 +191,15 @@ func (cp *PluginConfigParser) Parse(any interface{}) (res interface{}, err error
return conf, nil
}

func RegisterMetricsCallback(pluginName string, registerMetricFunc func(capi.ConfigCallbacks)) {
logger.Info("register metrics for plugin", "name", pluginName)
metricsRegister[pluginName] = registerMetricFunc
}

func LoadMetricsCallback(pluginName string) func(capi.ConfigCallbacks) {
return metricsRegister[pluginName]
}

// PluginMethodDefaultImpl provides reasonable implementation for optional methods
type PluginMethodDefaultImpl struct{}

Expand Down
6 changes: 6 additions & 0 deletions api/pkg/plugins/plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"github.com/agiledragon/gomonkey/v2"
capi "github.com/envoyproxy/envoy/contrib/golang/common/go/api"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/anypb"

Expand Down Expand Up @@ -285,3 +286,8 @@ func TestRegisterPluginWithType(t *testing.T) {
assert.NotNil(t, LoadPlugin("mock"))
assert.NotNil(t, LoadPluginType("mock"))
}

func TestRegisterPluginMetrics(t *testing.T) {
RegisterMetricsCallback("mock", func(cc capi.ConfigCallbacks) {})
assert.NotNil(t, LoadMetricsCallback("mock"))
}
13 changes: 12 additions & 1 deletion api/tests/integration/test_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"strconv"
"strings"

capi "github.com/envoyproxy/envoy/contrib/golang/common/go/api"

"mosn.io/htnn/api/pkg/filtermanager/api"
"mosn.io/htnn/api/pkg/plugins"
)
Expand Down Expand Up @@ -200,6 +202,7 @@ func (p *bufferPlugin) Factory() api.FilterFactory {
type localReplyPlugin struct {
plugins.PluginMethodDefaultImpl
basePlugin
usageCounter capi.CounterMetric
}

func localReplyFactory(c interface{}, callbacks api.FilterCallbackHandler) api.Filter {
Expand Down Expand Up @@ -242,6 +245,7 @@ func (f *localReplyFilter) DecodeRequest(headers api.RequestHeaderMap, buf api.B
f.reqHdr = headers
f.runFilters = headers.Values("run")
if f.config.Decode {
lrp.usageCounter.Increment(1)
return f.NewLocalResponse("reply", true)
}
return api.Continue
Expand Down Expand Up @@ -309,6 +313,11 @@ func (p *localReplyPlugin) Factory() api.FilterFactory {
return localReplyFactory
}

func (p *localReplyPlugin) MetricsDefinition(c capi.ConfigCallbacks) {
p.usageCounter = c.DefineCounterMetric("localreply.usage.counter")
// Define more metrics here
}

type badPlugin struct {
plugins.PluginMethodDefaultImpl
}
Expand Down Expand Up @@ -619,10 +628,12 @@ func (f *onLogFilter) OnLog(reqHeaders api.RequestHeaderMap, reqTrailers api.Req
api.LogWarnf("receive request trailers: %+v", trailers)
}

var lrp = &localReplyPlugin{}

func init() {
plugins.RegisterPlugin("stream", &streamPlugin{})
plugins.RegisterPlugin("buffer", &bufferPlugin{})
plugins.RegisterPlugin("localReply", &localReplyPlugin{})
plugins.RegisterPlugin("localReply", lrp)
plugins.RegisterPlugin("bad", &badPlugin{})
plugins.RegisterPlugin("consumer", &consumerPlugin{})
plugins.RegisterPlugin("init", &initPlugin{})
Expand Down

0 comments on commit d581790

Please sign in to comment.