Having structured logs of running services is always important in productions. E2E transaction log files contain all the information about interactions on your server. That information is divided into transactions which represent the client calls (http) and io calls which represent the back end access made by your server (DB, file system ...).
On another level you can also trace business processes using the same library and the same files. That allows to relate the business data level with the service level.
These files can then be parsed and loaded into DB to be used in Dashboards showing very informative system, operating and business views and reports of your services.
var http = require('http');
var transactionLogger = require('e2e-transaction-logger');
// create an http server
http.createServer(function (req, res) {
// start the transaction when a request arrives
var trx = transactionLogger.startTransaction('Hello World');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
// end the transaction when the response is sent
trx.end('OK');
}).listen(3000, '127.0.0.1');
npm install e2e-transaction-logger
A transaction logger allows to start transactions.
You can create your own transaction logger using the constructor.
Arguments
- logPath - The path where the log files will be written.
Example
var TransactionLogger = require('e2e-transaction-logger').TransactionLogger;
var myTransactionLogger = new TransactionLogger('my/custom/log/path');
Trace the start of a transaction and return a Transaction object.
Arguments
- name - The name of the transaction
Example
var trx = transactionLogger.startTransaction('MyTransaction');
An object of type ProcessLogger.
A process logger allows to trace processes.
Example
transactionLogger.processLogger.processStart('ProcessName', 'processId', 'StartEventName');
You can create your own process logger using the constructor.
Arguments
- logPath - The path where the log files will be written.
Example
var myProcessLogger = new transactionLogger.ProcessLogger('my/custom/log/path');
Trace the start of a process.
Arguments
- processName - The name of the process
- processId - The process id.
- eventName - The start event name.
Example
processLogger.processStart('ProcessName', 'processId', 'StartEventName');
Trace the end of a process.
Arguments
- processName - The name of the process
- processId - The process id.
- eventName - The end event name.
Example
processLogger.processEnd('ProcessName', 'processId', 'EndEventName');
Trace the start of a state.
Arguments
- processName - The name of the process
- processId - The process id.
- stateName - The state name.
Example
processLogger.processStateStart('ProcessName', 'processId', 'StateName');
Trace the end of a state.
Arguments
- processName - The name of the process
- processId - The process id.
- stateName - The state name.
Example
processLogger.processStateEnd('ProcessName', 'processId', 'StateName');
Trace a choice.
Arguments
- processName - The name of the process
- processId - The process id.
- gateway - The gateway name.
- choice - The choice name.
Example
processLogger.processChoice('ProcessName', 'processId', 'Gateway', 'Choice');
Trace an event.
Arguments
- processName - The name of the process
- processId - The process id.
- eventName - The event name.
Example
processLogger.processEvent('ProcessName', 'processId', 'EventName');
Trace a value of a String type.
Arguments
- processName - The name of the process
- processId - The process id.
- key - The key.
- value - The value.
Example
processLogger.processValueString('ProcessName', 'processId', 'Key', 'MyValue');
Trace a value of a Float type.
Arguments
- processName - The name of the process
- processId - The process id.
- key - The key.
- value - The value.
Example
processLogger.processValueFloat('ProcessName', 'processId', 'Key', 123.456);
Trace a value of a Date type.
Arguments
- processName - The name of the process
- processId - The process id.
- key - The key.
- value - The value.
Example
processLogger.processValueDateTime('ProcessName', 'processId', 'Key', new Date());
A Transaction object is returned by the startTransaction method of a TransactionLogger. It is then used to trace what happens inside the transaction using startIO or any ProcessLogger method. Using transactions to trace processes instead of a ProcessLogger directly allows you to correlate the process steps and ios of these transactions.
Trace the end of a transaction
Arguments
- state - The state of the transaction. It can be true or 'OK' for a success, false or 'ERROR' for a failed. Default: 'OK'
Example
var trx = transactionLogger.startTransaction('MyTransaction');
// do some things
trx.end();
Trace the start of an IO call and return an IO object.
Arguments
- name - the name of the IO call
- domain - the domain of the IO call
- system - the system of the IO call
Example
var io = trx.startIO('SELECT Users','SQL','oracle/XE');
An IO object is returned by the startIO method of a Transaction. It is then used to trace the end of the io call.
Trace the end of an io call
Arguments
- state - The state of the io. It can be true or 'OK' for a success, false or 'ERROR' for a failed. Default: 'OK'
Example
var io = trx.startIO('Read','FILE','HelloWorld.txt');
fs.readFile(__dirname + '/resource/HelloWorld.html',function(err, data){
io.end();
});
A function that returns a middleware which can be used with express. It will automatically start a transaction when a request arrives and end it when the response is sent. The Transaction object will be available in the request object as trx.
Arguments
- options - object
- logPath - The path where the log files will be written.
- name - The name that should be given to the transactions. Can be a function taking the request and result as parameter and returning the name. If it is a string some special replacement variables can be used: {METHOD}, {URL}, {URL_PATH}, {URL_PATHNAME}, {URL_QUERY}, {URL_ORIGINAL_PART}, {ORIGINAL_URL}. By default the name will be the method and the url of the request ('{METHOD} {URL}').
Example
var express = require('express');
var fs = require('fs');
var transactionLoggerMiddleware = require('e2e-transaction-logger').transactionLoggerMiddleware;
var app = express();
app.use(transactionLoggerMiddleware());
app.get('/hello.html', function(req, res){
var io = req.trx.startIO('Read','FILE','HelloWorld.html');
fs.readFile(__dirname + '/resource/HelloWorld.html',function(err, data){
if(err){
io.end('ERROR');
res.send(503, err); // the status code define if the transaction failed or succeed. >= 400 it failed
return;
}
io.end();
res.set('Content-Type', 'text/html');
res.send(data);
});
});