Microgateway logging using custom plugin

sujnana
Participant IV

@Dino@Anil Sagar @ Google

I am trying to implement microgateway custom plugin for Splunk logging. We need to log for every requests (one call to splunk with response code, errors in including plugin errors if any, proxy name etc) in Splunk.

Following is the snippet of the logging plugin.

module.exports.init = function(config, logger, stats) {
	return {
		ondata_response: function(req, res,data, next) {
		   console.log("ondata_response");
		   splunkLog(req, res,data, config); //Splunk log function	   
		   next(null, data);
		}
	}
}

Following is the plugin sequence in config file.

  plugins:
    sequence:      
      - healthcheck
      - plugin1
      - oauth
      - plugin2       
      - mgw-logging

Note here logging plugin is in the end (mgw-logging). This works well for success flow & for any target error. But it will not log if any plugin failed before logging plugin. For example it will not log if apikey validation (oauth) is failed.

So I changed plugin & its sequence as below.

module.exports.init = function(config, logger, stats) {
		return {
			onend_response: function(req, res,data, next) {
			   console.log("SplunkLog - onend_response");
			   splunkLog(req, res,data, config);
			   console.log("After splunkLog");
			   next(null, data);
			},
			onerror_response: function(req, res,data, next) {
			   console.log("SplunkLog - onerror_response");
			   splunkLog(req, res,data, config);
			   console.log("After splunkLog");
			   next(null, data);
			},
			onerror_request: function(req, res,data, next) {
			   console.log("SplunkLog - onerror_request");
			   splunkLog(req, res,data, config);
			   console.log("After splunkLog");
			   next(data);
			}
		}
	}

Plugin sequence

  plugins:
    sequence:  
      - mgw-logging    
      - healthcheck
      - plugin1
      - oauth
      - plugin2 

Now the logging is the first plugin. Now also the result is same - only logs success and target errors. I think onerror_response invoked only if there is some error in same plugin. It will not be invoked for other plugin errors.

Is there any way to catch all the request including failed requests due to plugin failure/errors or backend service errors & log the details similar to MessageLogging policy?

0 1 226
1 REPLY 1

nejra
Participant II

I am interested in this too... Did you manage to solve it ?