How to collect statistics on response attribute?

Not applicable

Hi,

I have a use case where i need to collect statistics on one of the response elements. In sample XML response below "vin" is the element , i have to collect for.

I am trying to extract vin numbers from response using extract variables policy. I have put correct Xpath(which i checked online) but still i don't see variable getting populated.

Because extract variable is not working , i guess statistics collector is also not working. Trace was always success and no errors were reported.

Could someone please assist me with this? Not sure what i am doing wrong here.

Is this the correct approach to collect stats on response element?

Sample XML response

<vins>
    <count>2</count>
    <vehicle>
        <vin>JHDCMX63X4C070196</vin>
        <year>2004</year>
        <make>Honda</make>
        <model>Accord LX</model>
    </vehicle>
    <vehicle>
        <vin>1FTZF1720XKB93XX5</vin>
        <year>1999</year>
        <make>Ford</make>
        <model>F150 Work Series / XL / XLT</model>
    </vehicle>
<vins>

Extract Variables policy

<ExtractVariables async="false" continueOnError="false" enabled="true" name="ExtractVariables.collectStats">
    <DisplayName>ExtractVariables.collectStats</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    <Source clearPayload="false">response</Source>
    <VariablePrefix>getvinstats</VariablePrefix>
    <XMLPayload stopPayloadProcessing="false">
        <Variable name="vehicleVin" type="string">
            <XPath>//vehicle/vin/text()</XPath>
        </Variable>
    </XMLPayload>
</ExtractVariables>

StatisticsCollector

<StatisticsCollector async="false" continueOnError="false" enabled="true" name="StatisticsCollector.getResponseVinStats">
    <DisplayName>StatisticsCollector.getResponseVinStats</DisplayName>
    <Properties/>
    <Statistics>
   <Statistic name="vin" ref="getvinstats.vehicleVin" type="String">No_Vin</Statistic>
    </Statistics>
</StatisticsCollector>
0 5 375
5 REPLIES 5

It appears that you are looking to get a list of VINS from an array of objects within an XML response. Unfortunately the extract variables policy in Edge won't work as you might expect. It will only populate a single value. If you need a list from an array you may need to use some JavaScript. See the link below.

https://community.apigee.com/questions/5548/using-xpath-in-extractvariables-policy-to-obtain-m.html

On another note there are a couple of things noticed in the example you gave, just FYI.

  1. I believe this is likely just a typo, but your XML sample needs to end in </vins>
  2. When you use a variable prefix sometimes the variables don't show up in trace. See this article

Hopefully that helps.

Note that once you have the list of VINs you still can't pass an array to the Stats Collector. Depending on your exact use case you may need to do something like write an additional proxy that is used to collect the stats. Then use your JavaScript to call the proxy for each VIN.

Again - your solution may vary depending on your use case.

Thanks to @Sai Saran Vaidyanathan for pointing out to to me the limitation with arrays.

@Greg Kuelgen

Thanks for responding and pointing me in right direction. But I am thinking how bad the performance would be, if I create another proxy for stats and call it for every single VIN. Because i might have more than 20 vins in a response and that means i will end up calling the new stats proxy 20 times.

Is the problem because i am doing in XML payload i can't get list of Vin's. What if i use JSON payload? Would it be straight forward?

I was looking for more sophisticated approach to handle this. Could you please let me know any other alternative?

The main issue isn't really getting the list of VINs. You can convert the XML to JSON using the XML to JSON policy as then it is a bit easier to work with in JavaScript. You can then loop through the array to get the list of VINs. Once you have the list of VINs, the issue is getting them into the analytics.

The statistics collector does not take an array. This is why the recommendation to develop another simply proxy that will take a VIN and add it to analytics using the statistics collector. Given that you may have many VINs performance is obviously important. Using JavaScript to call the statistics collector would allow you to make many calls in parallel. Depending on the importance of ensuring the stat is collected you may also be able to fire and forget.

Certainly not an ideal solution. Perhaps someone else in the community will offer a better suggestion as I can't claim to have all the answers:)

@Greg Kuelgen

I see your point about statistics collector limitation. I am thankful to you for providing a workaround solution, sure will keep it as an option. Thank you.