e851773367
This patch is a sample oslo.metrics code to gather oslo.messaging's metrics and export the metrics to prometheus. Some parts of this code doesn't follow OpenStack standards style, and this lacks test codes. Please use this patch as PoC for oslo.metrics. Co-Authored-By: Yuki Nishiwaki <yuki.nishiwaki@linecorp.com> Change-Id: I9434d11466e7626fdbebd1340a8bb3d664518bd1
90 lines
2.7 KiB
Python
90 lines
2.7 KiB
Python
# Copyright 2020 LINE Corp.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the 'License'); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
import os
|
|
import select
|
|
import socket
|
|
import sys
|
|
import threading
|
|
from wsgiref.simple_server import make_server
|
|
|
|
from oslo_config import cfg
|
|
from oslo_log import log as logging
|
|
from prometheus_client import make_wsgi_app
|
|
|
|
from oslo_metrics import message_router
|
|
|
|
|
|
oslo_metrics_configs = [
|
|
cfg.StrOpt('metrics_socket_file',
|
|
default='/var/tmp/metrics_collector.sock',
|
|
help='Unix domain socket file to be used'
|
|
'to send rpc related metrics'),
|
|
]
|
|
cfg.CONF.register_opts(oslo_metrics_configs, group='oslo_metrics')
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
CONF = cfg.CONF
|
|
logging.register_options(CONF)
|
|
logging.setup(CONF, 'oslo-metrics')
|
|
LOG.logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
class MetricsListener():
|
|
|
|
def __init__(self, socket_path):
|
|
self.socket_path = socket_path
|
|
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
|
|
self.socket.bind(self.socket_path)
|
|
self.start = True
|
|
self.router = message_router.MessageRouter()
|
|
|
|
def serve(self):
|
|
while self.start:
|
|
readable, writable, exceptional = select.select([self.socket], [], [], 1)
|
|
if len(readable) == 0:
|
|
continue
|
|
try:
|
|
LOG.debug("wait for socket.recv")
|
|
# 1 message size should be smaller than 65565
|
|
msg = self.socket.recv(65565)
|
|
LOG.debug("got message")
|
|
self.router.process(msg)
|
|
except socket.timeout:
|
|
pass
|
|
|
|
def stop(self):
|
|
self.socket.close()
|
|
self.start = False
|
|
|
|
if __name__ == "__main__":
|
|
cfg.CONF(sys.argv[1:])
|
|
m = MetricsListener(cfg.CONF.oslo_metrics.metrics_socket_file)
|
|
mt = threading.Thread(target=m.serve)
|
|
LOG.info("Start oslo.metrics")
|
|
mt.start()
|
|
|
|
app = make_wsgi_app()
|
|
try:
|
|
httpd = make_server('', 3000, app)
|
|
httpd.serve_forever()
|
|
except KeyboardInterrupt:
|
|
pass
|
|
finally:
|
|
LOG.info("Try to stop...")
|
|
os.remove(cfg.CONF.oslo_metrics.metrics_socket_file)
|
|
m.stop()
|
|
httpd.server_close()
|