1 | 'use strict';
|
2 |
|
3 | var debug = require('debug')('plugin:analytics');
|
4 | var volos = require('volos-analytics-apigee');
|
5 | module.exports.init = function(config, logger, stats) {
|
6 |
|
7 | config.finalizeRecord = function finalizeRecord(req, res, record, cb) {
|
8 | if (res.proxy) {
|
9 |
|
10 |
|
11 | var proxyPath = req.url.split('?')[0];
|
12 | if (config.proxyPath) {
|
13 | if (config.proxyPath == proxyPath) {
|
14 | record.apiproxy = res.proxy.name + "-health";
|
15 | record.apiproxy_revision = res.proxy.revision;
|
16 | }
|
17 | } else if (config.relativePath) {
|
18 | var relativePath = "/" + proxyPath.split('/')[2];
|
19 | if (config.relativePath == relativePath) {
|
20 | record.apiproxy = res.proxy.name + "-health";
|
21 | record.apiproxy_revision = res.proxy.revision;
|
22 | }
|
23 | } else {
|
24 | record.apiproxy = res.proxy.name;
|
25 | record.apiproxy_revision = res.proxy.revision;
|
26 | }
|
27 | }
|
28 |
|
29 | if (config.mask_request_uri) {
|
30 | record.request_uri = config.mask_request_uri;
|
31 | }
|
32 |
|
33 | if (config.mask_request_path) {
|
34 | record.request_path = config.mask_request_path;
|
35 | }
|
36 |
|
37 |
|
38 | var xffHeader = req.headers['x-forwarded-for'];
|
39 | if (xffHeader) {
|
40 | record.client_ip = xffHeader;
|
41 | }
|
42 |
|
43 | record.client_received_start_timestamp = req.headers['client_received_start_timestamp'];
|
44 | record.client_received_end_timestamp = req.headers['client_received_end_timestamp'];
|
45 |
|
46 | record.target_sent_start_timestamp = req.headers['target_sent_start_timestamp'];
|
47 | record.target_sent_end_timestamp = req.headers['target_sent_end_timestamp'] + 1;
|
48 |
|
49 | record.target_received_start_timestamp = req.headers['target_received_start_timestamp'];
|
50 | record.target_received_end_timestamp = req.headers['target_received_end_timestamp'];
|
51 |
|
52 | try {
|
53 | cb(null, record);
|
54 | } catch (e) {
|
55 | logger.error("Error encountered processing Apigee analytics. Allowing request processing to continue", e);
|
56 | }
|
57 | };
|
58 |
|
59 | var analytics = volos.create(config);
|
60 | var middleware = analytics.expressMiddleWare().apply();
|
61 |
|
62 | return {
|
63 |
|
64 | testprobe: function() {
|
65 | return analytics
|
66 | },
|
67 |
|
68 | onrequest: function(req, res, next) {
|
69 | var timestamp = Date.now();
|
70 | req.headers['client_received_start_timestamp'] = req.reqStartTimestamp || timestamp;
|
71 |
|
72 | if (!process.env.EDGEMICRO_LOCAL) {
|
73 | middleware(req, res, next);
|
74 | } else {
|
75 | next();
|
76 | }
|
77 | },
|
78 |
|
79 | onend_request: function(req, res, next) {
|
80 | var timestamp = Date.now();
|
81 | req.headers['client_received_end_timestamp'] = timestamp;
|
82 | next();
|
83 | },
|
84 |
|
85 | onresponse: function(req, res, next) {
|
86 | var timestamp = Date.now();
|
87 | req.headers['target_received_start_timestamp'] = timestamp;
|
88 | next();
|
89 | },
|
90 |
|
91 | onend_response: function(req, res, next) {
|
92 | var timestamp = Date.now();
|
93 | req.headers['target_received_end_timestamp'] = timestamp;
|
94 | next();
|
95 | }
|
96 |
|
97 | };
|
98 |
|
99 | } |
\ | No newline at end of file |