Skip to content

Commit be2bfd3

Browse files
author
Irving Popovetsky
authored
Enable Sentry tracing and profiling and fix some serious performance issues! (#403)
* Enable Sentry tracing and logging * disable Honeycomb beeline and configure sampling for nonessential Sentry traces * speed bost * another speed boost to auth endpoint, and cleanup * update documentation
1 parent 7c92cfc commit be2bfd3

24 files changed

Lines changed: 1503 additions & 490 deletions

.dev/dev-jwt-key

Lines changed: 0 additions & 51 deletions
This file was deleted.

.dev/dev-jwt-key.pub

Lines changed: 0 additions & 14 deletions
This file was deleted.

.dockerignore

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,23 @@ dist
1111
.coverage
1212
html/*
1313
**/__pycache__
14-
**/*.pyc
14+
**/*.pyc
15+
16+
# Development files - should not be in production
17+
.dev/
18+
src/.dev/
19+
src/.dev
20+
**/.dev/
21+
**/.dev
22+
*.sqlite3
23+
*.db
24+
db.sqlite3
25+
src/db.sqlite3
26+
**/db.sqlite3
27+
28+
# Test artifacts
29+
.pytest_cache/
30+
src/.pytest_cache/
31+
**/.pytest_cache/
32+
.coverage
33+
htmlcov/

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*.env
33
!example.env
44
local.py
5+
.dev/
56
env/
67
venv/
78
ENV/
@@ -81,4 +82,8 @@ pip-delete-this-directory.txt
8182

8283
## Database backups and tools with credentials
8384
backups/
84-
scripts/db-tools.sh
85+
scripts/db-tools.sh
86+
87+
# Claude
88+
settings.local.json
89+
.mcp.json

Dockerfile

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ ENV VIRTUAL_ENV=/venv \
2929
PATH="/venv/bin:$PATH"
3030

3131
# Install production dependencies only
32-
RUN poetry install --only=main --no-interaction --no-cache --compile
32+
RUN poetry install --only=main --no-interaction --no-cache
3333

3434
# =============================================================================
3535
# Test builder: add dev dependencies
3636
# =============================================================================
3737
FROM builder AS test-builder
3838

39-
RUN poetry install --no-interaction --no-cache --compile
39+
RUN poetry install --no-interaction --no-cache
4040

4141
# =============================================================================
4242
# Runtime base: minimal image shared by test and production
@@ -50,7 +50,6 @@ RUN apk upgrade --no-cache && \
5050
pip install --no-cache-dir --upgrade pip
5151

5252
ENV PYTHONUNBUFFERED=1 \
53-
PYTHONDONTWRITEBYTECODE=1 \
5453
PATH="/venv/bin:$PATH"
5554

5655
WORKDIR /app
@@ -79,7 +78,6 @@ FROM runtime-base AS production
7978

8079
COPY --from=builder /venv /venv
8180
COPY src ./src
82-
COPY .dev ./src/.dev
8381

8482
# Pre-compile Python bytecode for faster cold starts
8583
RUN python -m compileall -q ./src/

OPS.md

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
11
# Deploying a new backend version
22

3-
Once a release is built and deployed by CircleCI, deploy it to an environment using ArgoCD.
3+
The backend is deployed to AWS ECS (Elastic Container Service) with separate staging and production environments.
44

5-
1. First, to connect to ArgoCD:
6-
```
7-
kubectl -n argocd port-forward service/argocd-server 8443:443 &
8-
open https://localhost:8443
5+
## Building and Pushing Docker Images
6+
7+
Use the `docker-build.sh` script to build multi-architecture images and push to AWS ECR:
8+
9+
```bash
10+
# Build and push staging images
11+
./docker-build.sh staging
12+
13+
# Build and push production images
14+
./docker-build.sh prod
915
```
10-
2. login - credentials are in 1password, or ask someone for help
11-
3. pick up the new version in staging.
12-
- go to https://localhost:8443/applications/pyback-staging,
13-
- click the hamburger menu (3 dots, blue button), -> Details -> Parameters
14-
- update the images field with the build ID as the tag, like: `operationcode/back-end:staging-846`
15-
- as the new pods deploy, tail their logs to check for errors
16-
- validate the staging environment (notes below)
17-
4. repeat those steps for the production environment
16+
17+
This creates:
18+
- `back-end:staging-amd64` and `back-end:staging-arm64` images
19+
- A multi-arch manifest at `back-end:staging`
20+
21+
## Deploying to ECS
22+
23+
After images are pushed to ECR, deploy by updating the ECS service:
24+
25+
1. **Update task definition** with new image tag
26+
2. **Deploy to staging first** - Update ECS service to use new task definition
27+
3. **Monitor logs** in CloudWatch or Sentry
28+
4. **Validate staging** (see below)
29+
5. **Deploy to production** - Repeat for production ECS service
30+
31+
## Important: JWT Secret Key Migration
32+
33+
**Before deploying these performance changes**, you must update the production `JWT_SECRET_KEY` environment variable:
34+
35+
1. Generate a new secret: `openssl rand -base64 64 | tr -d '\n'`
36+
2. Set `JWT_SECRET_KEY` env var in ECS task definition to the generated string
37+
3. Remove `JWT_PUBLIC_KEY` env var (no longer needed with HS256)
38+
39+
⚠️ **This will log out all users** (one-time migration from RS256 to HS256)
1840

1941
# Validating the staging environment
2042

@@ -25,33 +47,30 @@ When you run the front-end repo in localdev mode, it automatically connects to t
2547
2. run the dev server: `docker run -it -v ${PWD}:/src -w /src -p 127.0.0.1:3000:3000/tcp node:lts yarn dev --hostname 0.0.0.0`
2648
3. Connect to the dev server: `open http://localhost:3000`
2749

28-
# Certificate management with certbot
50+
# Monitoring
2951

30-
Certbot runs continously as a kube operator and refreshes certs for you. To ensure it is working,
31-
check the logs of the `cert-manager` pod, like:
32-
```
33-
kubectl -n cert-manager logs -f cert-manager-dcc48bf99-skhn7
34-
```
52+
## Sentry Performance Monitoring
3553

36-
Current version running is v0.10.1
54+
The application is instrumented with Sentry for error tracking and performance monitoring:
55+
- Error tracking with breadcrumbs and context
56+
- Transaction tracing for HTTP requests
57+
- Database query performance tracking
58+
- Python profiling for CPU-intensive operations
3759

38-
if you need for some reason to upgrade:
39-
1. read the release notes for all versions between current and desired, watch for breaking changes
40-
2. ignore the instructions about helm and kubectly apply, one minor version at a time
41-
```
42-
kubectl apply \
43-
--validate=false \
44-
-f https://github.com/jetstack/cert-manager/releases/download/v0.10.1/cert-manager.yaml
45-
```
60+
Configure via environment variables (see `example.env`):
61+
- `SENTRY_DSN` - Sentry project DSN
62+
- `SENTRY_TRACES_SAMPLE_RATE` - Percentage of requests to trace (0.0-1.0)
63+
- `SENTRY_PROFILES_SAMPLE_RATE` - Percentage of transactions to profile (0.0-1.0)
4664

47-
certificates installed:
48-
```
49-
$ kubectl get Certificates --all-namespaces
50-
NAMESPACE NAME READY SECRET AGE
51-
monitoring grafana-tls True grafana-tls 299d
52-
operationcode-staging back-end-tls True back-end-tls 264d
53-
operationcode-staging resources-api-tls True resources-api-tls 299d
54-
operationcode back-end-tls True back-end-tls 264d
55-
operationcode resources-api-tls True resources-api-tls 299d
65+
## CloudWatch Logs
66+
67+
Application logs are sent to CloudWatch Logs. Access via AWS Console or CLI:
68+
69+
```bash
70+
# View recent logs for staging
71+
aws logs tail /ecs/back-end-staging --follow
72+
73+
# View recent logs for production
74+
aws logs tail /ecs/back-end-production --follow
5675
```
5776

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ For information about the maintainers of the project, check out [MAINTAINERS.md]
2525

2626
## Quick Start
2727
Recommended versions of tools used within the repo:
28-
- `python@3.7` or greater (in some environments, you may need to specify version of python i.e. `python test.py` vs `python3 test.py`))
28+
- `python@3.12` or greater
2929
- `git@2.17.1` or greater
30-
- `poetry@0.12.11` or greater
31-
- [Poetry](https://poetry.eustace.io/) is a packaging and dependency manager, similar to pip or pipenv
32-
- Poetry provides a custom installer that can be ran via `curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python`
33-
- Alternatively, poetry can be installed via pip/pip3 with `pip install --user poetry` or `pip3 install --user poetry`
34-
- See https://poetry.eustace.io/docs/
30+
- `poetry@2.3.0` or greater
31+
- [Poetry](https://python-poetry.org/) is a packaging and dependency manager
32+
- Install via: `curl -sSL https://install.python-poetry.org | python3 -`
33+
- Or via pip: `pip install --user poetry`
34+
- See https://python-poetry.org/docs/
3535

3636

3737
```bash
@@ -57,7 +57,7 @@ poetry run pytest
5757
poetry run black .
5858
# the next line shouldn't output anything to the terminal if it passes
5959
poetry run flake8
60-
poetry run isort -rc .
60+
poetry run isort .
6161
```
6262

6363
## Running [Bandit](https://github.com/PyCQA/bandit)

0 commit comments

Comments
 (0)