class TestMockConfiguration(object):

    namespace = 'UniqueNamespaceForMockConfigurationTesting'
    getters = staticconf.NamespaceGetters(namespace)

    def test_mock_configuration_context_manager(self):
        one = self.getters.get('one')
        three = self.getters.get_int('three', default=3)

        with testing.MockConfiguration(dict(one=7), namespace=self.namespace):
            assert_equal(one, 7)
            assert_equal(three, 3)
        assert_raises(errors.ConfigurationError, self.getters.get('one'))

    def test_mock_configuration(self):
        two = self.getters.get_string('two')
        stars = self.getters.get_bool('stars')

        mock_config = testing.MockConfiguration(dict(two=2, stars=False),
                                                namespace=self.namespace)
        mock_config.setup()
        assert_equal(two, '2')
        assert not stars
        mock_config.teardown()
        assert_raises(errors.ConfigurationError, self.getters.get('two'))
Exemple #2
0
    def test_config_modules(self, datafile_path):
        module_1_namespace = 'module1'
        module_1_getter = staticconf.NamespaceGetters(module_1_namespace)
        module_2_namespace = 'module2'
        module_2_getter = staticconf.NamespaceGetters(module_2_namespace)
        module_3_namespace = 'module3'
        module_3_getter = staticconf.NamespaceGetters(module_3_namespace)

        configs = [
            dict(namespace=module_1_namespace, config=dict(key='val')),
            dict(namespace=module_2_namespace,
                 config=dict(key1='val3'),
                 file=datafile_path),
            dict(initialize='%s.initialize_module_3' % __name__)
        ]

        config_util.configure_packages(configs)

        assert module_1_getter.get('key') == 'val'
        assert module_2_getter.get('key1') == 'val3'
        assert module_2_getter.get('key2') == 'val2'
        assert module_3_getter.get('key') == 'Number 3 lives'
class SomeClass(object):

    namespace = 'UniqueNamespaceForEndToEndTesting'
    alt_name = 'UniqueNamespaceForEndToEndTestingAlternative'

    getters = staticconf.NamespaceGetters(namespace)
    max = getters.get_int('SomeClass.max')
    min = getters.get_int('SomeClass.min')
    ratio = getters.get_float('SomeClass.ratio')
    alt_ratio = getters.get_float('SomeClass.alt_ratio', 6.0)
    msg = getters.get_string('SomeClass.msg', None)

    real_max = staticconf.get_int('SomeClass.max', namespace=alt_name)
    real_min = staticconf.get_int('SomeClass.min', namespace=alt_name)
Exemple #4
0
class JsonLogger(object):

    event_data = dict()

    NAMESPACE = 'clog'
    DEFAULT_LOG_STREAM_NAME = 'tmp_batch_logfeeder'
    clog_namespace = staticconf.NamespaceGetters(NAMESPACE)
    log_stream_name = clog_namespace.get_string(
        'log_stream_name', default=DEFAULT_LOG_STREAM_NAME)

    @classmethod
    def init_logger(cls, **event_data):
        """Initializes the logger with a set of event parameters that will be
        used in the subsequent `log_event()` calls when logging a specific
        event.
        """
        cls.event_data = event_data

    @classmethod
    def log_event(cls, event_name, **event_data):
        """Logs `items` dictionary as a JSON object into the default log.
        It also adds the timestamp in ISO 8601 format in the `timestamp` field.

        :param event_name: The name of the event to log.
        :type event_name: str
        :param event_data: A dictionary containing additional parameters
                           associated with the event.
        :type event_data: dict
        """
        event_data['event'] = event_name
        event_data['timestamp'] = current_time_in_iso_8601()

        # merge class event_data into the actual event_data
        event_data.update(cls.event_data)
        json_event_data = simplejson.dumps(event_data)
        logging.info({cls.log_stream_name.value: json_event_data})
Exemple #5
0
from __future__ import unicode_literals

import logging
import os

import clog
import clog.handlers
import staticconf

from replication_handler.servlib.logging_util import DETAILED_FORMAT

_current_pid = None


namespace = 'clog'
clog_namespace = staticconf.NamespaceGetters(namespace)


log_stream_name = clog_namespace.get_string('log_stream_name')
log_stream_format = clog_namespace.get_string(
    'log_stream_format', default=DETAILED_FORMAT
)
log_stream_level = clog_namespace.get_string(
    'log_stream_level', default='INFO'
)


def initialize():
    """Initialize clog from staticconf config."""
    add_clog_handler(
        name=log_stream_name.value,
Exemple #6
0
from __future__ import absolute_import
from __future__ import unicode_literals

import collections
import re

import staticconf
import staticconf.errors
import staticconf.getters

CONFIG_NAMESPACE = 'metric_config'
metric_config_getter = staticconf.NamespaceGetters(CONFIG_NAMESPACE)


class Group(
        collections.namedtuple(
            'Group',
            ['name', 'metrics', 'metric_expressions'],
        )):
    __slots__ = ()

    def contains(self, metric_name):
        return (metric_name in self.metrics or any(
            expr.search(metric_name) for expr in self.metric_expressions))

    @classmethod
    def from_yaml(cls, name, metrics, metric_expressions):
        if not metrics and not metric_expressions:
            raise staticconf.errors.ValidationError(
                'Group {0} must define at least one of '
                '`metrics` or `metric_expressions`'.format(name))
Exemple #7
0
#
#     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 logging
import socket

import clog.handlers
import staticconf

namespace = 'clog'
clog_namespace = staticconf.NamespaceGetters(namespace)  # type: ignore
DETAILED_FORMAT = '\t'.join([
    '%(asctime)s',
    socket.gethostname(), '%(process)s', '%(name)s', '%(levelname)s',
    '%(message)s'
])

log_stream_name = clog_namespace.get_string('log_stream_name')
log_stream_format = clog_namespace.get_string('log_stream_format',
                                              default=DETAILED_FORMAT)
log_stream_level = clog_namespace.get_string('log_stream_level',
                                             default='INFO')
enable_uwsgi_mule_offload = clog_namespace.get_bool(
    'enable_uwsgi_mule_offload', default=False)