summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/mongodb/collect_sharding.go
blob: 175004d34bd58f5f30bf028df1d74bacb30f67d2 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// SPDX-License-Identifier: GPL-3.0-or-later

package mongo

import (
	"fmt"
	"strings"

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

func (m *Mongo) collectSharding(mx map[string]int64) error {
	nodes, err := m.conn.shardNodes()
	if err != nil {
		return err
	}

	mx["shard_nodes_aware"] = nodes.ShardAware
	mx["shard_nodes_unaware"] = nodes.ShardUnaware

	dbPart, err := m.conn.shardDatabasesPartitioning()
	if err != nil {
		return err
	}

	mx["shard_databases_partitioned"] = dbPart.Partitioned
	mx["shard_databases_unpartitioned"] = dbPart.UnPartitioned

	collPart, err := m.conn.shardCollectionsPartitioning()
	if err != nil {
		return err
	}

	mx["shard_collections_partitioned"] = collPart.Partitioned
	mx["shard_collections_unpartitioned"] = collPart.UnPartitioned

	chunksPerShard, err := m.conn.shardChunks()
	if err != nil {
		return err
	}

	seen := make(map[string]bool)

	for shard, count := range chunksPerShard {
		seen[shard] = true
		mx["shard_id_"+shard+"_chunks"] = count
	}

	for id := range seen {
		if !m.shards[id] {
			m.shards[id] = true
			m.addShardCharts(id)
		}
	}

	for id := range m.shards {
		if !seen[id] {
			delete(m.shards, id)
			m.removeShardCharts(id)
		}
	}

	return nil
}

func (m *Mongo) addShardCharts(id string) {
	charts := chartsTmplShardingShard.Copy()

	for _, chart := range *charts {
		chart.ID = fmt.Sprintf(chart.ID, id)
		chart.Labels = []module.Label{
			{Key: "shard_id", Value: id},
		}
		for _, dim := range chart.Dims {
			dim.ID = fmt.Sprintf(dim.ID, id)
		}
	}

	if err := m.Charts().Add(*charts...); err != nil {
		m.Warning(err)
	}

}

func (m *Mongo) removeShardCharts(id string) {
	px := fmt.Sprintf("%s%s_", chartPxShard, id)

	for _, chart := range *m.Charts() {
		if strings.HasPrefix(chart.ID, px) {
			chart.MarkRemove()
			chart.MarkNotCreated()
		}
	}
}

func (m *Mongo) addShardingCharts() {
	charts := chartsSharding.Copy()

	if err := m.Charts().Add(*charts...); err != nil {
		m.Warning(err)
	}
}