How to dynamically retrieve variables from ServiceCallOut policy to make logging into splunk.

Not applicable
  1. is there a way to get dynamically to get {policy-name} of serviceCallout ? otherwise, I have to do hardcode of policy-name of servicecallout.
  2. is there any policy to retrieve serviceCallOut information for logging purpose ?

We have mostly used Servicecallout policy for 4-5 backend calls. How can we dynamically retrieve the request, request-form-params, request-queryparams, response payload in serviceCallout. That solution we can implement for other API proxies as well, once it worked for one API proxy. I have tried with servicecallout.{policy-name}.target.url, as mentioned in https://docs.apigee.com/api-services/reference/variables-reference but it returns null. Don't understand why it is not working or how to use it while doing the logging.

0 3 453
3 REPLIES 3

1) I am not sure of the benefits of trying to get the policy name logged into splunk. Would it make more sense to log the URL of the Service you are invoking? At this point i am guessing that you are using the policy name logging to associate the log record with the service being invoked. Personally, i think the URL is a much better way to do that.

2) Yes, you can get a hold of all the variables that you mentioned. Request and response in a service call out policy are context variables. And you should be able to access these variables in a message logging policy placed in the same context (assuming variables have not been cleared "clearPayload" attribute)

So, in a message logging policy if you did {serviceCallOutResponse.content}, it would log the content of the service call out response. Here, i have assumed that 'serviceCallOutResponse' is the name of the response variable in the Service Call out policy

Hi @rmishra,

Thanks for responding to my queries.

I would like to explain the point 1 here. We have a bunch of API Proxies. Each API Proxy has around 3 to 4 Service Callout policies where policy names are different. We want to log backend api request and response sent/received by each of this API Proxy.

In order to implement this logic, we want to create a reusable generic shared flow which can be reused in all the API Proxies however we are unable to retrieve service callout policy name dynamically as mentioned here below in the shared flow as it is giving a null value. servicecallout.{policy-name}.target.url Otherwise, we will have to hardcode the service callout policy name, request & response variable names and write the same duplicate logic in each of the API Proxy under PostClientFlow

Ex:

var getDummyUrl = context.getVariable("servicecallout.CalloutDummy.target.url");

var qParam1 = context.getVariable("myRequest1.queryparam.bookNumber");

var qParam2 = context.getVariable("myRequest1.queryparam.startDate");

var qParam3 = context.getVariable("myRequest1.queryparam.endDate");

var qParam4 = context.getVariable("myRequest1.queryparam.bookName");

var getDummyResponse = context.getVariable("serviceCalloutResponse.content");

The variables hardcoded here as mentioned below:

<policy-name> = "CalloutDummy"

Request variable of Service Callout = "myRequest1"

Response = "serviceCalloutResponse".

You can parameterize JavaScript with Properties.

<Javascript name='JS-1' timeLimit='1200' >
  <Properties>
    <!-- to retrieve properties in js code:   properties.PROPNAME -->
    <Property name='policyName'>SC-1</Property>
    <Property name='requestVar'>SC-1-Request</Property>
    <Property name='responseVar'>SC-1-Response</Property>
    <Property name='qparams'>bookNumber,startDate,endDate,bookName</Property>
  </Properties>
  <ResourceURL>jsc://collectServiceCalloutData.js</ResourceURL>
</Javascript>

Then in JS code:


var varName = "servicecallout." + properties.policyName + ".target.url";
var scUrl = context.getVariable(varName);


var scParams = properties.qparams
  .split(',')
  .map(function(pname) {
    return context.getVariable(properties.requestVar + ".queryparam." + pname);
  });


var scResponseBody = context.getVariable(properties.responseVar + ".content");


...