Skip to content

Commit

Permalink
refact(exporter): optimize code
Browse files Browse the repository at this point in the history
Signed-off-by: xiayu.lyt <[email protected]>
  • Loading branch information
Lyt99 committed Oct 10, 2023
1 parent a2bcd07 commit d7f7764
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 39 deletions.
32 changes: 12 additions & 20 deletions pkg/exporter/probe/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,47 @@ import (
)

var (
availableEventProbe = make(map[string]*EventProbeCreator)
availableEventProbe = make(map[string]*eventProbeCreator)
)

// type EventProbeCreator func(sink chan<- *Event, args map[string]interface{}) (EventProbe, error)
type EventProbeCreator struct {
type eventProbeCreator struct {
f reflect.Value
s *reflect.Type
}

func NewEventProbeCreator(creator interface{}) (*EventProbeCreator, error) {
func newEventProbeCreator(creator interface{}) (*eventProbeCreator, error) {
t := reflect.TypeOf(creator)
if t.Kind() != reflect.Func {
return nil, fmt.Errorf("metric probe creator %#v is not a func", creator)
}

err := validateProbeCreatorReturnValue[EventProbe](reflect.TypeOf(creator))
err := validateProbeCreatorReturnValue[EventProbe](t)
if err != nil {
return nil, err
}

if t.NumIn() != 1 && t.NumIn() != 2 {
return nil, fmt.Errorf("input parameter count of creator should be either 2 or 3")
return nil, fmt.Errorf("input parameter count of creator should be either 1 or 2")
}

ct := t.In(0)
et := reflect.TypeOf((*Event)(nil))
if ct.Kind() != reflect.Chan || ct.ChanDir() != reflect.SendDir || ct.Elem() != et {
return nil, fmt.Errorf("first input parameter should be a send channel of *Event")
return nil, fmt.Errorf("first input parameter type should be chan<- *Event")
}

ret := &EventProbeCreator{
ret := &eventProbeCreator{
f: reflect.ValueOf(creator),
}

if t.NumIn() == 2 {
st := t.In(1)
if st.Kind() != reflect.Struct && st.Kind() != reflect.Map {
return nil, fmt.Errorf("input parameter should be struct, but %s", st.Kind())
}
if st.Kind() == reflect.Map && st.Key().Kind() != reflect.String {
return nil, fmt.Errorf("map key type of input parameter should be string")
if err := validateParamTypeMapOrStruct(st); err != nil {
return nil, err
}
ret.s = &st
}

return ret, nil
}

func (e *EventProbeCreator) Call(sink chan<- *Event, args map[string]interface{}) (EventProbe, error) {
func (e *eventProbeCreator) Call(sink chan<- *Event, args map[string]interface{}) (EventProbe, error) {
in := []reflect.Value{
reflect.ValueOf(sink),
}
Expand All @@ -70,7 +62,7 @@ func (e *EventProbeCreator) Call(sink chan<- *Event, args map[string]interface{}
}

result := e.f.Call(in)
// return parameter count and type has been checked in NewEventProbeCreator
// return parameter count and type has been checked in newEventProbeCreator
ret := result[0].Interface().(EventProbe)
err := result[1].Interface()
if err == nil {
Expand Down Expand Up @@ -118,7 +110,7 @@ func MustRegisterEventProbe(name string, creator interface{}) {
panic(fmt.Errorf("duplicated event probe %s", name))
}

c, err := NewEventProbeCreator(creator)
c, err := newEventProbeCreator(creator)
if err != nil {
panic(fmt.Errorf("error register event probe %s: %s", name, err))
}
Expand Down
28 changes: 10 additions & 18 deletions pkg/exporter/probe/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,18 @@ const LegacyMetricsNamespace = "inspector"
const MetricsNamespace = "kubeskoop"

var (
availableMetricsProbes = make(map[string]*MetricsProbeCreator)
availableMetricsProbes = make(map[string]*metricsProbeCreator)
ErrUndeclaredMetrics = errors.New("undeclared metrics")
)

// type MetricsProbeCreator func(args map[string]interface{}) (MetricsProbe, error)
type MetricsProbeCreator struct {
type metricsProbeCreator struct {
f reflect.Value
s *reflect.Type
}

func NewMetricProbeCreator(creator interface{}) (*MetricsProbeCreator, error) {
func newMetricProbeCreator(creator interface{}) (*metricsProbeCreator, error) {
t := reflect.TypeOf(creator)
if t.Kind() != reflect.Func {
return nil, fmt.Errorf("metric probe creator %#v is not a func", creator)
}

err := validateProbeCreatorReturnValue[MetricsProbe](reflect.TypeOf(creator))
err := validateProbeCreatorReturnValue[MetricsProbe](t)
if err != nil {
return nil, err
}
Expand All @@ -40,25 +35,22 @@ func NewMetricProbeCreator(creator interface{}) (*MetricsProbeCreator, error) {
return nil, fmt.Errorf("input parameter count of creator should be either 0 or 1")
}

ret := &MetricsProbeCreator{
ret := &metricsProbeCreator{
f: reflect.ValueOf(creator),
}

if t.NumIn() == 1 {
st := t.In(0)
if st.Kind() != reflect.Struct && st.Kind() != reflect.Map {
return nil, fmt.Errorf("input parameter should be struct, but %s", st.Kind())
}
if st.Kind() == reflect.Map && st.Key().Kind() != reflect.String {
return nil, fmt.Errorf("map key type of input parameter should be string")
if err := validateParamTypeMapOrStruct(st); err != nil {
return nil, err
}
ret.s = &st
}

return ret, nil
}

func (m *MetricsProbeCreator) Call(args map[string]interface{}) (MetricsProbe, error) {
func (m *metricsProbeCreator) Call(args map[string]interface{}) (MetricsProbe, error) {
var in []reflect.Value
if m.s != nil {
s, err := createStructFromTypeWithArgs(*m.s, args)
Expand All @@ -69,7 +61,7 @@ func (m *MetricsProbeCreator) Call(args map[string]interface{}) (MetricsProbe, e
}

result := m.f.Call(in)
// return parameter count and type has been checked in NewMetricProbeCreator
// return parameter count and type has been checked in newMetricProbeCreator
ret := result[0].Interface().(MetricsProbe)
err := result[1].Interface()
if err == nil {
Expand Down Expand Up @@ -116,7 +108,7 @@ func MustRegisterMetricsProbe(name string, creator interface{}) {
panic(fmt.Errorf("duplicated metric probe %s", name))
}

c, err := NewMetricProbeCreator(creator)
c, err := newMetricProbeCreator(creator)
if err != nil {
panic(fmt.Errorf("error register metric probe %s: %s", name, err))
}
Expand Down
16 changes: 15 additions & 1 deletion pkg/exporter/probe/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func validateProbeCreatorReturnValue[T interface{}](t reflect.Type) error {
if t.Kind() != reflect.Func {
return fmt.Errorf("%s is not Func type", t)
return fmt.Errorf("creator %#v is not a func", t)
}

if t.NumOut() != 2 {
Expand All @@ -28,6 +28,20 @@ func validateProbeCreatorReturnValue[T interface{}](t reflect.Type) error {
return nil
}

func validateParamTypeMapOrStruct(t reflect.Type) error {
switch t.Kind() {
case reflect.Struct:
// no need to check
case reflect.Map:
if t.Key().Kind() != reflect.String {
return fmt.Errorf("map key type of input parameter should be string")
}
default:
return fmt.Errorf("input parameter type should be struct, but %s", t.Kind())
}
return nil
}

func createStructFromTypeWithArgs(st reflect.Type, args map[string]interface{}) (reflect.Value, error) {
v := reflect.New(st)
err := mapstructure.Decode(args, v.Interface())
Expand Down

0 comments on commit d7f7764

Please sign in to comment.