|
| 1 | +os = require 'os' |
| 2 | +{ argv } = require 'optimist' |
| 3 | +KONFIG = require('koding-config-manager').load("main.#{argv.c}") |
| 4 | +appmetrics = require 'appmetrics' |
| 5 | +MetricsBase = require './metricsbase' |
| 6 | + |
| 7 | +module.exports = class NodejsProfiler |
| 8 | + |
| 9 | + constructor : (@prefix) -> |
| 10 | + |
| 11 | + disabledTypes = ['profiling', 'mongo', 'http', 'socketio', 'mqlight' |
| 12 | + 'postgresql', 'mqtt', 'mysql', 'redis', 'memcached', 'requests', 'trace'] |
| 13 | + |
| 14 | + disabledTypes.forEach (type) -> |
| 15 | + appmetrics.disable(type) |
| 16 | + |
| 17 | + @monitor = appmetrics.monitor() |
| 18 | + |
| 19 | + |
| 20 | + populateNodejsTags : (tags) -> |
| 21 | + |
| 22 | + tags ?= [] |
| 23 | + |
| 24 | + tagList = { |
| 25 | + os : [ 'type', 'release', 'hostname' ] |
| 26 | + process : [ 'pid', 'version' ] |
| 27 | + } |
| 28 | + |
| 29 | + for key, value of tagList |
| 30 | + for prop in value |
| 31 | + tagName = "#{key}.#{prop}" |
| 32 | + tagValue = '' |
| 33 | + |
| 34 | + source = switch |
| 35 | + when key is 'process' then process |
| 36 | + when key is 'os' then os |
| 37 | + |
| 38 | + tagValue = switch |
| 39 | + when not source[prop] then 0 |
| 40 | + when Array.isArray source[prop] then source[prop].join('_') |
| 41 | + when typeof source[prop] is 'function' then source[prop].call() |
| 42 | + else source[prop] |
| 43 | + |
| 44 | + tagValue = MetricsBase.sanitizeTagValue tagValue |
| 45 | + tags.push "#{tagName}:#{tagValue}" |
| 46 | + |
| 47 | + return tags |
| 48 | + |
| 49 | + |
| 50 | + startMonitoring : -> |
| 51 | + |
| 52 | + prefix = @prefix |
| 53 | + |
| 54 | + tags = @populateNodejsTags() |
| 55 | + tags.push "version:#{KONFIG.version}" |
| 56 | + |
| 57 | + @monitor.on 'eventloop', (eventloop) -> |
| 58 | + eventloopMetrics = |
| 59 | + gauge : |
| 60 | + 'latency.min' : eventloop.latency.min |
| 61 | + 'latency.max' : eventloop.latency.max |
| 62 | + 'latency.avg' : eventloop.latency.avg |
| 63 | + |
| 64 | + MetricsBase.sendMetrics eventloopMetrics, "#{prefix}.eventloop", tags |
| 65 | + |
| 66 | + @monitor.on 'memory', (memory) -> |
| 67 | + memMetrics = |
| 68 | + gauge : |
| 69 | + 'process.private' : memory.private |
| 70 | + 'process.physical' : memory.physical |
| 71 | + 'process.virtual' : memory.virtual |
| 72 | + 'system.used' : memory.physical_used |
| 73 | + 'system.total' : memory.physical_total |
| 74 | + |
| 75 | + MetricsBase.sendMetrics memMetrics, "#{prefix}.memory", tags |
| 76 | + |
| 77 | + @monitor.on 'gc', (gc) -> |
| 78 | + gcMetrics = |
| 79 | + gauge : |
| 80 | + size : gc.size |
| 81 | + used : gc.used |
| 82 | + duration : gc.duration |
| 83 | + |
| 84 | + MetricsBase.sendMetrics gcMetrics, "#{prefix}.gc", tags |
| 85 | + |
| 86 | + @monitor.on 'cpu', (cpu) -> |
| 87 | + cpuMetrics = |
| 88 | + gauge : |
| 89 | + process : cpu.process |
| 90 | + system : cpu.system |
| 91 | + |
| 92 | + MetricsBase.sendMetrics cpuMetrics, "#{prefix}.cpu", tags |
| 93 | + |
0 commit comments