Skip to content

Commit

Permalink
added express plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kjin committed Feb 7, 2017
1 parent 7b306d7 commit fd9a524
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 141 deletions.
6 changes: 5 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
'use strict';

var path = require('path');

// Default configuration
module.exports = {
trace: {
Expand All @@ -38,7 +40,9 @@ module.exports = {
// An empty object means that no modules will be automatically traced at
// all.
// This field is experimental.
plugins: {},
plugins: {
express: path.join(__dirname, 'src/plugins/plugin-express.js')
},

// Valid entries are:
// 'express', 'hapi', 'http', 'restify'
Expand Down
2 changes: 0 additions & 2 deletions src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ var fs = require('fs');
// Note: the order in which filenames are defined in the hooks determines the
// order in which they are loaded.
var toInstrument = Object.create(null, {
'express': { enumerable: true, value: { file: './userspace/hook-express.js',
patches: {} } },
'grpc': { enumerable: true, value: { file: './userspace/hook-grpc.js',
patches: {} } },
'hapi': { enumerable: true, value: { file: './userspace/hook-hapi.js',
Expand Down
138 changes: 0 additions & 138 deletions src/hooks/userspace/hook-express.js

This file was deleted.

70 changes: 70 additions & 0 deletions src/plugins/plugin-express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict';
var shimmer = require('shimmer');
var methods = require('methods').concat('use', 'route', 'param', 'all');

var SUPPORTED_VERSIONS = '4.x';

function patchModuleRoot(express, api) {
var labels = api.labels;
function applicationActionWrap(method) {
return function expressActionTrace() {
if (!this._google_trace_patched && !this._router) {
this._google_trace_patched = true;
this.use(middleware);
}
return method.apply(this, arguments);
};
}

function middleware(req, res, next) {
var options = {
name: req.path,
traceContext: req.get(api.constants.TRACE_CONTEXT_HEADER_NAME),
url: req.originalUrl,
skipFrames: 3
};
api.runInRootSpan(options, function(transaction) {
if (!transaction) {
next();
return;
}

// Set outgoing trace context.
res.set(api.constants.TRACE_CONTEXT_HEADER_NAME,
transaction.getTraceContext());

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

var url = req.protocol + '://' + req.hostname + req.originalUrl;
transaction.addLabel(labels.HTTP_METHOD_LABEL_KEY, req.method);
transaction.addLabel(labels.HTTP_URL_LABEL_KEY, url);
transaction.addLabel(labels.HTTP_SOURCE_IP, req.connection.remoteAddress);

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

if (req.route && req.route.path) {
transaction.addLabel('express/request.route.path', req.route.path);
}
transaction.addLabel(labels.HTTP_RESPONSE_CODE_LABEL_KEY, res.statusCode);
transaction.endSpan();
return returned;
};

next();
});
}

methods.forEach(function(method) {
shimmer.wrap(express.application, method, applicationActionWrap);
});
express._plugin_patched = true;
}

module.exports = [
{ versions: SUPPORTED_VERSIONS, patch: patchModuleRoot }
];

0 comments on commit fd9a524

Please sign in to comment.