Skip to content

Commit

Permalink
Merge branch 'main' into week-3
Browse files Browse the repository at this point in the history
  • Loading branch information
beiciliang authored Mar 8, 2023
2 parents 3f30af1 + e2e0f07 commit 07d1604
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 39 deletions.
33 changes: 33 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "Cruddur Configuration",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
// Features to add to the dev container. More info: https://containers.dev/features.
"features": {
"ghcr.io/devcontainers/features/aws-cli:1": {}
},
"remoteEnv": {
"AWS_CLI_AUTO_PROMPT": "on-partial"
},
"customizations": {
"vscode": {
"extensions": [
"ms-azuretools.vscode-docker",
"ms-python.python",
"vscodevim.vim"
],
"settings": {
"terminal.integrated.fontSize": 14,
"editor.fontSize": 14,
"workbench.colorTheme": "Default Dark+ Experimental"
}
}
}
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as an existing user other than the container default. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "devcontainer"
}
12 changes: 7 additions & 5 deletions backend-flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@
# LOGGER.addHandler(cw_handler)
# LOGGER.info("test log")

# # x-ray initialization
# # Comment for saving AWS budget
# xray_url = os.getenv("AWS_XRAY_URL")
# xray_recorder.configure(service='backend-flask', dynamic_naming=xray_url)
# x-ray initialization
# Comment for saving AWS budget
xray_url = os.getenv("AWS_XRAY_URL")
xray_recorder.configure(service='backend-flask', dynamic_naming=xray_url)

# honeycomb initialization
# Tracing and an exporter that can send data to Honeycomb
Expand All @@ -75,7 +75,7 @@
)

# x-ray
# XRayMiddleware(app, xray_recorder)
XRayMiddleware(app, xray_recorder)

# honeycomb
# Initialize automatic instrumentation with Flask
Expand Down Expand Up @@ -167,6 +167,7 @@ def data_create_message():


@app.route("/api/activities/home", methods=['GET'])
@xray_recorder.capture('activities_home')
def data_home():
# data = HomeActivities.run()
# data = HomeActivities.run(logger=LOGGER)
Expand All @@ -192,6 +193,7 @@ def data_notifications():


@app.route("/api/activities/@<string:handle>", methods=['GET'])
@xray_recorder.capture('activities_users')
def data_handle(handle):
model = UserActivities.run(handle)
if model['errors'] is not None:
Expand Down
62 changes: 31 additions & 31 deletions backend-flask/services/user_activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@


class UserActivities:
def run(user_handle):
# # xray
# # Comment for saving AWS budget
# segment = xray_recorder.begin_segment('user_activities')
def run(user_handle):
try:
model = {
'errors': None,
'data': None
}

model = {
'errors': None,
'data': None
}
now = datetime.now(timezone.utc).astimezone()

now = datetime.now(timezone.utc).astimezone()
if user_handle == None or len(user_handle) < 1:
model['errors'] = ['blank_user_handle']
else:
now = datetime.now()
results = [{
'uuid': '248959df-3079-4947-b847-9e0892d1bab4',
'handle': 'Andrew Brown',
'message': 'Cloud is fun!',
'created_at': (now - timedelta(days=1)).isoformat(),
'expires_at': (now + timedelta(days=31)).isoformat()
}]
model['data'] = results

if user_handle == None or len(user_handle) < 1:
model['errors'] = ['blank_user_handle']
else:
now = datetime.now()
results = [{
'uuid': '248959df-3079-4947-b847-9e0892d1bab4',
'handle': 'Andrew Brown',
'message': 'Cloud is fun!',
'created_at': (now - timedelta(days=1)).isoformat(),
'expires_at': (now + timedelta(days=31)).isoformat()
}]
model['data'] = results

# # xray subsegment
# subsegment = xray_recorder.begin_subsegment('mock-data')
# dict = {
# "now": now.isoformat(),
# "results-size": len(model['data'])
# }
# subsegment.put_metadata('key', dict, 'namespace')

return model
# xray subsegment
subsegment = xray_recorder.begin_subsegment('mock-data')
dict = {
"now": now.isoformat(),
"results-size": len(model['data'])
}
subsegment.put_metadata('key', dict, 'namespace')
xray_recorder.end_subsegment()
finally:
xray_recorder.end_subsegment()

return model
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ version: "3.8"
services:
backend-flask:
environment:
# FRONTEND_URL: "https://${CODESPACE_NAME}-3000.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}"
# BACKEND_URL: "https://${CODESPACE_NAME}-4567.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}"
FRONTEND_URL: "https://3000-${GITPOD_WORKSPACE_ID}.${GITPOD_WORKSPACE_CLUSTER_HOST}"
BACKEND_URL: "https://4567-${GITPOD_WORKSPACE_ID}.${GITPOD_WORKSPACE_CLUSTER_HOST}"
OTEL_EXPORTER_OTLP_ENDPOINT: "https://api.honeycomb.io"
Expand All @@ -22,6 +24,7 @@ services:
- ./backend-flask:/backend-flask
frontend-react-js:
environment:
# REACT_APP_BACKEND_URL: "https://${CODESPACE_NAME}-4567.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}"
REACT_APP_BACKEND_URL: "https://4567-${GITPOD_WORKSPACE_ID}.${GITPOD_WORKSPACE_CLUSTER_HOST}"
REACT_APP_AWS_PROJECT_REGION: "${AWS_DEFAULT_REGION}"
REACT_APP_AWS_COGNITO_REGION: "${AWS_DEFAULT_REGION}"
Expand Down
Binary file added journal/assets/week02-xray-trace.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 8 additions & 3 deletions journal/week2.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ To instrument AWS X-Ray into backend flask application, make the following chang
- Instrument AWS X-Ray in `/backend-flask/app.py`
- Add `xray-daemon` and configure environment variables in `docker-compose.yml`

Then we can add segments and subsegments in `backend-flask/services/user_activities.py` for example as shown in [this commit](https://github.com/beiciliang/aws-bootcamp-cruddur-2023/commit/a5a131f954cff5516897fbf9269d459e0a2b383d). These data can be sent data back to the X-Ray API.
Then we can add segments and subsegments in `backend-flask/services/user_activities.py` for example as shown in [this commit](https://github.com/beiciliang/aws-bootcamp-cruddur-2023/commit/fabb76a787b233153a6e50a33af502e43e2727c0). These data can be sent data back to the X-Ray API.

To observe X-Ray traces within the AWS Console, setup AWS X-Ray resources in `aws/json/xray.json` as seen in [this commit](https://github.com/beiciliang/aws-bootcamp-cruddur-2023/commit/0fbb8e89bb3fc810e58f1d3e5fe7699a12da6af2), then run the following command lines to create tracing groups and sampling rules:
After docker compose up, we can browse the home and a user page where we've add xray recorder to capture. Then we can observe X-Ray traces within the AWS Console as shown in the screenshot below.

![Proof of xray trace](assets/week02-xray-trace.png)

We can setup AWS X-Ray resources in `aws/json/xray.json` as seen in [this commit](https://github.com/beiciliang/aws-bootcamp-cruddur-2023/commit/0fbb8e89bb3fc810e58f1d3e5fe7699a12da6af2), then run the following command lines to create tracing groups and sampling rules:

```sh
aws xray create-group --group-name "Cruddur" --filter-expression "service(\"backend-flask\")"
Expand All @@ -66,7 +70,7 @@ After `docker compose up` to launch the app and browse the backend endpoint of `

![Proof of cloudwatch log](assets/week02-cloudwatch-log.png)

Then we comment codes related to AWS X-Ray and CloudWatch Logger in order to save our AWS budgets.
Then we comment codes related to CloudWatch Logger in order to save our AWS budgets.

## Rollbar for Error Logging

Expand All @@ -91,4 +95,5 @@ We can further trigger an error and then observe this error with Rollbar.
## References

- AWS Developer Guide for AWS X-Ray daemon ([link](https://docs.aws.amazon.com/xray/latest/devguide/xray-daemon.html))
- How to add custom X-Ray Segments for Containerised Flask Application ([link](https://olley.hashnode.dev/aws-free-cloud-bootcamp-instrumenting-aws-x-ray-subsegments))
- Rollbar Flask Example ([link](https://github.com/rollbar/rollbar-flask-example/blob/master/hello.py))

0 comments on commit 07d1604

Please # to comment.