move to a common pattern on output plugins to catch common issues

This commit is contained in:
Cody Lee
2022-12-05 21:47:42 -06:00
parent 689423d4dd
commit bd51cf59f3
10 changed files with 155 additions and 39 deletions

View File

@@ -24,12 +24,17 @@ type Collect interface {
Outputs() []string
}
type OutputPlugin interface {
Run(Collect) error
Enabled() bool
}
// Output defines the output data for a metric exporter like influx or prometheus.
// Output packages should call NewOutput with this struct in init().
type Output struct {
Name string
Config any // Each config is passed into an unmarshaller later.
Method func(Collect) error // Called on startup for each configured output.
Config any // Each config is passed into an unmarshaller later.
OutputPlugin
}
// NewOutput should be called by each output package's init function.
@@ -37,8 +42,8 @@ func NewOutput(o *Output) {
outputSync.Lock()
defer outputSync.Unlock()
if o == nil || o.Method == nil {
panic("nil output or method passed to poller.NewOutput")
if o == nil {
panic("nil output passed to poller.NewOutput")
}
outputs = append(outputs, o)
@@ -81,9 +86,11 @@ func (u *UnifiPoller) runOutputMethods() (int, chan error) {
defer outputSync.RUnlock()
for _, o := range outputs {
go func(o *Output) {
err <- o.Method(u) // Run each output plugin
}(o)
if o != nil && o.Enabled() {
go func(o *Output) {
err <- o.Run(u) // Run each output plugin
}(o)
}
}
return len(outputs), err