Skip to content

Commit bb8a016

Browse files
committed
fix pg provider and moving instrumentation to backup file
1 parent eca9a74 commit bb8a016

File tree

4 files changed

+117
-106
lines changed

4 files changed

+117
-106
lines changed

examples/express/server.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var miniprofiler = require('../../lib/miniprofiler.js');
22
var pg = require('pg');
33

44
var express = require('express');
5-
var connString = 'postgres://postgres:postgres@localhost/async_demo';
5+
var connString = 'postgres://postgres:postgres@localhost/miniprofiler';
66

77
var app = express();
88
app.use(miniprofiler.profile());
@@ -15,12 +15,21 @@ app.get('/', function(req, res) {
1515
res.render('home');
1616
});
1717

18+
app.get('/sleep', function(req, res) {
19+
pg.connect(connString, function(err, client, done) {
20+
client.query('SELECT pg_sleep(1)', [], function(err, result) {
21+
done();
22+
res.render('home');
23+
});
24+
});
25+
});
26+
1827
app.get('/multi-query', function(req, res) {
1928
pg.connect(connString, function(err, client, done) {
2029
client.query('SELECT pg_sleep(1)', [], function(err, result) {
2130
client.query('SELECT $1::int AS number', ['2'], function(err, result) {
2231
done();
23-
res.render('multi-query');
32+
res.render('home');
2433
});
2534
});
2635
});

lib/_instrument.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//Backup for instrument functions
2+
//Do we need this?
3+
4+
//exports.instrument = addProfilingInstrumentation;
5+
6+
/*
7+
* Modifies `toInstrument` such that each function has been instrumented for miniprofiler purposes.
8+
*
9+
*
10+
* Descends recursively, but will terminate even if there are cycles in the object graph.
11+
*
12+
* Adds `miniprofiler_instrumented` to `toInstrument`, any contained objects, and any contained functions.
13+
14+
function addProfilingInstrumentation(toInstrument) {
15+
if(!toInstrument){
16+
throw new Error('toInstrument must be set');
17+
}
18+
19+
if(!_.isObject(toInstrument)) {
20+
throw new Error('toInstrument must be an object');
21+
}
22+
23+
if(!toInstrument.miniprofiler_instrumented) {
24+
addProfilingImpl(toInstrument);
25+
}
26+
27+
return toInstrument;
28+
}
29+
30+
31+
function instrument(func, defaultName) {
32+
var name = func.name || defaultName || 'Unnamed';
33+
34+
var ret = function() {
35+
var toApply = func;
36+
var that = this;
37+
var args = Array.prototype.slice.call(arguments);
38+
39+
if(args) {
40+
for(var i = 0; i < args.length; i++){
41+
var arg = args[i];
42+
if(arg && _.isFunction(arg)){
43+
args[i] = instrument(arg, name+' arg #'+i);
44+
}
45+
}
46+
}
47+
48+
return step(
49+
name,
50+
function() {
51+
var ret = toApply.apply(that, args);
52+
53+
return ret;
54+
}
55+
);
56+
};
57+
58+
ret.miniprofiler_instrumented = true;
59+
60+
return ret;
61+
}
62+
63+
function addProfilingImpl(toInstrument) {
64+
if(toInstrument.miniprofiler_instrumented) {
65+
throw new Error('already instrumented');
66+
}
67+
68+
toInstrument.miniprofiler_instrumented = true;
69+
70+
for(var prop in toInstrument) {
71+
if(!toInstrument.hasOwnProperty(prop)) continue;
72+
73+
var toWrap = toInstrument[prop];
74+
75+
if(!toWrap || toWrap.miniprofiler_instrumented) continue;
76+
77+
if(Array.isArray(toWrap)){
78+
for(var i = 0; i < toWrap.length; i++) {
79+
var member = toWrap[i];
80+
if(member.miniprofiler_instrumented) continue;
81+
82+
addProfilingImpl(member);
83+
}
84+
85+
continue;
86+
}
87+
88+
if(_.isFunction(toWrap)) {
89+
var wrappedFunc = instrument(toWrap, prop);
90+
91+
toInstrument[prop] = wrappedFunc;
92+
93+
continue;
94+
}
95+
96+
if(_.isObject(toWrap)) {
97+
addProfilingImpl(toWrap);
98+
99+
continue;
100+
}
101+
}
102+
}
103+
*/

lib/miniprofiler.js

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ _.templateSettings = {
2323

2424
// EXPORTS
2525
exports.configure = configure;
26-
exports.instrument = addProfilingInstrumentation;
2726
exports.profile = middleware;
2827
exports.for = {
2928
pg: require('./providers/miniprofiler.pg.js')
@@ -179,7 +178,7 @@ function middleware(f) {
179178
res.on('finish', function() {
180179
stopProfiling(req);
181180
});
182-
res.setHeader('X-MiniProfiler-Ids', '["' + id + '"]');
181+
res.setHeader('X-MiniProfiler-Ids', `["${id}"]`);
183182
}
184183
next();
185184
};
@@ -234,37 +233,10 @@ function configure(options){
234233
configured = true;
235234
}
236235

237-
/*
238-
* Modifies `toInstrument` such that each function has been instrumented for miniprofiler purposes.
239-
*
240-
*
241-
* Descends recursively, but will terminate even if there are cycles in the object graph.
242-
*
243-
* Adds `miniprofiler_instrumented` to `toInstrument`, any contained objects, and any contained functions.
244-
*/
245-
function addProfilingInstrumentation(toInstrument) {
246-
if(!toInstrument){
247-
throw new Error('toInstrument must be set');
248-
}
249-
250-
if(!_.isObject(toInstrument)) {
251-
throw new Error('toInstrument must be an object');
252-
}
253-
254-
if(!toInstrument.miniprofiler_instrumented)
255-
{
256-
addProfilingImpl(toInstrument);
257-
}
258-
259-
return toInstrument;
260-
}
261-
262236
/*
263237
* Begins profiling the given request.
264238
*/
265239
function startProfiling(request, enabled) {
266-
if(!configured) throw new Error('configure() must be called before the first call to startProfiling');
267-
268240
var currentRequestExtension = {
269241
enabled: enabled
270242
};
@@ -515,76 +487,3 @@ function describeCustomTimings(customTimings, root) {
515487
function makeStep(name, time, parent){
516488
return { name: name, startTime: time, stopTime: null, parent: parent, steps: [], customTimings: {} };
517489
}
518-
519-
function instrument(func, defaultName) {
520-
var name = func.name || defaultName || 'Unnamed';
521-
522-
var ret = function() {
523-
var toApply = func;
524-
var that = this;
525-
var args = Array.prototype.slice.call(arguments);
526-
527-
if(args) {
528-
for(var i = 0; i < args.length; i++){
529-
var arg = args[i];
530-
if(arg && _.isFunction(arg)){
531-
args[i] = instrument(arg, name+' arg #'+i);
532-
}
533-
}
534-
}
535-
536-
return step(
537-
name,
538-
function() {
539-
var ret = toApply.apply(that, args);
540-
541-
return ret;
542-
}
543-
);
544-
};
545-
546-
ret.miniprofiler_instrumented = true;
547-
548-
return ret;
549-
}
550-
551-
function addProfilingImpl(toInstrument) {
552-
if(toInstrument.miniprofiler_instrumented) {
553-
throw new Error('already instrumented');
554-
}
555-
556-
toInstrument.miniprofiler_instrumented = true;
557-
558-
for(var prop in toInstrument) {
559-
if(!toInstrument.hasOwnProperty(prop)) continue;
560-
561-
var toWrap = toInstrument[prop];
562-
563-
if(!toWrap || toWrap.miniprofiler_instrumented) continue;
564-
565-
if(Array.isArray(toWrap)){
566-
for(var i = 0; i < toWrap.length; i++) {
567-
var member = toWrap[i];
568-
if(member.miniprofiler_instrumented) continue;
569-
570-
addProfilingImpl(member);
571-
}
572-
573-
continue;
574-
}
575-
576-
if(_.isObject(toWrap)) {
577-
addProfilingImpl(toWrap);
578-
579-
continue;
580-
}
581-
582-
if(_.isFunction(toWrap)) {
583-
var wrappedFunc = instrument(toWrap, prop);
584-
585-
toInstrument[prop] = wrappedFunc;
586-
587-
continue;
588-
}
589-
}
590-
}

lib/providers/miniprofiler.pg.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
var pgQuery;
22

33
module.exports = function(pg) {
4-
pgQuery = pgQuery || pg.Client.prototype.query;
54
return function(req, res, next) {
5+
pgQuery = pgQuery || pg.Client.prototype.query;
66
if (req.miniprofiler.enabled) {
7-
pg.Client.query = function(config, values, callback) {
7+
pg.Client.prototype.query = function(config, values, callback) {
88
req.miniprofiler.timeQuery('sql', config.toString(), pgQuery.bind(this), config, values, callback);
99
};
1010
}

0 commit comments

Comments
 (0)