Skip to content

Commit 6a209ad

Browse files
committed
support expose metrics for plugins
1 parent 982b45c commit 6a209ad

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

api/pkg/filtermanager/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ func (p *FilterManagerConfigParser) Parse(any *anypb.Any, callbacks capi.ConfigC
216216

217217
for _, proto := range plugins {
218218
name := proto.Name
219+
220+
if registerMetrics := pkgPlugins.LoadMetricsCallback(name); registerMetrics != nil {
221+
api.LogInfof("register metrics for plugin %s", name)
222+
registerMetrics(callbacks)
223+
}
224+
219225
if plugin := pkgPlugins.LoadHTTPFilterFactoryAndParser(name); plugin != nil {
220226
config, err := plugin.ConfigParser.Parse(proto.Config)
221227
if err != nil {

api/pkg/plugins/plugins.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"errors"
2121
"runtime/debug"
2222

23+
capi "github.com/envoyproxy/envoy/contrib/golang/common/go/api"
2324
"mosn.io/htnn/api/internal/proto"
2425
"mosn.io/htnn/api/pkg/filtermanager/api"
2526
"mosn.io/htnn/api/pkg/log"
@@ -31,6 +32,7 @@ var (
3132
pluginTypes = map[string]Plugin{}
3233
plugins = map[string]Plugin{}
3334
httpFilterFactoryAndParser = map[string]*FilterFactoryAndParser{}
35+
metricsRegister = map[string]func(capi.ConfigCallbacks){}
3436
)
3537

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

193+
func RegisterMetricsCallback(pluginName string, registerMetricFunc func(capi.ConfigCallbacks)) {
194+
logger.Info("register metrics for plugin", "name", pluginName)
195+
metricsRegister[pluginName] = registerMetricFunc
196+
}
197+
198+
func LoadMetricsCallback(pluginName string) func(capi.ConfigCallbacks) {
199+
return metricsRegister[pluginName]
200+
}
201+
191202
// PluginMethodDefaultImpl provides reasonable implementation for optional methods
192203
type PluginMethodDefaultImpl struct{}
193204

api/pkg/plugins/plugins_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/stretchr/testify/assert"
2424
"google.golang.org/protobuf/types/known/anypb"
2525

26+
capi "github.com/envoyproxy/envoy/contrib/golang/common/go/api"
2627
"mosn.io/htnn/api/pkg/filtermanager/api"
2728
_ "mosn.io/htnn/api/plugins/tests/pkg/envoy" // for log implementation
2829
)
@@ -285,3 +286,8 @@ func TestRegisterPluginWithType(t *testing.T) {
285286
assert.NotNil(t, LoadPlugin("mock"))
286287
assert.NotNil(t, LoadPluginType("mock"))
287288
}
289+
290+
func TestRegisterPluginMetrics(t *testing.T) {
291+
RegisterMetricsCallback("mock", func(cc capi.ConfigCallbacks) {})
292+
assert.NotNil(t, LoadMetricsCallback("mock"))
293+
}

api/tests/integration/test_plugins.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strconv"
2222
"strings"
2323

24+
capi "github.com/envoyproxy/envoy/contrib/golang/common/go/api"
2425
"mosn.io/htnn/api/pkg/filtermanager/api"
2526
"mosn.io/htnn/api/pkg/plugins"
2627
)
@@ -200,6 +201,7 @@ func (p *bufferPlugin) Factory() api.FilterFactory {
200201
type localReplyPlugin struct {
201202
plugins.PluginMethodDefaultImpl
202203
basePlugin
204+
usageCounter capi.CounterMetric
203205
}
204206

205207
func localReplyFactory(c interface{}, callbacks api.FilterCallbackHandler) api.Filter {
@@ -242,6 +244,7 @@ func (f *localReplyFilter) DecodeRequest(headers api.RequestHeaderMap, buf api.B
242244
f.reqHdr = headers
243245
f.runFilters = headers.Values("run")
244246
if f.config.Decode {
247+
lrp.usageCounter.Increment(1)
245248
return f.NewLocalResponse("reply", true)
246249
}
247250
return api.Continue
@@ -309,6 +312,11 @@ func (p *localReplyPlugin) Factory() api.FilterFactory {
309312
return localReplyFactory
310313
}
311314

315+
func (p *localReplyPlugin) MetricsDefinition(c capi.ConfigCallbacks) {
316+
p.usageCounter = c.DefineCounterMetric("localreply.usage.counter")
317+
// Define more metrics here
318+
}
319+
312320
type badPlugin struct {
313321
plugins.PluginMethodDefaultImpl
314322
}
@@ -619,10 +627,12 @@ func (f *onLogFilter) OnLog(reqHeaders api.RequestHeaderMap, reqTrailers api.Req
619627
api.LogWarnf("receive request trailers: %+v", trailers)
620628
}
621629

630+
var lrp = &localReplyPlugin{}
631+
622632
func init() {
623633
plugins.RegisterPlugin("stream", &streamPlugin{})
624634
plugins.RegisterPlugin("buffer", &bufferPlugin{})
625-
plugins.RegisterPlugin("localReply", &localReplyPlugin{})
635+
plugins.RegisterPlugin("localReply", lrp)
626636
plugins.RegisterPlugin("bad", &badPlugin{})
627637
plugins.RegisterPlugin("consumer", &consumerPlugin{})
628638
plugins.RegisterPlugin("init", &initPlugin{})

0 commit comments

Comments
 (0)