forked from googleapis/cloud-trace-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin-express.js
More file actions
96 lines (83 loc) · 2.8 KB
/
Copy pathplugin-express.js
File metadata and controls
96 lines (83 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* 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';
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(rootSpan) {
// Set response trace context.
var responseTraceContext =
api.getResponseTraceContext(options.traceContext, !!rootSpan);
if (responseTraceContext) {
res.set(api.constants.TRACE_CONTEXT_HEADER_NAME, responseTraceContext);
}
if (!rootSpan) {
next();
return;
}
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() {
res.end = originalEnd;
var returned = res.end.apply(this, arguments);
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
}];