-
Notifications
You must be signed in to change notification settings - Fork 54
/
index.js
108 lines (87 loc) · 4.02 KB
/
index.js
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
97
98
99
100
101
102
103
104
105
106
107
108
'use strict';
// var debug = require('debug')('plugin:analytics');
var volos = require('volos-analytics-apigee');
module.exports.init = function(config, logger /*, stats */) {
config.finalizeRecord = function finalizeRecord(req, res, record, cb) {
if (res.proxy) {
//detect healthcheck paths; if detected, add -health to the proxy name so that ax
//can distinguish between healthcheck calls and regular apis calls.
var proxyPath = req.url.split('?')[0];
if (config.proxyPath) {
if (config.proxyPath === proxyPath) {
record.apiproxy = res.proxy.name + "-health";
record.apiproxy_revision = res.proxy.revision;
}
} else if (config.relativePath) {
var relativePath = "/" + proxyPath.split('/')[2];
if (config.relativePath === relativePath) {
record.apiproxy = res.proxy.name + "-health";
record.apiproxy_revision = res.proxy.revision;
}
} else {
record.apiproxy = res.proxy.name;
record.apiproxy_revision = res.proxy.revision;
}
}
if (config.mask_request_uri) {
record.request_uri = config.mask_request_uri;
}
if (config.mask_request_path) {
record.request_path = config.mask_request_path;
}
var xffHeader = req.headers['x-forwarded-for'];
if (xffHeader) {
record.client_ip = xffHeader;
}
record.client_received_start_timestamp = req.headers['client_received_start_timestamp'];
record.client_received_end_timestamp = req.headers['client_received_end_timestamp'];
record.target_sent_start_timestamp = req.headers['target_sent_start_timestamp'];
record.target_sent_end_timestamp = req.headers['target_sent_end_timestamp'] + 1; //tmp hack
record.target_received_start_timestamp = req.headers['target_received_start_timestamp'];
record.target_received_end_timestamp = req.headers['target_received_end_timestamp'];
record['target_basepath'] = req.targetPath;
record['target_host'] = req.targetHostname;
record['target_url'] = ( req.targetSecure ? 'https' : 'http' ) +
'://' + req.targetHostname +
( req.targetPort ? ':' + req.targetPort : "") + req.targetPath;
record['target_response_code'] = req.headers['target_response_code'];
record.api_product = req.api_product || record.api_product;
try {
cb(null, record);
} catch (e) {
logger.eventLog({level:'error', req: req, res: res, err:e, component:'analytics' },"Error encountered processing Apigee analytics. Allowing request processing to continue");
}
};
var analytics = volos.create(config);
var middleware = analytics.expressMiddleWare().apply();
return {
testprobe: function() {
return analytics
},
onrequest: function(req, res, next) {
var timestamp = Date.now();
req.headers['client_received_start_timestamp'] = req.reqStartTimestamp || timestamp;
//do not send analytics for MG operating in local mode
if (!process.env.EDGEMICRO_LOCAL) {
middleware(req, res, next);
} else {
next();
}
},
onend_request: function(req, res, next) {
var timestamp = Date.now();
req.headers['client_received_end_timestamp'] = timestamp;
next();
},
onresponse: function(req, res, next) {
var timestamp = Date.now();
req.headers['target_received_start_timestamp'] = timestamp;
next();
},
onend_response: function(req, res, next) {
var timestamp = Date.now();
req.headers['target_received_end_timestamp'] = timestamp;
next();
}
};
}