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

Key value store docker-compose addition and key value store implementation #73

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
139 changes: 123 additions & 16 deletions project/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import sys
import time
import yaml
import json
from flask import Flask, jsonify, make_response, Response, request
import html
from logging.config import dictConfig

from opentelemetry import trace, metrics
Expand Down Expand Up @@ -30,6 +32,7 @@

from opentelemetry.instrumentation.requests import RequestsInstrumentor

from redis import Redis

def get_yaml_file_path():
return os.getenv('CUSTOM_RULE_YAML_FILE', 'config/custom_rule.yaml')
Expand Down Expand Up @@ -134,6 +137,21 @@ def check_open_telemetry():
trace.set_tracer_provider(provider)
tracer = trace.get_tracer("my.tracer.name")

def get_cache_config():
if os.getenv('CACHE_FLAG', False) == "True":
redis_host = os.getenv('REDIS_HOST', 'localhost')
redis_port = os.getenv('REDIS_PORT', 16379)
return redis_host, redis_port

def connection_cache():
if os.getenv('CACHE_CLUSTER_FLAG', True) is True:
redis_host, redis_port = get_cache_config()
r = Redis(host=redis_host, port=redis_port, decode_responses=True)
return r
else:
redis_host, redis_port = get_cache_config()
r = Redis(host=redis_host, port=redis_port)
return r

dictConfig({
'version': 1,
Expand Down Expand Up @@ -223,55 +241,143 @@ def after_request(response):

@app.route('/')
def top():
return make_response(jsonify(top='Hello mock server'), 200)
data = {'top': 'Hello mock server'}
if os.getenv('CACHE_FLAG') == 'True':
r = connection_cache()
value = r.get("/")
if value:
app.logger.info("Hit /")
return make_response(jsonify(json.loads(value)), 200)
else:
app.logger.info("Miss Hit /")
r.set("/", json.dumps(data))
return make_response(jsonify(data), 200)
else:
return make_response(jsonify(data), 200)

@app.route('/<int:sleep_time>/<int:status_code>', methods=HTTP_METHODS)
@app.route('/<int:sleep_time>/<int:status_code>/', methods=HTTP_METHODS)
def index(sleep_time, status_code):
if 100 <= status_code <= 599:
time.sleep(sleep_time)
return make_response(jsonify(sleep_time=sleep_time, status_code=status_code), status_code)
data = {'sleep_time': sleep_time, 'status_code': status_code}
if os.getenv('CACHE_FLAG') == 'True':
r = connection_cache()
value = r.get("{}/{}".format(sleep_time,status_code))
if value:
time.sleep(sleep_time)
app.logger.info("Hit /")
return make_response(jsonify(json.loads(value)), 200)
else:
app.logger.info("Miss Hit /")
r.set("{}/{}".format(sleep_time,status_code), json.dumps(data))
return make_response(json.dumps({'sleep_time': html.escape(str(sleep_time)), 'status_code': html.escape(str(status_code))}), status_code)
else:
time.sleep(sleep_time)
return make_response(json.dumps({'sleep_time': html.escape(str(sleep_time)), 'status_code': html.escape(str(status_code))}), status_code)
else:
return make_response(jsonify(err="Not status code"), 400)

@app.route('/sleep/<int:sleep_time>', methods=HTTP_METHODS)
@app.route('/sleep/<int:sleep_time>/', methods=HTTP_METHODS)
def only_sleep_time(sleep_time):
time.sleep(sleep_time)
return make_response(jsonify(sleep_time=sleep_time, status_code=200), 200)
data = {'sleep_time': sleep_time, 'status_code': 200}
if os.getenv('CACHE_FLAG') == 'True':
r = connection_cache()
value = r.get("/sleep/{}".format(sleep_time))
if value:
time.sleep(sleep_time)
app.logger.info("Hit /sleep/{}".format(sleep_time))
return make_response(jsonify(data), 200)
else:
app.logger.info("Miss Hit /sleep/{}".format(sleep_time))
r.set("sleep/{}".format(sleep_time), json.dumps(value))
time.sleep(sleep_time)
return make_response(jsonify(data), 200)
else:
time.sleep(sleep_time)
return make_response(jsonify({'sleep_time': html.escape(str(sleep_time)), 'status_code': 200}), 200)

@app.route('/status/<int:status_code>', methods=HTTP_METHODS)
@app.route('/status/<int:status_code>/', methods=HTTP_METHODS)
def only_status_code(status_code, sleep_time=0):
if 100 <= status_code <= 599:
time.sleep(sleep_time)
return make_response(jsonify(sleep_time=sleep_time, status_code=status_code), status_code)
data = {'sleep_time': sleep_time, 'status_code': status_code}
if os.getenv('CACHE_FLAG') == 'True':
r = connection_cache()
value = r.get("/status/{}".format(status_code))
if value:
app.logger.info("Hit /status/{}".format(status_code))
return make_response(jsonify(json.loads(value)), 200)
else:
app.logger.info("Miss Hit /status/{}".format(status_code))
r.set("/status/{}".format(status_code), json.dumps(data))
return make_response(jsonify(data), status_code)
else:
return make_response(jsonify(data), status_code)
else:
return make_response(jsonify(err="Not status code"), 400)

@app.route('/<int:sleep_time>/<int:status_code>/query', methods=HTTP_METHODS)
def index_query(sleep_time, status_code):
if 100 <= status_code <= 599:
time.sleep(sleep_time)
query_params_dict = request.args.to_dict()
return make_response(jsonify(sleep_time=sleep_time, status_code=status_code, output=str(query_params_dict)), status_code)
data = {'sleep_time': sleep_time, 'status_code': status_code, 'output': str(query_params_dict)}
if os.getenv('CACHE_FLAG') == 'True':
r = connection_cache()
query_params_dict = request.args.to_dict()
value = r.get("{}/{}/query".format(sleep_time,status_code))
if value:
time.sleep(sleep_time)
app.logger.info("Hit /{}/{}/query".format(sleep_time,status_code))
return make_response(jsonify(json.loads(value)), 200)
else:
app.logger.info("Miss Hit /{}/{}/query".format(sleep_time,status_code))
r.set("{}/{}/query".format(sleep_time,status_code), json.dumps(data))
time.sleep(sleep_time)
return make_response(jsonify(data), status_code)
else:
time.sleep(sleep_time)
query_params_dict = request.args.to_dict()
return make_response(jsonify(data), status_code)
else:
return make_response(jsonify(err="Not status code"), 400)


@app.route('/sleep/<int:sleep_time>/query', defaults={'status_code': 200}, methods=HTTP_METHODS)
def only_sleep_time_query(sleep_time, status_code):
time.sleep(sleep_time)
query_params_dict = request.args.to_dict()
return make_response(jsonify(sleep_time=sleep_time, status_code=status_code, output=str(query_params_dict)), status_code)
data = {'sleep_time': sleep_time, 'status_code': status_code, 'output': str(query_params_dict)}
if os.getenv('CACHE_FLAG') == 'True':
r = connection_cache()
query_params_dict = request.args.to_dict()
value = r.get("sleep/{}/query".format(sleep_time))
if value:
time.sleep(sleep_time)
app.logger.info("Hit /sleep/{}/query".format(sleep_time))
return make_response(jsonify(json.loads(value)), 200)
else:
app.logger.info("Miss Hit /sleep/{}/query".format(sleep_time))
r.set("sleep/{}/query".format(sleep_time), json.dumps(data))
time.sleep(sleep_time)
return make_response(jsonify(data), status_code)
return make_response(jsonify(data), status_code)


@app.route('/status/<int:status_code>/query', methods=HTTP_METHODS)
def only_status_code_query(status_code, sleep_time=0):
data = {'sleep_time': sleep_time, 'status_code': status_code, 'output': str(query_params_dict)}
if 100 <= status_code <= 599:
time.sleep(sleep_time)
query_params_dict = request.args.to_dict()
return make_response(jsonify(sleep_time=sleep_time, status_code=status_code, output=str(query_params_dict)), status_code)
if os.getenv('CACHE_FLAG') == 'True':
r = connection_cache()
query_params_dict = request.args.to_dict()
value = r.get("status/{}/query".format(status_code))
if value:
app.logger.info("Hit /status/{}/query".format(status_code))
return make_response(jsonify(json.loads(value)), 200)
else:
app.logger.info("Miss Hit /status/{}/query".format(status_code))
r.set("status/{}/query".format(status_code), json.dumps(data))
time.sleep(sleep_time)
return make_response(jsonify(data), status_code)
else:
return make_response(jsonify(err="Not status code"), 400)

Expand All @@ -288,6 +394,7 @@ def custom_rule(path):
if yaml_data["custom_rule"][i]["rule"]["path"] == path and yaml_data["custom_rule"][i]["rule"]["method"] == request.method:
if response_body_pathx := yaml_data["custom_rule"][i]["rule"].get("response_body_path"):
try:
# TODO: Corresponding Redis.
with open(response_body_pathx, "r") as response_body_file:
response_body = response_body_file.read()
if sleep_time := yaml_data["custom_rule"][i]["rule"].get("sleep_time"):
Expand All @@ -300,7 +407,7 @@ def custom_rule(path):

@app.route('/favicon.ico')
def favicon():
return '', 204 # 空のレスポンスで204 No Contentを返す
return '', 204

@app.errorhandler(404)
def page_not_found(e):
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ opentelemetry-exporter-otlp-proto-common==1.27.0
opentelemetry-exporter-otlp-proto-http==1.27.0
opentelemetry-exporter-otlp-proto-grpc==1.27.0
opentelemetry-proto==1.27.0
redis==5.0.1
13 changes: 13 additions & 0 deletions sample_manifest/docker-compose/key_value/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# docker-compose
version: '3.7'
services:
redis:
image: redis:6.0.9
container_name: redis
ports:
- "16379:6379"
valkey:
image: valkey/valkey
container_name: valkey
ports:
- "26379:6379"
Loading