Example #1
0
def configure_metrics():
    # Check feature flag
    #if not settings.METRICS_ENABLED:
    #    return

    if False:
        elasticsearch = ElasticsearchBackend('localhost', index='metrics')
        settings.configure(backends=[elasticsearch])

    if True:
        metrics_backends = []
        async_es_metrics = ThreadedBackend(
            ElasticsearchBackend,
            backend_kwargs={
                'host': 'localhost',
                'port': '9200',
                #'url_prefix': settings.ELASTICSEARCH_PREFIX,
                #'use_ssl': settings.ELASTICSEARCH_SSL,
                #'verify_certs': settings.ELASTICSEARCH_VERIFY_CERTS,
                #'index': settings.ELASTICSEARCH_INDEX,
                #'http_auth': settings.ELASTICSEARCH_AUTH,
            },
        )
        metrics_backends.append(async_es_metrics)
        settings.configure(backends=metrics_backends,
                           hooks=[
                               status_code_hook,
                           ],
                           origin='inspire_next')
 def test_backend_args(self):
     self.MockedBackendClass.assert_called_with('arg1',
                                                'arg2',
                                                key1='kwarg1',
                                                key2='kwarg2')
     ThreadedBackend(self.MockedBackendClass)
     self.MockedBackendClass.assert_called_with()
 def test_backend_args(self):
     self.MockedBackendClass.assert_called_with("arg1",
                                                "arg2",
                                                key1="kwarg1",
                                                key2="kwarg2")
     ThreadedBackend(self.MockedBackendClass)
     self.MockedBackendClass.assert_called_with()
Example #4
0
    def configure_appmetrics(self, app):
        if not app.config.get('FEATURE_FLAG_ENABLE_APPMETRICS'):
            return

        if app.config['APPMETRICS_THREADED_BACKEND']:
            backend = ThreadedBackend(
                ElasticsearchBackend,
                backend_kwargs=dict(
                    hosts=app.config['APPMETRICS_ELASTICSEARCH_HOSTS'],
                    index=app.config['APPMETRICS_ELASTICSEARCH_INDEX']),
                lazy_init=True,
            )
        else:
            backend = ElasticsearchBackend(
                hosts=app.config['APPMETRICS_ELASTICSEARCH_HOSTS'],
                index=app.config['APPMETRICS_ELASTICSEARCH_INDEX'],
            )
        origin = 'inspire_next'

        hooks = [
            inspire_service_orcid_hooks.status_code_hook,
            inspire_service_orcid_hooks.orcid_error_code_hook,
            inspire_service_orcid_hooks.orcid_service_exception_hook,
            # Add other hooks here:
            exception_hook,
        ]
        time_execution.settings.configure(
            backends=[backend],
            hooks=hooks,
            origin=origin
        )
Example #5
0
    def configure_appmetrics(self, app):
        if not app.config.get("FEATURE_FLAG_ENABLE_APPMETRICS"):
            return

        if app.config["APPMETRICS_THREADED_BACKEND"]:
            backend = ThreadedBackend(
                ElasticsearchBackend,
                backend_kwargs=dict(
                    hosts=app.config["APPMETRICS_ELASTICSEARCH_HOSTS"],
                    index=app.config["APPMETRICS_ELASTICSEARCH_INDEX"],
                ),
            )
        else:
            backend = ElasticsearchBackend(
                hosts=app.config["APPMETRICS_ELASTICSEARCH_HOSTS"],
                index=app.config["APPMETRICS_ELASTICSEARCH_INDEX"],
            )
        origin = "inspirehep"
        hooks = [
            inspire_service_orcid_hooks.status_code_hook,
            inspire_service_orcid_hooks.orcid_error_code_hook,
            inspire_service_orcid_hooks.orcid_service_exception_hook,
        ]
        time_execution.settings.configure(
            backends=[backend], hooks=hooks, origin=origin
        )
 def setUp(self):
     self.qtime = 0.1
     self.backend = ThreadedBackend(
         elasticsearch.ElasticsearchBackend,
         backend_args=('elasticsearch', ),
         backend_kwargs=dict(index='threaded-metrics'),
         queue_timeout=self.qtime,
     )
     settings.configure(backends=[self.backend])
     self._clear(self.backend.backend)
    def setUp(self):
        self.qsize = 10
        self.qtimeout = 0.1

        self.mocked_backend = mock.Mock(
            spec=elasticsearch.ElasticsearchBackend)
        self.MockedBackendClass = mock.Mock(return_value=self.mocked_backend)

        self.backend = ThreadedBackend(
            self.MockedBackendClass,
            backend_args=('arg1', 'arg2'),
            backend_kwargs=dict(key1='kwarg1', key2='kwarg2'),
            queue_maxsize=self.qsize,
            queue_timeout=self.qtimeout,
        )
        self.backend.bulk_size = self.qsize / 2
        self.backend.bulk_timeout = self.qtimeout * 2
        settings.configure(backends=[self.backend])
Example #8
0
#!/usr/bin/env python
import os
import sys

# make sure we can import time_execution library
path = os.path.dirname(os.path.abspath(__file__))
sys.path.append("/".join(path.split("/")[:-1]))

from time_execution.backends.base import BaseMetricsBackend  # noqa isort:skip
from time_execution.backends.threaded import ThreadedBackend  # noqa isort:skip


class DummyBackend(BaseMetricsBackend):
    def write(self, name, **data):
        pass


ThreadedBackend(DummyBackend, queue_timeout=1)
    def test_backend_class(self):
        backend = ThreadedBackend(backend=elasticsearch.ElasticsearchBackend)

        assert isinstance(backend.backend, elasticsearch.ElasticsearchBackend)
 def test_backend_importpath_wrong_path(self):
     with pytest.raises(ImportError):
         ThreadedBackend(
             backend='time_execution.backends.wrong_path.NewBackend', )
    def test_backend_importpath(self, backend_string, expected_class):
        backend = ThreadedBackend(backend=backend_string, )

        assert isinstance(backend.backend, expected_class)
    def test_backend_importpath(self):
        backend = ThreadedBackend(
            backend="time_execution.backends.elasticsearch.ElasticsearchBackend"
        )

        assert isinstance(backend.backend, elasticsearch.ElasticsearchBackend)