| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package vsphere |
| 4 | |
| 5 | import ( |
| 6 | "github.com/netdata/netdata/go/plugins/pkg/metrix" |
| 7 | rs "github.com/netdata/netdata/go/plugins/plugin/go.d/collector/vsphere/resources" |
| 8 | ) |
| 9 | |
| 10 | type collectorMetrics struct { |
| 11 | meter metrix.SnapshotMeter |
| 12 | gauges map[string]metrix.SnapshotGauge |
| 13 | } |
| 14 | |
| 15 | const scaledPercent = 10000 |
| 16 | |
| 17 | func newCollectorMetrics(store metrix.CollectorStore) *collectorMetrics { |
| 18 | return &collectorMetrics{ |
| 19 | meter: store.Write().SnapshotMeter(""), |
| 20 | gauges: make(map[string]metrix.SnapshotGauge), |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func (mx *collectorMetrics) gauge(name string) metrix.SnapshotGauge { |
| 25 | // The gauge cache is mutated only from Collect while collectionLock is held. |
| 26 | gauge := mx.gauges[name] |
| 27 | if gauge == nil { |
| 28 | gauge = mx.meter.Gauge(name) |
| 29 | mx.gauges[name] = gauge |
| 30 | } |
| 31 | return gauge |
| 32 | } |
| 33 | |
| 34 | func (c *Collector) observeGauge(name string, value int64, labels metrix.LabelSet) { |
| 35 | c.observeGaugeFloat(name, float64(value), labels) |
| 36 | } |
| 37 | |
| 38 | func (c *Collector) observeGaugeFloat(name string, value float64, labels metrix.LabelSet) { |
| 39 | c.mx.gauge(name).Observe(metrix.SampleValue(value), labels) |
| 40 | } |
| 41 | |
| 42 | func (c *Collector) labelSet(labels []metrix.Label) metrix.LabelSet { |
| 43 | return c.mx.meter.LabelSet(labels...) |
| 44 | } |
| 45 | |
| 46 | func (c *Collector) v2MetricLabels(id string, base []metrix.Label, enrichment map[string]string) []metrix.Label { |
| 47 | labels := make([]metrix.Label, 0, len(base)+1) |
| 48 | labels = append(labels, metrix.Label{Key: "id", Value: id}) |
| 49 | labels = append(labels, base...) |
| 50 | labels = append(labels, resourceEnrichmentLabels(enrichment)...) |
| 51 | return labels |
| 52 | } |
| 53 | |
| 54 | func (c *Collector) inventoryLabelSet() metrix.LabelSet { |
| 55 | return c.labelSet(c.v2MetricLabels("inventory", nil, nil)) |
| 56 | } |
| 57 | |
| 58 | func (c *Collector) hostLabelSet(host *rs.Host) metrix.LabelSet { |
| 59 | return c.labelSet(c.v2MetricLabels(host.ID, c.hostLabels(host), host.Labels)) |
| 60 | } |
| 61 | |
| 62 | func (c *Collector) vmLabelSet(vm *rs.VM) metrix.LabelSet { |
| 63 | return c.labelSet(c.v2MetricLabels(vm.ID, c.vmLabels(vm), vm.Labels)) |
| 64 | } |
| 65 | |
| 66 | func (c *Collector) datastoreLabelSet(ds *rs.Datastore) metrix.LabelSet { |
| 67 | return c.labelSet(c.v2MetricLabels(ds.ID, c.datastoreLabels(ds), ds.Labels)) |
| 68 | } |
| 69 | |
| 70 | func (c *Collector) clusterLabelSet(cl *rs.Cluster) metrix.LabelSet { |
| 71 | return c.labelSet(c.v2MetricLabels(cl.ID, c.clusterLabels(cl), cl.Labels)) |
| 72 | } |
| 73 | |
| 74 | func (c *Collector) resourcePoolLabelSet(rp *rs.ResourcePool) metrix.LabelSet { |
| 75 | return c.labelSet(c.v2MetricLabels(rp.ID, c.resourcePoolLabels(rp), rp.Labels)) |
| 76 | } |
| 77 | |
| 78 | func (c *Collector) hostLabels(host *rs.Host) []metrix.Label { |
| 79 | return []metrix.Label{ |
| 80 | {Key: "datacenter", Value: host.Hier.DC.Name}, |
| 81 | {Key: "cluster", Value: getHostClusterName(host)}, |
| 82 | {Key: "host", Value: host.Name}, |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func (c *Collector) vmLabels(vm *rs.VM) []metrix.Label { |
| 87 | return []metrix.Label{ |
| 88 | {Key: "datacenter", Value: vm.Hier.DC.Name}, |
| 89 | {Key: "cluster", Value: getVMClusterName(vm)}, |
| 90 | {Key: "host", Value: vm.Hier.Host.Name}, |
| 91 | {Key: "vm", Value: vm.Name}, |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func (c *Collector) datastoreLabels(ds *rs.Datastore) []metrix.Label { |
| 96 | return []metrix.Label{ |
| 97 | {Key: "datacenter", Value: ds.Hier.DC.Name}, |
| 98 | {Key: "datastore", Value: ds.Name}, |
| 99 | {Key: "type", Value: ds.Type}, |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func (c *Collector) clusterLabels(cl *rs.Cluster) []metrix.Label { |
| 104 | return []metrix.Label{ |
| 105 | {Key: "datacenter", Value: cl.Hier.DC.Name}, |
| 106 | {Key: "cluster", Value: cl.Name}, |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func (c *Collector) resourcePoolLabels(rp *rs.ResourcePool) []metrix.Label { |
| 111 | return []metrix.Label{ |
| 112 | {Key: "datacenter", Value: rp.Hier.DC.Name}, |
| 113 | {Key: "cluster", Value: rp.Hier.Cluster.Name}, |
| 114 | {Key: "resource_pool", Value: rp.Name}, |
| 115 | } |
| 116 | } |