summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/example/collect.go
blob: 588d605dfee73e25f27e56a7c1c826ed0e40977f (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package example

import (
	"fmt"

	"github.com/netdata/netdata/go/go.d.plugin/agent/module"
)

func (e *Example) collect() (map[string]int64, error) {
	collected := make(map[string]int64)

	for _, chart := range *e.Charts() {
		e.collectChart(collected, chart)
	}
	return collected, nil
}

func (e *Example) collectChart(collected map[string]int64, chart *module.Chart) {
	var num int
	if chart.Opts.Hidden {
		num = e.Config.HiddenCharts.Dims
	} else {
		num = e.Config.Charts.Dims
	}

	for i := 0; i < num; i++ {
		name := fmt.Sprintf("random%d", i)
		id := fmt.Sprintf("%s_%s", chart.ID, name)

		if !e.collectedDims[id] {
			e.collectedDims[id] = true

			dim := &module.Dim{ID: id, Name: name}
			if err := chart.AddDim(dim); err != nil {
				e.Warning(err)
			}
			chart.MarkNotCreated()
		}
		if i%2 == 0 {
			collected[id] = e.randInt()
		} else {
			collected[id] = -e.randInt()
		}
	}
}