Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Got /list working to generate errors similar to our Angular app #75

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SENTRY_ORG=testorg-az
SENTRY_PROJECT=javascript-react
SENTRY_PROJECT=reactadamtf
RELEASE=$(shell ./calver.sh)
PREFIX=static/js

Expand All @@ -11,12 +11,6 @@ GCP_DEPLOY=gcloud run deploy $(shell whoami)
all: build_react setup_release build deploy-flask deploy-react
admin: setup_release build deploy-flask-admin deploy-react-admin

docker_compose:
cd react && npm run buildlocal
docker-compose build
docker-compose run -d -e RELEASE=$(RELEASE) -e FLASK_ENV=test -p 3003:3003 flask
docker-compose run -p 3002:3002 react

build_react:
cd react && source $(HOME)/.nvm/nvm.sh && nvm use && npm install && npm run build

Expand Down
105 changes: 71 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# tracing
SDK Tracing between a React javascript app and back-end Flask app. For prod deployment this uses GCP's Cloud Build, Cloud Container Registry and Cloud Run. See troubleshooting for how to run individually and work with the cloudbuild.yaml.
SDK Tracing between a React javascript app and back-end Flask app. For prod deployment this uses GCP's Cloud Build, and Cloud Run.

## Setup
#### Versions
this was tested on:

| dependency | version
| ------------- |:-------------:|
| sentry_sdk | 0.16.1 |
| @sentry/browser | 5.15.0 |
| sentry_sdk | 0.19.1 |
| @sentry/apm | 5.20.1 |
| @sentry/react 5.20.1 |
| @sentry/react | ^6.2.1 |
| @sentry/tracing |^6.2.1 |
| node | v.14.2 |
| redux | 4.0.5 |
| react-redux | 7.2.1 |
Expand All @@ -22,31 +22,78 @@ this was tested on:
| core | 2020.01.17 |
| gsutil 4.47 | gsutil 4.47 |
| docker | 19.03.12 |
#### Setup Instructions
1. Have an admin set you as Owner on the Project in GCP

Do the gcloud setup and project env setups here:

#### gcloud setup
1. Have an admin set your permissions in GCP
2. Download `gcloud` google cloud sdk https://cloud.google.com/sdk/docs/. This will have you 'initialize' your sdk from command line, and set some defaults. If you get asked for 'zone' select us-central1-a. 'region' is us-central1
3. `gcloud auth login` opens browser with Google OAUTH, select your Sentry email
4. `gcloud config set project <project ID>` get Google Cloud Project ID from console.cloud.google.com.
3. `gcloud auth login` opens browser with Google OAUTH, select your Sentry email.
4. `gcloud config set project <project ID>` get your team's Google Cloud Project ID from console.cloud.google.com.
5. `gcloud config set run/region us-central1` to set 'us-central1' as default region
6. Create a `react/.env` using `react/.env.default` as an example, and fill in the values. In the `REACT_APP_BACKEND_URL` put your `whoami` so your React container will call the right Flask container.
7. Create a `flask/.env` using `flask/.env.default` as an example, and fill in the values.
8. Whitelist your IP address in Cloud SQL for the database you see there.
9. **Dev - without docker** `cd ./flask && pip install -r requirements.txt`, recommended inside a virtualenv.
10. **Dev - without docker** `cd ./react` and `npm install`
6. `gcloud config list` and confirm your email, project and region are correct.
6. Permit your IP address in GCP Cloud SQL for the database instance.

#### project env setup
1. install [nvm](https://github.com/nvm-sh/nvm)
2. Create a `flask/.env` using `flask/.env.default` as an example, and fill in the values.
3. Create a `react/.env` using `react/.env.default` as an example, and fill in the values. In the `REACT_APP_BACKEND_URL` put your `whoami` so your React app instance will call the right Flask app instance.
4. set your SENTRY_PROJECT in both Makefile and react/Makefile
5. cd react && npm install
6. cd flask && pip install -r requirements.txt

## Run
#### Prod - GCP
1. `make all`
#### Development
Update your SENTRY_PROJECT in react/Makefile
1. `cd ./react && npm run deploylocal`
2. `cd ./flask && make deploy`

#### Dev - with docker
1. `make docker_compose`
docker-compose down
For Python 3
```
python3 -m venv env
source env/bin/activate
```
1. `cd flask && python3 main.py`

The dockerfile uses whatever is in `./react/build` so make sure you have an updated build.
#### Production Cloud Run
This command builds your react app, runs sentry-cli commands for releases, then uploads your source files to GCP where Cloud Build will build an Image and run it as a container in Cloud Run
```
make all
```

#### Dev - without docker
1. `cd ./react && npm run deploylocal`
2. `cd ./flask && make deploy`
#### Production App Engine
Update your react/.env with correct appspot (App Engine) URL's
```
cd flask && gcloud app deploy
cd react && npm run build && gcloud app deploy
```

## Upgrade Pathway

```
If you're on your fork
git remote -v
origin [email protected]:<your_handle>/tracing.git (fetch)
origin [email protected]:<your_handle>/tracing.git (push)
upstream [email protected]:sentry-demos/tracing.git (fetch)
upstream [email protected]:sentry-demos/tracing.git (push)

# If you don't have an upstream
git remote add upstream [email protected]:sentry-demos/tracing.git

# Make sure you're on master
git checkout master

# get updates from the upstream
git fetch upstream master
git merge upstream/master

# update sentry_sdk's and other modules
cd react && npm install
cd flask && pip install -r requirements.txt

# Check that your react/.env and flask/.env still have the right values
```

## Troubleshooting

Expand All @@ -70,7 +117,7 @@ THEN you need to change the URL (REACT_APP_BACKEND) in `.env` to reflect that
The container must listen for requests on 0.0.0.0 on the port defined by the GCP's $PORT environment variable. It is defaulted to 8080
https://cloud.google.com/run/docs/reference/container-contract#port

if you run `npm start` then the React app will bring you to a handled error page, instead of seeing User Feedback popup
If you run `npm start` then the React app will bring you to a handled error page, instead of seeing User Feedback popup

Warning: It is not recommended to use build-time variables for passing secrets like github keys, user credentials etc. Build-time variable values are visible to any user of the image with the docker history command.
https://docs.docker.com/engine/reference/builder/
Expand All @@ -81,14 +128,4 @@ see `clean.sh` for how to quickly remove all dead images and containers

`sentry-cli repos list`

## Sentry Documentation
docs
https://docs.sentry.io/performance/distributed-tracing/
https://forum.sentry.io/t/sentrys-apm-docs-alpha/7843

example
https://github.com/getsentry/sentry/blob/master/src/sentry/static/sentry/app/bootstrap.jsx

tracing implemented in sentry-demos/react and sentry-demos/flask
https://github.com/thinkocapo/react/tree/apm-alpha
https://github.com/thinkocapo/flask/tree/apm-alpha
If you get an error 'nvm is not compatible with the npm_config_prefix" environment variable: currently set to "/usr/local" then run `unset npm_config_prefix`
2 changes: 1 addition & 1 deletion cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ images:
- us.gcr.io/$PROJECT_ID/workspace_flask:$COMMIT_SHA
- us.gcr.io/$PROJECT_ID/workspace_flask:latest
- us.gcr.io/$PROJECT_ID/workspace_react:$COMMIT_SHA
- us.gcr.io/$PROJECT_ID/workspace_react:latest
- us.gcr.io/$PROJECT_ID/workspace_react:latest
3 changes: 2 additions & 1 deletion flask/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ README.md
*.pyo
*.pyd
__pycache__
.virtualenv
.virtualenv
env
2 changes: 1 addition & 1 deletion flask/.env.default
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ HOST=<HOST_URL_OR_ID>
DATABASE=<DATABASE>
USERNAME=<USERNAME>
PASSWORD=<PASSWORD>
FLASK_APP_DSN=<YOUR_DSN>
FLASK_APP_DSN=<YOUR_DSN>
42 changes: 42 additions & 0 deletions flask/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore

# Python pycache:
__pycache__/
# Ignored by the build system
/setup.cfg


env
env/

.virtualenv
**/.virtualenv

node_modules/
**/node_modules

.dockerignore
.env.default
app.pyc
db_og.py
db.pyc
Dockerfile
Makefile
script.py
utils.pyc
*.pyc
**/*.pyc
3 changes: 2 additions & 1 deletion flask/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
.virtualenv
.virtualenv
env
4 changes: 2 additions & 2 deletions flask/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM python:2.7-slim
FROM python:3.8-slim

COPY . ./app
WORKDIR /app

RUN pip install -r requirements.txt

CMD FLASK_APP=app.py flask run --host=0.0.0.0 -p $PORT
CMD FLASK_APP=main.py flask run --host=0.0.0.0 -p $PORT
2 changes: 1 addition & 1 deletion flask/Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
deploy:
FLASK_APP=app.py FLASK_ENV='test' flask run -p 3003
FLASK_APP=main.py FLASK_ENV='test' flask run -p 8080
3 changes: 3 additions & 0 deletions flask/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
service: tracing-flask
runtime: python38

5 changes: 2 additions & 3 deletions flask/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import operator
from dotenv import load_dotenv
load_dotenv()

HOST = os.getenv("HOST")
DATABASE = os.getenv("DATABASE")
USERNAME = os.getenv("USERNAME")
Expand All @@ -34,13 +35,11 @@ def randomString(stringLength=10):
return ''.join(random.choice(letters) for i in range(stringLength))

print("*****ENV *******", ENV)
if ENV == 'test':
if ENV == "test":
db = create_engine('postgresql://' + USERNAME + ':' + PASSWORD + '@' + HOST + ':5432/' + DATABASE)
else:
cloud_sql_connection_name = "sales-engineering-sf:us-central1:tracing-db-pg"
db = sqlalchemy.create_engine(
# Equivalent URL:
# postgres+pg8000://<db_user>:<db_pass>@/<db_name>?unix_sock=/cloudsql/<cloud_sql_instance_name>/.s.PGSQL.5432
sqlalchemy.engine.url.URL(
drivername='postgres+pg8000',
username=USERNAME,
Expand Down
19 changes: 12 additions & 7 deletions flask/app.py → flask/main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import os
from flask import Flask, request, json, abort, make_response, jsonify
from flask import Flask, request, json, make_response
from flask_cors import CORS
from dotenv import load_dotenv
from db import get_all_tools, get_inventory, update_inventory
from dotenv import load_dotenv
import datetime
from pytz import timezone
from utils import wait
import time
import numpy
import operator
import sys
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
load_dotenv()
Expand Down Expand Up @@ -78,7 +75,7 @@ def process_order(cart):
raise Exception("Not enough inventory for " + item['type'])
else:
tempInventory[item['type']] -= 1
print 'Success: ' + item['type'] + ' was purchased, remaining stock is ' + str(tempInventory[item['type']])
print("Success: " + item['type'] + " was purchased, remaining stock is " + str(tempInventory[item['type']]))
Inventory = tempInventory

@app.before_request
Expand All @@ -93,7 +90,7 @@ def sentry_event_context():
def checkout():

order = json.loads(request.data)
print "Processing order for: " + request.headers.get('email')
print("Processing order for: " + request.headers.get('email'))
cart = order["cart"]

with sentry_sdk.start_span(op="db function: get inventory"):
Expand Down Expand Up @@ -124,3 +121,11 @@ def get_tools():
sentry_sdk.capture_exception(err)
raise(err)
return rows


if __name__ == '__main__':
if sys.version_info[0] < 3:
raise SystemExit("Failed to start: need python3")
# This is used when running locally only. When deploying to Google App
# Engine, a webserver process such as Gunicorn will serve the app.
app.run(host='127.0.0.1', port=8080, debug=True)
10 changes: 5 additions & 5 deletions flask/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
flask==1.0.2
flask==1.1.2
flask-cors==3.0.10
sentry-sdk==0.19.2
flask-cors==3.0.7
gunicorn==0.14.2
gunicorn==20.1.0
blinker==1.4
numpy==1.16.6
psycopg2-binary==2.8.4
numpy==1.20.2
psycopg2-binary==2.8.6
python-dotenv==0.12.0
sqlalchemy==1.3.15
pg8000==1.12.5
Expand Down
12 changes: 0 additions & 12 deletions flask/script.py

This file was deleted.

2 changes: 1 addition & 1 deletion react/.env.development
Original file line number Diff line number Diff line change
@@ -1 +1 @@
REACT_APP_BACKEND_LOCAL=http://localhost:3003
REACT_APP_BACKEND_LOCAL=http://localhost:8080
32 changes: 32 additions & 0 deletions react/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore

# Python pycache:
__pycache__/
# Ignored by the build system
/setup.cfg

**/.virtualenv
flask/

node_modules/
**/node_modules
Dockerfile
Makefile
.nvmrc
.env.default
.env.development
.dockerignore
Loading