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