-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: HarrisChu <1726587+HarrisChu@users.noreply.github.com> This adds some structure and extracts common functionality for registering and retrieving extension information into a standalone package. Partly based on the work and feedback in #2754.
- Loading branch information
Showing
6 changed files
with
187 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Package ext contains the extension registry and all generic functionality for | ||
// k6 extensions. | ||
package ext |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package ext | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"runtime" | ||
"runtime/debug" | ||
"sort" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
// TODO: Make an ExtensionRegistry? | ||
// | ||
//nolint:gochecknoglobals | ||
var ( | ||
mx sync.RWMutex | ||
extensions = make(map[ExtensionType]map[string]*Extension) | ||
) | ||
|
||
// ExtensionType is the type of all supported k6 extensions. | ||
type ExtensionType uint8 | ||
|
||
// All supported k6 extension types. | ||
const ( | ||
JSExtension ExtensionType = iota + 1 | ||
OutputExtension | ||
) | ||
|
||
func (e ExtensionType) String() string { | ||
var s string | ||
switch e { | ||
case JSExtension: | ||
s = "js" | ||
case OutputExtension: | ||
s = "output" | ||
} | ||
return s | ||
} | ||
|
||
// Extension is a generic container for any k6 extension. | ||
type Extension struct { | ||
Name, Path, Version string | ||
Type ExtensionType | ||
Module interface{} | ||
} | ||
|
||
func (e Extension) String() string { | ||
return fmt.Sprintf("%s %s, %s [%s]", e.Path, e.Version, e.Name, e.Type) | ||
} | ||
|
||
// Register a new extension with the given name and type. This function will | ||
// panic if an unsupported extension type is provided, or if an extension of the | ||
// same type and name is already registered. | ||
func Register(name string, typ ExtensionType, mod interface{}) { | ||
mx.Lock() | ||
defer mx.Unlock() | ||
|
||
exts, ok := extensions[typ] | ||
if !ok { | ||
panic(fmt.Sprintf("unsupported extension type: %T", typ)) | ||
} | ||
|
||
if _, ok := exts[name]; ok { | ||
panic(fmt.Sprintf("extension already registered: %s", name)) | ||
} | ||
|
||
path, version := extractModuleInfo(mod) | ||
|
||
exts[name] = &Extension{ | ||
Name: name, | ||
Type: typ, | ||
Module: mod, | ||
Path: path, | ||
Version: version, | ||
} | ||
} | ||
|
||
// Get returns all extensions of the specified type. | ||
func Get(typ ExtensionType) map[string]*Extension { | ||
mx.RLock() | ||
defer mx.RUnlock() | ||
|
||
exts, ok := extensions[typ] | ||
if !ok { | ||
panic(fmt.Sprintf("unsupported extension type: %T", typ)) | ||
} | ||
|
||
result := make(map[string]*Extension, len(exts)) | ||
|
||
for name, ext := range exts { | ||
result[name] = ext | ||
} | ||
|
||
return result | ||
} | ||
|
||
// GetAll returns all extensions, sorted by their import path and name. | ||
func GetAll() []*Extension { | ||
mx.RLock() | ||
defer mx.RUnlock() | ||
|
||
js, out := extensions[JSExtension], extensions[OutputExtension] | ||
result := make([]*Extension, 0, len(js)+len(out)) | ||
|
||
for _, e := range js { | ||
result = append(result, e) | ||
} | ||
for _, e := range out { | ||
result = append(result, e) | ||
} | ||
|
||
sort.Slice(result, func(i, j int) bool { | ||
if result[i].Path == result[j].Path { | ||
return result[i].Name < result[j].Name | ||
} | ||
return result[i].Path < result[j].Path | ||
}) | ||
|
||
return result | ||
} | ||
|
||
// extractModuleInfo attempts to return the package path and version of the Go | ||
// module that created the given value. | ||
func extractModuleInfo(mod interface{}) (path, version string) { | ||
t := reflect.TypeOf(mod) | ||
|
||
switch t.Kind() { | ||
case reflect.Ptr: | ||
if t.Elem() != nil { | ||
path = t.Elem().PkgPath() | ||
} | ||
case reflect.Func: | ||
path = runtime.FuncForPC(reflect.ValueOf(mod).Pointer()).Name() | ||
default: | ||
return | ||
} | ||
|
||
buildInfo, ok := debug.ReadBuildInfo() | ||
if !ok { | ||
return | ||
} | ||
|
||
for _, dep := range buildInfo.Deps { | ||
depPath := strings.TrimSpace(dep.Path) | ||
if strings.HasPrefix(path, depPath) { | ||
if dep.Replace != nil { | ||
return depPath, dep.Replace.Version | ||
} | ||
return depPath, dep.Version | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
func init() { | ||
extensions[JSExtension] = make(map[string]*Extension) | ||
extensions[OutputExtension] = make(map[string]*Extension) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,12 @@ | ||
package output | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
import "go.k6.io/k6/ext" | ||
|
||
//nolint:gochecknoglobals | ||
var ( | ||
extensions = make(map[string]func(Params) (Output, error)) | ||
mx sync.RWMutex | ||
) | ||
|
||
// GetExtensions returns all registered extensions. | ||
func GetExtensions() map[string]func(Params) (Output, error) { | ||
mx.RLock() | ||
defer mx.RUnlock() | ||
res := make(map[string]func(Params) (Output, error), len(extensions)) | ||
for k, v := range extensions { | ||
res[k] = v | ||
} | ||
return res | ||
} | ||
// Constructor returns an instance of an output extension module. | ||
type Constructor func(Params) (Output, error) | ||
|
||
// RegisterExtension registers the given output extension constructor. This | ||
// function panics if a module with the same name is already registered. | ||
func RegisterExtension(name string, mod func(Params) (Output, error)) { | ||
mx.Lock() | ||
defer mx.Unlock() | ||
|
||
if _, ok := extensions[name]; ok { | ||
panic(fmt.Sprintf("output extension already registered: %s", name)) | ||
} | ||
extensions[name] = mod | ||
func RegisterExtension(name string, c Constructor) { | ||
ext.Register(name, ext.OutputExtension, c) | ||
} |