statsd_client.py
70 lines
| 2.1 KiB
| text/x-python
|
PythonLexer
r1100 | # RhodeCode VCSServer provides access to different vcs backends via network. | |||
r1327 | # Copyright (C) 2014-2024 RhodeCode GmbH | |||
r1100 | # | |||
# 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 | ||||
r1005 | 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: | ||||
r1114 | cls._instances[cls] = super().__call__(*args, **kwargs) | |||
r1005 | return cls._instances[cls] | |||
class Singleton(_Singleton("SingletonMeta", (object,), {})): | ||||
pass | ||||
class StatsdClientClass(Singleton): | ||||
setup_run = False | ||||
statsd_client = None | ||||
statsd = None | ||||
r1063 | strict_mode_init = False | |||
r1005 | ||||
def __getattribute__(self, name): | ||||
if name.startswith("statsd"): | ||||
if self.setup_run: | ||||
r1114 | return super().__getattribute__(name) | |||
r1005 | else: | |||
r1063 | if self.strict_mode_init: | |||
raise StatsdClientNotInitialised(f"requested key was {name}") | ||||
r1005 | return None | |||
r1114 | return super().__getattribute__(name) | |||
r1005 | ||||
def setup(self, settings): | ||||
""" | ||||
Initialize the client | ||||
""" | ||||
r1063 | strict_init_mode = settings.pop('statsd_strict_init', False) | |||
r1005 | statsd = client_from_config(settings) | |||
self.statsd = statsd | ||||
self.statsd_client = statsd | ||||
self.setup_run = True | ||||
StatsdClient = StatsdClientClass() | ||||