Skip to content

Commit

Permalink
Use closures instead of bind in plugins (#391)
Browse files Browse the repository at this point in the history
PR-URL: #391
  • Loading branch information
DominicKramer authored Feb 18, 2017
1 parent 0874b1a commit 533d28a
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 226 deletions.
82 changes: 42 additions & 40 deletions src/plugins/plugin-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,56 @@ var urlParse = require('url').parse;

var SUPPORTED_VERSIONS = '3.x';

function middleware(api, req, res, next) {
var options = {
name: urlParse(req.originalUrl).pathname,
traceContext: req.headers[api.constants.TRACE_CONTEXT_HEADER_NAME.toLowerCase()],
skipFrames: 3
};
api.runInRootSpan(options, function(root) {
if (!root) {
return next();
}
function createMiddleware(api) {
return function middleware(req, res, next) {
var options = {
name: urlParse(req.originalUrl).pathname,
traceContext: req.headers[api.constants.TRACE_CONTEXT_HEADER_NAME.toLowerCase()],
skipFrames: 3
};
api.runInRootSpan(options, function(root) {
if (!root) {
return next();
}

api.wrapEmitter(req);
api.wrapEmitter(res);
api.wrapEmitter(req);
api.wrapEmitter(res);

var url = (req.headers['X-Forwarded-Proto'] || 'http') +
'://' + req.headers.host + req.originalUrl;
var url = (req.headers['X-Forwarded-Proto'] || 'http') +
'://' + req.headers.host + req.originalUrl;

// we use the path part of the url as the span name and add the full
// url as a label
root.addLabel(api.labels.HTTP_METHOD_LABEL_KEY, req.method);
root.addLabel(api.labels.HTTP_URL_LABEL_KEY, url);
root.addLabel(api.labels.HTTP_SOURCE_IP, req.connection.remoteAddress);
// we use the path part of the url as the span name and add the full
// url as a label
root.addLabel(api.labels.HTTP_METHOD_LABEL_KEY, req.method);
root.addLabel(api.labels.HTTP_URL_LABEL_KEY, url);
root.addLabel(api.labels.HTTP_SOURCE_IP, req.connection.remoteAddress);

var context = root.getTraceContext();
if (context) {
res.setHeader(api.constants.TRACE_CONTEXT_HEADER_NAME, context);
}
var context = root.getTraceContext();
if (context) {
res.setHeader(api.constants.TRACE_CONTEXT_HEADER_NAME, context);
}

// wrap end
var originalEnd = res.end;
res.end = function(data, encoding, callback) {
res.end = originalEnd;
var returned = res.end(data, encoding, callback);
// wrap end
var originalEnd = res.end;
res.end = function(data, encoding, callback) {
res.end = originalEnd;
var returned = res.end(data, encoding, callback);

if (req.route && req.route.path) {
root.addLabel(
'connect/request.route.path', req.route.path);
}
if (req.route && req.route.path) {
root.addLabel(
'connect/request.route.path', req.route.path);
}

root.addLabel(
api.labels.HTTP_RESPONSE_CODE_LABEL_KEY, res.statusCode);
root.endSpan();
root.addLabel(
api.labels.HTTP_RESPONSE_CODE_LABEL_KEY, res.statusCode);
root.endSpan();

return returned;
};
return returned;
};

next();
});
next();
});
};
}

module.exports = [
Expand All @@ -76,7 +78,7 @@ module.exports = [
intercept: function(connect, api) {
return function() {
var app = connect();
app.use(middleware.bind(null, api));
app.use(createMiddleware(api));
return app;
};
}
Expand Down
96 changes: 50 additions & 46 deletions src/plugins/plugin-hapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,62 +20,66 @@ var urlParse = require('url').parse;

var SUPPORTED_VERSIONS = '8 - 16';

function connectionWrap(api, connection) {
return function connectionTrace() {
var server = connection.apply(this, arguments);
server.ext('onRequest', middleware.bind(null, api));
return server;
function createConnectionWrap(api) {
return function connectionWrap(connection) {
return function connectionTrace() {
var server = connection.apply(this, arguments);
server.ext('onRequest', createMiddleware(api));
return server;
};
};
}

function middleware(api, request, reply) {
var req = request.raw.req;
var res = request.raw.res;
var originalEnd = res.end;
var options = {
name: urlParse(req.url).pathname,
traceContext: req.headers[api.constants.TRACE_CONTEXT_HEADER_NAME],
skipFrames: 3
};
api.runInRootSpan(options, function(root) {
if (!root) {
return reply.continue();
}
function createMiddleware(api) {
return function middleware(request, reply) {
var req = request.raw.req;
var res = request.raw.res;
var originalEnd = res.end;
var options = {
name: urlParse(req.url).pathname,
traceContext: req.headers[api.constants.TRACE_CONTEXT_HEADER_NAME],
skipFrames: 3
};
api.runInRootSpan(options, function(root) {
if (!root) {
return reply.continue();
}

api.wrapEmitter(req);
api.wrapEmitter(res);
api.wrapEmitter(req);
api.wrapEmitter(res);

var url = (req.headers['X-Forwarded-Proto'] || 'http') +
'://' + req.headers.host + req.url;

// we use the path part of the url as the span name and add the full
// url as a label
// req.path would be more desirable but is not set at the time our middlewear runs.
root.addLabel(api.labels.HTTP_METHOD_LABEL_KEY, req.method);
root.addLabel(api.labels.HTTP_URL_LABEL_KEY, url);
root.addLabel(api.labels.HTTP_SOURCE_IP, req.connection.remoteAddress);
var url = (req.headers['X-Forwarded-Proto'] || 'http') +
'://' + req.headers.host + req.url;
// we use the path part of the url as the span name and add the full
// url as a label
// req.path would be more desirable but is not set at the time our middlewear runs.
root.addLabel(api.labels.HTTP_METHOD_LABEL_KEY, req.method);
root.addLabel(api.labels.HTTP_URL_LABEL_KEY, url);
root.addLabel(api.labels.HTTP_SOURCE_IP, req.connection.remoteAddress);

var context = root.getTraceContext();
res.setHeader(api.constants.TRACE_CONTEXT_HEADER_NAME, context);
var context = root.getTraceContext();
res.setHeader(api.constants.TRACE_CONTEXT_HEADER_NAME, context);

// wrap end
res.end = function(chunk, encoding) {
res.end = originalEnd;
var returned = res.end(chunk, encoding);
// wrap end
res.end = function(chunk, encoding) {
res.end = originalEnd;
var returned = res.end(chunk, encoding);

if (req.route && req.route.path) {
if (req.route && req.route.path) {
root.addLabel(
'hapi/request.route.path', req.route.path);
}
root.addLabel(
'hapi/request.route.path', req.route.path);
}
root.addLabel(
api.labels.HTTP_RESPONSE_CODE_LABEL_KEY, res.statusCode);
root.endSpan();
api.labels.HTTP_RESPONSE_CODE_LABEL_KEY, res.statusCode);
root.endSpan();

return returned;
};
return returned;
};

return reply.continue();
});
return reply.continue();
});
};
}

module.exports = [
Expand All @@ -85,7 +89,7 @@ module.exports = [
patch: function(hapi, api) {
shimmer.wrap(hapi.Server.prototype,
'connection',
connectionWrap.bind(null, api));
createConnectionWrap(api));
},
unpatch: function(hapi) {
shimmer.unwrap(hapi.Server.prototype, 'connection');
Expand Down
113 changes: 57 additions & 56 deletions src/plugins/plugin-koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,79 +20,80 @@ var urlParse = require('url').parse;

const SUPPORTED_VERSIONS = '1.x';

var api;

function useWrap(use) {
return function useTrace() {
if (!this._google_trace_patched) {
this._google_trace_patched = true;
this.use(middleware);
}
return use.apply(this, arguments);
function createUseWrap(api) {
return function useWrap(use) {
return function useTrace() {
if (!this._google_trace_patched) {
this._google_trace_patched = true;
this.use(createMiddleware(api));
}
return use.apply(this, arguments);
};
};
}

function* middleware(next) {
/* jshint validthis:true */
const req = this.req;
const res = this.res;
const originalEnd = res.end;
var options = {
name: urlParse(req.url).pathname,
traceContext: this.req.headers[api.constants.TRACE_CONTEXT_HEADER_NAME],
skipFrames: 3
};
api.runInRootSpan(options, function(root) {
if (!root) {
return;
}
function createMiddleware(api) {
return function* middleware(next) {
/* jshint validthis:true */
const req = this.req;
const res = this.res;
const originalEnd = res.end;
var options = {
name: urlParse(req.url).pathname,
traceContext: this.req.headers[api.constants.TRACE_CONTEXT_HEADER_NAME],
skipFrames: 3
};
api.runInRootSpan(options, function(root) {
if (!root) {
return;
}

api.wrapEmitter(req);
api.wrapEmitter(res);
api.wrapEmitter(req);
api.wrapEmitter(res);

const url = (req.headers['X-Forwarded-Proto'] || 'http') +
'://' + req.headers.host + req.url;

// we use the path part of the url as the span name and add the full
// url as a label
// req.path would be more desirable but is not set at the time our middlewear runs.
root.addLabel(api.labels.HTTP_METHOD_LABEL_KEY, req.method);
root.addLabel(api.labels.HTTP_URL_LABEL_KEY, url);
root.addLabel(api.labels.HTTP_SOURCE_IP, req.connection.remoteAddress);
const url = (req.headers['X-Forwarded-Proto'] || 'http') +
'://' + req.headers.host + req.url;

var context = root.getTraceContext();
if (context) {
res.setHeader(api.constants.TRACE_CONTEXT_HEADER_NAME, context);
}
// we use the path part of the url as the span name and add the full
// url as a label
// req.path would be more desirable but is not set at the time our middlewear runs.
root.addLabel(api.labels.HTTP_METHOD_LABEL_KEY, req.method);
root.addLabel(api.labels.HTTP_URL_LABEL_KEY, url);
root.addLabel(api.labels.HTTP_SOURCE_IP, req.connection.remoteAddress);

var context = root.getTraceContext();
if (context) {
res.setHeader(api.constants.TRACE_CONTEXT_HEADER_NAME, context);
}

// wrap end
res.end = function(chunk, encoding) {
res.end = originalEnd;
const returned = res.end(chunk, encoding);
// wrap end
res.end = function(chunk, encoding) {
res.end = originalEnd;
const returned = res.end(chunk, encoding);

if (req.route && req.route.path) {
if (req.route && req.route.path) {
root.addLabel(
'koa/request.route.path', req.route.path);
}
root.addLabel(
'koa/request.route.path', req.route.path);
}
root.addLabel(
api.labels.HTTP_RESPONSE_CODE_LABEL_KEY, res.statusCode);
root.endSpan();
api.labels.HTTP_RESPONSE_CODE_LABEL_KEY, res.statusCode);
root.endSpan();

return returned;
};
api.wrap(next);
});
return returned;
};
api.wrap(next);
});

yield next;
yield next;
};
}

module.exports = [
{
file: '',
versions: SUPPORTED_VERSIONS,
patch: function(koa, api_) {
api = api_;
shimmer.wrap(koa.prototype, 'use', useWrap);
patch: function(koa, api) {
shimmer.wrap(koa.prototype, 'use', createUseWrap(api));
},
unpatch: function(koa) {
shimmer.unwrap(koa.prototype, 'use');
Expand Down
Loading

0 comments on commit 533d28a

Please sign in to comment.