How do I get the usage metric from AWS API Gateway by API key, e.g. usage counter, usage logs, etc.?
I am currently sending the x-api-key
to my integration endpoint for manual logging. I would prefer not to do this and let AWS API Gateway measure and report this metric for me.
So far, there are no metrics in Cloudwatch for key usage. But the gateway itself keeps some usage statistics, although not very detailed.
Usage plan overview:
Invocation statistic of one API Key:
I found that all the logging was insufficient for what I needed - especially as it didn't log per API Key as yet (holding out that this will still one).
So I created my own custom logs -
This way, I can search my CloudWatch logs and get back the exact data that I'm wanting, even per API Key if required;
Under my stages, I enabled the "Custom Access Logging" and used the following format:
NOTE: These custom logs, currently only support context
variables.
input
variables: Documentation can be found here:
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference
With this custom logging in place, I can open CloudWatch
, filter per date and use a search string to search for anything I want (that fits my custom logging);
BcxvY1
on Endpoint /fees
[RequestId,APIKeyText,APIKeyValue="*BcxvY1*",HTTPText,MethodText,HTTPMethodType="*GET*",PathText,PathValue="*/fees,",StatusText,StatusCode,ErrorsText,ErrorsValue,DescriptionText,DescriptionValue=custom_log]
The great thing with this, is that is completely customisable. I can change my search query however I want, depending on the results I want. Making it more / less complex as needed.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With