Skip to content

Commit 95f96f2

Browse files
bug fix for logging
1 parent 4930ed1 commit 95f96f2

File tree

5 files changed

+28
-27
lines changed

5 files changed

+28
-27
lines changed

config/constants.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ module.exports.PORTS = {
1717
MIN: 1
1818
};
1919

20-
module.exports.SERVER_TYPES = {
21-
PROXY: "proxy",
22-
REVERSE_PROXY: "reverse proxy"
23-
};
24-
2520
module.exports.LINE_LENGTH = 70;
2621
module.exports.PROTOCOL_REGEX = /(^\w+:|^)\/\//;
2722

src/proxy.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,19 @@ var RdHandler = {
8181
* @param {http.ServerResponse} clientResponse
8282
*/
8383
requestHandler: function (clientRequest, clientResponse) {
84+
var parsedClientUrl = url.parse(clientRequest.url);
85+
var metaData = {
86+
clientRequestUrl: clientRequest.url,
87+
toolRequestUrl: RdGlobalConfig.SCHEME + "://" + parsedClientUrl.host + parsedClientUrl.path
88+
};
8489
clientRequest.id = ++RdHandler._requestCounter + '::' + uuidv4();
8590
var request = {
8691
method: clientRequest.method,
8792
url: clientRequest.url,
8893
headers: clientRequest.headers,
8994
data: []
9095
};
91-
RdGlobalConfig.reqLogger.info(constants.TOPICS.CLIENT_REQUEST_START, request.method + ' ' + clientRequest.url,
96+
RdGlobalConfig.reqLogger.info(constants.TOPICS.CLIENT_REQUEST_START, clientRequest.method + ' ' + metaData.clientRequestUrl,
9297
false, {
9398
headers: request.headers
9499
},
@@ -101,9 +106,9 @@ var RdHandler = {
101106
furtherRequestOptions: furtherRequestOptions
102107
};
103108

104-
ReqLib.call(paramsForRequest, clientRequest, constants.SERVER_TYPES.PROXY)
109+
ReqLib.call(paramsForRequest, clientRequest, metaData)
105110
.then(function (response) {
106-
RdGlobalConfig.reqLogger.info(constants.TOPICS.CLIENT_RESPONSE_END, clientRequest.method + ' ' + clientRequest.url + ', Status Code: ' + response.statusCode,
111+
RdGlobalConfig.reqLogger.info(constants.TOPICS.CLIENT_RESPONSE_END, clientRequest.method + ' ' + metaData.clientRequestUrl + ', Status Code: ' + response.statusCode,
107112
false, {
108113
data: response.data,
109114
headers: response.headers,
@@ -114,14 +119,14 @@ var RdHandler = {
114119
clientResponse.end(response.data);
115120
})
116121
.catch(function (err) {
117-
RdGlobalConfig.reqLogger.error(err.customTopic || constants.TOPICS.UNEXPECTED_ERROR, clientRequest.method + ' ' + clientRequest.url,
122+
RdGlobalConfig.reqLogger.error(err.customTopic || constants.TOPICS.UNEXPECTED_ERROR, clientRequest.method + ' ' + metaData.toolRequestUrl,
118123
false, {
119124
errorMessage: err.message.toString()
120125
},
121126
clientRequest.id);
122127

123128
var errorResponse = RdHandler._frameErrorResponse(furtherRequestOptions, err.message.toString());
124-
RdGlobalConfig.reqLogger.error(constants.TOPICS.CLIENT_RESPONSE_END, clientRequest.method + ' ' + clientRequest.url + ', Status Code: ' + errorResponse.statusCode,
129+
RdGlobalConfig.reqLogger.error(constants.TOPICS.CLIENT_RESPONSE_END, clientRequest.method + ' ' + metaData.clientRequestUrl + ', Status Code: ' + errorResponse.statusCode,
125130
false,
126131
errorResponse.data,
127132
clientRequest.id);

src/requestLib.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var RequestLib = {
2020
* @param {http.IncomingMessage} clientRequest
2121
* @param {Number} retries
2222
*/
23-
_makeRequest: function (schemeObj, params, clientRequest, serverType, retries) {
23+
_makeRequest: function (schemeObj, params, clientRequest, metaData, retries) {
2424
return new Promise(function (resolve, reject) {
2525
var requestOptions = Object.assign({}, params.furtherRequestOptions);
2626
requestOptions.agent = RdGlobalConfig.SCHEME === 'http' ? httpKeepAliveAgent : httpsKeepAliveAgent;
@@ -61,9 +61,8 @@ var RequestLib = {
6161

6262
// Log the request that will be initiated on behalf of the client
6363
request.on('finish', function () {
64-
var requestUrl = RdGlobalConfig.SCHEME + "://" + requestOptions.host + requestOptions.path;
6564
RdGlobalConfig.reqLogger.info(constants.TOPICS.TOOL_REQUEST_WITH_RETRIES + retries,
66-
clientRequest.method + ' ' + requestUrl, false, Object.assign({}, params.furtherRequestOptions, {
65+
clientRequest.method + ' ' + metaData.toolRequestUrl, false, Object.assign({}, params.furtherRequestOptions, {
6766
data: Buffer.concat(params.request.data).toString()
6867
}),
6968
clientRequest.id);
@@ -108,8 +107,7 @@ var RequestLib = {
108107
});
109108

110109
clientRequest.on('end', function () {
111-
var requestUrl = serverType === constants.SERVER_TYPES.PROXY ? clientRequest.url : "http://" + clientRequest.headers.host + requestOptions.path;
112-
RdGlobalConfig.reqLogger.info(constants.TOPICS.CLIENT_REQUEST_END, params.request.method + ' ' + requestUrl, false, {
110+
RdGlobalConfig.reqLogger.info(constants.TOPICS.CLIENT_REQUEST_END, params.request.method + ' ' + metaData.clientRequestUrl, false, {
113111
data: Buffer.concat(params.request.data).toString()
114112
},
115113
clientRequest.id);
@@ -128,26 +126,26 @@ var RequestLib = {
128126
* @param {http.IncomingMessage} clientRequest
129127
* @param {Number} retries
130128
*/
131-
call: function (params, clientRequest, serverType, retries) {
129+
call: function (params, clientRequest, metaData, retries) {
132130
retries = (typeof retries === 'number') ? Math.min(constants.MAX_RETRIES, Math.max(retries, 0)) : constants.MAX_RETRIES;
133131
var schemeObj = RdGlobalConfig.SCHEME === "http" ? http : https;
134-
return RequestLib._makeRequest(schemeObj, params, clientRequest, serverType, retries)
132+
return RequestLib._makeRequest(schemeObj, params, clientRequest, metaData, retries)
135133
.catch(function (err) {
136134
var errTopic = err.customTopic || constants.TOPICS.UNEXPECTED_ERROR;
137135
// Collect Network & Connectivity Logs whenever a request fails
138136
RdGlobalConfig.networkLogHandler(errTopic, clientRequest.id);
139137
RdGlobalConfig.connHandler(errTopic, clientRequest.id);
140138

141139
if (retries > 0) {
142-
RdGlobalConfig.reqLogger.error(errTopic, clientRequest.method + ' ' + clientRequest.url,
140+
RdGlobalConfig.reqLogger.error(errTopic, clientRequest.method + ' ' + metaData.toolRequestUrl,
143141
false, {
144142
errorMessage: err.message.toString()
145143
},
146144
clientRequest.id);
147145

148146
return Utils.delay(RdGlobalConfig.RETRY_DELAY)
149147
.then(function () {
150-
return RequestLib.call(params, clientRequest, serverType, retries - 1, false);
148+
return RequestLib.call(params, clientRequest, metaData, retries - 1, false);
151149
});
152150
} else {
153151
throw err;

src/reverseProxy.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,19 @@ var RdHandler = {
8383
* @param {http.ServerResponse} clientResponse
8484
*/
8585
requestHandler: function (clientRequest, clientResponse) {
86-
clientRequest.id = ++RdHandler._requestCounter + '::' + uuidv4();
8786
var parsedClientUrl = url.parse(clientRequest.url);
88-
var requestUrl = "http://" + clientRequest.headers.host + parsedClientUrl.path;
87+
var metaData = {
88+
clientRequestUrl: "http://" + clientRequest.headers.host + parsedClientUrl.path,
89+
toolRequestUrl: RdGlobalConfig.SCHEME + "://" + constants.HUB_HOST + parsedClientUrl.path
90+
};
91+
clientRequest.id = ++RdHandler._requestCounter + '::' + uuidv4();
8992
var request = {
9093
method: clientRequest.method,
9194
url: clientRequest.url,
9295
headers: clientRequest.headers,
9396
data: []
9497
};
95-
RdGlobalConfig.reqLogger.info(constants.TOPICS.CLIENT_REQUEST_START, request.method + ' ' + requestUrl,
98+
RdGlobalConfig.reqLogger.info(constants.TOPICS.CLIENT_REQUEST_START, clientRequest.method + ' ' + metaData.clientRequestUrl,
9699
false, {
97100
headers: request.headers
98101
},
@@ -104,9 +107,9 @@ var RdHandler = {
104107
request: request,
105108
furtherRequestOptions: furtherRequestOptions
106109
};
107-
ReqLib.call(paramsForRequest, clientRequest, constants.SERVER_TYPES.REVERSE_PROXY)
110+
ReqLib.call(paramsForRequest, clientRequest, metaData)
108111
.then(function (response) {
109-
RdGlobalConfig.reqLogger.info(constants.TOPICS.CLIENT_RESPONSE_END, clientRequest.method + ' ' + requestUrl + ', Status Code: ' + response.statusCode,
112+
RdGlobalConfig.reqLogger.info(constants.TOPICS.CLIENT_RESPONSE_END, clientRequest.method + ' ' + metaData.clientRequestUrl + ', Status Code: ' + response.statusCode,
110113
false, {
111114
data: response.data,
112115
headers: response.headers,
@@ -117,14 +120,14 @@ var RdHandler = {
117120
clientResponse.end(response.data);
118121
})
119122
.catch(function (err) {
120-
RdGlobalConfig.reqLogger.error(err.customTopic || constants.TOPICS.UNEXPECTED_ERROR, clientRequest.method + ' ' + requestUrl,
123+
RdGlobalConfig.reqLogger.error(err.customTopic || constants.TOPICS.UNEXPECTED_ERROR, clientRequest.method + ' ' + metaData.toolRequestUrl,
121124
false, {
122125
errorMessage: err.message.toString()
123126
},
124127
clientRequest.id);
125128

126129
var errorResponse = RdHandler._frameErrorResponse(furtherRequestOptions, err.message.toString());
127-
RdGlobalConfig.reqLogger.error(constants.TOPICS.CLIENT_RESPONSE_END, clientRequest.method + ' ' + requestUrl + ', Status Code: ' + errorResponse.statusCode,
130+
RdGlobalConfig.reqLogger.error(constants.TOPICS.CLIENT_RESPONSE_END, clientRequest.method + ' ' + metaData.clientRequestUrl + ', Status Code: ' + errorResponse.statusCode,
128131
false,
129132
errorResponse.data,
130133
clientRequest.id);

test/reverseProxy.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ describe('RdHandler', function () {
184184
host: 'localhost',
185185
port: RdGlobalConfig.RD_HANDLER_REVERSE_PROXY_PORT,
186186
headers: {},
187-
path: constants.HUB_STATUS_PATH
187+
path: "http://user1:pass1@" + constants.HUB_HOST + constants.HUB_STATUS_PATH
188188
};
189189

190190
var responseData = [];

0 commit comments

Comments
 (0)