Skip to content

Commit fb3db34

Browse files
committed
pg provider, back to the green field. refactor needed @ miniprofiler.js
1 parent 766c792 commit fb3db34

23 files changed

+213
-190
lines changed

docker-compose.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
redis:
22
image: redis
33

4+
postgres:
5+
image: postgres
6+
environment:
7+
- POSTGRES_USER=docker
8+
- POSTGRES_PASSWORD=docker
9+
410
test:
511
image: node
612
volumes:
713
- .:/usr/src/miniprofiler
814
links:
915
- redis
16+
- postgres:pg
1017
working_dir: /usr/src/miniprofiler
1118
command: npm run mocha

lib/middlewares/express.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
module.exports = function(f, handleRequest) {
2-
return function(req, res, next) {
3-
var enabled = f(req, res);
1+
module.exports = {
2+
instrument: function(provider) {
3+
return function(req, res, next) {
4+
provider(req, res, next);
5+
};
6+
},
7+
middleware: function(f, handleRequest) {
8+
return function(req, res, next) {
9+
var enabled = f(req, res);
410

5-
var respondWith = (res, result) => {
6-
res.writeHead(result.status, { 'Content-Type': result.type });
7-
res.end(result.body);
8-
};
11+
var respondWith = (res, result) => {
12+
res.writeHead(result.status, { 'Content-Type': result.type });
13+
res.end(result.body);
14+
};
915

10-
handleRequest(enabled, req, res, respondWith).then((handled) => {
11-
if (!handled)
12-
next();
13-
14-
}).catch(next);
15-
};
16+
handleRequest(enabled, req, res, respondWith).then((handled) => {
17+
if (!handled)
18+
next();
19+
}).catch(next);
20+
};
21+
}
1622
};

lib/middlewares/hapi.js

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,39 @@ var onHapiRequest = function(f, handleRequest, request, reply) {
1111
});
1212
};
1313

14-
module.exports = function(f, handleRequest) {
15-
var plugin = {
16-
register: (server, options, next) => {
17-
server.ext('onRequest', onHapiRequest.bind(null, f, handleRequest));
18-
next();
19-
}
20-
};
14+
module.exports = {
15+
instrument: function(provider, name) {
16+
var plugin = {
17+
register: (server, options, next) => {
18+
server.ext('onRequest', function(request, reply) {
19+
provider(request.raw.req, request.raw.res, () => {
20+
return reply.continue();
21+
});
22+
});
23+
next();
24+
}
25+
};
2126

22-
plugin.register.attributes = {
23-
name: 'miniprofiler-hapi',
24-
version: require('../../package.json').version
25-
};
27+
plugin.register.attributes = {
28+
name: `miniprofiler-hapi-${name}`,
29+
version: require('../../package.json').version
30+
};
2631

27-
return plugin;
28-
};
32+
return plugin;
33+
},
34+
middleware: function(f, handleRequest) {
35+
var plugin = {
36+
register: (server, options, next) => {
37+
server.ext('onRequest', onHapiRequest.bind(null, f, handleRequest));
38+
next();
39+
}
40+
};
41+
42+
plugin.register.attributes = {
43+
name: 'miniprofiler-hapi',
44+
version: require('../../package.json').version
45+
};
46+
47+
return plugin;
48+
}
49+
};

lib/middlewares/koa.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
module.exports = function(f, handleRequest) {
2-
return function *(next) {
3-
var enabled = f(this.req, this.res);
1+
module.exports = {
2+
instrument: function(provider) {
3+
return function *(next) {
4+
yield new Promise((resolve, reject) => {
5+
provider(this.req, this.res, resolve);
6+
});
7+
yield next;
8+
};
9+
},
10+
middleware: function(f, handleRequest) {
11+
return function *(next) {
12+
var enabled = f(this.req, this.res);
413

5-
var respondWith = (res, result) => {
6-
this.status = result.status;
7-
this.type = result.type;
8-
this.body = result.body;
9-
};
14+
var respondWith = (res, result) => {
15+
this.status = result.status;
16+
this.type = result.type;
17+
this.body = result.body;
18+
};
1019

11-
var handled = yield handleRequest(enabled, this.req, this.res, respondWith);
12-
if (!handled)
13-
yield next;
14-
};
20+
var handled = yield handleRequest(enabled, this.req, this.res, respondWith);
21+
if (!handled)
22+
yield next;
23+
};
24+
}
1525
};

lib/miniprofiler.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* Matt Jibson, 2013 @mjibsonF
88
* Guilherme Oenning, 2016 @goenning
99
*/
10-
1110
var _ = require('underscore');
1211
var fs = require('fs');
1312
var os = require('os');
@@ -16,6 +15,7 @@ var qs = require('querystring');
1615
var url = require('url');
1716
var uuid = require('node-uuid');
1817
var debug = require('debug')('miniprofiler');
18+
require('generator-bind').polyfill();
1919

2020
_.templateSettings = {
2121
interpolate: /\{(.+?)\}/g
@@ -29,17 +29,25 @@ var popupRenderPosition = 'left';
2929
// EXPORTS
3030
exports.configure = configure;
3131

32-
_(['express', 'hapi', 'koa', 'http']).each((framework) => {
32+
for (let framework of ['koa', 'express', 'hapi']) {
33+
let func = require(`./middlewares/${framework}.js`);
34+
func.name = framework;
3335
exports[framework] = function(f) {
3436
if(!f) f = () => { return true; };
35-
return require(`./middlewares/${framework}.js`)(f, handleRequest);
37+
return func.middleware(f, handleRequest);
3638
};
37-
});
3839

39-
exports.for = {
40-
pg: require('./providers/miniprofiler.pg.js'),
41-
redis: require('./providers/miniprofiler.redis.js')
42-
};
40+
exports[framework].for = {
41+
pg: function(pg) {
42+
var provider = require('./providers/miniprofiler.pg.js')(pg);
43+
return func.instrument(provider, 'pg');
44+
},
45+
redis: function(redis) {
46+
var provider = require('./providers/miniprofiler.redis.js')(redis);
47+
return func.instrument(provider, 'redis');
48+
}
49+
};
50+
}
4351

4452
// GLOBALS
4553
var storage = function(id, json) {

lib/providers/miniprofiler.pg.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
/*
21
var pgQuery;
32

43
module.exports = function(pg) {
4+
pgQuery = pgQuery || pg.Client.prototype.query;
55
return function(req, res, next) {
6-
pgQuery = pgQuery || pg.Client.prototype.query;
7-
86
pg.Client.prototype.query = !req.miniprofiler.enabled ? pgQuery : function(config, values, callback) {
9-
req.miniprofiler.timeQuery('sql', config.toString(), pgQuery.bind(this), config, values, callback);
10-
};
7+
req.miniprofiler.timeQuery('sql', config.toString(), pgQuery.bind(this), config, values, callback);
8+
};
119

1210
next();
1311
};
14-
};
15-
*/
12+
};
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
var redisSendCommand;
22

3+
var blacklist = ['info'];
4+
35
module.exports = function(redis) {
4-
redisSendCommand = redis.RedisClient.prototype.internal_send_command;
6+
redisSendCommand = redisSendCommand || redis.RedisClient.prototype.internal_send_command;
57
return function(req, res, next) {
6-
8+
79
redis.RedisClient.prototype.internal_send_command = !req.miniprofiler.enabled ? redisSendCommand : function(command, args, callback) {
810
var query = `${command} ${args.join(', ')}`.trim();
9-
if (this.ready)
11+
if (this.ready && blacklist.indexOf(command) == -1)
1012
req.miniprofiler.timeQuery('redis', query, redisSendCommand.bind(this), command, args, callback);
1113
else
1214
redisSendCommand.call(this, command, args, callback);
1315
};
16+
1417
next();
1518
};
1619
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"readmeFilename": "README.md",
3333
"dependencies": {
3434
"debug": "^2.2.0",
35+
"generator-bind": "^1.0.1",
3536
"node-uuid": "^1.4.7",
3637
"underscore": "^1.8.3"
3738
},

tests/provider-pg-test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var expect = require('chai').expect;
2+
3+
module.exports = function(server) {
4+
describe('Postgres Tests', function() {
5+
before(server.setUp.bind(null, 'default'));
6+
after(server.tearDown);
7+
8+
it('Should profile postgres SELECT command', function(done) {
9+
server.get('/pg-select', (err, response) => {
10+
var ids = JSON.parse(response.headers['x-miniprofiler-ids']);
11+
expect(ids).to.have.lengthOf(1);
12+
13+
server.post('/mini-profiler-resources/results/', { id: ids[0], popup: 1 }, (err, response, body) => {
14+
var result = JSON.parse(body);
15+
16+
expect(result.Id).to.equal(ids[0]);
17+
expect(result.Name).to.equal('/pg-select');
18+
expect(result.Root.Children).to.be.empty;
19+
expect(result.Root.CustomTimings).to.have.property('sql');
20+
expect(result.Root.CustomTimings.sql).to.have.lengthOf(1);
21+
22+
expect(result.Root.CustomTimings.sql[0].ExecuteType).to.be.equal('sql');
23+
expect(result.Root.CustomTimings.sql[0].CommandString).to.be.equal('SELECT $1::int AS number');
24+
expect(result.Root.CustomTimings.sql[0].DurationMilliseconds).to.be.below(result.DurationMilliseconds);
25+
done();
26+
});
27+
});
28+
29+
});
30+
31+
});
32+
};

tests/servers/express/default.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
var miniprofiler = require('../../../lib/miniprofiler.js');
22
var express = require('express');
33
var redis = require('redis');
4+
var pg = require('pg');
5+
var connString = `postgres://docker:docker@${process.env.PG_PORT_5432_TCP_ADDR}/docker`;
46

57
var app = express();
68
var client = redis.createClient(6379, process.env.REDIS_PORT_6379_TCP_ADDR);
79

810
app.use(miniprofiler.express());
9-
app.use(miniprofiler.for.redis(redis));
11+
app.use(miniprofiler.express.for.pg(pg));
12+
app.use(miniprofiler.express.for.redis(redis));
1013

1114
app.get('/', (req, res) => {
1215
res.send(req.miniprofiler.include());
@@ -46,4 +49,12 @@ app.get('/redis-set-get-key', function(req, res) {
4649
});
4750
});
4851

52+
app.get('/pg-select', function(req, res) {
53+
pg.connect(connString, function(err, pgClient, done) {
54+
pgClient.query('SELECT $1::int AS number', ['1'], function(err, result) {
55+
res.send(req.miniprofiler.include());
56+
});
57+
});
58+
});
59+
4960
module.exports = app;

0 commit comments

Comments
 (0)