mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-07 23:41:22 +08:00
change: report event for initializing plugin
This commit is contained in:
@@ -10,7 +10,6 @@ import (
|
|||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
"github.com/httprunner/hrp/internal/ga"
|
|
||||||
pluginHost "github.com/httprunner/hrp/plugin/host"
|
pluginHost "github.com/httprunner/hrp/plugin/host"
|
||||||
pluginShared "github.com/httprunner/hrp/plugin/shared"
|
pluginShared "github.com/httprunner/hrp/plugin/shared"
|
||||||
)
|
)
|
||||||
@@ -30,31 +29,19 @@ type Plugin interface {
|
|||||||
Quit() error // quit plugin
|
Quit() error // quit plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
// goPlugin implements golang official plugin
|
// GoPlugin implements golang official plugin
|
||||||
type goPlugin struct {
|
type GoPlugin struct {
|
||||||
*plugin.Plugin
|
*plugin.Plugin
|
||||||
cachedFunctions map[string]reflect.Value // cache loaded functions to improve performance
|
cachedFunctions map[string]reflect.Value // cache loaded functions to improve performance
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *goPlugin) Init(path string) error {
|
func (p *GoPlugin) Init(path string) error {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
log.Warn().Msg("go plugin does not support windows")
|
log.Warn().Msg("go plugin does not support windows")
|
||||||
return fmt.Errorf("go plugin does not support windows")
|
return fmt.Errorf("go plugin does not support windows")
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
// report event for loading go plugin
|
|
||||||
defer func() {
|
|
||||||
event := ga.EventTracking{
|
|
||||||
Category: "LoadGoPlugin",
|
|
||||||
Action: "plugin.Open",
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
event.Value = 1 // failed
|
|
||||||
}
|
|
||||||
go ga.SendEvent(event)
|
|
||||||
}()
|
|
||||||
|
|
||||||
p.Plugin, err = plugin.Open(path)
|
p.Plugin, err = plugin.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Str("path", path).Msg("load go plugin failed")
|
log.Error().Err(err).Str("path", path).Msg("load go plugin failed")
|
||||||
@@ -66,7 +53,7 @@ func (p *goPlugin) Init(path string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *goPlugin) Has(funcName string) bool {
|
func (p *GoPlugin) Has(funcName string) bool {
|
||||||
fn, ok := p.cachedFunctions[funcName]
|
fn, ok := p.cachedFunctions[funcName]
|
||||||
if ok {
|
if ok {
|
||||||
return fn.IsValid()
|
return fn.IsValid()
|
||||||
@@ -89,7 +76,7 @@ func (p *goPlugin) Has(funcName string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *goPlugin) Call(funcName string, args ...interface{}) (interface{}, error) {
|
func (p *GoPlugin) Call(funcName string, args ...interface{}) (interface{}, error) {
|
||||||
if !p.Has(funcName) {
|
if !p.Has(funcName) {
|
||||||
return nil, fmt.Errorf("function %s not found", funcName)
|
return nil, fmt.Errorf("function %s not found", funcName)
|
||||||
}
|
}
|
||||||
@@ -97,18 +84,18 @@ func (p *goPlugin) Call(funcName string, args ...interface{}) (interface{}, erro
|
|||||||
return CallFunc(fn, args...)
|
return CallFunc(fn, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *goPlugin) Quit() error {
|
func (p *GoPlugin) Quit() error {
|
||||||
// no need to quit for go plugin
|
// no need to quit for go plugin
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// hashicorpPlugin implements hashicorp/go-plugin
|
// HashicorpPlugin implements hashicorp/go-plugin
|
||||||
type hashicorpPlugin struct {
|
type HashicorpPlugin struct {
|
||||||
pluginShared.FuncCaller
|
pluginShared.FuncCaller
|
||||||
cachedFunctions map[string]bool // cache loaded functions to improve performance
|
cachedFunctions map[string]bool // cache loaded functions to improve performance
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *hashicorpPlugin) Init(path string) error {
|
func (p *HashicorpPlugin) Init(path string) error {
|
||||||
|
|
||||||
f, err := pluginHost.Init(path)
|
f, err := pluginHost.Init(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -122,7 +109,7 @@ func (p *hashicorpPlugin) Init(path string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *hashicorpPlugin) Has(funcName string) bool {
|
func (p *HashicorpPlugin) Has(funcName string) bool {
|
||||||
flag, ok := p.cachedFunctions[funcName]
|
flag, ok := p.cachedFunctions[funcName]
|
||||||
if ok {
|
if ok {
|
||||||
return flag
|
return flag
|
||||||
@@ -144,11 +131,11 @@ func (p *hashicorpPlugin) Has(funcName string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *hashicorpPlugin) Call(funcName string, args ...interface{}) (interface{}, error) {
|
func (p *HashicorpPlugin) Call(funcName string, args ...interface{}) (interface{}, error) {
|
||||||
return p.FuncCaller.Call(funcName, args...)
|
return p.FuncCaller.Call(funcName, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *hashicorpPlugin) Quit() error {
|
func (p *HashicorpPlugin) Quit() error {
|
||||||
// kill hashicorp plugin process
|
// kill hashicorp plugin process
|
||||||
pluginHost.Quit()
|
pluginHost.Quit()
|
||||||
return nil
|
return nil
|
||||||
@@ -165,7 +152,7 @@ func Init(path string) (Plugin, error) {
|
|||||||
pluginPath, err := locateFile(path, hashicorpGoPluginFile)
|
pluginPath, err := locateFile(path, hashicorpGoPluginFile)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// found hashicorp go plugin file
|
// found hashicorp go plugin file
|
||||||
plugin = &hashicorpPlugin{}
|
plugin = &HashicorpPlugin{}
|
||||||
err = plugin.Init(pluginPath)
|
err = plugin.Init(pluginPath)
|
||||||
return plugin, err
|
return plugin, err
|
||||||
}
|
}
|
||||||
@@ -174,7 +161,7 @@ func Init(path string) (Plugin, error) {
|
|||||||
pluginPath, err = locateFile(path, goPluginFile)
|
pluginPath, err = locateFile(path, goPluginFile)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// found go plugin file
|
// found go plugin file
|
||||||
plugin = &goPlugin{}
|
plugin = &GoPlugin{}
|
||||||
err = plugin.Init(pluginPath)
|
err = plugin.Init(pluginPath)
|
||||||
return plugin, err
|
return plugin, err
|
||||||
}
|
}
|
||||||
|
|||||||
21
runner.go
21
runner.go
@@ -506,6 +506,27 @@ func (r *caseRunner) parseConfig(config IConfig) error {
|
|||||||
|
|
||||||
// init plugin
|
// init plugin
|
||||||
var err error
|
var err error
|
||||||
|
defer func() {
|
||||||
|
if r.parser.plugin == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var pluginType string
|
||||||
|
if _, ok := r.parser.plugin.(*common.GoPlugin); ok {
|
||||||
|
pluginType = "go"
|
||||||
|
} else {
|
||||||
|
pluginType = "hashicorp"
|
||||||
|
}
|
||||||
|
|
||||||
|
// report event for initializing plugin
|
||||||
|
event := ga.EventTracking{
|
||||||
|
Category: "InitPlugin",
|
||||||
|
Action: fmt.Sprintf("Init %s plugin", pluginType),
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
event.Value = 1 // failed
|
||||||
|
}
|
||||||
|
go ga.SendEvent(event)
|
||||||
|
}()
|
||||||
r.parser.plugin, err = common.Init(cfg.Path)
|
r.parser.plugin, err = common.Init(cfg.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user