-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move from django-kaneda project. * Add integration tests. * Improve Django documentation
- Loading branch information
Showing
14 changed files
with
196 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import logging | ||
|
||
from django.utils.functional import LazyObject | ||
|
||
|
||
class LazyMetrics(LazyObject): | ||
|
||
def _setup(self): | ||
from kaneda import Metrics | ||
from kaneda.utils import import_class, get_object_from_settings | ||
from kaneda.exceptions import UnexistingKanedaClass, SettingsError | ||
from . import settings | ||
|
||
if settings.DEBUG: | ||
backend_class = import_class('kaneda.backends.LoggerBackend') | ||
if settings.LOGGER: | ||
backend = backend_class(logger=logging.getLogger(settings.LOGGER)) | ||
elif settings.LOGGER_FILENAME: | ||
backend = backend_class(filename=settings.LOGGER_FILENAME) | ||
else: | ||
backend = backend_class() | ||
_metrics = Metrics(backend=backend) | ||
else: | ||
if not settings.BACKEND and not settings.QUEUE: | ||
raise SettingsError('You need to set KANEDA_BACKEND or KANEDA_QUEUE on settings.py to django_kaneda') | ||
if settings.BACKEND: | ||
try: | ||
backend = get_object_from_settings(settings.BACKEND, settings) | ||
_metrics = Metrics(backend=backend) | ||
except UnexistingKanedaClass: | ||
raise UnexistingKanedaClass('The selected KANEDA_BACKEND class does not exists.') | ||
if settings.QUEUE: | ||
try: | ||
queue = get_object_from_settings(settings.QUEUE, settings) | ||
_metrics = Metrics(queue=queue) | ||
except UnexistingKanedaClass: | ||
raise UnexistingKanedaClass('The selected KANEDA_QUEUE class does not exists.') | ||
self._wrapped = _metrics | ||
|
||
metrics = LazyMetrics() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from django.conf import settings | ||
|
||
BACKEND = getattr(settings, 'KANEDA_BACKEND', None) | ||
QUEUE = getattr(settings, 'KANEDA_QUEUE', None) | ||
|
||
# Elasticsearch backend settings | ||
ELASTIC_INDEX_NAME = getattr(settings, 'KANEDA_ELASTIC_INDEX_NAME', 'kaneda') | ||
ELASTIC_APP_NAME = getattr(settings, 'KANEDA_ELASTIC_APP_NAME', 'default') | ||
ELASTIC_CONNECTION_URL = getattr(settings, 'KANEDA_ELASTIC_CONNECTION_URL', None) | ||
ELASTIC_HOST = getattr(settings, 'KANEDA_ELASTIC_HOST', None) | ||
ELASTIC_PORT = getattr(settings, 'KANEDA_ELASTIC_PORT', None) | ||
ELASTIC_USER = getattr(settings, 'KANEDA_ELASTIC_USER', None) | ||
ELASTIC_PASSWORD = getattr(settings, 'KANEDA_ELASTIC_PASSWORD', None) | ||
ELASTIC_TIMEOUT = getattr(settings, 'KANEDA_ELASTIC_TIMEOUT', 0.3) | ||
|
||
# MongoDB backend settings | ||
MONGO_DB_NAME = getattr(settings, 'KANEDA_MONGO_DB_NAME', 'kaneda') | ||
MONGO_COLLECTION_NAME = getattr(settings, 'KANEDA_MONGO_COLLECTION_NAME', 'default') | ||
MONGO_CONNECTION_URL = getattr(settings, 'KANEDA_MONGO_CONNECTION_URL', None) | ||
MONGO_HOST = getattr(settings, 'KANEDA_MONGO_HOST', None) | ||
MONGO_PORT = getattr(settings, 'KANEDA_MONGO_PORT', None) | ||
MONGO_TIMEOUT = getattr(settings, 'KANEDA_MONGO_TIMEOUT', 300) | ||
|
||
# Debug backend mode settings | ||
DEBUG = getattr(settings, 'KANEDA_DEBUG', False) | ||
LOGGER = getattr(settings, 'KANEDA_LOGGER', None) | ||
LOGGER_FILENAME = getattr(settings, 'KANEDA_LOGGER_FILENAME', None) | ||
|
||
# Celery queue settings | ||
CELERY_BROKER = getattr(settings, 'KANEDA_CELERY_BROKER', '') | ||
CELERY_QUEUE_NAME = getattr(settings, 'KANEDA_CELERY_QUEUE_NAME', '') | ||
|
||
# RQ queue settings | ||
RQ_REDIS_URL = getattr(settings, 'KANEDA_RQ_REDIS_URL', 'kaneda') | ||
RQ_QUEUE_NAME = getattr(settings, 'KANEDA_RQ_QUEUE_NAME', None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,10 @@ Example:: | |
:hidden: | ||
|
||
usage | ||
django | ||
metrics | ||
backends | ||
queues | ||
settings | ||
django | ||
changelog | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
# -*- coding: utf-8 -*- | ||
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def django_settings_backend(elastic_settings): | ||
elastic_settings.DEBUG = False | ||
elastic_settings.QUEUE = None | ||
return elastic_settings | ||
|
||
|
||
@pytest.fixture | ||
def django_settings_debug(): | ||
class Settings: | ||
DEBUG = True | ||
LOGGER = None | ||
LOGGER_FILENAME = None | ||
|
||
return Settings | ||
|
||
|
||
@pytest.fixture | ||
def django_settings_queue(celery_settings): | ||
celery_settings.DEBUG = False | ||
celery_settings.BACKEND = None | ||
return celery_settings | ||
|
||
|
||
def pytest_configure(): | ||
from django.conf import settings | ||
settings.configure() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from kaneda.backends import LoggerBackend, ElasticsearchBackend | ||
from kaneda.queues import CeleryQueue | ||
from django_kaneda import settings | ||
|
||
|
||
class TestDjango(object): | ||
|
||
def test_django_kaneda_with_backend(self, mocker, django_settings_backend): | ||
mocker.patch('django_kaneda.settings', django_settings_backend) | ||
from django_kaneda import LazyMetrics | ||
metrics = LazyMetrics() | ||
assert isinstance(metrics.backend, ElasticsearchBackend) | ||
result = metrics.gauge('test_gauge', 42) | ||
assert result | ||
|
||
def test_django_kaneda_with_debug(self, mocker, django_settings_debug): | ||
mocker.patch('django_kaneda.settings', django_settings_debug) | ||
from django_kaneda import LazyMetrics | ||
metrics = LazyMetrics() | ||
metrics.gauge('test_gauge', 42) | ||
assert isinstance(metrics.backend, LoggerBackend) | ||
|
||
def test_django_kaneda_with_queue(self, mocker, django_settings_queue): | ||
mocker.patch('django_kaneda.settings', django_settings_queue) | ||
from django_kaneda import LazyMetrics | ||
metrics = LazyMetrics() | ||
assert isinstance(metrics.queue, CeleryQueue) | ||
result = metrics.gauge('test_gauge', 42) | ||
assert result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.