prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <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.
  
  
  </issue>
  <code>
  [start of checks.d/marathon.py]
  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 of checks.d/marathon.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: 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)
  
verification_info: {"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"}]}