Skip to content

Commit

Permalink
fix: make unit tests pass again with elasticsearch 8.x client (#223)
Browse files Browse the repository at this point in the history
* use new convention from elasticsearch 8.x client

* handle multiple connection setups

* typo

* update docs
  • Loading branch information
lvaylet authored Mar 28, 2022
1 parent 83c36b9 commit 39dd26c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
20 changes: 18 additions & 2 deletions docs/providers/elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
22 changes: 21 additions & 1 deletion slo_generator/backends/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ElasticSearch backend implementation.
"""

import copy
import logging

from elasticsearch import Elasticsearch
Expand All @@ -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)
# 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'])
# Note: 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):
Expand Down

0 comments on commit 39dd26c

Please sign in to comment.