Log time taken of all policies in a proxy endpoint

Not applicable

If there are 2 policies in a flow, the scenario I am trying to create in the message logging policy is a concatenated statement with strings and variables:

--> FirstPolicyName + apigee.metrics.policy.FirstPolicyName.timetaken + SecondPolicyName + apigee.metrics.policy.SecondPolicyName.timetaken

Is there a way to dynamically do this through making the policy name a variable, in which may be used and be applicable to all flows? I know javascript is one option for this, but currently thinking that this will require a lot of js.

Solved Solved
2 4 1,209
1 ACCEPTED SOLUTION

If I were doing this, I would add a single JavaScript policy in the PostFlow that iterates through all the policy names. I don't think it requires a lot of JS. Just one loop. You will need to keep the list of policy names in the JS reconciled with the policies that execute in the proxy.

It would look something like this:

var policyNames = [ 'AM-RemoveHeader', 'XSL-1', 'OAuthV2-VerifyAccessToken'];

function doOnePolicy(name) {
  var s = name + ' ';
  var v = context.getVariable('apigee.metrics.policy.' + name + '.timetaken');
  s += (v) ? v : '-unknown-';
  return s;
}
var result = policyNames.map(doOnePolicy).join(',');
context.setVariable('bigString', result);

View solution in original post

4 REPLIES 4

If I were doing this, I would add a single JavaScript policy in the PostFlow that iterates through all the policy names. I don't think it requires a lot of JS. Just one loop. You will need to keep the list of policy names in the JS reconciled with the policies that execute in the proxy.

It would look something like this:

var policyNames = [ 'AM-RemoveHeader', 'XSL-1', 'OAuthV2-VerifyAccessToken'];

function doOnePolicy(name) {
  var s = name + ' ';
  var v = context.getVariable('apigee.metrics.policy.' + name + '.timetaken');
  s += (v) ? v : '-unknown-';
  return s;
}
var result = policyNames.map(doOnePolicy).join(',');
context.setVariable('bigString', result);

Hi Dino, we have incorporated the function you shared for the looping to get time taken for each policies in a flow. Thank you!

An elegant solution as always. Thanks.

Just a quick feedback as you have to replace '.timetaken' with '.timeTaken' if you want to get the elapsed time of a policy