open_sockets.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * decaffeinate suggestions:
  3. * DS102: Remove unnecessary code created because of implicit returns
  4. * DS205: Consider reworking code to avoid use of IIFEs
  5. * DS207: Consider shorter variations of null checks
  6. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  7. */
  8. let OpenSocketsMonitor;
  9. const URL = require("url");
  10. const seconds = 1000;
  11. // In Node 0.10 the default is 5, which means only 5 open connections at one.
  12. // Node 0.12 has a default of Infinity. Make sure we have no limit set,
  13. // regardless of Node version.
  14. require("http").globalAgent.maxSockets = Infinity;
  15. require("https").globalAgent.maxSockets = Infinity;
  16. module.exports = (OpenSocketsMonitor = {
  17. monitor(logger) {
  18. const interval = setInterval(() => OpenSocketsMonitor.gaugeOpenSockets()
  19. , 5 * seconds);
  20. const Metrics = require("./metrics");
  21. return Metrics.registerDestructor(() => clearInterval(interval));
  22. },
  23. gaugeOpenSockets() {
  24. let agents, hostname, url;
  25. const Metrics = require("./metrics");
  26. const object = require('http').globalAgent.sockets;
  27. for (url in object) {
  28. agents = object[url];
  29. url = URL.parse(`http://${url}`);
  30. hostname = url.hostname != null ? url.hostname.replace(/\./g, "_") : undefined;
  31. Metrics.gauge(`open_connections.http.${hostname}`, agents.length);
  32. }
  33. return (() => {
  34. const result = [];
  35. const object1 = require('https').globalAgent.sockets;
  36. for (url in object1) {
  37. agents = object1[url];
  38. url = URL.parse(`https://${url}`);
  39. hostname = url.hostname != null ? url.hostname.replace(/\./g, "_") : undefined;
  40. result.push(Metrics.gauge(`open_connections.https.${hostname}`, agents.length));
  41. }
  42. return result;
  43. })();
  44. }
  45. });