1 Commits

Author SHA1 Message Date
857d26d092 feat: add metrics for nameko services 2023-07-27 14:02:01 +08:00
3 changed files with 28 additions and 41 deletions

View File

@@ -2,7 +2,7 @@
A lightweight Python distributed microservice solution A lightweight Python distributed microservice solution
## CLI Usage ## Command Line Tool Usage
### Checkout Command ### Checkout Command
@@ -27,5 +27,5 @@ namekoplus init --directory <dir_name> --type <template_type>
See Documents: See Documents:
- [English](https://legendary-sopapillas-e2626d.netlify.app/)
- [中文](https://doc.bearcatlog.com/) - [中文](https://doc.bearcatlog.com/)
- [English](https://legendary-sopapillas-e2626d.netlify.app/)

View File

@@ -9,10 +9,6 @@ import shortuuid
from python_on_whales import DockerException, ClientNotFoundError, DockerClient, docker from python_on_whales import DockerException, ClientNotFoundError, DockerClient, docker
from mako.template import Template from mako.template import Template
INIT_TYPE_CHOICES = ['all', 'rpc', 'event', 'http', 'timer', 'demo']
MIDDLEWARE_CHOICES = ['rabbitmq', 'metrics']
TEST_TYPE_CHOICES = ['unit']
def check_docker(): def check_docker():
""" """
@@ -135,7 +131,7 @@ def start_grafana():
grafana_conf_dir = os.path.join(get_directory('chassis-agent'), 'metric-configs') grafana_conf_dir = os.path.join(get_directory('chassis-agent'), 'metric-configs')
grafana_provisioning_path = os.path.join(grafana_conf_dir, 'grafana_conf/provisioning') grafana_provisioning_path = os.path.join(grafana_conf_dir, 'grafana_conf/provisioning')
grafana_config_path = os.path.join(grafana_conf_dir, 'grafana_conf/config/grafana.ini') grafana_config_path = os.path.join(grafana_conf_dir, 'grafana_conf/config/grafana.ini')
grafana_dashboard_path = os.path.join('.', 'grafana_dashboards') grafana_dashboard_path = os.path.join(grafana_conf_dir, 'grafana_conf/dashboards')
returned_string = docker.run(image='grafana/grafana:latest', name='grafana', hostname='grafana', returned_string = docker.run(image='grafana/grafana:latest', name='grafana', hostname='grafana',
detach=True, restart='always', tty=True, interactive=True, detach=True, restart='always', tty=True, interactive=True,
publish=[(3100, 3000)], pull='missing', publish=[(3100, 3000)], pull='missing',
@@ -157,7 +153,6 @@ def stop_network(network_name):
def start_metric_servers(): def start_metric_servers():
# TODO 检查相应容器是否已启动,如果启动,则先删除
start_network('metric_servers') start_network('metric_servers')
sleep(0.5) sleep(0.5)
start_prometheus() start_prometheus()
@@ -237,7 +232,7 @@ def cli():
@click.option('-t', '--type', '_type', @click.option('-t', '--type', '_type',
default='all', default='all',
show_default=True, show_default=True,
type=click.Choice(INIT_TYPE_CHOICES, case_sensitive=False), type=click.Choice(['all', 'rpc', 'event', 'http', 'timer', 'demo'], case_sensitive=False),
help='The template type of nameko service') help='The template type of nameko service')
def init(directory, _type): def init(directory, _type):
""" """
@@ -262,7 +257,7 @@ def init(directory, _type):
@cli.command() @cli.command()
@click.option('-m', '--middleware', @click.option('-m', '--middleware',
required=True, required=True,
type=click.Choice(MIDDLEWARE_CHOICES, case_sensitive=False), type=click.Choice(['rabbitmq', 'metrics'], case_sensitive=False),
help='The middleware name') help='The middleware name')
def start(middleware): def start(middleware):
""" """
@@ -275,7 +270,7 @@ def start(middleware):
@cli.command() @cli.command()
@click.option('-m', '--middleware', @click.option('-m', '--middleware',
required=True, required=True,
type=click.Choice(MIDDLEWARE_CHOICES, case_sensitive=False), type=click.Choice(['rabbitmq', 'metrics'], case_sensitive=False),
help='The middleware name') help='The middleware name')
def stop(middleware): def stop(middleware):
""" """
@@ -292,7 +287,7 @@ def stop(middleware):
@click.option('-t', '--type', '_type', @click.option('-t', '--type', '_type',
default='unit', default='unit',
show_default=True, show_default=True,
type=click.Choice(TEST_TYPE_CHOICES, case_sensitive=False), type=click.Choice(['unit'], case_sensitive=False),
help='The test type of the nameko service') help='The test type of the nameko service')
def test_gen(directory, _type): def test_gen(directory, _type):
""" """
@@ -321,14 +316,12 @@ def metric_config_gen(module, class_name_str):
""" """
Generate metric config for nameko services. Generate metric config for nameko services.
""" """
import sys
from statsd.client.timer import Timer from statsd.client.timer import Timer
sys.path.append(os.getcwd())
# Extract information of statsd config from the class of nameko service # Extract information of statsd config from the class of nameko service
dest_dir = module.split('.')[0]
file_name = module.split('.')[-1] file_name = module.split('.')[-1]
_module = __import__(module) _module = __import__(module)
config_list = [] config_list = []
for class_name in class_name_str.split(','): for class_name in class_name_str.split(','):
members = inspect.getmembers(getattr(getattr(_module, file_name), class_name), predicate=inspect.isfunction) members = inspect.getmembers(getattr(getattr(_module, file_name), class_name), predicate=inspect.isfunction)
@@ -345,30 +338,24 @@ def metric_config_gen(module, class_name_str):
}) })
# Generate one file of statsd config yaml for statsd exporter # Generate one file of statsd config yaml for statsd exporter
with status(f'Creating statsd_mapping.yml'): metric_configs_dir = os.path.join(get_directory('chassis-agent'), 'metric-configs')
metric_configs_dir = os.path.join(get_directory('chassis-agent'), 'metric-configs') template_file_path = os.path.join(metric_configs_dir, 'statsd_mapping.yml.mako')
template_file_path = os.path.join(metric_configs_dir, 'statsd_mapping.yml.mako') output_file = os.path.join('.', 'statsd_mapping.yml')
output_file = os.path.join('.', 'statsd_mapping.yml') template_to_file(template_file=template_file_path, dest=output_file, output_encoding='utf-8',
template_to_file(template_file=template_file_path, dest=output_file, output_encoding='utf-8', **{'config_list': config_list})
**{'config_list': config_list})
# Generate files of json for grafana dashboard # Generate files of json for grafana dashboard
if not os.access('grafana_dashboards', os.F_OK): for class_name in class_name_str.split(','):
with status(f'Creating directory {os.path.abspath("grafana_dashboards")!r}'): grafana_list = []
os.makedirs('grafana_dashboards') for config in config_list:
if config['class_name'] == class_name:
with status(f'Creating files of Grafana.json into the directory of grafana_dashboards'): grafana_list.append(config)
for class_name in class_name_str.split(','): grafana_configs_dir = os.path.join(get_directory('chassis-agent'), 'metric-configs')
grafana_list = [] grafana_file_path = os.path.join(grafana_configs_dir, 'grafana.json.mako')
for config in config_list: output_file = os.path.join(dest_dir, f'{class_name}_Grafana.json')
if config['class_name'] == class_name: template_to_file(template_file=grafana_file_path, dest=output_file, output_encoding='utf-8',
grafana_list.append(config) **{'service_name': class_name, 'uid': shortuuid.uuid(),
grafana_configs_dir = os.path.join(get_directory('chassis-agent'), 'metric-configs') 'grafana_list': grafana_list})
grafana_file_path = os.path.join(grafana_configs_dir, 'grafana.json.mako')
output_file = os.path.join('grafana_dashboards', f'{class_name}_Grafana.json')
template_to_file(template_file=grafana_file_path, dest=output_file, output_encoding='utf-8',
**{'service_name': class_name, 'uid': shortuuid.uuid(),
'grafana_list': grafana_list})
if __name__ == '__main__': if __name__ == '__main__':

View File

@@ -9,7 +9,7 @@ with open(path.join(here, 'README.md'), encoding='utf-8') as f:
setup( setup(
name='namekoplus', name='namekoplus',
version='0.4.1', version='0.4.0',
description='A lightweight Python distributed microservice solution', description='A lightweight Python distributed microservice solution',
long_description=long_description, long_description=long_description,
long_description_content_type='text/markdown', long_description_content_type='text/markdown',
@@ -59,8 +59,8 @@ setup(
'ha': ['tenacity==8.2.2', 'ha': ['tenacity==8.2.2',
'cachetools==5.3.0', 'cachetools==5.3.0',
'circuitbreaker==2.0.0', 'circuitbreaker==2.0.0',
'logstash_formatter==0.5.17'], 'statsd==4.0.1',
'ob': ['statsd==4.0.1', 'logstash_formatter==0.5.17',
'nameko-sentry==1.0.0', 'nameko-sentry==1.0.0',
'nameko-tracer==1.4.0'], 'nameko-tracer==1.4.0'],
'apiflask': ['apiflask>=1.3.1', 'apiflask': ['apiflask>=1.3.1',