refactor(docker): Add docker demo

Add initiall commit that tests metrics reading using prometheus on docker

Signed-off-by: Rodney Osodo <socials@rodneyosodo.com>
This commit is contained in:
Rodney Osodo
2024-07-09 23:32:17 +03:00
parent 015e19fca1
commit e74f730d7a
8 changed files with 124 additions and 1 deletions
+11
View File
@@ -0,0 +1,11 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
.gitignore
docker-compose.yaml
LICENSE
Makefile
prometheus.yaml
README.md
+11
View File
@@ -0,0 +1,11 @@
FROM python:3.8.4-slim
RUN apt update && apt install -y wait-for-it && \
pip install --upgrade pip && \
pip install nameko nameko-prometheus
COPY . /var/run/my_service
WORKDIR /var/run/my_service
CMD ["nameko", "run", "service:HTTPService", "--config", "config.yaml"]
+11
View File
@@ -0,0 +1,11 @@
.PHONY: build-image
build-image:
docker build -t gitea.rodneyosodo.com/rodneyosodo/nameko-prometheus:latest .
.PHONY: push-image
push-image:
docker push gitea.rodneyosodo.com/rodneyosodo/nameko-prometheus:latest
.PHONY: run
run:
nameko run service:HTTPService --config config.yaml
+25 -1
View File
@@ -1,3 +1,27 @@
# nameko-prometheus
Testing nameko with Prometheus
Testing nameko with Prometheus
## Docker Compose
First step is to build the image:
```bash
make build-image
```
This step can be ignored if you want to use the image from Docker Registry.
Then, you can run the services:
```bash
docker compose -f docker-compose.yaml up
```
Simulate network traffic:
```bash
for i in {1..100}; do curl -X GET http://localhost:8000/goodbye && curl -X GET http://localhost:8000/hello; done
```
You can access the Prometheus dashboard at `http://localhost:9090`.
+1
View File
@@ -0,0 +1 @@
WEB_SERVER_ADDRESS: 0.0.0.0:8000
+30
View File
@@ -0,0 +1,30 @@
networks:
nameko-prometheus-base-net:
driver: bridge
volumes:
prometheus-data:
services:
my_service:
image: gitea.rodneyosodo.com/rodneyosodo/nameko-prometheus:latest
container_name: nameko-service
restart: on-failure
ports:
- 8000:8000
networks:
- nameko-prometheus-base-net
prometheus:
image: prom/prometheus:v2.45.6
container_name: nameko-prometheus
restart: unless-stopped
command:
- "--config.file=/etc/prometheus/prometheus.yaml"
ports:
- 9090:9090
volumes:
- ./prometheus.yaml:/etc/prometheus/prometheus.yaml
- prometheus-data:/prometheus
networks:
- nameko-prometheus-base-net
+12
View File
@@ -0,0 +1,12 @@
global:
scrape_interval: 5s
scrape_timeout: 5s
evaluation_interval: 15s
scrape_configs:
- job_name: "my_service"
honor_timestamps: true
scheme: http
metrics_path: /metrics
static_configs:
- targets:
- my_service:8000
+23
View File
@@ -0,0 +1,23 @@
import json
import random
import time
from nameko.web.handlers import http
from nameko_prometheus import PrometheusMetrics
class HTTPService:
name = "http_service"
metrics = PrometheusMetrics()
@http("GET", "/metrics")
def expose_metrics(self, request):
return self.metrics.expose_metrics(request)
@http("GET", "/hello")
def say_hello(self, request):
time.sleep(random.random() * 0.01)
return json.dumps({"message": "Hello!"})
@http("GET", "/goodbye")
def say_goodbye(self, request):
time.sleep(random.random() * 0.01)
return json.dumps({"message": "Goodbye!"})