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

Commit

Permalink
Add redis instrumentation, add copyright notice, refactor wrapping fu…
Browse files Browse the repository at this point in the history
…nction for jaeger instruments
  • Loading branch information
oguzcankirmemis committed Jan 17, 2019
1 parent 5094577 commit b66ea63
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 48 deletions.
2 changes: 1 addition & 1 deletion public-interface/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ 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();
Expand Down
27 changes: 20 additions & 7 deletions public-interface/iot-entities/postgresql/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/
'use strict';
var contextProvider = require('../../../lib/context-provider').instance(),
expressJaeger = require('../../../lib/express-jaeger'),
tracer = expressJaeger.tracer,
opentracing = require('opentracing'),
tracer = require('../../../lib/express-jaeger').tracer,
shimmer = require('shimmer'),
Sequelize = require('sequelize'),
config = require('../../../config').postgres,
accounts = require('./accounts'),
Expand Down Expand Up @@ -59,24 +60,36 @@ var sequelize = new Sequelize(
getSequelizeOptions()
);

var origQueryFunc = sequelize.query;
function patchQuery() {
return function(sql, options) {
// Patch sequelize.query for jaeger support
var wrapQuery = function (original) {
return function wrappedQuery (sql, options) {
var routeSpan = contextProvider.get('routeSpan');
var span = tracer.startSpan('postgres-call', { childOf: routeSpan });
return origQueryFunc.apply(this, arguments).then(
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;
}
);
}
}
sequelize['query'] = patchQuery();

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

var Accounts = new accounts(sequelize, Sequelize);
var Actuations = new actuations(sequelize, Sequelize);
Expand Down
40 changes: 38 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,52 @@
var config = require('../../config'),
redis = require("redis"),
logger = require('../../lib/logger').init(),
opentracing = require('opentracing'),
shimmer = require('shimmer'),
tracer = require('../../lib/express-jaeger').tracer,
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('routeSpan');
var span = tracer.startSpan('redis-call', { childOf: fatherSpan });
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 +74,4 @@ exports.redisClient = function () {
});

return client;
};
};
16 changes: 16 additions & 0 deletions public-interface/lib/express-jaeger/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/**
* Copyright (c) 2019 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var express = require('./traced-express'),
tracer = require('./jaeger-tracer');

Expand Down
31 changes: 28 additions & 3 deletions public-interface/lib/express-jaeger/jaeger-tracer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
var opentracing = require('opentracing'),
initJaegerTracer = require("jaeger-client").initTracer;
/**
* Copyright (c) 2019 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var initJaegerTracer = require("jaeger-client").initTracer;

var initTracer = function(serviceName) {
const config = {
Expand Down Expand Up @@ -27,4 +42,14 @@ var initTracer = function(serviceName) {
return initJaegerTracer(config, options);
}

module.exports = initTracer("frontend");
// Singleton
var tracer;

module.exports = function() {
if (tracer)
return tracer;
else {
tracer = initTracer('frontend');
return tracer;
}
}();
79 changes: 44 additions & 35 deletions public-interface/lib/express-jaeger/traced-express.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
/**
* Copyright (c) 2019 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var contextProvider = require('../../lib/context-provider').instance(),
shimmer = require('shimmer'),
express = require('express'),
tracer = require('./jaeger-tracer');

Expand All @@ -11,32 +28,26 @@ var startRequest = function(req, res, next) {
});
const middlewareSpan = tracer.startSpan('middleware', { childOf: span });
const routeSpan = tracer.startSpan(req.path, { childOf: span });
routeSpan.setTag(req.method);
routeSpan.setTag('METHOD', req.method);
contextProvider.set('middlewareSpan', middlewareSpan);
contextProvider.set('routeSpan', routeSpan);
res.on('finish', function() {
span.log({
event: 'request finish'
});
const routeSpan = contextProvider.get('routeSpan');
middlewareSpan.finish();
routeSpan.finish();
span.finish();
});
next();
}

// Override Express Server's register methods

var useOriginal = express.application.use,
getOriginal = express.application.get,
putOriginal = express.application.put,
postOriginal = express.application.post,
deleteOriginal = express.application.delete,
allOriginal = express.application.all;

express.application['startTracing'] = function() {
useOriginal.apply(this, ['/', startRequest]);
contextRegistered = true;
if (!contextRegistered) {
this.use('/', startRequest);
contextRegistered = true;
}
};

var patchMiddlewares = function(middlewares, startIndex, method, parentSpanName) {
Expand All @@ -52,7 +63,7 @@ var patchMiddlewares = function(middlewares, startIndex, method, parentSpanName)
const span = tracer.startSpan(name, {
childOf: parentSpan
});
span.setTag(method.toUpperCase());
span.setTag('METHOD', method.toUpperCase());
service(req, res, next);
span.finish();
};
Expand All @@ -62,29 +73,27 @@ var patchMiddlewares = function(middlewares, startIndex, method, parentSpanName)
}
}

var forkedRegister = function(original, method, parentSpanName) {
return function() {
if (contextRegistered) {
var path = typeof arguments[0] === "string" ? arguments[0] : '/';
var start = typeof arguments[0] === "string" ? 1 : 0;
patchMiddlewares(arguments, start, method, parentSpanName);
original.apply(this, arguments);
} else {
original.apply(this, arguments);
}
}
var forkedRegister = function(method, parentSpanName) {
return function(original) {
return function() {
if (contextRegistered) {
var path = typeof arguments[0] === "string" ? arguments[0] : '/';
var start = typeof arguments[0] === "string" ? 1 : 0;
patchMiddlewares(arguments, start, method, parentSpanName);
original.apply(this, arguments);
} else {
original.apply(this, arguments);
}
};
};
}

express.application['use'] = forkedRegister(useOriginal, 'use', 'middlewareSpan');

express.application['get'] = forkedRegister(getOriginal, 'get', 'routeSpan');

express.application['put'] = forkedRegister(putOriginal, 'put', 'routeSpan');

express.application['post'] = forkedRegister(postOriginal, 'post', 'routeSpan');

express.application['delete'] = forkedRegister(deleteOriginal, 'delete', 'routeSpan');

express.application['all'] = forkedRegister(allOriginal, 'all', 'routeSpan');
// Override Express Server's register functions
shimmer.wrap(express.application, 'use', forkedRegister('use', 'middlewareSpan'));
shimmer.wrap(express.application, 'get', forkedRegister('get', 'routeSpan'));
shimmer.wrap(express.application, 'put', forkedRegister('put', 'routeSpan'));
shimmer.wrap(express.application, 'post', forkedRegister('post', 'routeSpan'));
shimmer.wrap(express.application, 'delete', forkedRegister('delete', 'routeSpan'));
shimmer.wrap(express.application, 'all', forkedRegister('all', 'routeSpan'));

module.exports = express;
1 change: 1 addition & 0 deletions public-interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"request": "^2.88.0",
"sequelize": "^4.41.0",
"serve-favicon": "^2.3.0",
"shimmer": "^1.2.0",
"socket.io": "*",
"underscore": "^1.9.1",
"websocket": "^1.0.14",
Expand Down

0 comments on commit b66ea63

Please sign in to comment.