Add openstack alarm metrics command

This allows to retrieve metrics published on the v2/metrics
API endpoint created in https://review.opendev.org/c/openstack/aodh/+/925775
and extended in https://review.opendev.org/c/openstack/aodh/+/939754

Change-Id: Icec77554d4263dd575f7b8875b03ca300b643730
This commit is contained in:
Jaromir Wysoglad
2025-01-09 02:45:02 -05:00
parent 59a6f2e31c
commit 16fcf40058
10 changed files with 172 additions and 0 deletions

View File

@@ -29,6 +29,11 @@
zuul_copy_output:
/etc/prometheus/prometheus.yml: logs
/etc/openstack/prometheus.yaml: logs
devstack_local_conf:
post-config:
$AODH_CONF:
DEFAULT:
enable_evaluation_results_metrics: True
- project:
queue: telemetry

View File

@@ -29,6 +29,7 @@ from aodhclient import noauth
from aodhclient.v2 import alarm_cli
from aodhclient.v2 import alarm_history_cli
from aodhclient.v2 import capabilities_cli
from aodhclient.v2 import metrics_cli
class AodhCommandManager(commandmanager.CommandManager):
@@ -43,6 +44,7 @@ class AodhCommandManager(commandmanager.CommandManager):
"alarm-history show": alarm_history_cli.CliAlarmHistoryShow,
"alarm-history search": alarm_history_cli.CliAlarmHistorySearch,
"capabilities list": capabilities_cli.CliCapabilitiesList,
"alarm metrics": metrics_cli.CliMetrics,
}
def load_commands(self, namespace):

View File

@@ -28,6 +28,7 @@ class AodhClientTest(base.ClientTestBase):
self.aodh("help", params="alarm list")
self.aodh("help", params="alarm show")
self.aodh("help", params="alarm update")
self.aodh("help", params="alarm metrics")
def test_alarm_id_or_name_scenario(self):
def _test(name):

View File

@@ -0,0 +1,21 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from aodhclient.tests.functional import base
class MetricsTest(base.ClientTestBase):
def test_metrics_scenario(self):
# Test that the metrics command doesn't return errors
params = "metrics"
self.aodh('alarm', params=params)

View File

@@ -0,0 +1,65 @@
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import testtools
from unittest import mock
from aodhclient.v2 import metrics_cli
class MetricsTest(testtools.TestCase):
def setUp(self):
super(MetricsTest, self).setUp()
self.app = mock.Mock()
self.metrics_mgr_mock = self.app.client_manager.alarming.metrics
self.parser = mock.Mock()
self.metrics = (
metrics_cli.CliMetrics(self.app, self.parser))
def test_metrics(self):
self.metrics_mgr_mock.get.return_value = {
"evaluation_results": [
{
"alarm_id": "b8e17f58-089a-43fc-a96b-e9bcac4d4b53",
"project_id": "2dd8edd6c8c24f49bf04670534f6b357",
"state_counters": {
"insufficient data": 1646,
"ok": 0,
"alarm": 0
}
},
{
"alarm_id": "fa386719-67e3-42ff-aec8-17e547dac77a",
"project_id": "2dd8edd6c8c24f49bf04670534f6b357",
"state_counters": {
"insufficient data": 4,
"ok": 923,
"alarm": 0
}
},
{
"alarm_id": "0788bf8b-fc1f-4889-b5fd-5acd4d287bc3",
"project_id": "d45b070bcce04ca99546128a40854e7c",
"state_counters": {
"insufficient data": 1646,
"ok": 0,
"alarm": 0
}
}
]
}
ret = self.metrics.take_action([])
self.metrics_mgr_mock.get.assert_called_once_with()
self.assertIn('name', ret[0])
self.assertIn('evaluation_result', ret[1][0])

View File

@@ -17,6 +17,7 @@ from aodhclient import client
from aodhclient.v2 import alarm
from aodhclient.v2 import alarm_history
from aodhclient.v2 import capabilities
from aodhclient.v2 import metrics
from aodhclient.v2 import quota
@@ -35,3 +36,4 @@ class Client(object):
self.alarm_history = alarm_history.AlarmHistoryManager(self)
self.capabilities = capabilities.CapabilitiesManager(self)
self.quota = quota.QuotasManager(self)
self.metrics = metrics.MetricsManager(self)

26
aodhclient/v2/metrics.py Normal file
View File

@@ -0,0 +1,26 @@
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from aodhclient.v2 import base
class MetricsManager(base.Manager):
url = "v2/metrics"
def get(self, all_projects=False):
"""Get metrics"""
if all_projects:
return self._get(self.url + "?all_projects=true").json()
else:
return self._get(self.url).json()

View File

@@ -0,0 +1,45 @@
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from cliff import lister
from aodhclient import utils
METRIC_COLS = ["name", "labels", "value"]
class CliMetrics(lister.Lister):
"""Get Metrics"""
@staticmethod
def metrics2cols(metrics):
cols_data = []
if "evaluation_results" in metrics:
for alarm in metrics["evaluation_results"]:
for state, count in alarm["state_counters"].items():
# prometheus style labels
labels = ('alarm_id="{{{}}}", project_id="{{{}}}", '
'state="{{{}}}"').format(alarm['alarm_id'],
alarm['project_id'],
state)
cols_data.append(["evaluation_result", labels, count])
# Extend for other types of metrics here
return METRIC_COLS, cols_data
def get_parser(self, prog_name):
parser = super(CliMetrics, self).get_parser(prog_name)
return parser
def take_action(self, parsed_args):
metrics = utils.get_client(self).metrics.get()
return self.metrics2cols(metrics)

View File

@@ -0,0 +1,4 @@
---
features:
- |
Added a ``alarm metrics`` command to retrieve alarming metrics from the aodh /v2/metrics API endpoint.

View File

@@ -48,4 +48,5 @@ openstack.alarming.v2 =
alarming capabilities list = aodhclient.v2.capabilities_cli:CliCapabilitiesList
alarm quota show = aodhclient.v2.quota_cli:QuotaShow
alarm quota set = aodhclient.v2.quota_cli:QuotaSet
alarm metrics = aodhclient.v2.metrics_cli:CliMetrics