File: collector.rst

package info (click to toggle)
cloudkitty 23.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 3,532 kB
  • sloc: python: 21,803; sh: 528; makefile: 226; pascal: 54
file content (181 lines) | stat: -rw-r--r-- 5,348 bytes parent folder | download | duplicates (5)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
=========
Collector
=========

Data format
===========

Internally, CloudKitty's data format is a bit more detailled than what can be
found in the `architecture documentation`_.

The internal data format is the following:

.. code-block:: json

   {
       "bananas": [
           {
               "vol": {
                   "unit": "banana",
                   "qty": 1
               },
               "rating": {
                   "price": 1
               },
               "groupby": {
                   "xxx_id": "hello",
                   "yyy_id": "bye",
               },
               "metadata": {
                   "flavor": "chocolate",
                   "eaten_by": "gorilla",
               },
          }
       ],
   }

However, developers implementing a collector don't need to format the data
themselves, as there are helper functions for these matters.

Implementation
==============

Each collector must implement the following class:

.. autoclass:: cloudkitty.collector.BaseCollector
   :noindex:
   :members: fetch_all, check_configuration

The ``retrieve`` method of the ``BaseCollector`` class is called by the
orchestrator. This method calls the ``fetch_all`` method of the child class.

To create a collector, you need to implement at least the ``fetch_all`` method.


Data collection
+++++++++++++++

Collectors must implement a ``fetch_all`` method. This method is called for
each metric type, for each scope, for each collect period. It has the
following prototype:

.. autoclass:: cloudkitty.collector.BaseCollector
   :noindex:
   :members: fetch_all

This method is supposed to return a list of
``cloudkitty.dataframe.DataPoint`` objects.

Example code of a basic collector:

.. code-block:: python

    from cloudkitty.collector import BaseCollector

    class MyCollector(BaseCollector):
        def __init__(self, **kwargs):
            super(MyCollector, self).__init__(**kwargs)

        def fetch_all(self, metric_name, start, end,
                      project_id=None, q_filter=None):
            data = []
            for CONDITION:
                # do stuff
                data.append(dataframe.DataPoint(
                    unit,
                    qty, # int, float, decimal.Decimal or str
                    0, # price
                    groupby, # dict
                    metadata, # dict
                ))

            return data


``project_id`` can be misleading, as it is a legacy name. It contains the
ID of the current scope. The attribute corresponding to the scope is specified
in the configuration, under ``[collect]/scope_key``. Thus, all queries should
filter based on this attribute. Example:

.. code-block:: python

    from oslo_config import cfg

    from cloudkitty.collector import BaseCollector

    CONF = cfg.CONF

    class MyCollector(BaseCollector):
        def __init__(self, **kwargs):
            super(MyCollector, self).__init__(**kwargs)

        def fetch_all(self, metric_name, start, end,
                      project_id=None, q_filter=None):
            scope_key = CONF.collect.scope_key
            filters = {'start': start, 'stop': stop, scope_key: project_id}

            data = self.client.query(
                filters=filters,
                groupby=self.conf[metric_name]['groupby'])
            # Format data etc
            return output


Additional configuration
++++++++++++++++++++++++

If you need to extend the metric configuration (add parameters to the
``extra_args`` section of ``metrics.yml``), you can overload the
``check_configuration`` method of the base collector:

.. autoclass:: cloudkitty.collector.BaseCollector
   :noindex:
   :members: check_configuration

This method uses `voluptuous`_ for data validation. The base schema for each
metric can be found in ``cloudkitty.collector.METRIC_BASE_SCHEMA``. This schema
is meant to be extended by other collectors. Example taken from the gnocchi
collector code:

.. code-block:: python

   from cloudkitty import collector

   GNOCCHI_EXTRA_SCHEMA = {
       Required('extra_args'): {
           Required('resource_type'): All(str, Length(min=1)),
           # Due to Gnocchi model, metric are grouped by resource.
           # This parameter allows to adapt the key of the resource identifier
           Required('resource_key', default='id'): All(str, Length(min=1)),
           Required('aggregation_method', default='max'):
               In(['max', 'mean', 'min']),
       },
   }

   class GnocchiCollector(collector.BaseCollector):

       collector_name = 'gnocchi'

       @staticmethod
       def check_configuration(conf):
           conf = collector.BaseCollector.check_configuration(conf)
           metric_schema = Schema(collector.METRIC_BASE_SCHEMA).extend(
               GNOCCHI_EXTRA_SCHEMA)

           output = {}
           for metric_name, metric in conf.items():
               met = output[metric_name] = metric_schema(metric)

               if met['extra_args']['resource_key'] not in met['groupby']:
                   met['groupby'].append(met['extra_args']['resource_key'])

           return output


If your collector does not need any ``extra_args``, it is not required to
overload the ``check_configuration`` method.


.. _architecture documentation: ../admin/architecture.html

.. _voluptuous: https://github.com/alecthomas/voluptuous