Customize the quota policy to count only 2XX calls

You can customize the quota policy to only count 2XX calls?

The only thing I found in the documentation was how to "apply incoming request quota and count destination responses based on a specified condition." - How to configure shared quota counters

But that didn't work for me, as much as the response quota only incremented the counter when it met my condition, the request quota continued to increase regardless of the status code.

Solved Solved
0 4 435
1 ACCEPTED SOLUTION

I need to limit clients querying our APIs to 'N' times a day, but I can't use requests other than 2XX in the summation.

I assumed it would be possible to do this using the quota policy, but I haven't found anything (other than what I've shown above) to meet this demand.

It is possible. The EnforceOnly / CountOnly feature, coupled with a Condition, does that for you. I just tried this and it's working for me.

Attached please find a working loopback proxy that behaves this way. If you invoke it as

GET /quota-200-only/200

...then the quota count will increase.

If you invoke it with

GET /quota-200-only/302

...then the quota count will not increase.

View solution in original post

4 REPLIES 4

You can customize the quota policy to only count 2XX calls?

No. and also Yes. You cannot configure the Quota policy itself, to count only calls for which the upstream system returns a 2xx response. You can associate a Condition element to the Quota policy such that the policy executes only under certain circumstances, such as when the response status code is within a specific range. So the easy way to "count only 2xx calls" is to build a Condition that does that, and associate it to a Quota policy. I guess that would look something like this:

 

 

 <Response> 
   ...
  <Step>
    <Name>Quota-1</Name> 
    <Condition>response.status.code GreaterThanOrEquals 200 AND
        response.status.code LesserThan 300</Condition>
  </Step>
  ....

 

 

You need to attach that into a Response flow, probably on the ProxyEndpoint. It needs to be on the response flow, because it's only in the response flow that the API proxy would know the response status code from the upstream system.

But that didn't work for me, as much as the response quota only incremented the counter when it met my condition, the request quota continued to increase regardless of the status code.

I understand it didn't work the way you expected, though I am not clear on what specifically you are seeing. How did you attach the Quota? Can you show that? Can you show the Condition?

In Apigee X & hybrid, there's a cool new feature in the Quota policy that accrues counts on a Quota according to something from the response, and enforces the quota only on the inbound request.  I recommend that you use it for your purposes.  If you choose to do that,  you would configure a Quota policy that has the CountOnly element attached to the response flow, and then configure a separate, second Quota policy with the EnforceOnly element, attached to the request flow.

The effect is, for each new inbound request, the Apigee  proxy would CHECK the count on the Quota and possibly  enforce a limit, but it would not increment the count at that time.  It would increment the count only on the response flow, and only in the condition you specify. But it would not ENFORCE the rate limit at that time.  If the quota count exceeded the configured limit, then the rate limit would get enforced on the next inbound request.   

 

  • How did you attach the Quota? Can you show that? Can you show the Condition?

I did the configuration based on the documentation I attached above.

ProxyEndpoint:

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
    <PreFlow name="PreFlow">
        <Request>
            <Step>
                <Name>Quota-ConsultationPerDay</Name>
            </Step>
        </Request>
        <Response>
            <Step>
                <Name>Quota-CountOnly</Name>
                <Condition>response.status.code = 200</Condition>
            </Step>
        </Response>
    </PreFlow>
    <Flows/>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
    <HTTPProxyConnection>
        <BasePath>/operating-limit</BasePath>
        <VirtualHost>secure</VirtualHost>
    </HTTPProxyConnection>
    <RouteRule name="default">
        <TargetEndpoint>default</TargetEndpoint>
    </RouteRule>
</ProxyEndpoint>

 

 

Request quota:

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Quota async="false" continueOnError="false" enabled="true" name="Quota-ConsultationPerDay">
    <DisplayName>Quota-ConsultationPerDay</DisplayName>
    <Properties/>
    <Allow count="5"/>
    <Identifier ref="request.header.ClientId"/>
    <Interval>1</Interval>
    <TimeUnit>day</TimeUnit>
    <Distributed>true</Distributed>
    <Synchronous>true</Synchronous>
    <EnforceOnly>true</EnforceOnly>
    <SharedName>consultationPerDay</SharedName>
</Quota>

 

 

  Response quota: 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Quota async="false" continueOnError="false" enabled="true" name="Quota-CountOnly">
    <DisplayName>Quota-CountOnly</DisplayName>
    <Properties/>
    <Allow count="5"/>
    <Identifier ref="request.header.ClientId"/>
    <Interval>1</Interval>
    <TimeUnit>day</TimeUnit>
    <Distributed>true</Distributed>
    <Synchronous>true</Synchronous>
    <CountOnly>true</CountOnly>
    <SharedName>consultationPerDay</SharedName>
</Quota>

 

 

But this solution doesn't suit me, because in the request the quota keeps incrementing in the counter idependende from the status code of the response.

I need to limit clients querying our APIs to 'N' times a day, but I can't use requests other than 2XX in the summation.

I assumed it would be possible to do this using the quota policy, but I haven't found anything (other than what I've shown above) to meet this demand.

I need to limit clients querying our APIs to 'N' times a day, but I can't use requests other than 2XX in the summation.

I assumed it would be possible to do this using the quota policy, but I haven't found anything (other than what I've shown above) to meet this demand.

It is possible. The EnforceOnly / CountOnly feature, coupled with a Condition, does that for you. I just tried this and it's working for me.

Attached please find a working loopback proxy that behaves this way. If you invoke it as

GET /quota-200-only/200

...then the quota count will increase.

If you invoke it with

GET /quota-200-only/302

...then the quota count will not increase.

Hi, thanks for the idea of adding a condition before the quota policy is executed. I believe that option should work for Apigee Edge as <EnforceOnly> and <CountOnly> seem to be available for Apigee-X only.

Having said that, by only increasing the quota counter when we get 200 responses, I see we could introduce the risk of clients infinitely retrying whenever they get an error response. Do you have a recommendation to mitigate that?