diff --git a/libraries/metrics/metrics.coffee b/libraries/metrics/metrics.coffee index 7e2927e7ae..80dbf3cd03 100644 --- a/libraries/metrics/metrics.coffee +++ b/libraries/metrics/metrics.coffee @@ -1,6 +1,3 @@ -StatsD = require('lynx') -statsd = new StatsD(process.env["STATSD_HOST"] or "localhost", 8125, {on_error:->}) - traceAgent = require('@google-cloud/trace-agent') debugAgent = require('@google-cloud/debug-agent') @@ -8,7 +5,7 @@ prom = require('prom-client') Register = require('prom-client').register collectDefaultMetrics = prom.collectDefaultMetrics -name = "unknown" +appname = "unknown" hostname = require('os').hostname() buildKey = (key)-> "#{name}.#{hostname}.#{key}" @@ -24,13 +21,13 @@ require "./uv_threadpool_size" module.exports = Metrics = initialize: (_name) -> - name = _name + appname = _name collectDefaultMetrics({ timeout: 5000, prefix: Metrics.buildPromKey()}) traceAgent.start() debugAgent.start({ serviceContext: { allowExpressions: true, - service: name, + service: appname, version: '0.0.1' } }) @@ -45,7 +42,7 @@ module.exports = Metrics = ) buildPromKey: (key = "")-> - Metrics.sanitizeKey "#{name}_#{key}" + Metrics.sanitizeKey key sanitizeKey: (key) -> key.replace /[^a-zA-Z0-9]/g, "_" @@ -54,32 +51,29 @@ module.exports = Metrics = parseFloat(value) set : (key, value, sampleRate = 1)-> - statsd.set buildKey(key), value, sampleRate inc : (key, sampleRate = 1)-> - statsd.increment buildKey(key), sampleRate key = Metrics.buildPromKey(key) if !promMetrics[key]? promMetrics[key] = new prom.Counter({ name: key, help: key, - labelNames: ['name','host'] + labelNames: ['app','host'] }) - promMetrics[key].inc({name: name, host: hostname}) + console.log("doing inc", key, appname) + promMetrics[key].inc({app: appname, host: hostname}) count : (key, count, sampleRate = 1)-> - statsd.count buildKey(key), count, sampleRate key = Metrics.buildPromKey(key) if !promMetrics[key]? promMetrics[key] = new prom.Counter({ name: key, help: key, - labelNames: ['name','host'] + labelNames: ['app','host'] }) - promMetrics[key].inc({name: name, host: hostname}, count) + promMetrics[key].inc({app: appname, host: hostname}, count) timing: (key, timeSpan, sampleRate, opts = {})-> - statsd.timing(buildKey(key), timeSpan, sampleRate) key = Metrics.sanitizeKey("timer_" + key) if !promMetrics[key]? promMetrics[key] = new prom.Summary({ @@ -89,12 +83,14 @@ module.exports = Metrics = ageBuckets: 10, labelNames: ['app', 'path', 'status_code', 'method', 'collection', 'query'] }) - opts.app = name + opts.app = appname + console.log("doing timing", key, opts) promMetrics[key].observe(opts, timeSpan) Timer : class constructor :(key, sampleRate = 1, opts)-> this.start = new Date() + console.log("creating new timer", key) key = Metrics.sanitizeKey(key) this.key = key this.sampleRate = sampleRate @@ -106,7 +102,6 @@ module.exports = Metrics = return timeSpan gauge : (key, value, sampleRate = 1)-> - statsd.gauge buildKey(key), value, sampleRate key = Metrics.buildPromKey(key) if !promMetrics[key]? promMetrics[key] = new prom.Gauge({ @@ -114,10 +109,9 @@ module.exports = Metrics = help: key, labelNames: ['app','host'] }) - promMetrics[key].set({app: name, host: hostname}, this.sanitizeValue(value)) + promMetrics[key].set({app: appname, host: hostname}, this.sanitizeValue(value)) globalGauge: (key, value, sampleRate = 1)-> - statsd.gauge buildGlobalKey(key), value, sampleRate key = Metrics.buildPromKey(key) if !promMetrics[key]? promMetrics[key] = new prom.Gauge({ @@ -125,7 +119,7 @@ module.exports = Metrics = help: key, labelNames: ['app','host'] }) - promMetrics[key].set({app: name},this.sanitizeValue(value)) + promMetrics[key].set({app: appname},this.sanitizeValue(value)) mongodb: require "./mongodb" http: require "./http" @@ -138,4 +132,3 @@ module.exports = Metrics = close: () -> for func in destructors func() - statsd.close() diff --git a/libraries/metrics/timeAsyncMethod.coffee b/libraries/metrics/timeAsyncMethod.coffee index 6986299390..75eb9a131f 100644 --- a/libraries/metrics/timeAsyncMethod.coffee +++ b/libraries/metrics/timeAsyncMethod.coffee @@ -6,8 +6,14 @@ module.exports = (obj, methodName, prefix, logger) -> throw new Error("[Metrics] expected object property '#{methodName}' to be a function") realMethod = obj[methodName] - key = "#{prefix}.#{methodName}" + keys = prefix.split(".") + key = keys[0].toLowerCase() + + if keys[1]? + methodName = "#{keys[1]}_#{methodName}" + + console.log "Async method", keys, methodName obj[methodName] = (originalArgs...) -> [firstArgs..., callback] = originalArgs @@ -17,7 +23,8 @@ module.exports = (obj, methodName, prefix, logger) -> logger.log "[Metrics] expected wrapped method '#{methodName}' to be invoked with a callback" return realMethod.apply this, originalArgs - timer = new metrics.Timer(prefix, null, {method: methodName}) + console.log("creating timer for async method") + timer = new metrics.Timer(key, null, {method: methodName}) realMethod.call this, firstArgs..., (callbackArgs...) -> elapsedTime = timer.done()