Skip to content

Commit

Permalink
feat:export complex nested values as a metric
Browse files Browse the repository at this point in the history
  • Loading branch information
richardtief committed Feb 21, 2024
1 parent 3a679ff commit 5655948
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions BaseCollector.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self):
self.label_names = []
self.project_ids = []
self.collect_running = False
self.nested_value_metric_keys = []

@abstractmethod
def collect(self):
Expand Down
16 changes: 16 additions & 0 deletions collectors/PropertiesCollector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ class PropertiesCollector(BaseCollector):
def get_resource_uuids(self):
raise NotImplementedError("Please Implement this method")

def unlock_nested_values(self):
raise NotImplementedError("Please Implement this method")

def get_labels(self, resource_id: str, project_ids: list):
raise NotImplementedError("Please Implement this method")

def collect(self):
self.collect_running = True
logger.info(f'{self.name} starts with collecting the metrics')
if self.nested_value_metric_keys:
logger.info(f'Found nested metric values for: {self.name}, keys: {self.nested_value_metric_keys}')

token = self.get_target_tokens()
token = token.setdefault(self.target, '')
Expand Down Expand Up @@ -60,6 +65,17 @@ def collect(self):
metric_data = value_entry.get('data', [False])[0]
metric_value = value_entry.get('values', [False])[0]

if statkey in self.nested_value_metric_keys:
n_labels, n_label_values, n_value = self.unlock_nested_values(statkey, metric_value)
labels.extend(n_label_values)

if labels[0] not in metrics[statkey]['gauge']._labelnames:
for label in n_labels:
metrics[statkey]['gauge']._labelnames += (label,)

metrics[statkey]['gauge'].add_metric(labels=labels, value=n_value)
continue

if statkey in metrics:
# enum metrics
if metrics[statkey]['expected']:
Expand Down
56 changes: 56 additions & 0 deletions collectors/SDRSPropertiesCollector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from collectors.PropertiesCollector import PropertiesCollector
import json

class SDRSPropertiesCollector(PropertiesCollector):

def __init__(self):
super().__init__()
self.vrops_entity_name = 'storagepod'
self.label_names = [self.vrops_entity_name, 'vcenter', 'datacenter']
self.nested_value_metric_keys = [
"config|sdrsconfig|vmStorageAntiAffinityRules"
]

def get_resource_uuids(self):
return self.get_SDRS_clusters_by_target()

def get_labels(self, resource_id, project_ids):
return [self.sdrs_clusters[resource_id]['name'],
self.sdrs_clusters[resource_id]['vcenter'],
self.sdrs_clusters[resource_id]['parent_dc_name'].lower()] if resource_id in self.sdrs_clusters else []

def unlock_nested_values(self, statkey, metric_value):

match statkey:
case "config|sdrsconfig|vmStorageAntiAffinityRules":
return self.config_sdrsconfig_vmStorageAntiAffinityRules(metric_value)

def config_sdrsconfig_vmStorageAntiAffinityRules(self, metric_value):

metric_value = json.loads(metric_value)
rules = metric_value.get("rules") if metric_value.get("rules") else []

amount_rules = len(rules) if rules else 0

rule_labels = ['rules', 'rule_name', 'rule_type', 'valid', 'virtualmachines']
rule_label_values = []

for i, rule in enumerate(rules):
rule_label_values.extend([
f'{i+1}/{amount_rules}',
rule.get('name'),
rule.get('type'),
str(rule.get('valid')).lower(),
self.vm_mapping_helper(rule.get('virtualMachines'))
])

return rule_labels, rule_label_values, amount_rules

def vm_mapping_helper(self, vm_list):
mapped_vms = []
vms = self.get_vms(self.target)
for rule_vm in vm_list:
for vm in vms:
if rule_vm == vms[vm].get('internal_name'):
mapped_vms.append(vms[vm].get('name'))
return str(mapped_vms)
2 changes: 1 addition & 1 deletion collectors/SDRSStatsCollector.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ def get_resource_uuids(self):
def get_labels(self, resource_id, project_ids):
return [self.sdrs_clusters[resource_id]['name'],
self.sdrs_clusters[resource_id]['vcenter'],
self.sdrs_clusters[resource_id]['parent_dc_name'].lower()] if resource_id in self.sdrs_clusters else []
self.sdrs_clusters[resource_id]['parent_dc_name'].lower()] if resource_id in self.sdrs_clusters else []
5 changes: 5 additions & 0 deletions tests/collector_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ default_collectors:
- 'DistributedvSwitchPropertiesCollector'
- 'DatastoreStatsCollector'
- 'SDRSStatsCollector'
- 'SDRSPropertiesCollector'
- 'DatastorePropertiesCollector'
- 'HostSystemStatsCollector'
- 'HostSystemPropertiesCollector'
Expand Down Expand Up @@ -102,6 +103,10 @@ SDRSStatsCollector:
- metric_suffix: "capacity_remaining_percentage"
key: "OnlineCapacityAnalytics|capacityRemainingPercentage"

SDRSPropertiesCollector:
- metric_suffix: "config_sdrsconfig_vmStorageAntiAffinityRules"
key: "config|sdrsconfig|vmStorageAntiAffinityRules"

DatastorePropertiesCollector:
- metric_suffix: "summary_datastore_accessible"
expected: "PoweredOn"
Expand Down

0 comments on commit 5655948

Please sign in to comment.