Antipattern: Log data to third party servers using the JavaScript policy

You're viewing Apigee Edge documentation.
Go to the Apigee X documentation.
info

Logging is one of the efficient ways for debugging problems. The information about the API request such as headers, form params, query params, dynamic variables, etc. can all be logged for later reference. The information can be logged locally on the Message Processors (Apigee Edge for Private Cloud only) or to third party servers such as Sumo Logic, Splunk, or Loggly.

Antipattern

In the code below, the httpClient object in a JavaScript policy is used to log the data to a Sumo Logic server. This means that the process of logging actually takes place during request/response processing. This approach is counterproductive because it adds to the processing time, thereby increasing overall latencies.

LogData_JS:

<!-- /antipatterns/examples/1-4.xml -->
<Javascript async="false" continueOnError="false" enabled="true" timelimit="2000" name="LogData_JS">
  <DisplayName>LogData_JS</DisplayName>
  <ResourceURL>jsc://LogData.js</ResourceURL>
</Javascript>

LogData.js:

<!-- /antipatterns/examples/1-5.xml -->
var sumoLogicURL = "...";
httpClient.send(sumoLogicURL);
waitForComplete();

The culprit here is the call to waitForComplete(), which suspends the caller's operations until the logging process is complete. A valid pattern is to make this asynchronous by eliminating this call.

Impact

  • Executing the logging code via the JavaScript policy adds to the latency of the API request.
  • Concurrent requests can stress the resources on the Message Processor and consequently adversely affecting the processing of other requests.

Best Practice

  • Use the MessageLogging policy to transfer/log data to Log servers or third party log management services such as Sumo Logic, Splunk, Loggly, etc.
  • The biggest advantage of the Message Logging policy is that it can be defined in the Post Client Flow, which gets executed after the response is sent back to the requesting client app.
  • Having this policy defined in Post Client flow helps separate the logging activity and the request/response processing thereby avoiding latencies.
  • If there is a specific requirement or a reason to do logging via JavaScript (not the Post Client flow option mentioned above), then it needs to be done asynchronously. In other words, if you are using a httpClient object, you would need to ensure that the JavaScript does NOT implement waitForComplete

Further reading