Pārlūkot izejas kodu

HUE-65. Make dbug.(info|warn|error) messages use the log_frontend_event feature of HUE-60

Aaron T. Myers 15 gadi atpakaļ
vecāks
revīzija
ca98d3e154

+ 7 - 0
desktop/core/src/desktop/conf.py

@@ -226,6 +226,13 @@ FEEDBACK_URL = Config(
   dynamic_default=default_feedback_url
 )
 
+SEND_DBUG_MESSAGES = Config(
+  key="send_dbug_messages",
+  help="Whether to send dbug messages from JavaScript to the server logs.",
+  type=coerce_bool,
+  default=False
+)
+
 DATABASE_LOGGING = Config(
   key="database_logging",
   help="If true, log all database requests.",

+ 8 - 0
desktop/core/src/desktop/templates/index.mako

@@ -161,6 +161,14 @@
     });
   });
   </script>
+
+  <script>
+    % if send_dbug_messages:
+      window.sendDbug = true;
+    % else:
+      window.sendDbug = false;
+    % endif
+  </script>
 </head>
 <body>
   <div id="bg">

+ 2 - 1
desktop/core/src/desktop/views.py

@@ -196,7 +196,8 @@ def threads(request):
 @login_notrequired
 def index(request):
   return render("index.mako", request, dict(
-    feedback_url=desktop.conf.FEEDBACK_URL.get()
+    feedback_url=desktop.conf.FEEDBACK_URL.get(),
+    send_dbug_messages=desktop.conf.SEND_DBUG_MESSAGES.get()
   ))
 
 def serve_404_error(request, *args, **kwargs):

+ 46 - 0
desktop/core/static/js/Source/CCS/CCS.Desktop.Config.js

@@ -194,3 +194,49 @@ if (Browser.Engine.trident) {
 
 	Array.alias('forEach', 'each', true); 
 }
+
+// Monkey-patch the dbug.* functions to also log their events to the server. We don't want to
+// crush the server either with too-many or too-frequent messages, so we only send every 5 seconds,
+// unless there are more than 100 messages in the message queue.
+(function(info, warn, error) {
+
+  var monkeyPatchDbugFunction = function(dbugMethod, level) {
+    var messageQueue = [];
+
+    var sendQueuedMessages = function() {
+      if (window.sendDbug && messageQueue.length > 0) {
+        new Request.JSON({
+          url: '/log_frontend_event',
+          data: {
+            message: JSON.encode(messageQueue),
+            level: level
+          }
+        }).post();
+        // Immediately clear the queue after we try to send it.
+        // If the send fails, oh well.
+        messageQueue.empty();
+      }
+    };
+
+    // Poll the message queue every 5 seconds to see if it has messages to send.
+    sendQueuedMessages.periodical(5000);
+
+    return function(message) {
+      messageQueue.push(message);
+      // Immediately send the message queue if it's getting too big.
+      if (messageQueue.length > 100) {
+        sendQueuedMessages();
+      }
+      dbugMethod(message);
+    };
+  };
+
+  // We do the monkey-patching of the functions regardless of the initial value
+  // of window.sendDbug so that some one can turn this on in the browser at
+  // run-time without having to restart the server.
+  //
+  // These strings are what hue expects.
+  dbug.info = monkeyPatchDbugFunction(info, 'info');
+  dbug.warn = monkeyPatchDbugFunction(warn, 'warning');
+  dbug.error = monkeyPatchDbugFunction(error, 'error');
+})(dbug.info, dbug.warn, dbug.error);