This repository contains three assignments:
- CORS
- API thorughput logging
- Prometheus monitoring
This assignment demonstrates CORS policies by implementing servers in python and Go that restrict or allow specific origins. And only works for GET requests. It includes handling preflight requests when custom headers are used.
Running Python Server
cd CORS/cors-python/
python3 data-server.pyRunning the Client for Python data server
cd CORS/cors-python/
python3 -m http.server 7000Running Go server
cd CORS/cors-go/cmd/server
go build data-server.go
./data-serverRunning the Client for Go data server
cd CORS/cors-go/static
python3 -m http.server 9000In this assignment i ran two python servers one is ping-pong-flask.py and second one is ping-pong-no-flask.py, in this i aimed to see what difference it makes if we don't use Flask library for creating a server.
I ran these two python servers in a docker container along with a middleware script also in another docker container, I sent 20-20 requests to both servers one after another, and logged their throughput on the container log which includes:
- Total requests
- Successful responses
- Failed responses
- Total time taken to run 20 requests
- Average time taken by each request
As this assignment contains pushing more than one file to the docker so i used Docker-Compose to simplyfy the process of installation.
cd api-throughput
docker-compose up --buildThis will create 3 containers 2 running python server and one running middleware script.
After running the docker-compose command the middleware script will make http requests to both the servers and will print the throughput of servers onto log of container.
Wrote a simple ping-pong server with /metrics endpoint open for scraping metrics from the server, to achieve this in python i used pythons built-in prometheus_client library, and the Make_wsgi_app() to open the /metrics endpoint from where prometheus's scraper will scrape metrics.
I followed a structure where Python server, Prometheus and Grafana, all are running in separate docker containers following the 'One Task Per container' approach of Docker.
In this server I created a metrics named http_requests_total using the Counter class provided by prometheus_client library, which will tell the total number of HTTP requests made to this server.
cd prometheus-monitoringdocker build -t prom-testing-image .docker run -d --name=docker-testing-container -p 8000:8000 prom-testing-imageThis command runs the bitnami/prometheus Docker image in a container and mounts local prometheus.yml configuration file to the container's /opt/bitnami/conf/prometheus.yml path. Additionally, it maps port 9090 on local machine to port 9090 in the container.
docker run -d --name=prometheus -v prometheus.yml:/opt/bitnami/conf/prometheus.yml -p 9090:9090 bitnami/prometheusdocker run -d --name=grafana -p 3000:3000 grafana/grafanaOpen: http://localhost:9090
On this webpage there will be scraped metrics in text format.
- Open the following URL: http://localhost:3000
- Default Login Credentials:
- Username:
admin - Password:
admin
- Username:
- After logging in:
- Go to Connections and click on Add New Connection.
- Select Prometheus as the New conncetion.
- Click on Add New Data Source
- In the URL field, enter:
http://host.docker.internal:9090 - Click on Build a Dashboard
- Add Visualisation
- Select
prometheusas Data Source
The metric i've made is total_http_requests, so to check total number of HTTP requests, run total_http_requests query in Grafana, this will show total number of requests in a time series data format under the table-view section.