From 521e2aad86799921de5515b2730227cde4428377 Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Wed, 9 Nov 2011 09:42:10 -0700
Subject: [PATCH] don't log calls to __heartbeat__ closes #537

---
 bin/browserid    | 15 +++++++++------
 lib/heartbeat.js | 29 ++++++++++++++++++++---------
 2 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/bin/browserid b/bin/browserid
index 3b9a07784..92c84f079 100755
--- a/bin/browserid
+++ b/bin/browserid
@@ -71,6 +71,8 @@ if (!config.get('keysigner_url')) {
   process.exit(1);
 }
 
+// NOTE: ordering is important in this file.  Pay attention
+
 function router(app) {
   app.set("views", path.join(__dirname, "..", "resources", "views"));
 
@@ -171,12 +173,6 @@ function router(app) {
   // register all the WSAPI handlers
   wsapi.setup(app);
 
-  // setup health check / heartbeat
-  heartbeat.setup(app, function(cb) {
-    // let's check stuff!  first the heartbeat of our keysigner
-    heartbeat.check(config.get('keysigner_url'), cb);
-  });
-
   // the public key
   app.get("/pk", function(req, res) {
     res.json(config.get('public_key').toSimpleObject());
@@ -203,6 +199,13 @@ function router(app) {
   });
 };
 
+// #1 - Setup health check / heartbeat middleware.
+// This is in front of logging on purpose.  see issue #537
+heartbeat.setup(app, function(cb) {
+  // let's check stuff!  first the heartbeat of our keysigner
+  heartbeat.check(config.get('keysigner_url'), cb);
+});
+
 // request to logger, dev formatted which omits personal data in the requests
 app.use(express.logger({
   format: config.get('express_log_format'),
diff --git a/lib/heartbeat.js b/lib/heartbeat.js
index faa697c01..109d8cd56 100644
--- a/lib/heartbeat.js
+++ b/lib/heartbeat.js
@@ -1,18 +1,29 @@
-const urlparse = require('urlparse');
+const
+urlparse = require('urlparse'),
+logger = require('./logging.js').logger;
 
 // the path that heartbeats live at
 exports.path = '/__heartbeat__';
 
 // a helper function to set up a heartbeat check
 exports.setup = function(app, cb) {
-  app.get(exports.path, function(req, res) {
-    function ok(yeah) {
-      res.writeHead(yeah ? 200 : 500);
-      res.write(yeah ? 'ok' : 'not ok');
-      res.end();
+  app.use(function(req, res, next) {
+    if (req.method === 'GET' && req.path === exports.path) {
+      function ok(yeah) {
+        res.writeHead(yeah ? 200 : 500);
+        res.write(yeah ? 'ok' : 'not ok');
+        res.end();
+      }
+      try {
+        if (cb) cb(ok);
+        else ok(true);
+      } catch(e) {
+        logger.error("Exception caught in heartbeat handler: " + e.toString());
+        ok(false);
+      }
+    } else {
+      return next();
     }
-    if (cb) cb(ok);
-    else ok(true);
   });
 };
 
@@ -35,4 +46,4 @@ exports.check = function(url, cb) {
     logger.error("can't communicate with " + shortname + ".  fatal: " + e);
     cb(false);
   });
-};
\ No newline at end of file
+};
-- 
GitLab