Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Express Trace Plugin #363

Merged
merged 6 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module.exports = {
// This field is experimental.
plugins: {
'connect': path.join(__dirname, 'src/plugins/plugin-connect.js'),
'express': path.join(__dirname, 'src/plugins/plugin-express.js'),
'grpc': path.join(__dirname, 'src/plugins/plugin-grpc.js'),
'hapi': path.join(__dirname, 'src/plugins/plugin-hapi.js'),
'http': path.join(__dirname, 'src/plugins/plugin-http.js'),
Expand Down
5 changes: 1 addition & 4 deletions src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ var fs = require('fs');
// will result in the module itself being patched.
// 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: {} } }
});
var toInstrument = Object.create(null);

var logger;

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

This file was deleted.

93 changes: 93 additions & 0 deletions src/plugins/plugin-express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Copyright 2017 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';

This comment was marked as spam.

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

This comment was marked as spam.

This comment was marked as spam.

url: req.originalUrl,
skipFrames: 3
};
api.runInRootSpan(options, function(rootSpan) {
if (!rootSpan) {
next();
return;
}

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

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

var url = req.protocol + '://' + req.hostname + req.originalUrl;
rootSpan.addLabel(labels.HTTP_METHOD_LABEL_KEY, req.method);
rootSpan.addLabel(labels.HTTP_URL_LABEL_KEY, url);
rootSpan.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) {
rootSpan.addLabel('express/request.route.path', req.route.path);
}
rootSpan.addLabel(labels.HTTP_RESPONSE_CODE_LABEL_KEY, res.statusCode);
rootSpan.endSpan();
return returned;
};

next();
});
}

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

function unpatchModuleRoot(express) {
methods.forEach(function(method) {
shimmer.unwrap(express.application, method);
});
}

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