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