Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Implement Jaeger Support For Frontend #81

Merged
merged 7 commits into from
Jan 24, 2019
6 changes: 4 additions & 2 deletions public-interface/admin/resetDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/

var models = require('../iot-entities/postgresql/models'),
systemUsers = require('../lib/dp-users/systemUsers');
systemUsers = require('../lib/dp-users/systemUsers'),
tracer = require('../lib/express-jaeger').tracer;

var ResetDB = function(){};

Expand All @@ -35,11 +36,12 @@ ResetDB.prototype.reset = function(cb){
.then(() => {
return models.initSchema();
})
.then(function() {
.then(function() {
return systemUsers.create();
})
.finally(function() {
models.sequelize.close();
tracer.close();
});
}

Expand Down
13 changes: 6 additions & 7 deletions public-interface/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

"use strict";
var http = require('http'),
express = require('express'),
express = require('./lib/express-jaeger').express,
uuid = require('node-uuid'),
AlertMonitor = require('./lib/event-monitor'),
commServer = require('./lib/comm-server'),
Expand Down Expand Up @@ -52,6 +52,7 @@ var XSS = iotRoutes.cors,
/* We set the global agent to 1024 this is for
how many concurrent sockets the agent can have open per origin
*/

http.globalAgent.maxSockets = maxSockets;

security.rateLimiter.init(secConfig);
Expand All @@ -61,6 +62,8 @@ appServer.set('port', api_port);
appServer.disable('x-powered-by');
appServer.enable('trust proxy');
appServer.use(favicon(__dirname + '/dashboard/public/favicon.png'));
appServer.use(contextProvider.middleware);
appServer.startTracing();
appServer.use(compress());
if (config.api.forceSSL) {
appServer.use(forceSSL);
Expand All @@ -70,15 +73,12 @@ if (config.api.forceSSL) {
appServer.use('/ui/public', express.static('dashboard/public'));

appServer.use(httpHeadersInjector.forwardedHeaders);
appServer.use(contextProvider.middleware);

appServer.use(bodyParser.json({limit: config.api.bodySizeLimit}));
appServer.use(bodyParser.urlencoded({extended: true, limit: config.api.bodySizeLimit}));
appServer.use(XSS());

appServer.use(winstonLogger.httpLogger());


(function setAppServerUseSecurityAndCaptcha(){
var endpoints = ['/v1','/ui'];
for(var i in endpoints){
Expand All @@ -89,8 +89,6 @@ appServer.use(winstonLogger.httpLogger());
}
})();



if (process.env.NODE_ENV && (process.env.NODE_ENV.toLowerCase().indexOf("local") !== -1)) {
appServer.use('/ui', function (req, res, next) {
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
Expand All @@ -100,12 +98,13 @@ if (process.env.NODE_ENV && (process.env.NODE_ENV.toLowerCase().indexOf("local")
});
}

appServer.use('/v1', function (req, res, next) {
appServer.use('/v1', function setUUID (req, res, next) {
req.headers['x-iotkit-requestid'] = 'api:' + uuid.v4();
contextProvider.instance().set('requestid', req.headers['x-iotkit-requestid']);
next();
});


/* Modules that registered his routes
shall be unique or will be override
*/
Expand Down
18 changes: 13 additions & 5 deletions public-interface/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ var getOISPConfig = (function () {
return function () { return {}; };
}
var frontendConfig = JSON.parse(process.env.OISP_FRONTEND_CONFIG);

var resolveConfig = function (config, stack) {
if (!stack) {
stack = ["OISP_FRONTEND_CONFIG"];
}
for (var property in config) {
if (typeof config[property] === "string" &&
if (typeof config[property] === "string" &&
(config[property].substring(0,2) === "@@" || config[property].substring(0,2) === "%%")) {
var configName = config[property].substring(2, config[property].length);
if (!process.env[configName]) {
Expand All @@ -46,17 +46,17 @@ var getOISPConfig = (function () {
}
}
};

resolveConfig(frontendConfig);

return function(configName) {
if (!frontendConfig[configName])
return {};
else {
console.log(configName + " is set to: " + JSON.stringify(frontendConfig[configName]));
return frontendConfig[configName];
}
};
};
})();

var postgres_config = getOISPConfig("postgresConfig"),
Expand Down Expand Up @@ -233,6 +233,14 @@ var config = {
},
interactionTokenGenerator: {
permissionKey: dashboardSecurity_config.interaction_token_permision_key
},
jaeger : {
serviceName: 'frontend',
agentHost: process.env.DASHBOARD_SERVICE_HOST ? 'localhost' : 'jaeger',
agentPort: 6832,
logSpans: true,
samplerType: 'const',
samplerParam: 1
}
};

Expand Down
43 changes: 41 additions & 2 deletions public-interface/iot-entities/postgresql/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
* limitations under the License.
*/
'use strict';

var Sequelize = require('sequelize'),
var contextProvider = require('../../../lib/context-provider').instance(),
opentracing = require('opentracing'),
tracer = require('../../../lib/express-jaeger').tracer,
spanContext = require('../../../lib/express-jaeger').spanContext,
shimmer = require('shimmer'),
Sequelize = require('sequelize'),
config = require('../../../config').postgres,
accounts = require('./accounts'),
settings = require('./settings'),
Expand Down Expand Up @@ -57,6 +61,40 @@ var sequelize = new Sequelize(
getSequelizeOptions()
);

// Patch sequelize.query for jaeger support
var wrapQuery = function (original) {
return function wrappedQuery (sql, options) {
var fatherSpan = contextProvider.get(spanContext.parent);
// Track if request coming from express
if (!fatherSpan)
fatherSpan = {};
var span = tracer.startSpan('postgres-call', { childOf: fatherSpan.span });
span.log({
event: 'postgres query',
query: sql
});
return original.apply(this, arguments).then(
result => {
span.finish();
return result;
},
err => {
span.setTag(opentracing.Tags.ERROR, true);
span.log({
event: 'postgres query error',
err: err,
message: err.message,
stack: err.stack
})
span.finish();
throw err;
}
);
}
}

shimmer.wrap(sequelize, 'query', wrapQuery);

var Accounts = new accounts(sequelize, Sequelize);
var Actuations = new actuations(sequelize, Sequelize);
var Users = new users(sequelize, Sequelize);
Expand Down Expand Up @@ -383,6 +421,7 @@ var executeScriptsWithoutTransaction = function () {
});
};


exports.initSchema = function () {
return executeScriptsWithTransaction()
.then(function() {
Expand Down
44 changes: 42 additions & 2 deletions public-interface/iot-entities/redis/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,56 @@
var config = require('../../config'),
redis = require("redis"),
logger = require('../../lib/logger').init(),
opentracing = require('opentracing'),
shimmer = require('shimmer'),
tracer = require('../../lib/express-jaeger').tracer,
spanContext = require('../../lib/express-jaeger').spanContext,
contextProvider = require('../../lib/context-provider').instance(),
client;


// Patch redis client for jaeger support
var wrapSend = function (original) {
return function wrappedSend(commandObj) {
var fatherSpan = contextProvider.get(spanContext.parent);
// Track if request coming from express
if (!fatherSpan)
fatherSpan = {};
var span = tracer.startSpan('redis-call', { childOf: fatherSpan.span });
span.log({
event: 'redis command',
command: commandObj.command
});

var originalCb = commandObj.callback;
commandObj.callback = function (err, replies) {
if (err) {
span.log({
event: 'redis command error',
err: err,
message: err.message,
stack: err.stack
});
span.setTag(opentracing.Tags.ERROR, true);
}
span.finish();
if (originalCb)
originalCb(err, replies);
}

original.call(this, commandObj);
}
}

shimmer.wrap(redis.RedisClient.prototype, 'internal_send_command', wrapSend);

exports.redisClient = function () {
if(client){
return client;
}
if (config.redis.port) {
client = redis.createClient(config.redis.port, config.redis.host, {});
client.auth(config.redis.password);

} else {
client = redis.createClient();
}
Expand All @@ -38,4 +78,4 @@ exports.redisClient = function () {
});

return client;
};
};
Loading