Show More
@@ -1,49 +1,52 b'' | |||
|
1 | 1 | from rhodecode.lib._vendor.statsd import client_from_config |
|
2 | 2 | |
|
3 | 3 | |
|
4 | 4 | class StatsdClientNotInitialised(Exception): |
|
5 | 5 | pass |
|
6 | 6 | |
|
7 | 7 | |
|
8 | 8 | class _Singleton(type): |
|
9 | 9 | """A metaclass that creates a Singleton base class when called.""" |
|
10 | 10 | |
|
11 | 11 | _instances = {} |
|
12 | 12 | |
|
13 | 13 | def __call__(cls, *args, **kwargs): |
|
14 | 14 | if cls not in cls._instances: |
|
15 | 15 | cls._instances[cls] = super().__call__(*args, **kwargs) |
|
16 | 16 | return cls._instances[cls] |
|
17 | 17 | |
|
18 | 18 | |
|
19 | 19 | class Singleton(_Singleton("SingletonMeta", (object,), {})): |
|
20 | 20 | pass |
|
21 | 21 | |
|
22 | 22 | |
|
23 | 23 | class StatsdClientClass(Singleton): |
|
24 | 24 | setup_run = False |
|
25 | 25 | statsd_client = None |
|
26 | 26 | statsd = None |
|
27 | 27 | |
|
28 | def __repr__(self): | |
|
29 | return f"{self.__class__}(statsd={self.statsd})" | |
|
30 | ||
|
28 | 31 | def __getattribute__(self, name): |
|
29 | 32 | |
|
30 | 33 | if name.startswith("statsd"): |
|
31 | 34 | if self.setup_run: |
|
32 | 35 | return super().__getattribute__(name) |
|
33 | 36 | else: |
|
34 | 37 | return None |
|
35 | 38 | #raise StatsdClientNotInitialised("requested key was %s" % name) |
|
36 | 39 | |
|
37 | 40 | return super().__getattribute__(name) |
|
38 | 41 | |
|
39 | 42 | def setup(self, settings): |
|
40 | 43 | """ |
|
41 | 44 | Initialize the client |
|
42 | 45 | """ |
|
43 | 46 | statsd = client_from_config(settings) |
|
44 | 47 | self.statsd = statsd |
|
45 | 48 | self.statsd_client = statsd |
|
46 | 49 | self.setup_run = True |
|
47 | 50 | |
|
48 | 51 | |
|
49 | 52 | StatsdClient = StatsdClientClass() |
General Comments 0
You need to be logged in to leave comments.
Login now