From: Simon Kollberg Date: Thu, 24 Nov 2016 14:23:37 +0000 (+0100) Subject: Add get metrics X-Git-Tag: 0.1.0~37^2 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=0b9e0ad499afba7c8a3b86a82e3cc5d42da4887d;hp=db399019ac73dfb980d48b231d69a267de27dd87;p=osm%2FN2VC.git Add get metrics --- diff --git a/juju/application.py b/juju/application.py index 4a98622..5a30235 100644 --- a/juju/application.py +++ b/juju/application.py @@ -49,6 +49,10 @@ class Application(model.ModelEntity): """ return self.data['status']['message'] + @property + def tag(self): + return 'application-%s' % self.name + async def add_relation(self, local_relation, remote_relation): """Add a relation to another application. @@ -337,3 +341,6 @@ class Application(model.ModelEntity): """ pass + + async def get_metrics(self): + return await self.model.get_metrics(self.tag) diff --git a/juju/model.py b/juju/model.py index b731899..56924cf 100644 --- a/juju/model.py +++ b/juju/model.py @@ -1206,6 +1206,36 @@ class Model(object): def charmstore(self): return self._charmstore + async def get_metrics(self, *tags): + """Retrieve metrics. + + :param str \*tags: Tags of entities from which to retrieve metrics. + No tags retrieves the metrics of all units in the model. + """ + log.debug("Retrieving metrics for %s", + ', '.join(tags) if tags else "all units") + + metrics_facade = client.MetricsDebugFacade() + metrics_facade.connect(self.connection) + + entities = [client.Entity(tag) for tag in tags] + metrics_result = await metrics_facade.GetMetrics(entities) + + metrics = collections.defaultdict(list) + + for entity_metrics in metrics_result.results: + error = entity_metrics.error + if error: + if "is not a valid tag" in error: + raise ValueError(error.message) + else: + raise Exception(error.message) + + for metric in entity_metrics.metrics: + metrics[metric.unit].append(metric.to_json()) + + return metrics + class BundleHandler(object): """ diff --git a/juju/unit.py b/juju/unit.py index 2ec1769..8511a47 100644 --- a/juju/unit.py +++ b/juju/unit.py @@ -238,3 +238,7 @@ class Unit(model.ModelEntity): # that case, we simply return False, as a destroyed unit # is not a leader. return False + + async def get_metrics(self): + metrics = await self.model.get_metrics(self.tag) + return metrics[self.name]