We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[marathon] Marathon plugin slows down agent when marathon has many apps running
We are monitoring a marathon framework using datadog which has over 150 apps, and the marathon check seems to be slowing down the entire datadog process.

After investigating what the plugin actually does, the problem seems to be this loop: https://github.com/DataDog/dd-agent/blob/5.4.4/checks.d/marathon.py#L46.  It appears that the agent is sequentially hitting the API 150 times, which is enough to stop the agent from reporting metrics long enough to trigger some of our other alerts.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `checks.d/marathon.py`
Content:
```
1 # (C) Datadog, Inc. 2014-2016
2 # (C)  graemej <graeme.johnson@jadedpixel.com> 2014
3 # All rights reserved
4 # Licensed under Simplified BSD License (see LICENSE)
5 
6 
7 # stdlib
8 from urlparse import urljoin
9 
10 # 3rd party
11 import requests
12 
13 # project
14 from checks import AgentCheck
15 
16 
17 class Marathon(AgentCheck):
18 
19     DEFAULT_TIMEOUT = 5
20     SERVICE_CHECK_NAME = 'marathon.can_connect'
21 
22     APP_METRICS = [
23         'backoffFactor',
24         'backoffSeconds',
25         'cpus',
26         'disk',
27         'instances',
28         'mem',
29         'taskRateLimit',
30         'tasksRunning',
31         'tasksStaged'
32     ]
33 
34     def check(self, instance):
35         if 'url' not in instance:
36             raise Exception('Marathon instance missing "url" value.')
37 
38         # Load values from the instance config
39         url = instance['url']
40         user = instance.get('user')
41         password = instance.get('password')
42         if user is not None and password is not None:
43             auth = (user,password)
44         else:
45             auth = None
46         instance_tags = instance.get('tags', [])
47         default_timeout = self.init_config.get('default_timeout', self.DEFAULT_TIMEOUT)
48         timeout = float(instance.get('timeout', default_timeout))
49 
50         response = self.get_json(urljoin(url, "/v2/apps"), timeout, auth)
51         if response is not None:
52             self.gauge('marathon.apps', len(response['apps']), tags=instance_tags)
53             for app in response['apps']:
54                 tags = ['app_id:' + app['id'], 'version:' + app['version']] + instance_tags
55                 for attr in self.APP_METRICS:
56                     if attr in app:
57                         self.gauge('marathon.' + attr, app[attr], tags=tags)
58 
59                 query_url = urljoin(url, "/v2/apps/{0}/versions".format(app['id']))
60                 versions_reply = self.get_json(query_url, timeout, auth)
61 
62                 if versions_reply is not None:
63                     self.gauge('marathon.versions', len(versions_reply['versions']), tags=tags)
64 
65     def get_json(self, url, timeout, auth):
66         try:
67             r = requests.get(url, timeout=timeout, auth=auth)
68             r.raise_for_status()
69         except requests.exceptions.Timeout:
70             # If there's a timeout
71             self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL,
72                 message='%s timed out after %s seconds.' % (url, timeout),
73                 tags = ["url:{0}".format(url)])
74             raise Exception("Timeout when hitting %s" % url)
75 
76         except requests.exceptions.HTTPError:
77             self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL,
78                 message='%s returned a status of %s' % (url, r.status_code),
79                 tags = ["url:{0}".format(url)])
80             raise Exception("Got %s when hitting %s" % (r.status_code, url))
81 
82         else:
83             self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK,
84                 tags = ["url:{0}".format(url)]
85             )
86 
87         return r.json()
88
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/checks.d/marathon.py b/checks.d/marathon.py
--- a/checks.d/marathon.py
+++ b/checks.d/marathon.py
@@ -56,12 +56,6 @@
                     if attr in app:
                         self.gauge('marathon.' + attr, app[attr], tags=tags)
 
-                query_url = urljoin(url, "/v2/apps/{0}/versions".format(app['id']))
-                versions_reply = self.get_json(query_url, timeout, auth)
-
-                if versions_reply is not None:
-                    self.gauge('marathon.versions', len(versions_reply['versions']), tags=tags)
-
     def get_json(self, url, timeout, auth):
         try:
             r = requests.get(url, timeout=timeout, auth=auth)

{"golden_diff": "diff --git a/checks.d/marathon.py b/checks.d/marathon.py\n--- a/checks.d/marathon.py\n+++ b/checks.d/marathon.py\n@@ -56,12 +56,6 @@\n                     if attr in app:\n                         self.gauge('marathon.' + attr, app[attr], tags=tags)\n \n-                query_url = urljoin(url, \"/v2/apps/{0}/versions\".format(app['id']))\n-                versions_reply = self.get_json(query_url, timeout, auth)\n-\n-                if versions_reply is not None:\n-                    self.gauge('marathon.versions', len(versions_reply['versions']), tags=tags)\n-\n     def get_json(self, url, timeout, auth):\n         try:\n             r = requests.get(url, timeout=timeout, auth=auth)\n", "issue": "[marathon] Marathon plugin slows down agent when marathon has many apps running\nWe are monitoring a marathon framework using datadog which has over 150 apps, and the marathon check seems to be slowing down the entire datadog process.\n\nAfter investigating what the plugin actually does, the problem seems to be this loop: https://github.com/DataDog/dd-agent/blob/5.4.4/checks.d/marathon.py#L46.  It appears that the agent is sequentially hitting the API 150 times, which is enough to stop the agent from reporting metrics long enough to trigger some of our other alerts.\n\n", "before_files": [{"content": "# (C) Datadog, Inc. 2014-2016\n# (C)  graemej <graeme.johnson@jadedpixel.com> 2014\n# All rights reserved\n# Licensed under Simplified BSD License (see LICENSE)\n\n\n# stdlib\nfrom urlparse import urljoin\n\n# 3rd party\nimport requests\n\n# project\nfrom checks import AgentCheck\n\n\nclass Marathon(AgentCheck):\n\n    DEFAULT_TIMEOUT = 5\n    SERVICE_CHECK_NAME = 'marathon.can_connect'\n\n    APP_METRICS = [\n        'backoffFactor',\n        'backoffSeconds',\n        'cpus',\n        'disk',\n        'instances',\n        'mem',\n        'taskRateLimit',\n        'tasksRunning',\n        'tasksStaged'\n    ]\n\n    def check(self, instance):\n        if 'url' not in instance:\n            raise Exception('Marathon instance missing \"url\" value.')\n\n        # Load values from the instance config\n        url = instance['url']\n        user = instance.get('user')\n        password = instance.get('password')\n        if user is not None and password is not None:\n            auth = (user,password)\n        else:\n            auth = None\n        instance_tags = instance.get('tags', [])\n        default_timeout = self.init_config.get('default_timeout', self.DEFAULT_TIMEOUT)\n        timeout = float(instance.get('timeout', default_timeout))\n\n        response = self.get_json(urljoin(url, \"/v2/apps\"), timeout, auth)\n        if response is not None:\n            self.gauge('marathon.apps', len(response['apps']), tags=instance_tags)\n            for app in response['apps']:\n                tags = ['app_id:' + app['id'], 'version:' + app['version']] + instance_tags\n                for attr in self.APP_METRICS:\n                    if attr in app:\n                        self.gauge('marathon.' + attr, app[attr], tags=tags)\n\n                query_url = urljoin(url, \"/v2/apps/{0}/versions\".format(app['id']))\n                versions_reply = self.get_json(query_url, timeout, auth)\n\n                if versions_reply is not None:\n                    self.gauge('marathon.versions', len(versions_reply['versions']), tags=tags)\n\n    def get_json(self, url, timeout, auth):\n        try:\n            r = requests.get(url, timeout=timeout, auth=auth)\n            r.raise_for_status()\n        except requests.exceptions.Timeout:\n            # If there's a timeout\n            self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL,\n                message='%s timed out after %s seconds.' % (url, timeout),\n                tags = [\"url:{0}\".format(url)])\n            raise Exception(\"Timeout when hitting %s\" % url)\n\n        except requests.exceptions.HTTPError:\n            self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL,\n                message='%s returned a status of %s' % (url, r.status_code),\n                tags = [\"url:{0}\".format(url)])\n            raise Exception(\"Got %s when hitting %s\" % (r.status_code, url))\n\n        else:\n            self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK,\n                tags = [\"url:{0}\".format(url)]\n            )\n\n        return r.json()\n", "path": "checks.d/marathon.py"}], "after_files": [{"content": "# (C) Datadog, Inc. 2014-2016\n# (C)  graemej <graeme.johnson@jadedpixel.com> 2014\n# All rights reserved\n# Licensed under Simplified BSD License (see LICENSE)\n\n\n# stdlib\nfrom urlparse import urljoin\n\n# 3rd party\nimport requests\n\n# project\nfrom checks import AgentCheck\n\n\nclass Marathon(AgentCheck):\n\n    DEFAULT_TIMEOUT = 5\n    SERVICE_CHECK_NAME = 'marathon.can_connect'\n\n    APP_METRICS = [\n        'backoffFactor',\n        'backoffSeconds',\n        'cpus',\n        'disk',\n        'instances',\n        'mem',\n        'taskRateLimit',\n        'tasksRunning',\n        'tasksStaged'\n    ]\n\n    def check(self, instance):\n        if 'url' not in instance:\n            raise Exception('Marathon instance missing \"url\" value.')\n\n        # Load values from the instance config\n        url = instance['url']\n        user = instance.get('user')\n        password = instance.get('password')\n        if user is not None and password is not None:\n            auth = (user,password)\n        else:\n            auth = None\n        instance_tags = instance.get('tags', [])\n        default_timeout = self.init_config.get('default_timeout', self.DEFAULT_TIMEOUT)\n        timeout = float(instance.get('timeout', default_timeout))\n\n        response = self.get_json(urljoin(url, \"/v2/apps\"), timeout, auth)\n        if response is not None:\n            self.gauge('marathon.apps', len(response['apps']), tags=instance_tags)\n            for app in response['apps']:\n                tags = ['app_id:' + app['id'], 'version:' + app['version']] + instance_tags\n                for attr in self.APP_METRICS:\n                    if attr in app:\n                        self.gauge('marathon.' + attr, app[attr], tags=tags)\n\n    def get_json(self, url, timeout, auth):\n        try:\n            r = requests.get(url, timeout=timeout, auth=auth)\n            r.raise_for_status()\n        except requests.exceptions.Timeout:\n            # If there's a timeout\n            self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL,\n                message='%s timed out after %s seconds.' % (url, timeout),\n                tags = [\"url:{0}\".format(url)])\n            raise Exception(\"Timeout when hitting %s\" % url)\n\n        except requests.exceptions.HTTPError:\n            self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL,\n                message='%s returned a status of %s' % (url, r.status_code),\n                tags = [\"url:{0}\".format(url)])\n            raise Exception(\"Got %s when hitting %s\" % (r.status_code, url))\n\n        else:\n            self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK,\n                tags = [\"url:{0}\".format(url)]\n            )\n\n        return r.json()\n", "path": "checks.d/marathon.py"}]}