diff --git a/configs/.env b/configs/.env index f41cfac1a..11c1e6949 100644 --- a/configs/.env +++ b/configs/.env @@ -20,6 +20,7 @@ HASHIDS_LENGTH=6 DOCKER_IMAGE_BUILDER_NAME="runnable/image-builder" DOCKER_IMAGE_BUILDER_VERSION="d1.3.1-v1.4.3" DOCKER_IMAGE_BUILDER_CACHE="/tmp" +DOCKER_TIMEOUT=5000 BUILD_END_TIMEOUT=100 DATADOG_HOST="localhost" DATADOG_PORT=8125 diff --git a/lib/express-app.js b/lib/express-app.js index 23bf491b1..ca6fa047a 100644 --- a/lib/express-app.js +++ b/lib/express-app.js @@ -10,19 +10,19 @@ var pkg = require('../package.json'); var dogstatsd = require('models/datadog'); var app = module.exports = express(); -app.use(require('connect-datadog')({ - 'dogstatsd': dogstatsd, - 'response_code':true, - 'method':true, - 'tags': ['name:api', 'logType:express', 'env:'+process.env.NODE_ENV] -})); +if (envIs('production')) { + app.use(require('connect-datadog')({ + 'dogstatsd': dogstatsd, + 'response_code':true, + 'method':true, + 'tags': ['name:api', 'logType:express', 'env:'+process.env.NODE_ENV] + })); -if (envIs('development', 'local', 'io')) { - app.use(morganFreeman()); - app.use(function (req, res, next) { - console.log(req.method, '-', req.url); - next(); - }); + app.use(dogstatsd.captureSocketCount); +} + +if (envIs('development', 'local', 'stage')) { + app.use(morganFreeman('short')); } app.use(require('./routes/github')); diff --git a/lib/models/apis/docker.js b/lib/models/apis/docker.js index b8ec31ef0..1761f21e5 100644 --- a/lib/models/apis/docker.js +++ b/lib/models/apis/docker.js @@ -26,7 +26,8 @@ function Docker (dockerHost) { this.port = parsed.port || 4243; this.docker = dogerode(new Dockerode({ host: this.dockerHost, - port: this.port + port: this.port, + timeout: process.env.DOCKER_TIMEOUT }), { service: 'api', host: process.env.DATADOG_HOST, diff --git a/lib/models/datadog/index.js b/lib/models/datadog/index.js index c0987629f..e552af45c 100644 --- a/lib/models/datadog/index.js +++ b/lib/models/datadog/index.js @@ -3,7 +3,7 @@ var StatsD = require('node-dogstatsd').StatsD; var client = module.exports = new StatsD( process.env.DATADOG_HOST, process.env.DATADOG_PORT); - +var exec = require('child_process').exec; function captureSteamData (streamName, stream) { stream.on('data', function(){ @@ -21,5 +21,26 @@ function captureSteamData (streamName, stream) { } +function captureSocketCount (req, res, next) { + var sockets = require('http').globalAgent.sockets; + var request = require('http').globalAgent.requests; + var key; + + for (key in sockets) { + client.gauge('api.sockets_open', sockets[key].length, 1, ['target:'+key]); + } + + for (key in request) { + client.gauge('api.sockets_pending', request[key].length, 1, ['target:'+key]); + } + + exec('lsof -p ' + process.pid + ' | wc -l', function (err, stdout) { + if (err) { return; } + client.gauge('api.openFiles', parseInt(stdout), 1, ['target:'+key]); + }); + + next(); +} module.exports.captureSteamData = captureSteamData; +module.exports.captureSocketCount = captureSocketCount;