Page cover image

2.2.0 CloudWatch Logs

0. Learning Materials

Task List

Env Variables

Micro service
Variables

backend

  • HONEYCOMB_API_KEY

  • OTEL_SERVICE_NAME

  • OTEL_EXPORTER_OTLP_ENDPOINT

  • OTEL_EXPORTER_OTLP_HEADERS

  • AWS_XRAY_URL

  • AWS_XRAY_DAEMON_ADDRESS

  • AWS_DEFAULT_REGION

  • AWS_ACCESS_KEY_ID

  • AWS_SECRET_ACCESS_KEY

x-ray daemon

  • AWS_ACCESS_KEY_ID

  • AWS_SECRET_ACCESS_KEY

  • AWS_REGION


1. Workflow

1. Implement a CloudWatch agent in backend

1.1. Plant a logger in the flask server app.

The python library watchtower is a log handler for Amazon Web Services CloudWatch Logs. Run pip install to install it in local.

requirements.txt
+ watchtower
  • pip install -r requirements.txt

Update app.py by adding the following snippets across the file. Refer to the actual code here.

app.py
+ import watchtower
+ import logging
+ from time import strftime
app.py
# CONFIGURE LOGGER TO USE CLOUDWATCH
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
cw_handler = watchtower.CloudWatchLogHandler(log_group='cruddur') # set a log group within AWS CloudWatch. The log group will be called "cruddur"
LOGGER.addHandler(console_handler)
LOGGER.addHandler(cw_handler)
LOGGER.info("test log")

## CLOUDWATH -----
@app.after_request
def after_request(response):
  timestamp = strftime('[%Y-%b-%d %H:%M]')
  LOGGER.error("%s %s %s %s %s %s", timestamp, request.remote_addr, request.method, request.scheme, request.full_path, response.status)
  return response
app.py
@app.route("/api/activities/home", methods=['GET'])
+ def data_home(logger=LOGGER):
    data = HomeActivities.run()
    return data, 200

1.2. Set logger in the service (home_activities)

home_activities.py
from datetime import datetime, timedelta, timezone
from opentelemetry import trace

...

class HomeActivities:
  def run():
+    logger.info("HomeActivities")

...

2. Update the backend docker container

The following env variables will be used by watchtower.

docker-compose.yml
version: "3.8"
services:
  backend-flask:
    environment:
      ...
      AWS_XRAY_DAEMON_ADDRESS: "xray-daemon:2000"
+     AWS_DEFAULT_REGION: "${AWS_DEFAULT_REGION}"
+     AWS_ACCESS_KEY_ID: "${AWS_ACCESS_KEY_ID}"
+     AWS_SECRET_ACCESS_KEY: "${AWS_SECRET_ACCESS_KEY}"
    build: ./backend-flask
    ...

Now our app is ready to test the CloudWatch agent in action.

  • Run docker-compose

    • then hit some backend endpoints by clicking around in the frontend

  • Check your AWS CloudWatch console.

Log streams are coming through.

Last updated