Skip to content

Commit

Permalink
Merge pull request #51 from beerdeaap/master
Browse files Browse the repository at this point in the history
Add flag to skip index + mapping creation
  • Loading branch information
kammala authored Jan 4, 2021
2 parents d5d363a + 6faa1fa commit d5f9ee1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,13 @@ There is a Makefile with a few targets that we use often:

``make test`` command will run tests for the python versions specified in ``tox.ini`` spinning up all necessary services via docker.

In some cases (on Ubuntu 18.04) the Elasticsearch Docker image might not be able to start and will exit with the following error::

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
This can be solved by adding the following line to `/etc/sysctl.conf`::

vm.max_map_count=262144

.. _Grafana: http://grafana.org/
.. _Kibana: https://www.elastic.co/products/kibana
Expand Down
9 changes: 8 additions & 1 deletion tests/test_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class TestConnectionErrors(TestBaseBackend):
@mock.patch("time_execution.backends.elasticsearch.logger")
def test_error_resilience(self, mocked_logger):
backend = ElasticsearchBackend(hosts=["non-existant-domain"])
backend = ElasticsearchBackend(hosts=["non-existent-domain"], max_retries=1)
# ensure index and mapping setup failures are caught and logged
self.assertEqual(2, len(mocked_logger.error.call_args_list))
# ensure write failure is caught and logged
Expand Down Expand Up @@ -145,3 +145,10 @@ def test_bulk_write_error(self, mocked_logger):
with es_index_error_ctx:
self.backend.bulk_write(metrics)
mocked_logger.warning.assert_called_once_with("bulk_write metrics %r failure %r", metrics, transport_error)

@mock.patch("time_execution.backends.elasticsearch.ElasticsearchBackend._setup_mapping")
@mock.patch("time_execution.backends.elasticsearch.ElasticsearchBackend._setup_index")
def test_do_not_create_index(self, setup_index, setup_mapping):
ElasticsearchBackend(ELASTICSEARCH_HOST, index="unittest", create_index=False)
setup_index.assert_not_called()
setup_mapping.assert_not_called()
27 changes: 17 additions & 10 deletions time_execution/backends/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@

class ElasticsearchBackend(BaseMetricsBackend):
def __init__(
self, hosts=None, index="metrics", doc_type="metric", index_pattern="{index}-{date:%Y.%m.%d}", *args, **kwargs
self,
hosts=None,
index="metrics",
doc_type="metric",
index_pattern="{index}-{date:%Y.%m.%d}",
create_index=True,
*args,
**kwargs,
):
# Assign these in the backend as they are needed when writing metrics
# to elasticsearch
Expand All @@ -22,15 +29,15 @@ def __init__(
# setup the client
self.client = Elasticsearch(hosts=hosts, *args, **kwargs)

# ensure the index is created
try:
self._setup_index()
except TransportError as exc:
logger.error("index setup error %r", exc)
try:
self._setup_mapping()
except TransportError as exc:
logger.error("mapping setup error %r", exc)
if create_index:
try:
self._setup_index()
except TransportError as exc:
logger.error("index setup error %r", exc)
try:
self._setup_mapping()
except TransportError as exc:
logger.error("mapping setup error %r", exc)

def get_index(self):
return self.index_pattern.format(index=self.index, date=datetime.now())
Expand Down

0 comments on commit d5f9ee1

Please sign in to comment.