libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r1485912 - in /libcloud/branches/0.12.x/libcloud: __init__.py common/base.py
Date Fri, 24 May 2013 00:58:46 GMT
Author: tomaz
Date: Fri May 24 00:58:45 2013
New Revision: 1485912

URL: http://svn.apache.org/r1485912
Log:
Add a utility function which prints some simple HTTP request statistics if
LIBCLOUD_DEBUG and LIBCLOUD_REQUESTS_STATS environment variable is set.

Modified:
    libcloud/branches/0.12.x/libcloud/__init__.py
    libcloud/branches/0.12.x/libcloud/common/base.py

Modified: libcloud/branches/0.12.x/libcloud/__init__.py
URL: http://svn.apache.org/viewvc/libcloud/branches/0.12.x/libcloud/__init__.py?rev=1485912&r1=1485911&r2=1485912&view=diff
==============================================================================
--- libcloud/branches/0.12.x/libcloud/__init__.py (original)
+++ libcloud/branches/0.12.x/libcloud/__init__.py Fri May 24 00:58:45 2013
@@ -22,6 +22,9 @@ libcloud provides a unified interface to
 __all__ = ['__version__', 'enable_debug']
 __version__ = '0.12.4'
 
+import os
+import atexit
+
 try:
     import paramiko
     have_paramiko = True
@@ -29,6 +32,9 @@ except ImportError:
     have_paramiko = False
 
 
+from libcloud.utils.debug import print_request_statistics
+
+
 def enable_debug(fo):
     """
     Enable library wide debugging to a file-like object.
@@ -38,12 +44,22 @@ def enable_debug(fo):
     """
     from libcloud.common.base import (Connection,
                                       LoggingHTTPConnection,
-                                      LoggingHTTPSConnection)
+                                      LoggingHTTPSConnection,
+                                      REQUESTS_LOG)
     LoggingHTTPSConnection.log = fo
     LoggingHTTPConnection.log = fo
     Connection.conn_classes = (LoggingHTTPConnection,
                                LoggingHTTPSConnection)
 
+    # Register a handler which prints some request statistics upon exit
+    enable_requests_stats = os.getenv('LIBCLOUD_REQUESTS_STATS')
+
+    if enable_requests_stats:
+        LoggingHTTPSConnection.enable_requests_stats = True
+        LoggingHTTPConnection.enable_requests_stats = True
+        atexit.register(print_request_statistics, fo=fo,
+                        requests_log=REQUESTS_LOG)
+
 
 def _init_once():
     """
@@ -52,7 +68,6 @@ def _init_once():
     This checks for the LIBCLOUD_DEBUG enviroment variable, which if it exists
     is where we will log debug information about the provider transports.
     """
-    import os
     path = os.getenv('LIBCLOUD_DEBUG')
     if path:
         fo = open(path, 'a')

Modified: libcloud/branches/0.12.x/libcloud/common/base.py
URL: http://svn.apache.org/viewvc/libcloud/branches/0.12.x/libcloud/common/base.py?rev=1485912&r1=1485911&r2=1485912&view=diff
==============================================================================
--- libcloud/branches/0.12.x/libcloud/common/base.py (original)
+++ libcloud/branches/0.12.x/libcloud/common/base.py Fri May 24 00:58:45 2013
@@ -44,6 +44,12 @@ from libcloud.httplib_ssl import Libclou
 LibcloudHTTPConnection = httplib.HTTPConnection
 
 
+# Stores information about all of the issued HTTP request.
+# Request logger is only active is LIBCLOUD_DEBUG and LIBCLOUD_REQUESTS_STATS
+# environment variable is set and should NOT be used in production.
+REQUESTS_LOG = []
+
+
 class HTTPResponse(httplib.HTTPResponse):
     # On python 2.6 some calls can hang because HEAD isn't quite properly
     # supported.
@@ -239,6 +245,7 @@ class LoggingConnection():
     @cvar log: file-like object that logs entries are written to.
     """
     log = None
+    enable_requests_stats = False
 
     def _log_response(self, r):
         rv = "# -------- begin %d:%d response ----------\n" % (id(self), id(r))
@@ -312,11 +319,17 @@ class LoggingConnection():
         if body is not None and len(body) > 0:
             cmd.extend(["--data-binary", pquote(body)])
 
+        url = pquote(self._get_url(path=url))
+
         cmd.extend(["--compress"])
-        cmd.extend([pquote("%s://%s:%d%s" % (self.protocol, self.host,
-                                             self.port, url))])
+        cmd.extend([url])
         return " ".join(cmd)
 
+    def _get_url(self, path):
+        url = '%s://%s:%d%s' % (self.protocol, self.host,
+                                self.port, path)
+        return url
+
 
 class LoggingHTTPSConnection(LoggingConnection, LibcloudHTTPSConnection):
     """
@@ -335,11 +348,18 @@ class LoggingHTTPSConnection(LoggingConn
 
     def request(self, method, url, body=None, headers=None):
         headers.update({'X-LC-Request-ID': str(id(self))})
+
         if self.log is not None:
             pre = "# -------- begin %d request ----------\n" % id(self)
             self.log.write(pre +
                            self._log_curl(method, url, body, headers) + "\n")
             self.log.flush()
+
+        if self.enable_requests_stats:
+            full_url = self._get_url(path=url)
+            obj = {'method': method, 'url': full_url}
+            REQUESTS_LOG.append(obj)
+
         return LibcloudHTTPSConnection.request(self, method, url, body,
                                                headers)
 



Mime
View raw message