Custom API metering

Not applicable

We would like to implement custom API metering / quota by counting the number of seconds for processed audio/video. Note that audio and video are not measured by bytes. The actual time depends on the audio/video formats.

e.g. When a user submits an API request for a audio/video service, the backend would add a custom header called "x-duration" as follows:

{
"date": "Mon, 11 Jul 2016 18:20:09 GMT",
"x-duration": "00:00:07.39",
"content-type": "application/json",
"vary": "Accept-Encoding",
"content-length": "129”
}

We wish the API gateway to count the number of seconds (in the above example, it is 7.39 seconds) grouped by API key. This would allow us to (1) see analytics report for total audio/video processed for each API key and (2) support multi-tiered billing. e.g. FREE and PREMIUM tiers.

I suppose the variable is "response.header.x-duration.values".

Appreciate any insight and pointer.

thanks, Eric

Solved Solved
1 7 1,168
1 ACCEPTED SOLUTION

@eric.law ,

I believe you can implement Quota based on response header value & deduct Quota from Assigned Quota based on the x-duration value. You need to use MessageWeight & that can be referenced using flow variable.

MessageWeight element :

Use to specify the weight assigned to each message. Use message weight to increase impact of request messages that, for example, consume more computational resources than others.

For example, you want to count POST messages as being twice as "heavy" or expensive, as GET messages. Therefore, you set the MessageWeight to 2 for a POST and 1 for a GET. You can even set the MessageWeight to 0 so the request does not affect the counter. In this example, if the quota is 10 messages per minute and the MessageWeight for POST requests is 2, then the quota will permits 5 POST requests in any 10 minute interval. Any additional request, POST or GET, before the counter resets are rejected.

A value representing MessageWeight must be specified by a flow variable, and can be extracted from HTTP headers, query parameters, an XML or JSON request payload, or any other flow variable. For example, you set it in a header named weight:

<MessageWeight ref="request.header.weight"/>

See sample proxy that demonstrates same attached below, Keep us posted if you have any queries.

quotaweightdemo-rev1-2016-07-12.zip

View solution in original post

7 REPLIES 7

Eric, you will need to populate a custom variable using a statistics collector policy. Documentation is here

http://docs.apigee.com/api-services/reference/statistics-collector-policy

@spadmanabhan thanks for the hints.

As shown in http://docs.apigee.com/analytics-services/content/analyze-api-message-content-using-custom-analytics

I can create "Custom Analytics Collection" from API proxy -> develop in the management console.

We can change the variable string from "00:00:07.39" to round up to 8 seconds as the input must be integer. This is not a problem as we control the backend. I think this would allow us to show custom analytics report.

However, this does not address the need to do a custom "quota" or API metering. Please provide additional hints.

Can you provide some more details on custom quota/metering? How quickly should the quota be enforced? What is an example quota or metering?

Ideally if the current Quota Policy can be extended with custom variables. e.g. specify "Interval" and "TimeUnit" with "ref" pointing to the custom variable.

For our use case, Interval=1 and TimeUnit=hour.

@eric.law ,

I believe you can implement Quota based on response header value & deduct Quota from Assigned Quota based on the x-duration value. You need to use MessageWeight & that can be referenced using flow variable.

MessageWeight element :

Use to specify the weight assigned to each message. Use message weight to increase impact of request messages that, for example, consume more computational resources than others.

For example, you want to count POST messages as being twice as "heavy" or expensive, as GET messages. Therefore, you set the MessageWeight to 2 for a POST and 1 for a GET. You can even set the MessageWeight to 0 so the request does not affect the counter. In this example, if the quota is 10 messages per minute and the MessageWeight for POST requests is 2, then the quota will permits 5 POST requests in any 10 minute interval. Any additional request, POST or GET, before the counter resets are rejected.

A value representing MessageWeight must be specified by a flow variable, and can be extracted from HTTP headers, query parameters, an XML or JSON request payload, or any other flow variable. For example, you set it in a header named weight:

<MessageWeight ref="request.header.weight"/>

See sample proxy that demonstrates same attached below, Keep us posted if you have any queries.

quotaweightdemo-rev1-2016-07-12.zip

thank you so much for the worked example

Anytime 🙂 Glad your query is resolved. Keep us posted if any moving forward.