Read logs and metrics
Write log events and metrics, read them back and trigger an alarm with CloudWatch and Cloud Logging against pogi.
pogi keeps the logs and metrics your code writes, so you can read them back and test alarms. You'll write log events, read them with a filter, raise a CloudWatch alarm, and do the same for logs on Google Cloud.
Before you start
-
Start pogi in another terminal:
pogi start -
Load the settings that point your tools at pogi:
eval "$(pogi env)"
You need the AWS CLI for the CloudWatch sections and gcloud for the Cloud Logging section. No Docker needed.
AWS: CloudWatch Logs
You'll make a log group (a named collection of logs) with one stream, write two events, and read them back.
-
Create the group and the stream. Neither command prints anything:
aws logs create-log-group --log-group-name /demo/app aws logs create-log-stream --log-group-name /demo/app --log-stream-name web-1 -
Write two events. A timestamp is in milliseconds since 1970:
NOW=$(($(date +%s) * 1000)) aws logs put-log-events --log-group-name /demo/app --log-stream-name web-1 \ --log-events timestamp=$NOW,message="order 1 created" timestamp=$((NOW + 1)),message="ERROR payment failed"Output { "nextSequenceToken": "1" } -
Read the events back:
aws logs get-log-events --log-group-name /demo/app --log-stream-name web-1 --query 'events[].message'Output [ "order 1 created", "ERROR payment failed" ] -
Search the whole group for events that match a pattern:
aws logs filter-log-events --log-group-name /demo/app --filter-pattern ERROR --query 'events[].message'Output [ "ERROR payment failed" ]
AWS: CloudWatch metrics and alarms
You'll make an alarm that fires when Orders adds up to more than 2 in a minute, then write a metric value that crosses it.
-
Create the alarm. It prints nothing:
aws cloudwatch put-metric-alarm --alarm-name too-many-orders --namespace Demo --metric-name Orders \ --statistic Sum --period 60 --evaluation-periods 1 --threshold 2 --comparison-operator GreaterThanThreshold -
Write a value of 5. It prints nothing:
aws cloudwatch put-metric-data --namespace Demo --metric-name Orders --value 5 --unit Count -
Wait until the alarm fires, then show it. The alarm can take up to a minute to fire:
for i in $(seq 1 30); do STATE=$(aws cloudwatch describe-alarms --alarm-names too-many-orders --query 'MetricAlarms[0].StateValue' --output text) [ "$STATE" = ALARM ] && break sleep 5 done aws cloudwatch describe-alarms --alarm-names too-many-orders --query 'MetricAlarms[].[AlarmName,StateValue,StateReason]' --output textOutput too-many-orders ALARM Threshold Crossed: 1 out of the last 1 datapoints [5] breached the threshold (GreaterThanThreshold 2). -
List the metric you wrote:
aws cloudwatch list-metrics --namespace Demo --query 'Metrics[].MetricName' --output textOutput Orders
An alarm can also notify an SNS topic or run a Lambda function when it fires. Set that with --alarm-actions on put-metric-alarm.
Google Cloud: Cloud Logging
You'll write two log entries and read back the ones at ERROR or above.
-
Write an entry at each severity:
gcloud logging write demo-log "order 1 created" --severity=INFO gcloud logging write demo-log "payment failed" --severity=ERROROutput Created log entry. Created log entry. -
Read the errors. The filter uses the Cloud Logging query language:
gcloud logging read 'logName="projects/pogi/logs/demo-log" AND severity>=ERROR' --limit=5 --format='value(severity,textPayload)'Output ERROR payment failed -
List the logs in the project:
gcloud logging logs listOutput NAME projects/pogi/logs/demo-log
Limits
- CloudWatch Logs: Insights does not run
statsorparseyet. Metric filters read JSON fields only, not space-delimited fields such as$name. - CloudWatch metrics: composite alarms are not evaluated yet.
- Cloud Logging: log views, links, analytics and log scopes are not supported yet.
- Azure Monitor is left out.
See the CloudWatch Logs and Metrics and Cloud Logging rows in the coverage page.