open_sockets.coffee 757 B

12345678910111213141516171819202122
  1. URL = require "url"
  2. seconds = 1000
  3. module.exports = OpenSocketsMonitor =
  4. monitor: (logger) ->
  5. interval = setInterval () ->
  6. OpenSocketsMonitor.gaugeOpenSockets()
  7. , 5 * seconds
  8. Metrics = require "./metrics"
  9. Metrics.registerDestructor () ->
  10. clearInterval(interval)
  11. gaugeOpenSockets: () ->
  12. Metrics = require "./metrics"
  13. for url, agents of require('http').globalAgent.sockets
  14. url = URL.parse("http://#{url}")
  15. hostname = url.hostname?.replace(/\./g, "_")
  16. Metrics.gauge "open_connections.http.#{hostname}", agents.length
  17. for url, agents of require('https').globalAgent.sockets
  18. url = URL.parse("https://#{url}")
  19. hostname = url.hostname?.replace(/\./g, "_")
  20. Metrics.gauge "open_connections.https.#{hostname}", agents.length