From 95ac5886c34ba820e7d9a1942fffe856b1567078 Mon Sep 17 00:00:00 2001 From: xavki Date: Tue, 29 Oct 2024 21:55:58 +0100 Subject: [PATCH] feat : improvements to test configmaps & metrics - add environment variables and/or config.yml file to define ports, hosts... - add some metrics for hasher and rng --- dockercoins/hasher/Dockerfile | 5 ++- dockercoins/hasher/hasher.rb | 62 +++++++++++++++++++++++++++--- dockercoins/rng/Dockerfile | 3 +- dockercoins/rng/rng.py | 40 ++++++++++++++----- dockercoins/webui/Dockerfile | 6 ++- dockercoins/webui/files/index.html | 2 +- dockercoins/webui/webui.js | 41 +++++++++++++++----- dockercoins/worker/Dockerfile | 3 +- dockercoins/worker/worker.py | 50 +++++++++++++++++------- 9 files changed, 166 insertions(+), 46 deletions(-) diff --git a/dockercoins/hasher/Dockerfile b/dockercoins/hasher/Dockerfile index 62be9282a..3331b90c2 100644 --- a/dockercoins/hasher/Dockerfile +++ b/dockercoins/hasher/Dockerfile @@ -1,7 +1,8 @@ FROM ruby:alpine RUN apk add --update build-base curl -RUN gem install sinatra +RUN gem install sinatra --version '~> 3' RUN gem install thin +RUN gem install yaml +RUN gem install prometheus-client ADD hasher.rb / CMD ["ruby", "hasher.rb"] -EXPOSE 80 diff --git a/dockercoins/hasher/hasher.rb b/dockercoins/hasher/hasher.rb index 28a929f36..21054c062 100644 --- a/dockercoins/hasher/hasher.rb +++ b/dockercoins/hasher/hasher.rb @@ -1,18 +1,68 @@ require 'digest' require 'sinatra' require 'socket' +require 'yaml' +require 'prometheus/client' +require 'prometheus/client/formats/text' # Import the text formatter +# Define default configuration +default_config = { + 'port' => 80, + 'sleep_duration' => 0.1 +} + +# Load configuration from YAML file if it exists, otherwise use defaults +config = if File.exist?('config.yml') + YAML.load_file('config.yml') + else + default_config + end + +# Set port with precedence: ENV > YAML > default (80) +set :port, (ENV['PORT'] || config['port'] || default_config['port']).to_i set :bind, '0.0.0.0' -set :port, 80 + +# Set sleep duration with precedence: ENV > YAML > default (0.1) +SLEEP_DURATION = (ENV['SLEEP_DURATION'] || config['sleep_duration'] || default_config['sleep_duration']).to_f + +# Initialize Prometheus client +prometheus = Prometheus::Client.registry + +# Create metrics +request_counter = prometheus.counter(:hash_requests_total, docstring: 'Total number of hash requests', labels: [:method]) +request_duration = prometheus.histogram(:hash_request_duration_seconds, docstring: 'Duration of hash requests in seconds', labels: [:method]) post '/' do - # Simulate a bit of delay - sleep 0.1 - content_type 'text/plain' - "#{Digest::SHA2.new().update(request.body.read)}" + start_time = Time.now + + # Simulate a bit of delay using the configured sleep duration + sleep SLEEP_DURATION + content_type 'text/plain' + hash_value = "#{Digest::SHA2.new.update(request.body.read)}" + + # Record metrics + request_counter.increment(labels: { method: 'POST' }) + request_duration.observe(Time.now - start_time, labels: { method: 'POST' }) + + hash_value end get '/' do - "HASHER running on #{Socket.gethostname}\n" + start_time = Time.now + + sleep SLEEP_DURATION + response = "HASHER running on #{Socket.gethostname}\n" + + # Record metrics + request_counter.increment(labels: { method: 'GET' }) + request_duration.observe(Time.now - start_time, labels: { method: 'GET' }) + + response end +# Endpoint to expose metrics +get '/metrics' do + content_type 'text/plain' + # Use the Text formatter to convert metrics to the right format + Prometheus::Client::Formats::Text.marshal(prometheus) +end diff --git a/dockercoins/rng/Dockerfile b/dockercoins/rng/Dockerfile index 34cf43b2e..bd7c4991e 100644 --- a/dockercoins/rng/Dockerfile +++ b/dockercoins/rng/Dockerfile @@ -1,5 +1,4 @@ FROM python:alpine -RUN pip install Flask +RUN pip install Flask pyyaml prometheus-flask-exporter COPY rng.py / CMD ["python", "rng.py"] -EXPOSE 80 diff --git a/dockercoins/rng/rng.py b/dockercoins/rng/rng.py index b327e922a..2653945c1 100644 --- a/dockercoins/rng/rng.py +++ b/dockercoins/rng/rng.py @@ -1,32 +1,54 @@ from flask import Flask, Response +from prometheus_flask_exporter import PrometheusMetrics import os import socket import time +import yaml + +# Load configuration from YAML file if it exists, otherwise use defaults +default_config = { + 'port': 80, + 'sleep_duration': 0.1 +} + +if os.path.exists('config.yml'): + with open('config.yml', 'r') as file: + config = yaml.safe_load(file) +else: + config = default_config + +# Get configurations with precedence: ENV > YAML > default +port = int(os.environ.get("PORT", config.get('port', default_config['port']))) +sleep_duration = float(os.environ.get("SLEEP_DURATION", config.get('sleep_duration', default_config['sleep_duration']))) app = Flask(__name__) +# Enable Prometheus metrics +metrics = PrometheusMetrics(app) + # Enable debugging if the DEBUG environment variable is set and starts with Y app.debug = os.environ.get("DEBUG", "").lower().startswith('y') hostname = socket.gethostname() - urandom = os.open("/dev/urandom", os.O_RDONLY) - @app.route("/") def index(): - return "RNG running on {}\n".format(hostname) - + time.sleep(sleep_duration) # Simulate processing time + return f"RNG running on {hostname}\n" @app.route("/") def rng(how_many_bytes): - # Simulate a little bit of delay - time.sleep(0.1) + time.sleep(sleep_duration) # Simulate processing time return Response( os.read(urandom, how_many_bytes), - content_type="application/octet-stream") + content_type="application/octet-stream" + ) +# Prometheus metrics will be exposed automatically on the /metrics endpoint +# The default metrics will include: +# 1. `flask_http_requests_total`: Total number of HTTP requests +# 2. `flask_http_request_duration_seconds`: Duration of HTTP requests if __name__ == "__main__": - app.run(host="0.0.0.0", port=80, threaded=False) - + app.run(host="0.0.0.0", port=port, threaded=False) diff --git a/dockercoins/webui/Dockerfile b/dockercoins/webui/Dockerfile index 5e8c9be66..8ef2fa1af 100644 --- a/dockercoins/webui/Dockerfile +++ b/dockercoins/webui/Dockerfile @@ -1,6 +1,8 @@ -FROM node:4-slim +FROM node:10-slim RUN npm install express -RUN npm install redis +RUN npm install redis@3 +RUN npm install yamljs +RUN npm install prom-client COPY files/ /files/ COPY webui.js / CMD ["node", "webui.js"] diff --git a/dockercoins/webui/files/index.html b/dockercoins/webui/files/index.html index f53472013..769a106d8 100644 --- a/dockercoins/webui/files/index.html +++ b/dockercoins/webui/files/index.html @@ -13,7 +13,7 @@ color: royalblue; } - +