chassis/examples/nameko_demo/demo.py

49 lines
1.6 KiB
Python
Raw Normal View History

2023-05-22 15:54:01 +08:00
import json
import datetime
import requests
from nameko.web.handlers import http
from nameko.timer import timer
from statsd import StatsClient
from circuitbreaker import circuit
2023-06-17 01:38:04 +08:00
from chassis.chassis import init_logger, init_statsd, init_sentry
from config.config import Config
2023-05-22 15:54:01 +08:00
class DemoChassisService:
name = "demo_chassis_service"
2023-06-17 01:38:04 +08:00
sentry = init_sentry()
statsd = init_statsd(Config.STATSD_PERMISSION_PREFIX, Config.STATSD_HOST, Config.STATSD_PORT)
logger = init_logger()
2023-05-22 15:54:01 +08:00
@http('GET', '/health')
@statsd.timer('health')
def health(self, _request):
return json.dumps({'ok': datetime.datetime.utcnow().__str__()})
@http('GET', '/external')
@circuit(failure_threshold=5, expected_exception=ConnectionError)
@statsd.timer('external')
def external_request(self, _request):
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
return json.dumps({'code': response.status_code, 'body': response.text})
@http('GET', '/error')
@circuit(failure_threshold=5, expected_exception=ZeroDivisionError)
@statsd.timer('http_error')
def error_http_request(self):
return json.dumps({1 / 0})
class HealthCheckService:
name = "health_check_service"
statsd = StatsClient('localhost', 8125, prefix='simplebank-nameko_demo')
@timer(interval=10)
@statsd.timer('check_demo_service')
def check_demo_service(self):
response = requests.get('http://0.0.0.0:8000/health')
print("DemoChassisService HEALTH CHECK: status_code {}, response: {}".format(
response.status_code, response.text))