add plugin start log in sdk (#1831)

This commit is contained in:
rinfx
2025-03-03 15:37:23 +08:00
committed by GitHub
parent 4f1901586a
commit 988e2c1fa7
2 changed files with 17 additions and 4 deletions

View File

@@ -32,6 +32,7 @@ const (
CustomLogKey = "custom_log"
AILogKey = "ai_log"
TraceSpanTagPrefix = "trace_span_tag."
PluginIDKey = "_plugin_id_"
)
type HttpContext interface {
@@ -255,7 +256,7 @@ func parseEmptyPluginConfig[PluginConfig any](gjson.Result, *PluginConfig, Log)
}
func NewCommonVmCtx[PluginConfig any](pluginName string, options ...CtxOption[PluginConfig]) *CommonVmCtx[PluginConfig] {
logger := &DefaultLog{pluginName}
logger := &DefaultLog{pluginName, "nil"}
opts := append([]CtxOption[PluginConfig]{WithLogger[PluginConfig](logger)}, options...)
return NewCommonVmCtxWithOptions(pluginName, opts...)
}
@@ -314,7 +315,10 @@ func (ctx *CommonPluginCtx[PluginConfig]) OnPluginStart(int) types.OnPluginStart
}
jsonData = gjson.ParseBytes(data)
}
pluginID := jsonData.Get(PluginIDKey).String()
if pluginID != "" {
ctx.vm.log.resetID(pluginID)
}
var parseOverrideConfig func(gjson.Result, PluginConfig, *PluginConfig) error
if ctx.vm.parseRuleConfig != nil {
parseOverrideConfig = func(js gjson.Result, global PluginConfig, cfg *PluginConfig) error {
@@ -329,15 +333,18 @@ func (ctx *CommonPluginCtx[PluginConfig]) OnPluginStart(int) types.OnPluginStart
)
if err != nil {
ctx.vm.log.Warnf("parse rule config failed: %v", err)
ctx.vm.log.Error("plugin start failed")
return types.OnPluginStartStatusFailed
}
if globalOnTickFuncs != nil {
ctx.onTickFuncs = globalOnTickFuncs
if err := proxywasm.SetTickPeriodMilliSeconds(100); err != nil {
ctx.vm.log.Error("SetTickPeriodMilliSeconds failed, onTick functions will not take effect.")
ctx.vm.log.Error("plugin start failed")
return types.OnPluginStartStatusFailed
}
}
ctx.vm.log.Error("plugin start successfully")
return types.OnPluginStartStatusOK
}