github.com/netdata/go.d.plugin@v0.58.1/modules/example/example.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package example 4 5 import ( 6 _ "embed" 7 "math/rand" 8 9 "github.com/netdata/go.d.plugin/agent/module" 10 ) 11 12 //go:embed "config_schema.json" 13 var configSchema string 14 15 func init() { 16 module.Register("example", module.Creator{ 17 JobConfigSchema: configSchema, 18 Defaults: module.Defaults{ 19 UpdateEvery: module.UpdateEvery, 20 AutoDetectionRetry: module.AutoDetectionRetry, 21 Priority: module.Priority, 22 Disabled: true, 23 }, 24 Create: func() module.Module { return New() }, 25 }) 26 } 27 28 func New() *Example { 29 return &Example{ 30 Config: Config{ 31 Charts: ConfigCharts{ 32 Num: 1, 33 Dims: 4, 34 }, 35 HiddenCharts: ConfigCharts{ 36 Num: 0, 37 Dims: 4, 38 }, 39 }, 40 41 randInt: func() int64 { return rand.Int63n(100) }, 42 collectedDims: make(map[string]bool), 43 } 44 } 45 46 type ( 47 Config struct { 48 Charts ConfigCharts `yaml:"charts"` 49 HiddenCharts ConfigCharts `yaml:"hidden_charts"` 50 } 51 ConfigCharts struct { 52 Type string `yaml:"type"` 53 Num int `yaml:"num"` 54 Contexts int `yaml:"contexts"` 55 Dims int `yaml:"dimensions"` 56 Labels int `yaml:"labels"` 57 } 58 ) 59 60 type Example struct { 61 module.Base // should be embedded by every module 62 Config `yaml:",inline"` 63 64 randInt func() int64 65 charts *module.Charts 66 collectedDims map[string]bool 67 } 68 69 func (e *Example) Init() bool { 70 err := e.validateConfig() 71 if err != nil { 72 e.Errorf("config validation: %v", err) 73 return false 74 } 75 76 charts, err := e.initCharts() 77 if err != nil { 78 e.Errorf("charts init: %v", err) 79 return false 80 } 81 e.charts = charts 82 return true 83 } 84 85 func (e *Example) Check() bool { 86 return len(e.Collect()) > 0 87 } 88 89 func (e *Example) Charts() *module.Charts { 90 return e.charts 91 } 92 93 func (e *Example) Collect() map[string]int64 { 94 mx, err := e.collect() 95 if err != nil { 96 e.Error(err) 97 } 98 99 if len(mx) == 0 { 100 return nil 101 } 102 return mx 103 } 104 105 func (e *Example) Cleanup() {}