Skip to content

Commit

Permalink
Merge pull request #911 from pauloppenheim/instrument
Browse files Browse the repository at this point in the history
I'll probably tweak this a little bit, but the bug is real, and so is the fix. Thanks, @pauloppenheim!
  • Loading branch information
Forrest L Norvell committed Mar 7, 2012
2 parents 04292b5 + 6fd8312 commit 369f146
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 46 deletions.
76 changes: 37 additions & 39 deletions Collections/Links/links.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,71 @@
/*
*
* Copyright (C) 2011, The Locker Project
* All rights reserved.
*
* Please see the LICENSE file for more information.
*
*/
*
* Copyright (C) 2011-2012, The Locker Project
* All rights reserved.
*
* Please see the LICENSE file for more information.
*
*/

// merge links from connectors

var fs = require('fs'),
url = require('url'),
request = require('request'),
lconfig = require('lconfig.js');
locker = require('locker.js');
var async = require("async");
var crypto = require("crypto");
var jsonStream = require("express-jsonstream");
var logger;
var fs = require('fs')
, url = require('url')
, request = require('request')
, lconfig = require('lconfig.js')
, locker = require('locker.js')
, async = require("async")
, crypto = require("crypto")
, jsonStream = require("express-jsonstream")
, logger;


var dataIn = require('./dataIn'); // for processing incoming twitter/facebook/etc data types
var dataStore = require("./dataStore"); // storage/retreival of raw links and encounters
var util = require("./util"); // handy things for anyone and used within dataIn
var oembed = require("./oembed"); // wrapper to do best oembed possible
var dataIn = require('./dataIn') // for processing incoming twitter/facebook/etc data types
, dataStore = require("./dataStore") // storage/retreival of raw links and encounters
, util = require("./util") // handy things for anyone and used within dataIn
, oembed = require("./oembed"); // wrapper to do best oembed possible

var lockerInfo;
var express = require('express'),
connect = require('connect');

var app = express.createServer(connect.bodyParser());

app.use(jsonStream());

app.set('views', __dirname);

app.get('/update', function(req, res) {
dataIn.reIndex(locker, function(){
app.get('/update', function (req, res) {
dataIn.reIndex(locker, function () {
res.writeHead(200);
res.end('Extra mince!');
});
});

// simple oembed util internal api
app.get('/embed', function(req, res) {
oembed.fetch({url:req.query.url}, function(e) {
app.get('/embed', function (req, res) {
oembed.fetch({url:req.query.url}, function (e) {
if(e) return res.send(e);
res.send({});
});
});

app.post('/events', function(req, res) {
app.post('/events', function (req, res) {
var q = async.queue(dataIn.processEvent, 1);
req.jsonStream(q.push, function(error){
req.jsonStream(q.push, function (error) {
if(error) logger.error(error);
res.send(200);
});
});

function genericApi(name,f)
{
app.get(name,function(req,res){
function genericApi(name,f) {
app.get(name, function (req,res) {
var results = [];
f(req.query,function(item){results.push(item);},function(err){
if(err)
{
f(req.query, function (item) { results.push(item); }, function (err) {
if(err) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end(err);
}else{
} else {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(results));
}
Expand All @@ -75,9 +74,8 @@ function genericApi(name,f)
}

// expose all utils
for(var f in util)
{
if(f == 'init') continue;
for (var f in util) {
if (f == 'init') continue;
genericApi('/'+f,util[f]);
}

Expand All @@ -89,7 +87,7 @@ if (lconfig.airbrakeKey) {

// Process the startup JSON object
process.stdin.resume();
process.stdin.on('data', function(data) {
process.stdin.on('data', function (data) {
lockerInfo = JSON.parse(data);
locker.initClient(lockerInfo);
locker.lockerBase = lockerInfo.lockerUrl;
Expand All @@ -101,11 +99,11 @@ process.stdin.on('data', function(data) {
lconfig.load('../../Config/config.json');
logger = require("logger");

locker.connectToMongo(function(mongo) {
locker.connectToMongo(function (mongo) {
// initialize all our libs
dataStore.init(mongo, locker);
dataIn.init(locker, dataStore, logger);
app.listen(0, 'localhost', function() {
app.listen(0, 'localhost', function () {
var returnedInfo = {port: app.address().port};
process.stdout.write(JSON.stringify(returnedInfo));
});
Expand Down
11 changes: 9 additions & 2 deletions Common/node/instrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*
*/
var dgram = require('dgram')
, util = require('util')
, logger = require('./logger.js');

function StatsdDispatcher(config) {
Expand All @@ -21,11 +22,17 @@ StatsdDispatcher.prototype.send = function (msg) {
return;
}
if (this.prefix) msg = this.prefix + '.' + msg;


// create closure; if there's an error, can report the problematic host/port
f_ctx = this;
var f = function () {with(f_ctx){ return [f_ctx.host, f_ctx.port, f_ctx.prefix];}}

var socket = dgram.createSocket('udp4');
var buf = new Buffer(msg);
socket.send(buf, 0, buf.length, this.port, this.host, function (err, bytes) {
if (err) console.error('statsd error: ' + err);
if (err) {
console.error('statsd error: %s --- %s', err, util.inspect(f()));
}

socket.close();
});
Expand Down
7 changes: 2 additions & 5 deletions Common/node/lconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ var path = require('path');

exports.load = function(filepath) {
var config = {};
try {
if (path.existsSync(filepath))
config = JSON.parse(fs.readFileSync(filepath));
} catch(err) {
if(err.code !== 'EBADF')
throw err;
}

exports.lockerHost = config.lockerHost || 'localhost';
exports.externalHost = config.externalHost || 'localhost';
exports.lockerListenIP = config.lockerListenIP || '0.0.0.0';
Expand Down
4 changes: 4 additions & 0 deletions Ops/webservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@ locker.get('/core/stats', function(req, res) {
stats.serviceManager[type].running += 1;
}

// serviceManager never reports that a connector is running
if ('connector' in stats.serviceManager)
delete stats.serviceManager.connector['running'];

res.send(JSON.stringify(stats), 200);
});

Expand Down

0 comments on commit 369f146

Please sign in to comment.