mirror of
https://github.com/alibaba/higress.git
synced 2026-03-11 04:00:49 +08:00
add plugin start log in sdk (#1831)
This commit is contained in:
@@ -44,10 +44,12 @@ type Log interface {
|
||||
Errorf(format string, args ...interface{})
|
||||
Critical(msg string)
|
||||
Criticalf(format string, args ...interface{})
|
||||
resetID(pluginID string)
|
||||
}
|
||||
|
||||
type DefaultLog struct {
|
||||
pluginName string
|
||||
pluginID string
|
||||
}
|
||||
|
||||
func (l *DefaultLog) log(level LogLevel, msg string) {
|
||||
@@ -56,7 +58,7 @@ func (l *DefaultLog) log(level LogLevel, msg string) {
|
||||
if requestID == "" {
|
||||
requestID = "nil"
|
||||
}
|
||||
msg = fmt.Sprintf("[%s] [%s] %s", l.pluginName, requestID, msg)
|
||||
msg = fmt.Sprintf("[%s] [%s] [%s] %s", l.pluginName, l.pluginID, requestID, msg)
|
||||
switch level {
|
||||
case LogLevelTrace:
|
||||
proxywasm.LogTrace(msg)
|
||||
@@ -79,7 +81,7 @@ func (l *DefaultLog) logFormat(level LogLevel, format string, args ...interface{
|
||||
if requestID == "" {
|
||||
requestID = "nil"
|
||||
}
|
||||
format = fmt.Sprintf("[%s] [%s] %s", l.pluginName, requestID, format)
|
||||
format = fmt.Sprintf("[%s] [%s] [%s] %s", l.pluginName, l.pluginID, requestID, format)
|
||||
switch level {
|
||||
case LogLevelTrace:
|
||||
proxywasm.LogTracef(format, args...)
|
||||
@@ -143,3 +145,7 @@ func (l *DefaultLog) Critical(msg string) {
|
||||
func (l *DefaultLog) Criticalf(format string, args ...interface{}) {
|
||||
l.logFormat(LogLevelCritical, format, args...)
|
||||
}
|
||||
|
||||
func (l *DefaultLog) resetID(pluginID string) {
|
||||
l.pluginID = pluginID
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user