nats-hemera
Advanced tools
Comparing version 0.1.51 to 0.1.52
@@ -9,3 +9,4 @@ /*! | ||
const Util = require('./util') | ||
const Util = require('./util'), | ||
Hoek = require('hoek') | ||
@@ -36,22 +37,10 @@ /** | ||
*/ | ||
static preHandler(ctx, msg, pattern) { | ||
static onPreHandler(ctx, msg, pattern) { | ||
//Check if request$ was successfully transfered | ||
//Can fail when message is not returned | ||
if (!msg.request$) { | ||
msg.request$ = {} | ||
msg.request$.pattern = Util.pattern(pattern) | ||
msg.request$.endTime = Util.nowHrTime() | ||
msg.request$.duration = msg.request$.endTime - msg.request$.startTime | ||
} else { | ||
msg.request$.endTime = Util.nowHrTime() | ||
msg.request$.pattern = Util.pattern(pattern) | ||
msg.request$.duration = msg.request$.endTime - msg.request$.startTime | ||
} | ||
//Pass request to act context | ||
ctx.request$ = msg.request$ | ||
//Pass to act context | ||
ctx.request$ = msg.request$ || {} | ||
ctx.request$.pattern = Util.pattern(pattern) | ||
ctx.trace$ = msg.trace$ || {} | ||
ctx.trace$.service = Util.pattern(pattern) | ||
ctx.meta$ = msg.meta$ || {} | ||
} | ||
@@ -63,2 +52,3 @@ /** | ||
* @param {any} pattern | ||
* @param {any} cleanPattern | ||
* @param {any} ctx | ||
@@ -69,14 +59,30 @@ * @param {any} prevCtx | ||
*/ | ||
static preRequest(pattern, ctx, prevCtx) { | ||
static onPreRequest(pattern, cleanPattern, ctx, prevCtx) { | ||
//Set context by pattern or current context$ | ||
//Shared context | ||
ctx.context$ = pattern.context$ || prevCtx.context$ | ||
//Set metadata by passed pattern or previous context | ||
ctx.meta$ = (pattern.meta$ || ctx.meta$) || {} | ||
ctx.meta$.traceId = ctx.meta$.traceId || Util.createUniqueId() | ||
//Set metadata by passed pattern or current message context | ||
ctx.meta$ = Hoek.merge(pattern.meta$ || {}, ctx.meta$) | ||
//Expose to current act handler | ||
ctx.parentId$ = ctx.requestId$; | ||
ctx.requestId$ = pattern.requestId$ || Util.createUniqueId() | ||
//Tracing | ||
ctx.trace$ = pattern.trace$ || {} | ||
ctx.trace$.parentSpanId = prevCtx.trace$.spanId | ||
ctx.trace$.traceId = prevCtx.trace$.traceId || Util.createUniqueId() | ||
ctx.trace$.spanId = pattern.trace$ ? pattern.trace$.spanId : Util.createUniqueId() | ||
ctx.trace$.timestamp = Util.nowHrTime() | ||
//Request | ||
let request = {} | ||
request.id = pattern.requestId$ || Util.createUniqueId() | ||
request.parentId = ctx.request$.id | ||
request.timestamp = Util.nowHrTime() | ||
//Build msg | ||
ctx.msg = { | ||
pattern: cleanPattern, | ||
meta$: ctx.meta$, | ||
trace$: ctx.trace$, | ||
request$: request | ||
} | ||
} | ||
@@ -83,0 +89,0 @@ |
@@ -9,3 +9,4 @@ /*! | ||
const Util = require('./util') | ||
const Util = require('./util'), | ||
Errio = require('errio') | ||
@@ -27,8 +28,3 @@ /** | ||
*/ | ||
static start(ctx) { | ||
ctx.delegationData = {} | ||
ctx.delegationData.response$ = {} | ||
ctx.delegationData.response$.startTime = Util.nowHrTime() | ||
} | ||
static start(ctx) {} | ||
/** | ||
@@ -43,17 +39,10 @@ * | ||
*/ | ||
static preRequest(ctx, msg) { | ||
static onPreRequest(ctx, msg) { | ||
//Pass metadata to request payload | ||
let result = msg.pattern | ||
result.meta$ = msg.meta$ | ||
result.request$ = msg.request$ | ||
//Pass parentId$, requestId$ from previous request to current add handler context to preserve chain | ||
ctx.parentId$ = msg.request$.id | ||
ctx.requestId$ = msg.request$.id | ||
ctx.meta$ = msg.meta$ || {} | ||
ctx.trace$ = msg.trace$ || {} | ||
ctx.request$ = msg.request$ || {} | ||
//Append to response to transfer metadata to the next | ||
ctx.delegationData.meta$ = msg.meta$ || {} | ||
ctx.delegationData.request$ = msg.request$ | ||
return result | ||
@@ -70,6 +59,17 @@ } | ||
*/ | ||
static preResponse(ctx, msg) { | ||
static OnPreResponse(ctx, result) { | ||
ctx.delegationData.response$.endTime = Util.nowHrTime() | ||
ctx.delegationData.response$.duration = ctx.delegationData.response$.endTime - ctx.delegationData.response$.startTime | ||
let message = {} | ||
message.meta$ = ctx.meta$ || {} | ||
message.trace$ = ctx.trace$ || {} | ||
message.request$ = ctx.request$ | ||
message.result = result instanceof Error ? null : result | ||
message.error = result instanceof Error ? Errio.stringify(result) : null | ||
let endTime = Util.nowHrTime() | ||
message.request$.duration = endTime - message.request$.timestamp | ||
message.trace$.duration = endTime - message.request$.timestamp | ||
ctx.message = message | ||
} | ||
@@ -76,0 +76,0 @@ |
@@ -48,30 +48,32 @@ /*! | ||
super() | ||
super() | ||
this._config = Hoek.applyToDefaults(defaultConfig, params || {}) | ||
this._catalog = Bloomrun() | ||
this._transport = transport | ||
this._events = new Kilt([this, this.transport]) | ||
this._topics = {} | ||
this._plugins = {} | ||
this._config = Hoek.applyToDefaults(defaultConfig, params || {}) | ||
this._catalog = Bloomrun() | ||
this._transport = transport | ||
this._events = new Kilt([this, this.transport]) | ||
this._topics = {} | ||
this._plugins = {} | ||
//Special variables for act and add | ||
this.context$ = {} | ||
this.meta$ = {} | ||
this.plugin$ = {} | ||
//Special variables for act and add | ||
this.context$ = {} | ||
this.meta$ = {} | ||
this.plugin$ = {} | ||
this.trace$ = {} | ||
this.request$ = {} | ||
this.log = this._config.logger || new DefaultLogger({ | ||
level: this._config.logLevel | ||
}) | ||
this.log = this._config.logger || new DefaultLogger({ | ||
level: this._config.logLevel | ||
}) | ||
this.payloadValidator = this._config.payloadValidator || DefaultPayloadValidator | ||
this.payloadValidator = this._config.payloadValidator || DefaultPayloadValidator | ||
} | ||
/** | ||
* | ||
* | ||
* @readonly | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
} | ||
/** | ||
* | ||
* | ||
* @readonly | ||
* | ||
* @memberOf Hemera | ||
*/ | ||
get plugins() { | ||
@@ -218,22 +220,13 @@ | ||
*/ | ||
reply(data) { | ||
reply(result) { | ||
AddContext.preResponse(this, data) | ||
if (result instanceof Error) { | ||
if (data instanceof Error) { | ||
this.log.error(result) | ||
} | ||
this.log.error(data) | ||
AddContext.OnPreResponse(this, result) | ||
return Util.stringifyJSON(Hoek.merge({ | ||
result: null, | ||
error: Errio.stringify(data) | ||
}, this.delegationData)) | ||
return Util.stringifyJSON(this.message) | ||
} | ||
return Util.stringifyJSON(Hoek.merge({ | ||
result: data, | ||
error: null | ||
}, this.delegationData)) | ||
} | ||
@@ -287,3 +280,3 @@ /** | ||
result = AddContext.preRequest(ctx, result.value) | ||
result = AddContext.onPreRequest(ctx, result.value) | ||
@@ -457,25 +450,14 @@ try { | ||
ActContext.preRequest(pattern, ctx, this) | ||
//Clean special $ variables | ||
let cleanPattern = Util.cleanPattern(pattern) | ||
//Parse msg as JSON | ||
let msg = { | ||
pattern: cleanPattern, | ||
meta$: ctx.meta$, | ||
request$: { | ||
id: ctx.requestId$, | ||
parentId: ctx.parentId$, | ||
startTime: Util.nowHrTime() | ||
} | ||
} | ||
ActContext.onPreRequest(pattern, cleanPattern, ctx, this) | ||
this.log.info(pattern, `ACT_OUTBOUND - ID:${msg.request$.id}`) | ||
this.log.info(pattern, `ACT_OUTBOUND - ID:${ctx.msg.request$.id}`) | ||
//Emit event | ||
this.emit('outbound', msg) | ||
this.emit('outbound', ctx.msg) | ||
//Request to topic | ||
let sid = this.sendRequest(pattern.topic, Util.stringifyJSON(msg), (response) => { | ||
let sid = this.sendRequest(pattern.topic, Util.stringifyJSON(ctx.msg), (response) => { | ||
@@ -500,3 +482,3 @@ //Parse response as JSON | ||
ActContext.preHandler(ctx, msg.value, cleanPattern) | ||
ActContext.onPreHandler(ctx, msg.value, cleanPattern) | ||
@@ -507,3 +489,3 @@ //Emit event | ||
//Log finished request and show id and duration in ms. | ||
this.log.info(`ACT_INBOUND - ID:${msg.value.request$.id} (${msg.value.request$.duration / 1000000}ms)`) | ||
this.log.info(`ACT_INBOUND - ID:${ctx.request$.id} (${ctx.request$.duration / 1000000}ms)`) | ||
@@ -510,0 +492,0 @@ if (typeof cb === 'function') { |
{ | ||
"name": "nats-hemera", | ||
"author": "Dustin Deus (https://github.com/StarpTech)", | ||
"version": "0.1.51", | ||
"version": "0.1.52", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "homepage": "https://hemerajs.github.io/hemera/", |
@@ -266,24 +266,22 @@ # Hemera | ||
### Tracing capabilities | ||
Tracing in the style of [Google’s Dapper](http://static.googleusercontent.com/media/research.google.com/en//pubs/archive/36356.pdf) | ||
In any act or add you can access the property `this.parentId$` or `this.requestId$` to get information about your current or parent call. You can listen on the `inbound` event to get detail information about you calls this includes response/request timing and durations. | ||
In any act or add you can access the property `this.request$` or `this.trace$` to get information about your current or parent call. You can listen on the `inbound` event to get detail information. | ||
```js | ||
result: 10 | ||
error: null | ||
response$: { | ||
"startTime": 456447212806, //You can check how long did it take to execute your server-side implementation | ||
"endTime": 456447216611, | ||
"duration": 3805 //Total time in nanoseconds | ||
meta$: {} | ||
trace$: { | ||
"traceId": "CRCNVG28BUVOBUS7MDY067", | ||
"spanId": "CRCNVG28BUVOLJT4L6B2DW", | ||
"timestamp": 887381084442, | ||
"duration": 10851, | ||
"service": "a:1,b:20,cmd:add,topic:math" | ||
} | ||
meta$: { | ||
"traceId": "XBZPK4DHF2HI3FBODLGB1P" //All requests associated with a specific trace id | ||
}, | ||
request$: { | ||
"id": "32HDHW7G1WRMNF2B6Y3KOD", //Current request id | ||
"parentId": "32HDHW7G1WRMU0ZHX9J864", //Previous request id | ||
"startTime": 456447211853, | ||
"endTime": 456447216759, | ||
"duration": 4906, //Total time in nanoseconds | ||
"pattern": "a:1,b:3,cmd:sub,topic:math" //Current matched pattern | ||
"id": "CRCNVG28BUVONL3P5L76AR", | ||
"timestamp": 887381084459, | ||
"duration": 10851, | ||
"pattern": "a:1,b:20,cmd:add,topic:math" | ||
} | ||
result: 50 | ||
``` | ||
@@ -290,0 +288,0 @@ |
43380
858
483