Skip to content

Commit

Permalink
Improved metrics and handling of messages with missing header.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjus committed May 24, 2017
1 parent 6683c32 commit 145ea33
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
30 changes: 25 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1123,11 +1123,12 @@ class Hydra extends EventEmitter {

this.redisdb.get(`${redisPreKey}:${instance.serviceName}:${instance.instanceID}:presence`, (err, _result) => {
if (err) {
this.emit('metric', `service:unavailable|${instance.serviceName}|${instance.instanceID}`);
this.emit('metric', `service:unavailable|${instance.serviceName}|${instance.instanceID}|presence:not:found`);
reject(err);
} else {
this.redisdb.hget(`${redisPreKey}:nodes`, instance.instanceID, (err, result) => {
if (err) {
this.emit('metric', `service:unavailable|${instance.serviceName}|${instance.instanceID}|instance:not:found`);
reject(err);
} else {
instance = Utils.safeJSONParse(result);
Expand All @@ -1151,18 +1152,19 @@ class Hydra extends EventEmitter {
options.body = Utils.safeJSONStringify(umfmsg.body);
serverRequest.send(Object.assign(options, sendOpts))
.then((res) => {
if (res.payLoad && res.headers['content-type'].indexOf('json') > -1) {
if (res.payLoad && res.headers['content-type'] && res.headers['content-type'].indexOf('json') > -1) {
res = Object.assign(res, Utils.safeJSONParse(res.payLoad.toString('utf8')));
delete res.payLoad;
}
resolve(serverResponse.createResponseObject(res.statusCode, res));
})
.catch((_err) => {
this.emit('metric', `service:unavailable|${instance.serviceName}|${instance.instanceID}`);
.catch((err) => {
instanceList.shift();
if (instanceList.length === 0) {
this.emit('metric', `service:unavailable|${instance.serviceName}|${instance.instanceID}|attempts:exhausted`);
resolve(this._createServerResponseWithReason(ServerResponse.HTTP_SERVICE_UNAVAILABLE, `An instance of ${instance.serviceName} is unavailable`));
} else {
this.emit('metric', `service:unavailable|${instance.serviceName}|${instance.instanceID}|${err.message}`);
this._tryAPIRequest(instanceList, parsedRoute, umfmsg, resolve, reject, sendOpts);
}
});
Expand All @@ -1183,7 +1185,7 @@ class Hydra extends EventEmitter {
* @return {promise} promise - response from API in resolved promise or
* error in rejected promise.
*/
_makeAPIRequest(message, sendOpts={}) {
_makeAPIRequest(message, sendOpts = { }) {
return new Promise((resolve, reject) => {
let umfmsg = UMFMessage.createMessage(message);
if (!umfmsg.validate()) {
Expand Down Expand Up @@ -1545,6 +1547,15 @@ class Hydra extends EventEmitter {
return require('./lib/umfmessage');
}

/**
* @name _getServerRequestHelper
* @summary returns ServerRequest helper
* @return {object} helper - service request helper
*/
_getServerRequestHelper() {
return require('./lib/server-request');
}

/**
* @name _getServerResponseHelper
* @summary returns ServerResponse helper
Expand Down Expand Up @@ -2084,6 +2095,15 @@ class IHydra extends Hydra {
return super._getUMFMessageHelper();
}

/**
* @name getServerRequestHelper
* @summary returns ServerRequest helper
* @return {object} helper - service request helper
*/
getServerRequestHelper() {
return super._getServerRequestHelper();
}

/**
* @name getServerResponseHelper
* @summary returns ServerResponse helper
Expand Down
1 change: 1 addition & 0 deletions lib/server-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ServerRequest {
send(options) {
return new Promise((resolve, reject) => {
if (options.method === 'POST' || options.method === 'PUT') {
options.headers = options.headers || {};
options.headers['content-length'] = Buffer.byteLength(options.body, 'utf8');
} else {
delete options.body;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hydra",
"version": "1.3.13",
"version": "1.4.6",
"license": "MIT",
"author": "Carlos Justiniano",
"contributors": [
Expand Down

0 comments on commit 145ea33

Please sign in to comment.