diff --git a/libraries/metrics/metrics.coffee b/libraries/metrics/metrics.coffee index ea5c5f8e1d..0e0709d74b 100644 --- a/libraries/metrics/metrics.coffee +++ b/libraries/metrics/metrics.coffee @@ -5,6 +5,8 @@ traceAgent = require('@google-cloud/trace-agent') debugAgent = require('@google-cloud/debug-agent') prom = require('prom-client') +Register = require('prom-client').register +collectDefaultMetrics = prom.collectDefaultMetrics name = "unknown" hostname = require('os').hostname() @@ -12,6 +14,9 @@ hostname = require('os').hostname() buildKey = (key)-> "#{name}.#{hostname}.#{key}" buildGlobalKey = (key)-> "#{name}.global.#{key}" +counters = {} +gauges = {} + destructors = [] require "./uv_threadpool_size" @@ -30,14 +35,36 @@ module.exports = Metrics = } }) + collectDefaultMetrics({ timeout: 5000, prefix: name + "_" }) + registerDestructor: (func) -> destructors.push func + injectMetricsRoute: (app) -> + app.get('/metrics', (req, res) -> + res.set('Content-Type', Register.contentType) + res.end(Register.metrics()) + ) + + sanitizeKey: (key) -> + key.replace /[^a-zA-Z0-9]/g, "_" + + sanitizeValue: (value) -> + parseFloat(value) + set : (key, value, sampleRate = 1)-> statsd.set buildKey(key), value, sampleRate inc : (key, sampleRate = 1)-> statsd.increment buildKey(key), sampleRate + key = this.sanitizeKey(key) + if !counters[key] + counters[key] = new prom.Counter({ + name: key, + help: key, + labelNames: ['name','host'] + }) + counters[key].inc({name: name, host: hostname}) count : (key, count, sampleRate = 1)-> statsd.count buildKey(key), count, sampleRate @@ -57,9 +84,25 @@ module.exports = Metrics = gauge : (key, value, sampleRate = 1)-> statsd.gauge buildKey(key), value, sampleRate + key = this.sanitizeKey(key) + if !gauges[key] + gauges[key] = new prom.Gauge({ + name: key, + help: key, + labelNames: ['name','host'] + }) + gauges[key].set({name: name, host: hostname},this.sanitizeValue(value)) globalGauge: (key, value, sampleRate = 1)-> statsd.gauge buildGlobalKey(key), value, sampleRate + key = this.sanitizeKey(key) + if !gauges[key] + gauges[key] = new prom.Gauge({ + name: key, + help: key, + labelNames: ['name','host'] + }) + gauges[key].set({name: name},this.sanitizeValue(value)) mongodb: require "./mongodb" http: require "./http" diff --git a/libraries/metrics/package.json b/libraries/metrics/package.json index 648d4d627b..450b588158 100644 --- a/libraries/metrics/package.json +++ b/libraries/metrics/package.json @@ -1,6 +1,6 @@ { "name": "metrics-sharelatex", - "version": "1.8.1", + "version": "1.9.0", "description": "A drop-in metrics and monitoring module for node.js apps", "repository": { "type": "git",