Skip to content

Commit

Permalink
Support Connect tracing through the plugin API (#381)
Browse files Browse the repository at this point in the history
PR-URL: #381
  • Loading branch information
DominicKramer authored Feb 16, 2017
1 parent 5c5e3e2 commit 4e20f68
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 128 deletions.
5 changes: 3 additions & 2 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ module.exports = {
// all.
// This field is experimental.
plugins: {
'restify': path.join(__dirname, 'src/plugins/plugin-restify.js'),
'connect': path.join(__dirname, 'src/plugins/plugin-connect.js'),
'koa': path.join(__dirname, 'src/plugins/plugin-koa.js'),
'mongodb-core': path.join(__dirname, 'src/plugins/plugin-mongodb-core.js')
'mongodb-core': path.join(__dirname, 'src/plugins/plugin-mongodb-core.js'),
'restify': path.join(__dirname, 'src/plugins/plugin-restify.js')
},

// Valid entries are:
Expand Down
2 changes: 0 additions & 2 deletions src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ var toInstrument = Object.create(null, {
'mysql': { enumerable: true, value: { file: './userspace/hook-mysql.js',
patches: {} } },
'redis': { enumerable: true, value: { file: './userspace/hook-redis.js',
patches: {} } },
'connect': { enumerable: true, value: { file: './userspace/hook-connect.js',
patches: {} } }
});

Expand Down
124 changes: 0 additions & 124 deletions src/hooks/userspace/hook-connect.js

This file was deleted.

85 changes: 85 additions & 0 deletions src/plugins/plugin-connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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.
*/
'use strict';

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();
}

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

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);

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);

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();

return returned;
};

next();
});
}

module.exports = [
{
file: '',
versions: SUPPORTED_VERSIONS,
intercept: function(connect, api) {
return function() {
var app = connect();
app.use(middleware.bind(null, api));
return app;
};
}
}
];

0 comments on commit 4e20f68

Please sign in to comment.