Skip to content

cathyyngo/python-flask

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flask Web Application Setup

  1. Install virtualenv, if not already done
  2. Create a virtual environment for the application, if needed
  3. Activate virtual environment
  4. Upgrade pip in virtual environment
  5. Install dependencies listed in requirements.txt
Step Windows (PowerShell) Linux / MacOS (bash)
1 pip install virtualenv
2 python -m venv venv python3 -m venv venv
3 .\venv\Scripts\Activate.ps1 source venv/bin/activate
4 python -m pip install --upgrade pip python3 -m pip install --upgrade pip
5 pip install -r requirements.txt

Running the Application

To send data to New Relic APM, uncomment the the following lines in webserver.py:

import newrelic.agent
newrelic.agent.initialize()
Windows (PowerShell) Linux / MacOS (bash)
$Env:NEW_RELIC_APP_NAME = "Local Python App" export NEW_RELIC_APP_NAME="Local Python App"
$Env:NEW_RELIC_LICENSE_KEY = "XXXX...NRAL" export NEW_RELIC_LICENSE_KEY="XXXX...NRAL"
python datacrunch-consulting\webserver.py python3 datacrunch-consulting/webserver.py

Endpoints to Test

API Endpoint Notes
Ping http://127.0.0.1:5000/ping JSON: pong
Convert Celcius to Fahrenheit http://127.0.0.1:5000/convertC/123
Convert Fahrenheit to Celcius http://127.0.0.1:5000/convertF/--143 Generate Server Error
Calculate n-th Prime Number http://127.0.0.1:5000/api/prime/12345 Put load on CPU / RAM
Calculate n-th Fibonacci number http://127.0.0.1:5000/api/fib/42 JSON result
Validate with Luhn algorithm http://127.0.0.1:5000/api/luhn/951847623 Masked in logs
Get COVID data for Ontario http://127.0.0.1:5000/covid
Get status for services http://127.0.0.1:5000/statuspage AJAX request
Get something from Redis DB http://127.0.0.1:5000/redis Redis database

OpenTelemetry

  1. To send telemetry data to New Relic, set the following environment variables
OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp.nr-data.net:4317
OTEL_EXPORTER_OTLP_HEADERS=api-key=XXXX...NRAL
OTEL_RESOURCE_ATTRIBUTES=service.name=python-flask.otel,service.instance.id=localhost-pc
  1. Download the following packages to your virtual environment
pip install opentelemetry-api
pip install opentelemetry-sdk
pip install opentelemetry-instrumentation-flask
pip install opentelemetry-exporter-otlp-proto-grpc
pip install opentelemetry-distro
  1. No changes to the code is needed, just run the app as usual but with opentelemetry-instrument at the front
opentelemetry-instrument python .\datacrunch-consulting\webserver.py

Docker Image

  1. Build the Docker image with docker build -t python-flask:latest .
  2. Run the app with your INGEST - LICENSE key and give your application a name:
docker run -d -e NEW_RELIC_LICENSE_KEY="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXNRAL" -e NEW_RELIC_APP_NAME="python-flask.docker" -p 5000:5000 python-flask:latest`
  1. Access the application with the same endpoints above at http://127.0.0.1:5000

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 44.5%
  • HTML 26.1%
  • JavaScript 19.6%
  • CSS 7.8%
  • Dockerfile 1.8%
  • Procfile 0.2%