Repository: paambaati/mae

Issue:
An app without any labels can crash mae's response
When the app definition does not have any labels, the response is empty. This can fixed by adding defaults to the `get()` calls here —

https://github.com/paambaati/mae/blob/ab1eadba7bec0c47199827dfae5bfa57a6ac94b1/mae/mae.py#L48

Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b188730..a12be4c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,10 @@
 
 `mae` follows [Semantic Versioning](http://semver.org/).
 
+## [1.0.7] - 2019-02-28
+### Fixed
+- 🐞 Fixed a bug ([#1](https://github.com/paambaati/mae/issues/1)) where an app without labels can cause an empty response.
+
 ## [1.0.6] - 2018-12-31
 ### Added
 - Tests for CLI.
diff --git a/mae/__init__.py b/mae/__init__.py
index d2c89c9..a89ccfb 100644
--- a/mae/__init__.py
+++ b/mae/__init__.py
@@ -1,5 +1,5 @@
-__name__ = 'mae'
-__version__ = '1.0.6'
+__name__ = 'Mesos-App-Exporter'
+__version__ = '1.0.7'
 __author__ = 'GP'
 __maintainer__ = 'GP'
 __email__ = 'me@httgp.com'
diff --git a/mae/mae.py b/mae/mae.py
index 8626e9f..a333b5b 100644
--- a/mae/mae.py
+++ b/mae/mae.py
@@ -45,7 +45,7 @@ class MesosAppExporter:
 
         app_metrics_endpoints = list()
         for index, task in enumerate(tasks):
-            labels = task.get('labels').get('labels')
+            labels = task.get('labels', {}).get('labels', {})
             label_dict = dict()
             for label in labels:
                 label_dict.update({label.get('key'): label.get('value')})


Test patch:
diff --git a/tests/fixtures/task_data_4.json b/tests/fixtures/task_data_4.json
new file mode 100644
index 0000000..0e5bd6c
--- /dev/null
+++ b/tests/fixtures/task_data_4.json
@@ -0,0 +1,99 @@
+{
+	"get_tasks": {
+		"launched_tasks": [
+			{
+				"task_id": {
+					"value": "production.labelless-app.36dec447-0567-11e9-a0d5-12164c467f0e"
+				},
+				"name": "production.labelless-app",
+				"statuses": [
+					{
+						"executor_id": {
+							"value": "production.labelless-app.36dec447-0567-11e9-a0d5-12164c467f0e"
+						},
+						"uuid": "nPcAC+mhSnKooebv+6SnfA==",
+						"task_id": {
+							"value": "production.labelless-app.36dec447-0567-11e9-a0d5-12164c467f0e"
+						},
+						"healthy": true,
+						"timestamp": 1545427722.56431,
+						"container_status": {
+							"executor_pid": 7277,
+							"network_infos": [
+								{
+									"ip_addresses": [
+										{
+											"ip_address": "10.181.9.9"
+										}
+									]
+								}
+							]
+						},
+						"source": "SOURCE_EXECUTOR",
+						"state": "TASK_RUNNING",
+						"agent_id": {
+							"value": "d59f7ce8-7b0a-48e1-a71d-81ddad9cfa8c-S461"
+						}
+					}
+				],
+				"status_update_state": "TASK_RUNNING",
+				"status_update_uuid": "nPcAC+mhSnKooebv+6SnfA==",
+				"framework_id": {
+					"value": "545b613d-52cc-4f5f-922c-4dacc444e544-0000"
+				},
+				"state": "TASK_RUNNING",
+				"agent_id": {
+					"value": "d59f7ce8-7b0a-48e1-a71d-81ddad9cfa8c-S461"
+				},
+				"resources": [
+					{
+						"type": "SCALAR",
+						"scalar": {
+							"value": 2.0
+						},
+						"role": "*",
+						"name": "cpus"
+					},
+					{
+						"type": "SCALAR",
+						"scalar": {
+							"value": 14000.0
+						},
+						"role": "*",
+						"name": "mem"
+					},
+					{
+						"ranges": {
+							"range": [
+								{
+									"begin": 31064,
+									"end": 31065
+								}
+							]
+						},
+						"type": "RANGES",
+						"role": "*",
+						"name": "ports"
+					}
+				],
+				"discovery": {
+					"name": "production.labelless-app",
+					"visibility": "FRAMEWORK",
+					"ports": {
+						"ports": [
+							{
+								"protocol": "tcp",
+								"number": 31064
+							},
+							{
+								"protocol": "tcp",
+								"number": 31065
+							}
+						]
+					}
+				}
+			}
+		]
+	},
+	"type": "GET_TASKS"
+}
\ No newline at end of file
diff --git a/tests/test_mae.py b/tests/test_mae.py
index f421c40..e8a34c0 100644
--- a/tests/test_mae.py
+++ b/tests/test_mae.py
@@ -71,6 +71,14 @@ class TestMesosAppExporter(unittest.TestCase):
         endpoints = self.exporter.get_app_metrics_endpoints(task_data)
         self.assertEqual(len(endpoints), 0)
 
+    def test_mae_get_endpoint_4_no_labels(self):
+        """
+        Test if task data without any labels do not throw any errors.
+        """
+        task_data = self.__readJsonFile('fixtures/task_data_4.json')
+        endpoints = self.exporter.get_app_metrics_endpoints(task_data)
+        self.assertEqual(len(endpoints), 0)
+
     @patch('requests.get')
     def test_get_metrics_1_single_app(self, mock_request):
         """