From 5dbd5cd61c18341ef20ad757facb7513fb764e43 Mon Sep 17 00:00:00 2001 From: Laurent VAYLET Date: Thu, 24 Mar 2022 21:27:55 +0000 Subject: [PATCH 1/4] use new convention from elasticsearch 8.x client --- slo_generator/backends/elasticsearch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slo_generator/backends/elasticsearch.py b/slo_generator/backends/elasticsearch.py index 2b5d4b0e..a1b77a98 100644 --- a/slo_generator/backends/elasticsearch.py +++ b/slo_generator/backends/elasticsearch.py @@ -38,7 +38,7 @@ class ElasticsearchBackend: def __init__(self, client=None, **es_config): self.client = client if self.client is None: - self.client = Elasticsearch(**es_config) + self.client = Elasticsearch(es_config['url']) # pylint: disable=unused-argument def good_bad_ratio(self, timestamp, window, slo_config): From ebd3343047a8e4fd4625424bd1abe0be44211d6f Mon Sep 17 00:00:00 2001 From: Laurent VAYLET Date: Sat, 26 Mar 2022 14:37:48 +0000 Subject: [PATCH 2/4] handle multiple connection setups --- slo_generator/backends/elasticsearch.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/slo_generator/backends/elasticsearch.py b/slo_generator/backends/elasticsearch.py index a1b77a98..7196c9e3 100644 --- a/slo_generator/backends/elasticsearch.py +++ b/slo_generator/backends/elasticsearch.py @@ -16,6 +16,7 @@ ElasticSearch backend implementation. """ +import copy import logging from elasticsearch import Elasticsearch @@ -38,7 +39,26 @@ class ElasticsearchBackend: def __init__(self, client=None, **es_config): self.client = client if self.client is None: - self.client = Elasticsearch(es_config['url']) + # Copy the given client configuration and process it to address + # multiple connection setups (such as Elastic Cloud, basic auth, + # multiple nodes, API token...) before actually instantiating the + # client. + # Note: `es_config.copy()` and `dict(es_config)` only make *shallow* + # copies. We require a full nested copy of the configuration to + # work on. + conf = copy.deepcopy(es_config) + url = conf.pop('url', None) + basic_auth = conf.pop('basic_auth', None) + api_key = conf.pop('api_key', None) + if url: + conf['hosts'] = url + if basic_auth: + conf['basic_auth'] = ( + basic_auth['username'], basic_auth['password']) + if api_key: + conf['api_key'] = (api_key['id'], api_key['value']) + # Mote: Either `hosts` or `cloud_id` must be specified in v8.x.x + self.client = Elasticsearch(**conf) # pylint: disable=unused-argument def good_bad_ratio(self, timestamp, window, slo_config): From 416d413538666abe1b8dd31355323bcd39bdd0e7 Mon Sep 17 00:00:00 2001 From: Laurent VAYLET Date: Sat, 26 Mar 2022 16:03:51 +0000 Subject: [PATCH 3/4] typo --- slo_generator/backends/elasticsearch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slo_generator/backends/elasticsearch.py b/slo_generator/backends/elasticsearch.py index 7196c9e3..36c6142c 100644 --- a/slo_generator/backends/elasticsearch.py +++ b/slo_generator/backends/elasticsearch.py @@ -57,7 +57,7 @@ def __init__(self, client=None, **es_config): basic_auth['username'], basic_auth['password']) if api_key: conf['api_key'] = (api_key['id'], api_key['value']) - # Mote: Either `hosts` or `cloud_id` must be specified in v8.x.x + # Note: Either `hosts` or `cloud_id` must be specified in v8.x.x self.client = Elasticsearch(**conf) # pylint: disable=unused-argument From 1da03ee4f6d5cc555cb7c7456afff8a20ddb0d07 Mon Sep 17 00:00:00 2001 From: Laurent VAYLET Date: Sun, 27 Mar 2022 14:41:35 +0000 Subject: [PATCH 4/4] update docs --- docs/providers/elasticsearch.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/docs/providers/elasticsearch.md b/docs/providers/elasticsearch.md index c30b1dbb..1f4e08b8 100644 --- a/docs/providers/elasticsearch.md +++ b/docs/providers/elasticsearch.md @@ -8,8 +8,24 @@ Elasticsearch to create an SLO. ```yaml backends: elasticsearch: - api_token: ${DYNATRACE_API_TOKEN} - api_url: ${DYNATRACE_API_URL} + url: ${ELASTICSEARCH_URL} +``` + +Note that `url` can be either a single string (when connecting to a single node) +or a list of strings (when connecting to multiple nodes): + +```yaml +backends: + elasticsearch: + url: https://localhost:9200 +``` + +```yaml +backends: + elasticsearch: + url: + - https://localhost:9200 + - https://localhost:9201 ``` The following methods are available to compute SLOs with the `elasticsearch`