@cryptotaxi247 / netdata-1 / commits / 0320acd53

go.d rename example => testrandom (#18561)

Ilya Mashchenko committed Sep 16, 2024 at 12:57 UTC 0320acd537ce2a2aadc00dc8adff4ea4850eb5d7
14 files changed +158 -290
src/go/plugin/go.d/README.md
-1
@@ -76,7 +76,6 @@ see the appropriate collector readme.
76 | [dovecot](https://github.com/netdata/netdata/tree/master/src/go/plugin/go.d/modules/dovecot) | Dovecot |
77 | [elasticsearch](https://github.com/netdata/netdata/tree/master/src/go/plugin/go.d/modules/elasticsearch) | Elasticsearch/OpenSearch |
78 | [envoy](https://github.com/netdata/netdata/tree/master/src/go/plugin/go.d/modules/envoy) | Envoy |
79 -| [example](https://github.com/netdata/netdata/tree/master/src/go/plugin/go.d/modules/example) | - |
79 | [exim](https://github.com/netdata/netdata/tree/master/src/go/plugin/go.d/modules/exim) | Exim |
80 | [fail2ban](https://github.com/netdata/netdata/tree/master/src/go/plugin/go.d/modules/fail2ban) | Fail2Ban Jails |
81 | [filecheck](https://github.com/netdata/netdata/tree/master/src/go/plugin/go.d/modules/filecheck) | Files and Directories |
src/go/plugin/go.d/config/go.d.conf
-1
@@ -41,7 +41,6 @@ modules:
41 # dovecot: yes
42 # elasticsearch: yes
43 # envoy: yes
44 -# example: no
44 # exim: yes
45 # fail2ban: yes
46 # filecheck: yes
src/go/plugin/go.d/config/go.d/example.conf deleted
-5
@@ -1,5 +0,0 @@
1 -## All available configuration options, their descriptions and default values:
2 -## https://github.com/netdata/netdata/tree/master/src/go/plugin/go.d/modules/example#readme
3 -
4 -jobs:
5 - - name: example
src/go/plugin/go.d/modules/example/README.md deleted
-80
@@ -1,80 +0,0 @@
1 -<!--
2 -title: "Example module"
3 -description: "Use this example data collection module, which produces example charts with random values, to better understand how to build your own collector in Go."
4 -custom_edit_url: "https://github.com/netdata/go.d.plugin/edit/master/modules/example/README.md"
5 -sidebar_label: "Example module in Go"
6 -learn_status: "Published"
7 -learn_topic_type: "References"
8 -learn_rel_path: "Integrations/Monitor/Mock Collectors"
9 --->
10 -
11 -# Example module
12 -
13 -An example data collection module. Use it as an example writing a new module.
14 -
15 -## Charts
16 -
17 -This module produces example charts with random values. Number of charts, dimensions and chart type is configurable.
18 -
19 -## Configuration
20 -
21 -Edit the `go.d/example.conf` configuration file using `edit-config` from the
22 -Netdata [config directory](/docs/netdata-agent/configuration/README.md), which is typically at `/etc/netdata`.
23 -
24 -```bash
25 -cd /etc/netdata # Replace this path with your Netdata config directory
26 -sudo ./edit-config go.d/example.conf
27 -```
28 -
29 -Disabled by default. Should be explicitly enabled
30 -in [go.d.conf](https://github.com/netdata/netdata/blob/master/src/go/plugin/go.d/config/go.d.conf).
31 -
32 -```yaml
33 -# go.d.conf
34 -modules:
35 - example: yes
36 -```
37 -
38 -Here is an example configuration with several jobs:
39 -
40 -```yaml
41 -jobs:
42 - - name: example
43 - charts:
44 - num: 3
45 - dimensions: 5
46 -
47 - - name: hidden_example
48 - hidden_charts:
49 - num: 3
50 - dimensions: 5
51 -```
52 -
53 ----
54 -
55 -For all available options, see the Example
56 -collector's [configuration file](https://github.com/netdata/netdata/blob/master/src/go/plugin/go.d/config/go.d/example.conf).
57 -
58 -## Troubleshooting
59 -
60 -To troubleshoot issues with the `example` collector, run the `go.d.plugin` with the debug option enabled. The output
61 -should give you clues as to why the collector isn't working.
62 -
63 -- Navigate to the `plugins.d` directory, usually at `/usr/libexec/netdata/plugins.d/`. If that's not the case on
64 - your system, open `netdata.conf` and look for the `plugins` setting under `[directories]`.
65 -
66 - ```bash
67 - cd /usr/libexec/netdata/plugins.d/
68 - ```
69 -
70 -- Switch to the `netdata` user.
71 -
72 - ```bash
73 - sudo -u netdata -s
74 - ```
75 -
76 -- Run the `go.d.plugin` to debug the collector:
77 -
78 - ```bash
79 - ./go.d.plugin -d -m example
80 - ```
src/go/plugin/go.d/modules/example/init.go deleted
-64
@@ -1,64 +0,0 @@
1 -// SPDX-License-Identifier: GPL-3.0-or-later
2 -
3 -package example
4 -
5 -import (
6 - "errors"
7 -
8 - "github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
9 -)
10 -
11 -func (e *Example) validateConfig() error {
12 - if e.Config.Charts.Num <= 0 && e.Config.HiddenCharts.Num <= 0 {
13 - return errors.New("'charts->num' or `hidden_charts->num` must be > 0")
14 - }
15 - if e.Config.Charts.Num > 0 && e.Config.Charts.Dims <= 0 {
16 - return errors.New("'charts->dimensions' must be > 0")
17 - }
18 - if e.Config.HiddenCharts.Num > 0 && e.Config.HiddenCharts.Dims <= 0 {
19 - return errors.New("'hidden_charts->dimensions' must be > 0")
20 - }
21 - return nil
22 -}
23 -
24 -func (e *Example) initCharts() (*module.Charts, error) {
25 - charts := &module.Charts{}
26 -
27 - var ctx int
28 - v := calcContextEvery(e.Config.Charts.Num, e.Config.Charts.Contexts)
29 - for i := 0; i < e.Config.Charts.Num; i++ {
30 - if i != 0 && v != 0 && ctx < (e.Config.Charts.Contexts-1) && i%v == 0 {
31 - ctx++
32 - }
33 - chart := newChart(i, ctx, e.Config.Charts.Labels, module.ChartType(e.Config.Charts.Type))
34 -
35 - if err := charts.Add(chart); err != nil {
36 - return nil, err
37 - }
38 - }
39 -
40 - ctx = 0
41 - v = calcContextEvery(e.Config.HiddenCharts.Num, e.Config.HiddenCharts.Contexts)
42 - for i := 0; i < e.Config.HiddenCharts.Num; i++ {
43 - if i != 0 && v != 0 && ctx < (e.Config.HiddenCharts.Contexts-1) && i%v == 0 {
44 - ctx++
45 - }
46 - chart := newHiddenChart(i, ctx, e.Config.HiddenCharts.Labels, module.ChartType(e.Config.HiddenCharts.Type))
47 -
48 - if err := charts.Add(chart); err != nil {
49 - return nil, err
50 - }
51 - }
52 -
53 - return charts, nil
54 -}
55 -
56 -func calcContextEvery(charts, contexts int) int {
57 - if contexts <= 1 {
58 - return 0
59 - }
60 - if contexts > charts {
61 - return 1
62 - }
63 - return charts / contexts
64 -}
src/go/plugin/go.d/modules/init.go
+1 -1
@@ -30,7 +30,6 @@ import (
30 _ "github.com/netdata/netdata/go/plugins/plugin/go.d/modules/dovecot"
31 _ "github.com/netdata/netdata/go/plugins/plugin/go.d/modules/elasticsearch"
32 _ "github.com/netdata/netdata/go/plugins/plugin/go.d/modules/envoy"
33 - _ "github.com/netdata/netdata/go/plugins/plugin/go.d/modules/example"
33 _ "github.com/netdata/netdata/go/plugins/plugin/go.d/modules/exim"
34 _ "github.com/netdata/netdata/go/plugins/plugin/go.d/modules/fail2ban"
35 _ "github.com/netdata/netdata/go/plugins/plugin/go.d/modules/filecheck"
@@ -101,6 +100,7 @@ import (
100 _ "github.com/netdata/netdata/go/plugins/plugin/go.d/modules/supervisord"
101 _ "github.com/netdata/netdata/go/plugins/plugin/go.d/modules/systemdunits"
102 _ "github.com/netdata/netdata/go/plugins/plugin/go.d/modules/tengine"
103 + _ "github.com/netdata/netdata/go/plugins/plugin/go.d/modules/testrandom"
104 _ "github.com/netdata/netdata/go/plugins/plugin/go.d/modules/tomcat"
105 _ "github.com/netdata/netdata/go/plugins/plugin/go.d/modules/tor"
106 _ "github.com/netdata/netdata/go/plugins/plugin/go.d/modules/traefik"
src/go/plugin/go.d/modules/testrandom/charts.go renamed
+7 -7
@@ -1,6 +1,6 @@
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 -package example
3 +package testrandom
4
5 import (
6 "fmt"
@@ -13,7 +13,7 @@ var chartTemplate = module.Chart{
13 Title: "A Random Number",
14 Units: "random",
15 Fam: "random",
16 - Ctx: "example.random",
16 + Ctx: "testrandom.random",
17 }
18
19 var hiddenChartTemplate = module.Chart{
@@ -21,7 +21,7 @@ var hiddenChartTemplate = module.Chart{
21 Title: "A Random Number",
22 Units: "random",
23 Fam: "random",
24 - Ctx: "example.random",
24 + Ctx: "testrandom.random",
25 Opts: module.Opts{
26 Hidden: true,
27 },
@@ -36,8 +36,8 @@ func newChart(num, ctx, labels int, typ module.ChartType) *module.Chart {
36 }
37 for i := 0; i < labels; i++ {
38 chart.Labels = append(chart.Labels, module.Label{
39 - Key: fmt.Sprintf("example_name_%d", i),
40 - Value: fmt.Sprintf("example_value_%d_%d", num, i),
39 + Key: fmt.Sprintf("random_name_%d", i),
40 + Value: fmt.Sprintf("random_value_%d_%d", num, i),
41 })
42 }
43 return chart
@@ -52,8 +52,8 @@ func newHiddenChart(num, ctx, labels int, typ module.ChartType) *module.Chart {
52 }
53 for i := 0; i < labels; i++ {
54 chart.Labels = append(chart.Labels, module.Label{
55 - Key: fmt.Sprintf("example_name_%d", i),
56 - Value: fmt.Sprintf("example_value_%d_%d", num, i),
55 + Key: fmt.Sprintf("random_name_%d", i),
56 + Value: fmt.Sprintf("random_value_%d_%d", num, i),
57 })
58 }
59 return chart
src/go/plugin/go.d/modules/testrandom/collect.go renamed
+12 -12
@@ -1,6 +1,6 @@
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 -package example
3 +package testrandom
4
5 import (
6 "fmt"
@@ -8,40 +8,40 @@ import (
8 "github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
9 )
10
11 -func (e *Example) collect() (map[string]int64, error) {
11 +func (tr *TestRandom) collect() (map[string]int64, error) {
12 collected := make(map[string]int64)
13
14 - for _, chart := range *e.Charts() {
15 - e.collectChart(collected, chart)
14 + for _, chart := range *tr.Charts() {
15 + tr.collectChart(collected, chart)
16 }
17 return collected, nil
18 }
19
20 -func (e *Example) collectChart(collected map[string]int64, chart *module.Chart) {
20 +func (tr *TestRandom) collectChart(collected map[string]int64, chart *module.Chart) {
21 var num int
22 if chart.Opts.Hidden {
23 - num = e.Config.HiddenCharts.Dims
23 + num = tr.Config.HiddenCharts.Dims
24 } else {
25 - num = e.Config.Charts.Dims
25 + num = tr.Config.Charts.Dims
26 }
27
28 for i := 0; i < num; i++ {
29 name := fmt.Sprintf("random%d", i)
30 id := fmt.Sprintf("%s_%s", chart.ID, name)
31
32 - if !e.collectedDims[id] {
33 - e.collectedDims[id] = true
32 + if !tr.collectedDims[id] {
33 + tr.collectedDims[id] = true
34
35 dim := &module.Dim{ID: id, Name: name}
36 if err := chart.AddDim(dim); err != nil {
37 - e.Warning(err)
37 + tr.Warning(err)
38 }
39 chart.MarkNotCreated()
40 }
41 if i%2 == 0 {
42 - collected[id] = e.randInt()
42 + collected[id] = tr.randInt()
43 } else {
44 - collected[id] = -e.randInt()
44 + collected[id] = -tr.randInt()
45 }
46 }
47 }
src/go/plugin/go.d/modules/testrandom/config_schema.json renamed
src/go/plugin/go.d/modules/testrandom/init.go new
+64
@@ -0,0 +1,64 @@
1 +// SPDX-License-Identifier: GPL-3.0-or-later
2 +
3 +package testrandom
4 +
5 +import (
6 + "errors"
7 +
8 + "github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
9 +)
10 +
11 +func (tr *TestRandom) validateConfig() error {
12 + if tr.Config.Charts.Num <= 0 && tr.Config.HiddenCharts.Num <= 0 {
13 + return errors.New("'charts->num' or `hidden_charts->num` must be > 0")
14 + }
15 + if tr.Config.Charts.Num > 0 && tr.Config.Charts.Dims <= 0 {
16 + return errors.New("'charts->dimensions' must be > 0")
17 + }
18 + if tr.Config.HiddenCharts.Num > 0 && tr.Config.HiddenCharts.Dims <= 0 {
19 + return errors.New("'hidden_charts->dimensions' must be > 0")
20 + }
21 + return nil
22 +}
23 +
24 +func (tr *TestRandom) initCharts() (*module.Charts, error) {
25 + charts := &module.Charts{}
26 +
27 + var ctx int
28 + v := calcContextEvery(tr.Config.Charts.Num, tr.Config.Charts.Contexts)
29 + for i := 0; i < tr.Config.Charts.Num; i++ {
30 + if i != 0 && v != 0 && ctx < (tr.Config.Charts.Contexts-1) && i%v == 0 {
31 + ctx++
32 + }
33 + chart := newChart(i, ctx, tr.Config.Charts.Labels, module.ChartType(tr.Config.Charts.Type))
34 +
35 + if err := charts.Add(chart); err != nil {
36 + return nil, err
37 + }
38 + }
39 +
40 + ctx = 0
41 + v = calcContextEvery(tr.Config.HiddenCharts.Num, tr.Config.HiddenCharts.Contexts)
42 + for i := 0; i < tr.Config.HiddenCharts.Num; i++ {
43 + if i != 0 && v != 0 && ctx < (tr.Config.HiddenCharts.Contexts-1) && i%v == 0 {
44 + ctx++
45 + }
46 + chart := newHiddenChart(i, ctx, tr.Config.HiddenCharts.Labels, module.ChartType(tr.Config.HiddenCharts.Type))
47 +
48 + if err := charts.Add(chart); err != nil {
49 + return nil, err
50 + }
51 + }
52 +
53 + return charts, nil
54 +}
55 +
56 +func calcContextEvery(charts, contexts int) int {
57 + if contexts <= 1 {
58 + return 0
59 + }
60 + if contexts > charts {
61 + return 1
62 + }
63 + return charts / contexts
64 +}
src/go/plugin/go.d/modules/testrandom/testdata/config.json renamed
src/go/plugin/go.d/modules/testrandom/testdata/config.yaml renamed
src/go/plugin/go.d/modules/testrandom/testrandom.go renamed
+20 -20
@@ -1,6 +1,6 @@
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 -package example
3 +package testrandom
4
5 import (
6 _ "embed"
@@ -13,7 +13,7 @@ import (
13 var configSchema string
14
15 func init() {
16 - module.Register("example", module.Creator{
16 + module.Register("testrandom", module.Creator{
17 JobConfigSchema: configSchema,
18 Defaults: module.Defaults{
19 UpdateEvery: module.UpdateEvery,
@@ -25,8 +25,8 @@ func init() {
25 })
26 }
27
28 -func New() *Example {
29 - return &Example{
28 +func New() *TestRandom {
29 + return &TestRandom{
30 Config: Config{
31 Charts: ConfigCharts{
32 Num: 1,
@@ -58,7 +58,7 @@ type (
58 }
59 )
60
61 -type Example struct {
61 +type TestRandom struct {
62 module.Base // should be embedded by every module
63 Config `yaml:",inline"`
64
@@ -67,38 +67,38 @@ type Example struct {
67 collectedDims map[string]bool
68 }
69
70 -func (e *Example) Configuration() any {
71 - return e.Config
70 +func (tr *TestRandom) Configuration() any {
71 + return tr.Config
72 }
73
74 -func (e *Example) Init() error {
75 - err := e.validateConfig()
74 +func (tr *TestRandom) Init() error {
75 + err := tr.validateConfig()
76 if err != nil {
77 - e.Errorf("config validation: %v", err)
77 + tr.Errorf("config validation: %v", err)
78 return err
79 }
80
81 - charts, err := e.initCharts()
81 + charts, err := tr.initCharts()
82 if err != nil {
83 - e.Errorf("charts init: %v", err)
83 + tr.Errorf("charts init: %v", err)
84 return err
85 }
86 - e.charts = charts
86 + tr.charts = charts
87 return nil
88 }
89
90 -func (e *Example) Check() error {
90 +func (tr *TestRandom) Check() error {
91 return nil
92 }
93
94 -func (e *Example) Charts() *module.Charts {
95 - return e.charts
94 +func (tr *TestRandom) Charts() *module.Charts {
95 + return tr.charts
96 }
97
98 -func (e *Example) Collect() map[string]int64 {
99 - mx, err := e.collect()
98 +func (tr *TestRandom) Collect() map[string]int64 {
99 + mx, err := tr.collect()
100 if err != nil {
101 - e.Error(err)
101 + tr.Error(err)
102 }
103
104 if len(mx) == 0 {
@@ -107,4 +107,4 @@ func (e *Example) Collect() map[string]int64 {
107 return mx
108 }
109
110 -func (e *Example) Cleanup() {}
110 +func (tr *TestRandom) Cleanup() {}
src/go/plugin/go.d/modules/testrandom/testrandom_test.go renamed
+54 -99
@@ -1,6 +1,6 @@
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 -package example
3 +package testrandom
4
5 import (
6 "os"
@@ -26,23 +26,15 @@ func Test_testDataIsValid(t *testing.T) {
26 }
27 }
28
29 -func TestExample_ConfigurationSerialize(t *testing.T) {
30 - module.TestConfigurationSerialize(t, &Example{}, dataConfigJSON, dataConfigYAML)
29 +func TestTestRandom_ConfigurationSerialize(t *testing.T) {
30 + module.TestConfigurationSerialize(t, &TestRandom{}, dataConfigJSON, dataConfigYAML)
31 }
32
33 func TestNew(t *testing.T) {
34 - // We want to ensure that module is a reference type, nothing more.
35 -
36 - assert.IsType(t, (*Example)(nil), New())
34 + assert.IsType(t, (*TestRandom)(nil), New())
35 }
36
39 -func TestExample_Init(t *testing.T) {
40 - // 'Init() bool' initializes the module with an appropriate config, so to test it we need:
41 - // - provide the config.
42 - // - set module.Config field with the config.
43 - // - call Init() and compare its return value with the expected value.
44 -
45 - // 'test' map contains different test cases.
37 +func TestTestRandom_Init(t *testing.T) {
38 tests := map[string]struct {
39 config Config
40 wantFail bool
@@ -113,106 +105,87 @@ func TestExample_Init(t *testing.T) {
105
106 for name, test := range tests {
107 t.Run(name, func(t *testing.T) {
116 - example := New()
117 - example.Config = test.config
108 + tr := New()
109 + tr.Config = test.config
110
111 if test.wantFail {
120 - assert.Error(t, example.Init())
112 + assert.Error(t, tr.Init())
113 } else {
122 - assert.NoError(t, example.Init())
114 + assert.NoError(t, tr.Init())
115 }
116 })
117 }
118 }
119
128 -func TestExample_Check(t *testing.T) {
129 - // 'Check() bool' reports whether the module is able to collect any data, so to test it we need:
130 - // - provide the module with a specific config.
131 - // - initialize the module (call Init()).
132 - // - call Check() and compare its return value with the expected value.
133 -
134 - // 'test' map contains different test cases.
120 +func TestTestRandom_Check(t *testing.T) {
121 tests := map[string]struct {
136 - prepare func() *Example
122 + prepare func() *TestRandom
123 wantFail bool
124 }{
139 - "success on default": {prepare: prepareExampleDefault},
140 - "success when only 'charts' set": {prepare: prepareExampleOnlyCharts},
141 - "success when only 'hidden_charts' set": {prepare: prepareExampleOnlyHiddenCharts},
142 - "success when 'charts' and 'hidden_charts' set": {prepare: prepareExampleChartsAndHiddenCharts},
125 + "success on default": {prepare: prepareTRDefault},
126 + "success when only 'charts' set": {prepare: prepareTROnlyCharts},
127 + "success when only 'hidden_charts' set": {prepare: prepareTROnlyHiddenCharts},
128 + "success when 'charts' and 'hidden_charts' set": {prepare: prepareTRChartsAndHiddenCharts},
129 }
130
131 for name, test := range tests {
132 t.Run(name, func(t *testing.T) {
147 - example := test.prepare()
148 - require.NoError(t, example.Init())
133 + tr := test.prepare()
134 + require.NoError(t, tr.Init())
135
136 if test.wantFail {
151 - assert.Error(t, example.Check())
137 + assert.Error(t, tr.Check())
138 } else {
153 - assert.NoError(t, example.Check())
139 + assert.NoError(t, tr.Check())
140 }
141 })
142 }
143 }
144
159 -func TestExample_Charts(t *testing.T) {
160 - // We want to ensure that initialized module does not return 'nil'.
161 - // If it is not 'nil' we are ok.
162 -
163 - // 'test' map contains different test cases.
145 +func TestTestRandom_Charts(t *testing.T) {
146 tests := map[string]struct {
165 - prepare func(t *testing.T) *Example
147 + prepare func(t *testing.T) *TestRandom
148 wantNil bool
149 }{
150 "not initialized collector": {
151 wantNil: true,
170 - prepare: func(t *testing.T) *Example {
152 + prepare: func(t *testing.T) *TestRandom {
153 return New()
154 },
155 },
156 "initialized collector": {
175 - prepare: func(t *testing.T) *Example {
176 - example := New()
177 - require.NoError(t, example.Init())
178 - return example
157 + prepare: func(t *testing.T) *TestRandom {
158 + tr := New()
159 + require.NoError(t, tr.Init())
160 + return tr
161 },
162 },
163 }
164
165 for name, test := range tests {
166 t.Run(name, func(t *testing.T) {
185 - example := test.prepare(t)
167 + tr := test.prepare(t)
168
169 if test.wantNil {
188 - assert.Nil(t, example.Charts())
170 + assert.Nil(t, tr.Charts())
171 } else {
190 - assert.NotNil(t, example.Charts())
172 + assert.NotNil(t, tr.Charts())
173 }
174 })
175 }
176 }
177
196 -func TestExample_Cleanup(t *testing.T) {
197 - // Since this module has nothing to clean up,
198 - // we want just to ensure that Cleanup() not panics.
199 -
178 +func TestTestRandom_Cleanup(t *testing.T) {
179 assert.NotPanics(t, New().Cleanup)
180 }
181
203 -func TestExample_Collect(t *testing.T) {
204 - // 'Collect() map[string]int64' returns collected data, so to test it we need:
205 - // - provide the module with a specific config.
206 - // - initialize the module (call Init()).
207 - // - call Collect() and compare its return value with the expected value.
208 -
209 - // 'test' map contains different test cases.
182 +func TestTestRandom_Collect(t *testing.T) {
183 tests := map[string]struct {
211 - prepare func() *Example
184 + prepare func() *TestRandom
185 wantCollected map[string]int64
186 }{
187 "default config": {
215 - prepare: prepareExampleDefault,
188 + prepare: prepareTRDefault,
189 wantCollected: map[string]int64{
190 "random_0_random0": 1,
191 "random_0_random1": -1,
@@ -221,7 +194,7 @@ func TestExample_Collect(t *testing.T) {
194 },
195 },
196 "only 'charts' set": {
224 - prepare: prepareExampleOnlyCharts,
197 + prepare: prepareTROnlyCharts,
198 wantCollected: map[string]int64{
199 "random_0_random0": 1,
200 "random_0_random1": -1,
@@ -236,7 +209,7 @@ func TestExample_Collect(t *testing.T) {
209 },
210 },
211 "only 'hidden_charts' set": {
239 - prepare: prepareExampleOnlyHiddenCharts,
212 + prepare: prepareTROnlyHiddenCharts,
213 wantCollected: map[string]int64{
214 "hidden_random_0_random0": 1,
215 "hidden_random_0_random1": -1,
@@ -251,7 +224,7 @@ func TestExample_Collect(t *testing.T) {
224 },
225 },
226 "'charts' and 'hidden_charts' set": {
254 - prepare: prepareExampleChartsAndHiddenCharts,
227 + prepare: prepareTRChartsAndHiddenCharts,
228 wantCollected: map[string]int64{
229 "hidden_random_0_random0": 1,
230 "hidden_random_0_random1": -1,
@@ -279,41 +252,23 @@ func TestExample_Collect(t *testing.T) {
252
253 for name, test := range tests {
254 t.Run(name, func(t *testing.T) {
282 - example := test.prepare()
283 - require.NoError(t, example.Init())
255 + tr := test.prepare()
256 + require.NoError(t, tr.Init())
257
285 - collected := example.Collect()
258 + mx := tr.Collect()
259
287 - assert.Equal(t, test.wantCollected, collected)
288 - ensureCollectedHasAllChartsDimsVarsIDs(t, example, collected)
260 + assert.Equal(t, test.wantCollected, mx)
261 + module.TestMetricsHasAllChartsDims(t, tr.Charts(), mx)
262 })
263 }
264 }
265
293 -func ensureCollectedHasAllChartsDimsVarsIDs(t *testing.T, e *Example, collected map[string]int64) {
294 - for _, chart := range *e.Charts() {
295 - if chart.Obsolete {
296 - continue
297 - }
298 - for _, dim := range chart.Dims {
299 - _, ok := collected[dim.ID]
300 - assert.Truef(t, ok,
301 - "collected metrics has no data for dim '%s' chart '%s'", dim.ID, chart.ID)
302 - }
303 - for _, v := range chart.Vars {
304 - _, ok := collected[v.ID]
305 - assert.Truef(t, ok,
306 - "collected metrics has no data for var '%s' chart '%s'", v.ID, chart.ID)
307 - }
308 - }
309 -}
310 -
311 -func prepareExampleDefault() *Example {
312 - return prepareExample(New().Config)
266 +func prepareTRDefault() *TestRandom {
267 + return prepareTR(New().Config)
268 }
269
315 -func prepareExampleOnlyCharts() *Example {
316 - return prepareExample(Config{
270 +func prepareTROnlyCharts() *TestRandom {
271 + return prepareTR(Config{
272 Charts: ConfigCharts{
273 Num: 2,
274 Dims: 5,
@@ -321,8 +276,8 @@ func prepareExampleOnlyCharts() *Example {
276 })
277 }
278
324 -func prepareExampleOnlyHiddenCharts() *Example {
325 - return prepareExample(Config{
279 +func prepareTROnlyHiddenCharts() *TestRandom {
280 + return prepareTR(Config{
281 HiddenCharts: ConfigCharts{
282 Num: 2,
283 Dims: 5,
@@ -330,8 +285,8 @@ func prepareExampleOnlyHiddenCharts() *Example {
285 })
286 }
287
333 -func prepareExampleChartsAndHiddenCharts() *Example {
334 - return prepareExample(Config{
288 +func prepareTRChartsAndHiddenCharts() *TestRandom {
289 + return prepareTR(Config{
290 Charts: ConfigCharts{
291 Num: 2,
292 Dims: 5,
@@ -343,9 +298,9 @@ func prepareExampleChartsAndHiddenCharts() *Example {
298 })
299 }
300
346 -func prepareExample(cfg Config) *Example {
347 - example := New()
348 - example.Config = cfg
349 - example.randInt = func() int64 { return 1 }
350 - return example
301 +func prepareTR(cfg Config) *TestRandom {
302 + tr := New()
303 + tr.Config = cfg
304 + tr.randInt = func() int64 { return 1 }
305 + return tr
306 }