|
|
# RhodeCode VCSServer provides access to different vcs backends via network.
|
|
|
# Copyright (C) 2014-2023 RhodeCode GmbH
|
|
|
#
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
|
# (at your option) any later version.
|
|
|
#
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
# GNU General Public License for more details.
|
|
|
#
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
# along with this program; if not, write to the Free Software Foundation,
|
|
|
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
from vcsserver.lib._vendor.statsd import client_from_config
|
|
|
|
|
|
|
|
|
class StatsdClientNotInitialised(Exception):
|
|
|
pass
|
|
|
|
|
|
|
|
|
class _Singleton(type):
|
|
|
"""A metaclass that creates a Singleton base class when called."""
|
|
|
|
|
|
_instances = {}
|
|
|
|
|
|
def __call__(cls, *args, **kwargs):
|
|
|
if cls not in cls._instances:
|
|
|
cls._instances[cls] = super().__call__(*args, **kwargs)
|
|
|
return cls._instances[cls]
|
|
|
|
|
|
|
|
|
class Singleton(_Singleton("SingletonMeta", (object,), {})):
|
|
|
pass
|
|
|
|
|
|
|
|
|
class StatsdClientClass(Singleton):
|
|
|
setup_run = False
|
|
|
statsd_client = None
|
|
|
statsd = None
|
|
|
strict_mode_init = False
|
|
|
|
|
|
def __getattribute__(self, name):
|
|
|
|
|
|
if name.startswith("statsd"):
|
|
|
if self.setup_run:
|
|
|
return super().__getattribute__(name)
|
|
|
else:
|
|
|
if self.strict_mode_init:
|
|
|
raise StatsdClientNotInitialised(f"requested key was {name}")
|
|
|
return None
|
|
|
|
|
|
return super().__getattribute__(name)
|
|
|
|
|
|
def setup(self, settings):
|
|
|
"""
|
|
|
Initialize the client
|
|
|
"""
|
|
|
strict_init_mode = settings.pop('statsd_strict_init', False)
|
|
|
|
|
|
statsd = client_from_config(settings)
|
|
|
self.statsd = statsd
|
|
|
self.statsd_client = statsd
|
|
|
self.setup_run = True
|
|
|
|
|
|
|
|
|
StatsdClient = StatsdClientClass()
|
|
|
|