Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report number of services per namespace and type #2247

Merged
merged 2 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
count services via generic count_objects_by_tags method
  • Loading branch information
xvello committed Oct 2, 2018
commit 288ba9fa1543ca9705a6f07e50c13a6ba45a040c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ def __init__(self, name, init_config, agentConfig, instances=None):
'unknown': self.UNKNOWN
}

# Parameters for the count_objects_by_tags method
self.object_count_params = {
'kube_persistentvolume_status_phase': {
'metric_name': 'persistentvolumes.by_phase',
'allowed_labels': ['storageclass', 'phase'],
},
'kube_service_spec_type': {
'metric_name': 'service.count',
'allowed_labels': ['namespace', 'type'],
},
}

self.METRIC_TRANSFORMERS = {
'kube_pod_status_phase': self.kube_pod_status_phase,
'kube_pod_container_status_waiting_reason': self.kube_pod_container_status_waiting_reason,
Expand All @@ -84,7 +96,8 @@ def __init__(self, name, init_config, agentConfig, instances=None):
'kube_node_spec_unschedulable': self.kube_node_spec_unschedulable,
'kube_resourcequota': self.kube_resourcequota,
'kube_limitrange': self.kube_limitrange,
'kube_persistentvolume_status_phase': self.kube_persistentvolume_status_phase
'kube_persistentvolume_status_phase': self.count_objects_by_tags,
'kube_service_spec_type': self.count_objects_by_tags,
}

def check(self, instance):
Expand Down Expand Up @@ -169,7 +182,6 @@ def _create_kubernetes_state_prometheus_instance(self, instance):
'kube_replicationcontroller_status_fully_labeled_replicas': 'replicationcontroller.fully_labeled_replicas', # noqa: E501
'kube_replicationcontroller_status_ready_replicas': 'replicationcontroller.replicas_ready',
'kube_replicationcontroller_status_replicas': 'replicationcontroller.replicas',
'kube_service_spec_type': 'service.count',
'kube_statefulset_replicas': 'statefulset.replicas_desired',
'kube_statefulset_status_replicas': 'statefulset.replicas',
'kube_statefulset_status_replicas_current': 'statefulset.replicas_current',
Expand Down Expand Up @@ -637,16 +649,17 @@ def kube_limitrange(self, metric, scraper_config):
else:
self.log.error("Metric type %s unsupported for metric %s" % (metric.type, metric.name))

def kube_persistentvolume_status_phase(self, metric, scraper_config):
""" The persistent volumes by phase. """
metric_name = scraper_config['namespace'] + '.persistentvolumes.by_phase'
by_phase_counter = Counter()
def count_objects_by_tags(self, metric, scraper_config):
""" Count objects by whitelisted tags and submit counts as gauges. """
config = self.object_count_params[metric.name]
metric_name = "{}.{}".format(scraper_config['namespace'], config['metric_name'])
object_counter = Counter()

for sample in metric.samples:
tags = [
self._label_to_tag("storageclass", sample[self.SAMPLE_LABELS], scraper_config),
self._label_to_tag("phase", sample[self.SAMPLE_LABELS], scraper_config)
self._label_to_tag(l, sample[self.SAMPLE_LABELS], scraper_config) for l in config['allowed_labels']
] + scraper_config['custom_tags']
by_phase_counter[tuple(sorted(tags))] += sample[self.SAMPLE_VALUE]
object_counter[tuple(sorted(tags))] += sample[self.SAMPLE_VALUE]

for tags, count in by_phase_counter.iteritems():
for tags, count in object_counter.iteritems():
self.gauge(metric_name, count, tags=list(tags))
2 changes: 0 additions & 2 deletions kubernetes_state/tests/test_kubernetes_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@
'type:ClusterIP',
'type:NodePort',
'type:LoadBalancer',
'service:redis-db2',
'service:tiller-deploy',
],
}

Expand Down