Use new metrics package

This commit is contained in:
James Allen 2014-05-07 15:29:04 +01:00
parent 628fb65bc3
commit b29e94b4d5
7 changed files with 8 additions and 121 deletions

View file

@ -3,6 +3,8 @@ logger = require 'logger-sharelatex'
logger.initialize("web-sharelatex") logger.initialize("web-sharelatex")
logger.logger.serializers.user = require("./app/js/infrastructure/LoggerSerializers").user logger.logger.serializers.user = require("./app/js/infrastructure/LoggerSerializers").user
logger.logger.serializers.project = require("./app/js/infrastructure/LoggerSerializers").project logger.logger.serializers.project = require("./app/js/infrastructure/LoggerSerializers").project
metrics = require("metrics-sharelatex")
metrics.initialize("web")
Server = require("./app/js/infrastructure/Server") Server = require("./app/js/infrastructure/Server")
BackgroundTasks = require("./app/js/infrastructure/BackgroundTasks") BackgroundTasks = require("./app/js/infrastructure/BackgroundTasks")
Errors = require "./app/js/errors" Errors = require "./app/js/errors"

View file

@ -1,27 +1 @@
StatsD = require('lynx') module.exports = require("metrics-sharelatex")
settings = require('settings-sharelatex')
statsd = new StatsD('localhost', 8125, {on_error:->})
buildKey = (key)-> "web.#{process.env.NODE_ENV}.#{key}"
module.exports =
set : (key, value, sampleRate = 1)->
statsd.set buildKey(key), value, sampleRate
inc : (key, sampleRate = 1)->
statsd.increment buildKey(key), sampleRate
timing: (key, timeSpan, sampleRate)->
statsd.timing(key, timeSpan, sampleRate)
Timer : class
constructor :(key, sampleRate = 1)->
this.start = new Date()
this.key = buildKey(key)
done:->
timeSpan = new Date - this.start
statsd.timing(this.key, timeSpan, this.sampleRate)
gauge : (key, value, sampleRate = 1)->
statsd.gauge key, value, sampleRate

View file

@ -1,5 +0,0 @@
require("./Monitor/MongoDB").monitor()
exports.logger = require("./Monitor/HTTP").logger

View file

@ -1,30 +0,0 @@
settings = require("settings-sharelatex")
logger = require "logger-sharelatex"
os = require("os")
metrics = require("../Metrics")
module.exports.logger = (req, res, next) ->
startTime = new Date()
end = res.end
res.end = () ->
end.apply(this, arguments)
responseTime = new Date() - startTime
routePath = req.route.path.toString().replace(/\//g, '-').slice(1)
processName = if settings.internal.web.name? then "web-#{settings.internal.web.name}" else "web"
key = "#{os.hostname()}.#{processName}.#{routePath}".toLowerCase().trim()
logger.log
req:
url: req.originalUrl || req.url
method: req.method
key:key
referrer: req.headers['referer'] || req.headers['referrer']
"remote-addr": req.ip || req.socket?.socket?.remoteAddress || req.socket?.remoteAddress
"user-agent": req.headers["user-agent"]
"content-length": req.headers["content-length"]
res:
"content-length": res._headers?["content-length"]
"response-time": responseTime
"http request"
next()

View file

@ -1,57 +0,0 @@
Connection = require("mongoose/node_modules/mongodb/lib/mongodb/connection/connection").Connection
MongoReply = require("mongoose/node_modules/mongodb/lib/mongodb/responses/mongo_reply").MongoReply
Metrics = require("../Metrics")
logger = require "logger-sharelatex"
_ = require("underscore")
connectionMonitor =
newConnection: (id, db) ->
Metrics.inc "mongo-requests"
@connections[id] =
timer: new Metrics.Timer("mongo-request-times")
db: db
start: new Date()
setTimeout (=> @connectionDone(id)), 60000
connectionDone: (id) ->
queryIsNoise = (query)->
isNoise = false
if query? && _.isObject(query)
keys = _.keys(query)
if keys[0] == "ismaster" or keys[0] == "ping"
isNoise = true
return isNoise
logItOut = (db)->
logger.log
request_id: db.requestId,
query: db.query,
collection: db.collectionName,
"response-time": new Date() - start
"mongo request"
if @connections[id]?
@connections[id].timer.done()
db = @connections[id].db
start = @connections[id].start
if !queryIsNoise(db.query)
logItOut(db)
delete @connections[id]
connections: {}
monkeyPatchMongo = () ->
write = Connection::write
Connection::write = (db) ->
write.apply(this, arguments)
connectionMonitor.newConnection db.requestId, db
parseHeader = MongoReply::parseHeader
MongoReply::parseHeader = () ->
parseHeader.apply this, arguments
connectionMonitor.connectionDone this.responseTo
module.exports.monitor = monkeyPatchMongo

View file

@ -1,3 +1,4 @@
Path = require "path"
express = require('express') express = require('express')
Settings = require('settings-sharelatex') Settings = require('settings-sharelatex')
logger = require 'logger-sharelatex' logger = require 'logger-sharelatex'
@ -16,7 +17,8 @@ cookieParser = express.cookieParser(Settings.security.sessionSecret)
oneDayInMilliseconds = 86400000 oneDayInMilliseconds = 86400000
ReferalConnect = require('../Features/Referal/ReferalConnect') ReferalConnect = require('../Features/Referal/ReferalConnect')
Monitor = require "./Monitor" metrics.mongodb.monitor(Path.resolve(__dirname + "/../../../node_modules/mongojs/node_modules/mongodb"), logger)
metrics.mongodb.monitor(Path.resolve(__dirname + "/../../../node_modules/mongoose/node_modules/mongodb"), logger)
Settings.editorIsOpen ||= true Settings.editorIsOpen ||= true
@ -69,7 +71,7 @@ app.configure 'production', ->
logger.info "Production Enviroment" logger.info "Production Enviroment"
app.enable('view cache') app.enable('view cache')
app.use Monitor.logger app.use metrics.http.monitor(logger)
app.use (req, res, next)-> app.use (req, res, next)->
metrics.inc "http-request" metrics.inc "http-request"

View file

@ -25,6 +25,7 @@
"bufferedstream": "1.6.0", "bufferedstream": "1.6.0",
"settings-sharelatex": "git+https://github.com/sharelatex/settings-sharelatex.git#master", "settings-sharelatex": "git+https://github.com/sharelatex/settings-sharelatex.git#master",
"logger-sharelatex": "git+https://github.com/sharelatex/logger-sharelatex.git#master", "logger-sharelatex": "git+https://github.com/sharelatex/logger-sharelatex.git#master",
"metrics-sharelatex": "git+https://github.com/sharelatex/metrics-sharelatex.git#master",
"soa-req-id": "git+https://github.com/sharelatex/soa-req-id.git#master", "soa-req-id": "git+https://github.com/sharelatex/soa-req-id.git#master",
"fairy": "0.0.2", "fairy": "0.0.2",
"node-uuid": "1.4.1", "node-uuid": "1.4.1",