/
configuration.py
111 lines (94 loc) · 3.55 KB
/
configuration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from __future__ import (
absolute_import,
unicode_literals,
)
import copy
from typing import (
Any,
Dict,
List,
Optional,
)
import attr
from conformity import (
fields,
validator,
)
import six
from pymetrics.publishers.base import MetricsPublisher
__all__ = (
'Configuration',
'CONFIGURATION_SCHEMA',
'create_configuration',
)
CONFIGURATION_SCHEMA = fields.Polymorph(
switch_field='version',
contents_map={
2: fields.Dictionary(
{
'version': fields.Constant(2),
'enable_meta_metrics': fields.Boolean(
description='If true, meta-metrics will be recorded documenting the performance of '
'PyMetrics itself.',
),
'error_logger_name': fields.UnicodeString(
description='By default, errors encountered when publishing metrics are suppressed and lost. If '
'this value is truthy, a Logger is created with this name and used to log publication '
'errors.',
),
'publishers': fields.Sequence(
fields.ClassConfigurationSchema(
base_class=MetricsPublisher,
description='Import path and arguments for a publisher.',
),
min_length=1,
description='The configuration for all publishers.',
),
},
optional_keys=('enable_meta_metrics', 'error_logger_name'),
),
},
description='The configuration schema changes slightly based on which config version you specify.',
)
"""""" # Empty docstring to make autodoc document this data
@attr.s
class Configuration(object):
version = attr.ib() # type: int
publishers = attr.ib(default=attr.Factory(list)) # type: List[MetricsPublisher]
error_logger_name = attr.ib(default=None) # type: Optional[six.text_type]
enable_meta_metrics = attr.ib(default=False) # type: bool
@validator.validate_call(
args=fields.Tuple(copy.deepcopy(CONFIGURATION_SCHEMA)),
kwargs=None,
returns=fields.ObjectInstance(Configuration),
)
def create_configuration(config_dict): # type: (Dict[six.text_type, Any]) -> Configuration
"""
Creates a `Configuration` object using the provided configuration dictionary. Works in similar fashion to logging's
configuration.
Expected format of config is a dict:
.. code-block:: python
{
'version': 2,
'error_logger_name': 'pymetrics', # name of the error logger to use, or `None` (the default) to suppress
'enable_meta_metrics': False, # whether to enable the collection of meta-metrics
'publishers': [
{
'path': 'path.to.publisher:ClassName',
'kwargs': {
... # constructor arguments for the publisher
},
},
],
}
If multiple publishers are specified, metrics will be emitted to each publisher in the order it is specified in
the configuration list.
"""
configuration = Configuration(
version=config_dict['version'],
enable_meta_metrics=config_dict.get('enable_meta_metrics', False),
error_logger_name=config_dict.get('error_logger_name'),
)
for publisher in config_dict['publishers']:
configuration.publishers.append(publisher['object'](**publisher.get('kwargs', {})))
return configuration