summaryrefslogtreecommitdiff
path: root/oslo_messaging/_metrics/client.py
blob: 46916a150942fdad6513890db854dcc2a92571ed (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256

# Copyright 2020 LINE Corp.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import queue
import socket
import threading
import time

from oslo_config import cfg
from oslo_log import log as logging
from oslo_metrics import message_type
from oslo_utils import eventletutils
from oslo_utils import importutils


LOG = logging.getLogger(__name__)

eventlet = importutils.try_import('eventlet')
if eventlet and eventletutils.is_monkey_patched("thread"):
    # Here we initialize module with the native python threading module
    # if it was already monkey patched by eventlet/greenlet.
    stdlib_threading = eventlet.patcher.original('threading')
else:
    # Manage the case where we run this driver in a non patched environment
    # and where user even so configure the driver to run heartbeat through
    # a python thread, if we don't do that when the heartbeat will start
    # we will facing an issue by trying to override the threading module.
    stdlib_threading = threading

oslo_messaging_metrics = [
    cfg.BoolOpt('metrics_enabled', default=False,
                help='Boolean to send rpc metrics to oslo.metrics.'),
    cfg.IntOpt('metrics_buffer_size', default=1000,
               help='Buffer size to store in oslo.messaging.'),
    cfg.StrOpt('metrics_socket_file',
               default='/var/tmp/metrics_collector.sock',
               help='Unix domain socket file to be used'
                    ' to send rpc related metrics'),
    cfg.StrOpt('metrics_process_name',
               default='',
               help='Process name which is used to identify which process'
                    ' produce metrics'),
    cfg.IntOpt('metrics_thread_stop_timeout',
               default=10,
               help='Sending thread stop once metrics_thread_stop_timeout'
                    ' seconds after the last successful metrics send.'
                    ' So that this thread will not be the blocker'
                    ' when process is shutting down.'
                    ' If the process is still running, sending thread will'
                    ' be restarted at the next metrics queueing time')
]
cfg.CONF.register_opts(oslo_messaging_metrics, group='oslo_messaging_metrics')


class MetricsCollectorClient(object):

    def __init__(self, conf, metrics_type, **kwargs):
        self.conf = conf.oslo_messaging_metrics
        self.unix_socket = self.conf.metrics_socket_file
        buffer_size = self.conf.metrics_buffer_size
        self.tx_queue = queue.Queue(buffer_size)
        self.next_send_metric = None
        self.metrics_type = metrics_type
        self.args = kwargs
        self.send_thread = threading.Thread(target=self.send_loop)
        self.send_thread.start()

    def __enter__(self):
        if not self.conf.metrics_enabled:
            return None
        self.start_time = time.time()
        send_method = getattr(self, self.metrics_type +
                              "_invocation_start_total")
        send_method(**self.args)
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        if self.conf.metrics_enabled:
            duration = time.time() - self.start_time
            send_method = getattr(
                self, self.metrics_type + "_processing_seconds")
            send_method(duration=duration, **self.args)
            send_method = getattr(
                self, self.metrics_type + "_invocation_end_total")
            send_method(**self.args)

    def put_into_txqueue(self, metrics_name, action, **labels):

        labels['process'] = \
            self.conf.metrics_process_name
        m = message_type.Metric("oslo_messaging", metrics_name, action,
                                **labels)

        try:
            self.tx_queue.put_nowait(m)
        except queue.Full:
            LOG.warning("tx queues is already full(%s/%s). Fails to "
                        "send the metrics(%s)" %
                        (self.tx_queue.qsize(), self.tx_queue.maxsize, m))

        if not self.send_thread.is_alive():
            self.send_thread = threading.Thread(target=self.send_loop)
            self.send_thread.start()

    def send_loop(self):
        timeout = self.conf.metrics_thread_stop_timeout
        stoptime = time.time() + timeout
        while stoptime > time.time():
            if self.next_send_metric is None:
                try:
                    self.next_send_metric = self.tx_queue.get(timeout=timeout)
                except queue.Empty:
                    continue
            try:
                self.send_metric(self.next_send_metric)
                self.next_send_metric = None
                stoptime = time.time() + timeout
            except Exception as e:
                LOG.error("Failed to send metrics: %s. "
                          "Wait 1 seconds for next try." % e)
                time.sleep(1)

    def send_metric(self, metric):
        s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
        s.connect(self.unix_socket)
        s.send(metric.to_json().encode())
        s.close()

    def put_rpc_client_metrics_to_txqueue(self, metric_name, action,
                                          target, method, call_type, timeout,
                                          exception=None):
        kwargs = {
            'call_type': call_type,
            'exchange': target.exchange,
            'topic': target.topic,
            'namespace': target.namespace,
            'version': target.version,
            'server': target.server,
            'fanout': target.fanout,
            'method': method,
            'timeout': timeout,
        }
        if exception:
            kwargs['exception'] = exception

        self.put_into_txqueue(metric_name, action, **kwargs)

    def rpc_client_invocation_start_total(self, target, method, call_type,
                                          timeout=None):
        self.put_rpc_client_metrics_to_txqueue(
            "rpc_client_invocation_start_total",
            message_type.MetricAction("inc", None),
            target, method, call_type, timeout
        )

    def rpc_client_invocation_end_total(self, target, method, call_type,
                                        timeout=None):
        self.put_rpc_client_metrics_to_txqueue(
            "rpc_client_invocation_end_total",
            message_type.MetricAction("inc", None),
            target, method, call_type, timeout
        )

    def rpc_client_processing_seconds(self, target, method, call_type,
                                      duration, timeout=None):
        self.put_rpc_client_metrics_to_txqueue(
            "rpc_client_processing_seconds",
            message_type.MetricAction("observe", duration),
            target, method, call_type, timeout
        )

    def rpc_client_exception_total(self, target, method, call_type, exception,
                                   timeout=None):
        self.put_rpc_client_metrics_to_txqueue(
            "rpc_client_exception_total",
            message_type.MetricAction("inc", None),
            target, method, call_type, timeout, exception
        )

    def put_rpc_server_metrics_to_txqueue(self, metric_name, action,
                                          target, endpoint, ns, ver, method,
                                          exception=None):
        kwargs = {
            'endpoint': endpoint,
            'namespace': ns,
            'version': ver,
            'method': method,
            'exchange': None,
            'topic': None,
            'server': None
        }
        if target:
            kwargs['exchange'] = target.exchange
            kwargs['topic'] = target.topic
            kwargs['server'] = target.server
        if exception:
            kwargs['exception'] = exception

        self.put_into_txqueue(metric_name, action, **kwargs)

    def rpc_server_invocation_start_total(self, target, endpoint,
                                          ns, ver, method):
        self.put_rpc_server_metrics_to_txqueue(
            "rpc_server_invocation_start_total",
            message_type.MetricAction("inc", None),
            target, endpoint, ns, ver, method
        )

    def rpc_server_invocation_end_total(self, target, endpoint,
                                        ns, ver, method):
        self.put_rpc_server_metrics_to_txqueue(
            "rpc_server_invocation_end_total",
            message_type.MetricAction("inc", None),
            target, endpoint, ns, ver, method
        )

    def rpc_server_processing_seconds(self, target, endpoint, ns, ver,
                                      method, duration):
        self.put_rpc_server_metrics_to_txqueue(
            "rpc_server_processing_seconds",
            message_type.MetricAction("observe", duration),
            target, endpoint, ns, ver, method
        )

    def rpc_server_exception_total(self, target, endpoint, ns, ver,
                                   method, exception):
        self.put_rpc_server_metrics_to_txqueue(
            "rpc_server_exception_total",
            message_type.MetricAction("inc", None),
            target, endpoint, ns, ver, method, exception=exception
        )


METRICS_COLLECTOR = None


def get_collector(conf, metrics_type, **kwargs):
    global threading
    threading = stdlib_threading
    global METRICS_COLLECTOR
    if METRICS_COLLECTOR is None:
        METRICS_COLLECTOR = MetricsCollectorClient(
            conf, metrics_type, **kwargs)
    return METRICS_COLLECTOR