Skip to content

Commit

Permalink
Set elasticsearch client version to >=7.17,<8.0.0
Browse files Browse the repository at this point in the history
The latest versions of the elasticsearch client have a compatibility
mode which can also be used for newer server versions; however, the other
way around does not work. Fixing the version to >=7.17,<8.0.0 is
therefore a save option for the near future.
This commit also includes a fix for the case where "resource_status" is
not part of `resource_attributes`, which occasionally caused crashes. If
this key is not present, `resource_status` will be set to an empty
string.
  • Loading branch information
stefan-k committed Apr 14, 2022
1 parent 1c30c48 commit 0eb31b1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def get_cryptography_version():
"cobald>=0.12.3",
"asyncssh",
"aiotelegraf",
"elasticsearch",
"elasticsearch>=7.17,<8.0.0",
"aioprometheus>=21.9.0",
"kubernetes_asyncio",
"pydantic",
Expand Down
6 changes: 4 additions & 2 deletions tardis/plugins/elasticsearchmonitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def __init__(self):
self._index = config.index
self._meta = getattr(config, "meta", "")

self._es = Elasticsearch([{"host": config.host, "port": config.port}])
self._es = Elasticsearch(
[{"scheme": "http", "host": config.host, "port": config.port}]
)

async def notify(self, state: State, resource_attributes: AttributeDict) -> None:
"""
Expand All @@ -47,7 +49,7 @@ async def notify(self, state: State, resource_attributes: AttributeDict) -> None
"state": str(state),
"meta": self._meta,
"timestamp": int(time() * 1000),
"resource_status": str(resource_attributes["resource_status"]),
"resource_status": str(resource_attributes.get("resource_status", "")),
}

await self.async_execute(document)
Expand Down
36 changes: 36 additions & 0 deletions tests/plugins_t/test_elasticsearchmonitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,39 @@ def test_notify(self):
id=f"{test_param.drone_uuid}-2",
index=f"{self.plugin._index}-{self.mock_datetime.now.return_value.strftime.return_value}", # noqa: B950
)

def test_notify_resource_status_missing(self):
test_param = AttributeDict(
site_name="test-site",
machine_type="test_machine_type",
created=datetime.now(),
updated=datetime.now(),
drone_uuid="test-drone",
)

test_param_ext = {
**test_param,
"state": str(CleanupState()),
"meta": self.plugin._meta,
"timestamp": int(self.mock_time.return_value * 1000),
"resource_status": "",
"revision": 2,
}

self.mock_elasticsearch.return_value.search.return_value = {
"hits": {"total": {"value": 2}}
}

run_async(
self.plugin.notify, state=CleanupState(), resource_attributes=test_param
)

self.mock_elasticsearch.return_value.search.assert_called_with(
index=f"{self.plugin._index}*",
body={"query": {"term": {"drone_uuid.keyword": test_param.drone_uuid}}},
)
self.mock_elasticsearch.return_value.create.assert_called_with(
body=test_param_ext,
id=f"{test_param.drone_uuid}-2",
index=f"{self.plugin._index}-{self.mock_datetime.now.return_value.strftime.return_value}", # noqa: B950
)

0 comments on commit 0eb31b1

Please sign in to comment.