Merge "Add noop metrics client"

This commit is contained in:
Jenkins 2016-11-03 17:41:53 +00:00 committed by Gerrit Code Review
commit 8b14ba68c6
5 changed files with 141 additions and 2 deletions

View File

@ -37,7 +37,11 @@ Usage examples:
"""
import monascastatsd
try:
import monascastatsd as stats_client
except Exception:
import designate.metrics_client.noop as stats_client
from oslo_config import cfg
from oslo_log import log as logging
@ -67,7 +71,7 @@ class Metrics(object):
"""Initialize Monasca-Statsd client with its default configuration.
Do not start sending metrics yet.
"""
self._client = monascastatsd.Client(dimensions={
self._client = stats_client.Client(dimensions={
'service_name': 'dns'
})
# cfg.CONF is not available at this time

View File

View File

@ -0,0 +1,90 @@
#
# Copyright (C) 2016 Red Hat, Inc.
#
# 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.
#
from designate.i18n import _LE
from oslo_log import log as logging
LOG = logging.getLogger(__name__)
class NoopConnection(object):
def __init__(self):
pass
def _flush_buffer(self):
pass
def close_buffer(self):
pass
def connect(self, *a, **kw):
LOG.error(_LE('Using noop metrics client. Metrics will be ignored.'))
pass
def open_buffer(self):
pass
class NoopCounter(object):
def __init__(self):
pass
def increment(self, *a, **kw):
pass
def decrement(self, *a, **kw):
pass
def __add__(self, value):
pass
def __sub__(self, value):
pass
class NoopGuage(object):
def __init__(self):
pass
def send(self, *a, **kw):
pass
class NoopTimer(object):
def __init__(self):
pass
def timed(self, *a, **kw):
pass
class Client(object):
def __init__(self, *a, **kw):
self._counter = NoopCounter()
self._guage = NoopGuage()
self._timer = NoopTimer()
self.connection = NoopConnection()
pass
def get_counter(self, *a, **kw):
return None
def get_guage(self, *a, **kw):
return None
def get_timer(self):
return None

View File

@ -0,0 +1,45 @@
#
# Copyright (C) 2016 Red Hat, Inc.
#
# 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 mock
from designate.metrics import Metrics
from designate.metrics_client import noop
from designate.tests import TestCase
from oslo_config import cfg
from oslo_config import fixture as cfg_fixture
class TestNoopMetrics(TestCase):
def setUp(self):
super(TestCase, self).setUp()
self.CONF = self.useFixture(cfg_fixture.Config(cfg.CONF)).conf
self.metrics = Metrics()
self.metrics._client = noop.Client()
def test_noop_metrics_enabled(self):
self.CONF.set_override('enabled', True, 'monasca:statsd')
with mock.patch('designate.metrics_client.noop.LOG') as log_mock:
self.metrics.init()
log_mock.error.assert_called_once_with(
"Using noop metrics client. Metrics will be ignored.")
def test_noop_metrics_disabled(self):
with mock.patch('designate.metrics_client.noop.LOG') as log_mock:
self.metrics.init()
log_mock.error.assert_not_called()