-
Notifications
You must be signed in to change notification settings - Fork 110
/
registry.go
77 lines (66 loc) · 2.27 KB
/
registry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package data
import (
"time"
"github.com/edaniels/golog"
"github.com/mitchellh/copystructure"
"github.com/pkg/errors"
"google.golang.org/protobuf/types/known/anypb"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/services/datamanager/datacapture"
)
// CollectorConstructor contains a function for constructing an instance of a Collector.
type CollectorConstructor func(resource interface{}, params CollectorParams) (Collector, error)
// CollectorParams contain the parameters needed to construct a Collector.
type CollectorParams struct {
ComponentName string
Interval time.Duration
MethodParams map[string]*anypb.Any
Target *datacapture.File
QueueSize int
BufferSize int
Logger golog.Logger
}
// Validate validates that p contains all required parameters.
func (p CollectorParams) Validate() error {
if p.Target == nil {
return errors.New("missing required parameter target")
}
if p.Logger == nil {
return errors.New("missing required parameter logger")
}
if p.ComponentName == "" {
return errors.New("missing required parameter component name")
}
return nil
}
// MethodMetadata contains the metadata identifying a component method that we are going to capture and collect.
type MethodMetadata struct {
Subtype resource.SubtypeName
MethodName string
}
var collectorRegistry = map[MethodMetadata]CollectorConstructor{}
// RegisterCollector registers a Collector to its corresponding MethodMetadata.
func RegisterCollector(method MethodMetadata, c CollectorConstructor) {
_, old := collectorRegistry[method]
if old {
panic(errors.Errorf("trying to register two of the same method on the same component: "+
"component %s, method %s", method.Subtype, method.MethodName))
}
collectorRegistry[method] = c
}
// CollectorLookup looks up a Collector by the given MethodMetadata. nil is returned if
// there is None.
func CollectorLookup(method MethodMetadata) *CollectorConstructor {
if registration, ok := RegisteredCollectors()[method]; ok {
return ®istration
}
return nil
}
// RegisteredCollectors returns a copy of the registry.
func RegisteredCollectors() map[MethodMetadata]CollectorConstructor {
copied, err := copystructure.Copy(collectorRegistry)
if err != nil {
panic(err)
}
return copied.(map[MethodMetadata]CollectorConstructor)
}