Browse Source

Make statsd self-monitoring optional

Story: 2001873
Task: 14333

Change-Id: I35eaa298f1153e6b71a5bfe33a894adec6b593a3
Martin Chacon Piza 1 year ago
parent
commit
cdb2e4398d
2 changed files with 55 additions and 5 deletions
  1. 53
    5
      monasca_notification/common/utils.py
  2. 2
    0
      monasca_notification/conf/statsd.py

+ 53
- 5
monasca_notification/common/utils.py View File

@@ -79,9 +79,57 @@ def grab_stored_notification_method(db_repo, notification_id):
79 79
 def get_statsd_client(dimensions=None):
80 80
     local_dims = dimensions.copy() if dimensions else {}
81 81
     local_dims.update(NOTIFICATION_DIMENSIONS)
82
-    client = monascastatsd.Client(name='monasca',
83
-                                  host=CONF.statsd.host,
84
-                                  port=CONF.statsd.port,
85
-                                  dimensions=local_dims)
86
-
82
+    if CONF.statsd.enable:
83
+        LOG.debug("Establishing connection with statsd on {0}:{1}"
84
+                  .format(CONF.statsd.host, CONF.statsd.port))
85
+        client = monascastatsd.Client(name='monasca',
86
+                                      host=CONF.statsd.host,
87
+                                      port=CONF.statsd.port,
88
+                                      dimensions=local_dims)
89
+    else:
90
+        LOG.warn("StatsD monitoring disabled. Overriding monascastatsd.Client to use it offline")
91
+        client = OfflineClient(name='monasca',
92
+                               host=CONF.statsd.host,
93
+                               port=CONF.statsd.port,
94
+                               dimensions=local_dims)
87 95
     return client
96
+
97
+
98
+class OfflineClient(monascastatsd.Client):
99
+
100
+    def _set_connection(self, connection, host, port):
101
+        if connection is None:
102
+            self.connection = OfflineConnection(host=host,
103
+                                                port=port,
104
+                                                max_buffer_size=self._max_buffer_size)
105
+        else:
106
+            self.connection = connection
107
+
108
+
109
+class OfflineConnection(monascastatsd.Connection):
110
+
111
+    def __init__(self, host='localhost', port=8125, max_buffer_size=50):
112
+        """Initialize an Offline Connection object.
113
+
114
+        >>> monascastatsd = MonascaStatsd()
115
+
116
+        :name: the name for this client.  Everything sent by this client
117
+            will be prefixed by name
118
+        :param host: the host of the MonascaStatsd server.
119
+        :param port: the port of the MonascaStatsd server.
120
+        :param max_buffer_size: Maximum number of metric to buffer before
121
+         sending to the server if sending metrics in batch
122
+        """
123
+        self.max_buffer_size = max_buffer_size
124
+        self._send = self._send_to_server
125
+        self.connect(host, port)
126
+        self.encoding = 'utf-8'
127
+
128
+    def connect(self, host, port):
129
+        """Avoid connecting to the monascastatsd server.
130
+
131
+        """
132
+        pass
133
+
134
+    def _send_to_server(self, packet):
135
+        pass

+ 2
- 0
monasca_notification/conf/statsd.py View File

@@ -24,6 +24,8 @@ statsd_group = cfg.OptGroup('statsd',
24 24
                             'to statsd server launched by monasca-agent.')
25 25
 
26 26
 statsd_opts = [
27
+    cfg.BoolOpt('enable', default=True,
28
+                help='Enable or disable self monitoring.'),
27 29
     cfg.HostAddressOpt('host', default=_DEFAULT_HOST,
28 30
                        help='IP address of statsd server.'),
29 31
     cfg.PortOpt('port', default=_DEFAULT_PORT, help='Port of statsd server.'),

Loading…
Cancel
Save