Browse Source

fix function monitor wrapper

dev_h
lovasoa 4 years ago
parent
commit
66b4700e3e
No account linked to committer's email address
2 changed files with 26 additions and 13 deletions
  1. 25
    12
      server/log.js
  2. 1
    1
      server/sockets.js

+ 25
- 12
server/log.js View File

30
 }
30
 }
31
 
31
 
32
 if (statsd) {
32
 if (statsd) {
33
-  setInterval(function reportHealth(){
34
-    statsd.gauge('memory', process.memoryUsage().heapUsed);
35
-  }, 1000);
33
+  setInterval(function reportHealth() {
34
+    statsd.gauge("memory", process.memoryUsage().heapUsed);
35
+  }, 30 * 1000);
36
 }
36
 }
37
 
37
 
38
 /**
38
 /**
44
   var msg = type;
44
   var msg = type;
45
   if (infos) msg += "\t" + JSON.stringify(infos);
45
   if (infos) msg += "\t" + JSON.stringify(infos);
46
   if (statsd) {
46
   if (statsd) {
47
-    const tags = {};
48
-    if (infos.board) tags.board = infos.board;
49
-    if (infos.original_ip) tags.original_ip = infos.original_ip;
50
-    statsd.increment(type, 1, tags);
47
+    let stat_name = type;
48
+    if (infos.board) stat_name += "." + infos.board;
49
+    statsd.increment(stat_name, 1);
51
   }
50
   }
52
   console.log(msg);
51
   console.log(msg);
53
 }
52
 }
58
  * @returns {F}
57
  * @returns {F}
59
  */
58
  */
60
 function monitorFunction(f) {
59
 function monitorFunction(f) {
61
-  if (statsd) return statsd.helpers.wrapCallback(f.name, f);
62
-  else return f;
60
+  if (!statsd) {
61
+    return f;
62
+  }
63
+  let client = statsd.getChildClient(f.name);
64
+  return function () {
65
+    let startTime = new Date();
66
+    try {
67
+      f.apply(null, arguments);
68
+      client.increment("ok", 1);
69
+    } catch (e) {
70
+      client.increment("err", 1);
71
+      throw e;
72
+    } finally {
73
+      client.timing("time", startTime);
74
+    }
75
+  };
63
 }
76
 }
64
 
77
 
65
 /**
78
 /**
66
  * Report a number
79
  * Report a number
67
- * @param {string} name 
68
- * @param {number} value 
80
+ * @param {string} name
81
+ * @param {number} value
69
  * @param {{[name:string]: string}=} tags
82
  * @param {{[name:string]: string}=} tags
70
  */
83
  */
71
-function gauge(name, value, tags){
84
+function gauge(name, value, tags) {
72
   if (statsd) statsd.gauge(name, value, tags);
85
   if (statsd) statsd.gauge(name, value, tags);
73
 }
86
 }
74
 
87
 

+ 1
- 1
server/sockets.js View File

72
 
72
 
73
   socket.on(
73
   socket.on(
74
     "error",
74
     "error",
75
-    noFail(function onError(error) {
75
+    noFail(function onSocketError(error) {
76
       log("ERROR", error);
76
       log("ERROR", error);
77
     })
77
     })
78
   );
78
   );

Loading…
Cancel
Save