I wrote a step by step workshop to demo cloudwatch log insights, which is the new launch in re:Invent 2018. This workshop contains telemetric simulator + apigw + lambda + cloudwatch log inisghts and x-ray. It will be helpful to quickly understand the power of cloudwatch insights. The benefit to customers is you don't need to setup Elastic Search + Kibana to create simple dashboard.
This lambda function will log the beacon body into the lambda cloudwatch log. The souce code is here.
- function name: apigw-log-body
- runtime: python 3.7
- paste the source code into editor. It's a very simple code. 7 lines!!
Let us create a api gateway endpoint to collect client beacon information and pass through lamdba. API name is beacon-receiver
.
- Set the method
post
and proxy mode to lambdaapigw-log-body
- deploy the api gateway to stage
dev
- After deploy to
dev
. We need to setup the monitor log. Click on stagedev
then click on the tablogs/Tracing
- Enable CloudWatch Logs and set level to INFO. You can change to ERROR anytime.
- Enable Access Logging. Create a log group
/aws/apigw/beacon
in cloud watch log. The arn format isarn:aws:logs:us-east-1:123456789012:log-group:my-log-group
- Logformat use
json
- Enable X-Ray Tracing
Use the mock client to send out the beacon. The beacon information is like this kind of format:
{
"custom_metric_float_value": "9.92114586405",
"clientid": "user369",
"page": "page_20.html",
"custom_metric_name": "compute_render_time",
"Referer": "referer_18",
"event": "exception"
}
The mock client python code is here. Please use the following command to send out random beacon metrics to your api endpoint.
Parameters:
- api gateway endpoint
- 1000 mean number of sending
- 0.5 means delay (second)
python post-beacon-body.py https://<your-endpoint>.execute-api.us-east-1.amazonaws.com/dev/poc 1000 0.5
group: /aws/apigw/beacon
fields @timestamp, @message
| sort @timestamp desc
| limit 20
group: /aws/lambda/apigw-log-body
fields event, page, clientid, Referer, custom_metric_name | filter @message like /event/
| limit 20
Click on the add to dashboard
group: /aws/lambda/apigw-log-body
fields event, page, clientid, Referer, custom_metric_name | filter @message like /event/
| filter event = 'login'
create dashboard
Then you will see it in the dashboard
fields event | filter @message like /event/
| stats count(event) by bin(30s)
Click on Visulazation
Click on "View traces". It goes to detail traces to see the slow query
select log groups /aws/lambda/apigw-log-body
then create metric filter.
In the filter pattern, input {$.event = "exception"}
create filter
create alarm