{"size": 819, "ext": "js", "max_stars_repo_path": "middleware/metrics.js", "max_stars_repo_name": "gamechanger/penalty-box", "max_stars_repo_stars_event_min_datetime": "2020-08-27T23:06:08.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-13T17:02:19.000Z", "max_issues_repo_path": "middleware/metrics.js", "max_issues_repo_name": "gamechanger/penalty-box", "max_issues_repo_issues_event_min_datetime": "2016-04-19T20:17:13.000Z", "max_issues_repo_issues_event_max_datetime": "2019-11-06T15:45:58.000Z", "max_forks_repo_path": "middleware/metrics.js", "max_forks_repo_name": "gamechanger/penalty-box", "max_forks_repo_forks_event_min_datetime": "2020-08-27T23:06:10.000Z", "max_forks_repo_forks_event_max_datetime": "2020-08-27T23:06:10.000Z"}
{"max_stars_count": 5, "max_issues_count": 14, "max_forks_count": 1, "avg_line_length": 31.5, "max_line_length": 124, "alphanum_fraction": 0.6214896215}
var http = require('http');
var _ = require('underscore');
var StatsD = require('node-dogstatsd').StatsD;
var config = require('../lib/config');

statsd = new StatsD(config.datadog_host, config.datadog_port);
prefix = "penalty-box."

exports.handle = function(req, res, next){
    tags = ['request_path:'+ req.path];
    statsd.increment(prefix + 'requests', 1, tags);
    statsd.gauge(prefix + 'open_sockets', _(http.globalAgent.sockets).reduce(function(m, arr) {return m + arr.length;}, 0));

    var start = Date.now();
    res.on('finish', function() {
        var tags = [
            'http_code:'+res.statusCode,
            'request_path:'+req.path
        ];
        statsd.increment(prefix + 'responses', 1, tags);
        statsd.timing(prefix + 'response_time', Date.now()-start, tags)
    });

    next();
}
