| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package mongo |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 10 | ) |
| 11 | |
| 12 | func (c *Collector) collectSharding(mx map[string]int64) error { |
| 13 | nodes, err := c.conn.shardNodes() |
| 14 | if err != nil { |
| 15 | return err |
| 16 | } |
| 17 | |
| 18 | mx["shard_nodes_aware"] = nodes.ShardAware |
| 19 | mx["shard_nodes_unaware"] = nodes.ShardUnaware |
| 20 | |
| 21 | dbPart, err := c.conn.shardDatabasesPartitioning() |
| 22 | if err != nil { |
| 23 | return err |
| 24 | } |
| 25 | |
| 26 | mx["shard_databases_partitioned"] = dbPart.Partitioned |
| 27 | mx["shard_databases_unpartitioned"] = dbPart.UnPartitioned |
| 28 | |
| 29 | collPart, err := c.conn.shardCollectionsPartitioning() |
| 30 | if err != nil { |
| 31 | return err |
| 32 | } |
| 33 | |
| 34 | mx["shard_collections_partitioned"] = collPart.Partitioned |
| 35 | mx["shard_collections_unpartitioned"] = collPart.UnPartitioned |
| 36 | |
| 37 | chunksPerShard, err := c.conn.shardChunks() |
| 38 | if err != nil { |
| 39 | return err |
| 40 | } |
| 41 | |
| 42 | seen := make(map[string]bool) |
| 43 | |
| 44 | for shard, count := range chunksPerShard { |
| 45 | seen[shard] = true |
| 46 | mx["shard_id_"+shard+"_chunks"] = count |
| 47 | } |
| 48 | |
| 49 | for id := range seen { |
| 50 | if !c.shards[id] { |
| 51 | c.shards[id] = true |
| 52 | c.addShardCharts(id) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | for id := range c.shards { |
| 57 | if !seen[id] { |
| 58 | delete(c.shards, id) |
| 59 | c.removeShardCharts(id) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return nil |
| 64 | } |
| 65 | |
| 66 | func (c *Collector) addShardCharts(id string) { |
| 67 | charts := chartsTmplShardingShard.Copy() |
| 68 | |
| 69 | for _, chart := range *charts { |
| 70 | chart.ID = fmt.Sprintf(chart.ID, id) |
| 71 | chart.Labels = []collectorapi.Label{ |
| 72 | {Key: "shard_id", Value: id}, |
| 73 | } |
| 74 | for _, dim := range chart.Dims { |
| 75 | dim.ID = fmt.Sprintf(dim.ID, id) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if err := c.Charts().Add(*charts...); err != nil { |
| 80 | c.Warning(err) |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | |
| 85 | func (c *Collector) removeShardCharts(id string) { |
| 86 | px := fmt.Sprintf("%s%s_", chartPxShard, id) |
| 87 | |
| 88 | for _, chart := range *c.Charts() { |
| 89 | if strings.HasPrefix(chart.ID, px) { |
| 90 | chart.MarkRemove() |
| 91 | chart.MarkNotCreated() |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func (c *Collector) addShardingCharts() { |
| 97 | charts := chartsSharding.Copy() |
| 98 | |
| 99 | if err := c.Charts().Add(*charts...); err != nil { |
| 100 | c.Warning(err) |
| 101 | } |
| 102 | } |