python-observabilityclient 0.4.0__py3-none-any.whl → 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- observabilityclient/prometheus_client.py +18 -7
- observabilityclient/{v1/rbac.py → rbac.py} +58 -52
- observabilityclient/tests/functional/test_cli.py +39 -81
- observabilityclient/tests/functional/test_python_api.py +2 -2
- observabilityclient/tests/unit/test_cli.py +6 -4
- observabilityclient/tests/unit/test_prometheus_client.py +7 -4
- observabilityclient/tests/unit/test_python_api.py +25 -23
- observabilityclient/tests/unit/test_rbac.py +125 -82
- observabilityclient/tests/unit/test_utils.py +77 -6
- observabilityclient/utils/metric_utils.py +39 -5
- observabilityclient/v1/base.py +21 -2
- observabilityclient/v1/client.py +9 -4
- observabilityclient/v1/python_api.py +13 -9
- {python_observabilityclient-0.4.0.dist-info → python_observabilityclient-1.0.0.dist-info}/AUTHORS +2 -0
- {python_observabilityclient-0.4.0.dist-info → python_observabilityclient-1.0.0.dist-info}/METADATA +1 -1
- python_observabilityclient-1.0.0.dist-info/RECORD +31 -0
- {python_observabilityclient-0.4.0.dist-info → python_observabilityclient-1.0.0.dist-info}/WHEEL +1 -1
- python_observabilityclient-1.0.0.dist-info/pbr.json +1 -0
- python_observabilityclient-0.4.0.dist-info/RECORD +0 -31
- python_observabilityclient-0.4.0.dist-info/pbr.json +0 -1
- {python_observabilityclient-0.4.0.dist-info → python_observabilityclient-1.0.0.dist-info}/LICENSE +0 -0
- {python_observabilityclient-0.4.0.dist-info → python_observabilityclient-1.0.0.dist-info}/entry_points.txt +0 -0
- {python_observabilityclient-0.4.0.dist-info → python_observabilityclient-1.0.0.dist-info}/top_level.txt +0 -0
|
@@ -53,10 +53,18 @@ class PrometheusMetric(object):
|
|
|
53
53
|
|
|
54
54
|
|
|
55
55
|
class PrometheusAPIClient(object):
|
|
56
|
-
def __init__(self, host):
|
|
56
|
+
def __init__(self, host, session=None, root_path=""):
|
|
57
57
|
self._host = host
|
|
58
|
-
|
|
58
|
+
if not self._host.endswith('/'):
|
|
59
|
+
self._host += '/'
|
|
60
|
+
if session is None:
|
|
61
|
+
self._session = requests.Session()
|
|
62
|
+
else:
|
|
63
|
+
self._session = session
|
|
59
64
|
self._session.verify = False
|
|
65
|
+
self._root_path = root_path
|
|
66
|
+
if root_path != "" and not self._root_path.endswith('/'):
|
|
67
|
+
self._root_path += '/'
|
|
60
68
|
|
|
61
69
|
def set_ca_cert(self, ca_cert):
|
|
62
70
|
self._session.verify = ca_cert
|
|
@@ -68,11 +76,15 @@ class PrometheusAPIClient(object):
|
|
|
68
76
|
def set_basic_auth(self, auth_user, auth_password):
|
|
69
77
|
self._session.auth = (auth_user, auth_password)
|
|
70
78
|
|
|
79
|
+
def _get_url(self, endpoint):
|
|
80
|
+
scheme = 'https' if self._session.verify else 'http'
|
|
81
|
+
return f"{scheme}://{self._host}{self._root_path}api/v1/{endpoint}"
|
|
82
|
+
|
|
71
83
|
def _get(self, endpoint, params=None):
|
|
72
|
-
url =
|
|
73
|
-
f"{self._host}/api/v1/{endpoint}")
|
|
84
|
+
url = self._get_url(endpoint)
|
|
74
85
|
resp = self._session.get(url, params=params,
|
|
75
|
-
headers={'Accept': 'application/json'
|
|
86
|
+
headers={'Accept': 'application/json',
|
|
87
|
+
'Accept-Encoding': 'identity'})
|
|
76
88
|
if resp.status_code != requests.codes.ok:
|
|
77
89
|
raise PrometheusAPIClientError(resp)
|
|
78
90
|
decoded = resp.json()
|
|
@@ -82,8 +94,7 @@ class PrometheusAPIClient(object):
|
|
|
82
94
|
return decoded
|
|
83
95
|
|
|
84
96
|
def _post(self, endpoint, params=None):
|
|
85
|
-
url =
|
|
86
|
-
f"{self._host}/api/v1/{endpoint}")
|
|
97
|
+
url = self._get_url(endpoint)
|
|
87
98
|
resp = self._session.post(url, params=params,
|
|
88
99
|
headers={'Accept': 'application/json'})
|
|
89
100
|
if resp.status_code != requests.codes.ok:
|
|
@@ -14,32 +14,15 @@
|
|
|
14
14
|
|
|
15
15
|
import re
|
|
16
16
|
|
|
17
|
-
from keystoneauth1.exceptions.auth_plugins import MissingAuthPlugin
|
|
18
|
-
|
|
19
17
|
from observabilityclient.utils.metric_utils import format_labels
|
|
20
18
|
|
|
21
19
|
|
|
22
|
-
class
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
self.client = client
|
|
29
|
-
self.session = session
|
|
30
|
-
self.disable_rbac = disable_rbac
|
|
31
|
-
try:
|
|
32
|
-
self.project_id = self.session.get_project_id()
|
|
33
|
-
self.default_labels = {
|
|
34
|
-
"project": self.project_id
|
|
35
|
-
}
|
|
36
|
-
self.rbac_init_successful = True
|
|
37
|
-
except MissingAuthPlugin:
|
|
38
|
-
self.project_id = None
|
|
39
|
-
self.default_labels = {
|
|
40
|
-
"project": "no-project"
|
|
41
|
-
}
|
|
42
|
-
self.rbac_init_successful = False
|
|
20
|
+
class PromQLRbac(object):
|
|
21
|
+
def __init__(self, prom_api_client, project_id):
|
|
22
|
+
self.client = prom_api_client
|
|
23
|
+
self.labels = {
|
|
24
|
+
"project": project_id
|
|
25
|
+
}
|
|
43
26
|
|
|
44
27
|
def _find_label_value_end(self, query, start, quote_char):
|
|
45
28
|
end = start
|
|
@@ -71,6 +54,10 @@ class Rbac(object):
|
|
|
71
54
|
return end
|
|
72
55
|
|
|
73
56
|
def _find_label_section_end(self, query, start):
|
|
57
|
+
if query[start:].startswith('{}'):
|
|
58
|
+
# We have an empty label section, without any
|
|
59
|
+
# label pair
|
|
60
|
+
return start + 1
|
|
74
61
|
nearest_curly_brace_pos = None
|
|
75
62
|
while nearest_curly_brace_pos != -1:
|
|
76
63
|
pair_end = self._find_label_pair_end(query, start)
|
|
@@ -84,23 +71,26 @@ class Rbac(object):
|
|
|
84
71
|
# and the "=" or "~" is a part of the next section.
|
|
85
72
|
return nearest_curly_brace_pos
|
|
86
73
|
start = pair_end
|
|
74
|
+
# TODO(tkajinam): We should probably raise an exception here because
|
|
75
|
+
# this indicates illegal format without closing } .
|
|
87
76
|
return -1
|
|
88
77
|
|
|
89
|
-
def
|
|
90
|
-
""
|
|
78
|
+
def _insert_labels(self, query, location, comma=False, braces=False):
|
|
79
|
+
comma_str = ", " if comma else ""
|
|
80
|
+
formatted_labels = format_labels(self.labels)
|
|
81
|
+
labels_str = f"{{{formatted_labels}}}" if braces else formatted_labels
|
|
82
|
+
return (f"{query[:location]}{comma_str}"
|
|
83
|
+
f"{labels_str}"
|
|
84
|
+
f"{query[location:]}")
|
|
85
|
+
|
|
86
|
+
def modify_query(self, query):
|
|
87
|
+
"""Add rbac labels to a query.
|
|
91
88
|
|
|
92
|
-
:param query: The query to
|
|
89
|
+
:param query: The query to modify
|
|
93
90
|
:type query: str
|
|
94
|
-
:param disable_rbac: Disables rbac injection if set to True
|
|
95
|
-
:type disable_rbac: boolean
|
|
96
91
|
"""
|
|
97
|
-
# TODO(jwysogla): This should be properly tested
|
|
98
|
-
if disable_rbac:
|
|
99
|
-
return query
|
|
100
|
-
labels = self.default_labels
|
|
101
|
-
|
|
102
92
|
# We need to get all metric names, no matter the rbac
|
|
103
|
-
metric_names = self.client.
|
|
93
|
+
metric_names = self.client.label_values("__name__")
|
|
104
94
|
|
|
105
95
|
# We need to detect the locations of metric names
|
|
106
96
|
# inside the query
|
|
@@ -117,35 +107,51 @@ class Rbac(object):
|
|
|
117
107
|
name_end_locations.append(potential_name.end())
|
|
118
108
|
|
|
119
109
|
name_end_locations = sorted(name_end_locations, reverse=True)
|
|
110
|
+
if len(name_end_locations) == 0:
|
|
111
|
+
name_end_locations = [0]
|
|
120
112
|
for name_end_location in name_end_locations:
|
|
121
113
|
if (name_end_location < len(query) and
|
|
122
114
|
query[name_end_location] == "{"):
|
|
123
|
-
# There already
|
|
124
|
-
labels_end = self._find_label_section_end(
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
115
|
+
# There is already a label section
|
|
116
|
+
labels_end = self._find_label_section_end(
|
|
117
|
+
query,
|
|
118
|
+
name_end_location
|
|
119
|
+
)
|
|
120
|
+
if labels_end == name_end_location + 1:
|
|
121
|
+
query = self._insert_labels(
|
|
122
|
+
query,
|
|
123
|
+
labels_end,
|
|
124
|
+
comma=False,
|
|
125
|
+
braces=False
|
|
126
|
+
)
|
|
127
|
+
else:
|
|
128
|
+
query = self._insert_labels(
|
|
129
|
+
query,
|
|
130
|
+
labels_end,
|
|
131
|
+
comma=True,
|
|
132
|
+
braces=False
|
|
133
|
+
)
|
|
129
134
|
else:
|
|
130
|
-
query = (
|
|
131
|
-
|
|
132
|
-
|
|
135
|
+
query = self._insert_labels(
|
|
136
|
+
query,
|
|
137
|
+
name_end_location,
|
|
138
|
+
comma=False,
|
|
139
|
+
braces=True
|
|
140
|
+
)
|
|
133
141
|
return query
|
|
134
142
|
|
|
135
|
-
def
|
|
143
|
+
def append_rbac_labels(self, query):
|
|
136
144
|
"""Append rbac labels to queries.
|
|
137
145
|
|
|
138
|
-
It's a simplified and faster version of
|
|
146
|
+
It's a simplified and faster version of modify_query(). This just
|
|
139
147
|
appends the labels at the end of the query string. For proper handling
|
|
140
148
|
of complex queries, where metric names might occure elsewhere than
|
|
141
|
-
just at the end, please use the
|
|
149
|
+
just at the end, please use the modify_query() function.
|
|
142
150
|
|
|
143
151
|
:param query: The query to append to
|
|
144
152
|
:type query: str
|
|
145
|
-
:param disable_rbac: Disables rbac injection if set to True
|
|
146
|
-
:type disable_rbac: boolean
|
|
147
153
|
"""
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
154
|
+
if any(c in query for c in "{}"):
|
|
155
|
+
return self.modify_query(query)
|
|
156
|
+
else:
|
|
157
|
+
return f"{query}{{{format_labels(self.labels)}}}"
|
|
@@ -14,98 +14,56 @@ from observabilityclient.tests.functional import base
|
|
|
14
14
|
import time
|
|
15
15
|
|
|
16
16
|
|
|
17
|
-
class
|
|
18
|
-
"""Functional tests for cli commands
|
|
17
|
+
class CliTestFunctionalRBACNOOP(base.CliTestCase):
|
|
18
|
+
"""Functional tests for cli commands testing RBAC option."""
|
|
19
19
|
|
|
20
20
|
def test_list(self):
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
name_list = [item.get('metric_name') for item in cmd_output]
|
|
26
|
-
self.assertIn(
|
|
27
|
-
'up',
|
|
28
|
-
name_list
|
|
29
|
-
)
|
|
30
|
-
|
|
31
|
-
def test_show(self):
|
|
32
|
-
cmd_output = self.openstack(
|
|
33
|
-
'metric show up --disable-rbac',
|
|
34
|
-
parse_output=True,
|
|
35
|
-
)
|
|
36
|
-
for metric in cmd_output:
|
|
37
|
-
self.assertEqual(
|
|
38
|
-
"up",
|
|
39
|
-
metric["__name__"]
|
|
21
|
+
for cmdstr in ('metric list', 'metric list --disable-rbac'):
|
|
22
|
+
cmd_output = self.openstack(
|
|
23
|
+
cmdstr,
|
|
24
|
+
parse_output=True,
|
|
40
25
|
)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
26
|
+
name_list = [item.get('metric_name') for item in cmd_output]
|
|
27
|
+
self.assertIn(
|
|
28
|
+
'ceilometer_image_size',
|
|
29
|
+
name_list
|
|
44
30
|
)
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
'metric query up --disable-rbac',
|
|
49
|
-
parse_output=True,
|
|
50
|
-
)
|
|
51
|
-
for metric in cmd_output:
|
|
52
|
-
self.assertEqual(
|
|
53
|
-
"up",
|
|
54
|
-
metric["__name__"]
|
|
55
|
-
)
|
|
56
|
-
self.assertEqual(
|
|
57
|
-
"1",
|
|
58
|
-
metric["value"]
|
|
31
|
+
self.assertIn(
|
|
32
|
+
'up',
|
|
33
|
+
name_list
|
|
59
34
|
)
|
|
60
35
|
|
|
61
|
-
|
|
62
|
-
class CliTestFunctionalRBACEnabled(base.CliTestCase):
|
|
63
|
-
"""Functional tests for cli commands with enabled RBAC."""
|
|
64
|
-
|
|
65
|
-
def test_list(self):
|
|
66
|
-
cmd_output = self.openstack(
|
|
67
|
-
'metric list',
|
|
68
|
-
parse_output=True,
|
|
69
|
-
)
|
|
70
|
-
name_list = [item.get('metric_name') for item in cmd_output]
|
|
71
|
-
self.assertIn(
|
|
72
|
-
'ceilometer_image_size',
|
|
73
|
-
name_list
|
|
74
|
-
)
|
|
75
|
-
self.assertNotIn(
|
|
76
|
-
'up',
|
|
77
|
-
name_list
|
|
78
|
-
)
|
|
79
|
-
|
|
80
36
|
def test_show(self):
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
for metric in cmd_output:
|
|
86
|
-
self.assertEqual(
|
|
87
|
-
"ceilometer_image_size",
|
|
88
|
-
metric["__name__"]
|
|
89
|
-
)
|
|
90
|
-
self.assertEqual(
|
|
91
|
-
"sg-core",
|
|
92
|
-
metric["job"]
|
|
37
|
+
for cmdstr in ('metric show up', 'metric show up --disable-rbac'):
|
|
38
|
+
cmd_output = self.openstack(
|
|
39
|
+
cmdstr,
|
|
40
|
+
parse_output=True,
|
|
93
41
|
)
|
|
42
|
+
for metric in cmd_output:
|
|
43
|
+
self.assertEqual(
|
|
44
|
+
"up",
|
|
45
|
+
metric["__name__"]
|
|
46
|
+
)
|
|
47
|
+
self.assertEqual(
|
|
48
|
+
"1",
|
|
49
|
+
metric["value"]
|
|
50
|
+
)
|
|
94
51
|
|
|
95
52
|
def test_query(self):
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
for metric in cmd_output:
|
|
101
|
-
self.assertEqual(
|
|
102
|
-
"ceilometer_image_size",
|
|
103
|
-
metric["__name__"]
|
|
104
|
-
)
|
|
105
|
-
self.assertEqual(
|
|
106
|
-
"sg-core",
|
|
107
|
-
metric["job"]
|
|
53
|
+
for cmdstr in ('metric query up', 'metric query up --disable-rbac'):
|
|
54
|
+
cmd_output = self.openstack(
|
|
55
|
+
cmdstr,
|
|
56
|
+
parse_output=True,
|
|
108
57
|
)
|
|
58
|
+
for metric in cmd_output:
|
|
59
|
+
self.assertEqual(
|
|
60
|
+
"up",
|
|
61
|
+
metric["__name__"]
|
|
62
|
+
)
|
|
63
|
+
self.assertEqual(
|
|
64
|
+
"1",
|
|
65
|
+
metric["value"]
|
|
66
|
+
)
|
|
109
67
|
|
|
110
68
|
|
|
111
69
|
class CliTestFunctionalAdminCommands(base.CliTestCase):
|
|
@@ -52,7 +52,7 @@ class PythonAPITestFunctionalRBACEnabled(base.PythonAPITestCase):
|
|
|
52
52
|
for metric in ret:
|
|
53
53
|
self.assertEqual("ceilometer_image_size",
|
|
54
54
|
metric.labels["__name__"])
|
|
55
|
-
self.assertEqual("
|
|
55
|
+
self.assertEqual("custom",
|
|
56
56
|
metric.labels["job"])
|
|
57
57
|
|
|
58
58
|
def test_query(self):
|
|
@@ -62,7 +62,7 @@ class PythonAPITestFunctionalRBACEnabled(base.PythonAPITestCase):
|
|
|
62
62
|
for metric in ret:
|
|
63
63
|
self.assertEqual("ceilometer_image_size",
|
|
64
64
|
metric.labels["__name__"])
|
|
65
|
-
self.assertEqual("
|
|
65
|
+
self.assertEqual("custom", metric.labels["job"])
|
|
66
66
|
|
|
67
67
|
|
|
68
68
|
class PythonAPITestFunctionalAdminCommands(base.PythonAPITestCase):
|
|
@@ -43,8 +43,9 @@ class CliTest(testtools.TestCase):
|
|
|
43
43
|
return_value=self.client), \
|
|
44
44
|
mock.patch.object(self.client.query, 'list',
|
|
45
45
|
return_value=metric_names) as m:
|
|
46
|
+
# NOTE: disable-rbac option is NOOP, RBAC is disabled by default
|
|
46
47
|
ret1 = cli_list.take_action(test_parsed_args_enabled)
|
|
47
|
-
m.assert_called_with(disable_rbac=
|
|
48
|
+
m.assert_called_with(disable_rbac=True)
|
|
48
49
|
|
|
49
50
|
ret2 = cli_list.take_action(test_parsed_args_disabled)
|
|
50
51
|
m.assert_called_with(disable_rbac=True)
|
|
@@ -75,9 +76,9 @@ class CliTest(testtools.TestCase):
|
|
|
75
76
|
return_value=self.client), \
|
|
76
77
|
mock.patch.object(self.client.query, 'show',
|
|
77
78
|
return_value=prom_metric) as m:
|
|
78
|
-
|
|
79
|
+
# NOTE: disable-rbac option is NOOP, RBAC is disabled by default
|
|
79
80
|
ret1 = cli_show.take_action(test_parsed_args_enabled)
|
|
80
|
-
m.assert_called_with('metric_name', disable_rbac=
|
|
81
|
+
m.assert_called_with('metric_name', disable_rbac=True)
|
|
81
82
|
|
|
82
83
|
ret2 = cli_show.take_action(test_parsed_args_disabled)
|
|
83
84
|
m.assert_called_with('metric_name', disable_rbac=True)
|
|
@@ -113,8 +114,9 @@ class CliTest(testtools.TestCase):
|
|
|
113
114
|
mock.patch.object(self.client.query, 'query',
|
|
114
115
|
return_value=prom_metric) as m:
|
|
115
116
|
|
|
117
|
+
# NOTE: disable-rbac option is NOOP, RBAC is disabled by default
|
|
116
118
|
ret1 = cli_query.take_action(test_parsed_args_enabled)
|
|
117
|
-
m.assert_called_with(query, disable_rbac=
|
|
119
|
+
m.assert_called_with(query, disable_rbac=True)
|
|
118
120
|
|
|
119
121
|
ret2 = cli_query.take_action(test_parsed_args_disabled)
|
|
120
122
|
m.assert_called_with(query, disable_rbac=True)
|
|
@@ -85,7 +85,8 @@ class PrometheusAPIClientTestBase(testtools.TestCase):
|
|
|
85
85
|
class PrometheusAPIClientTest(PrometheusAPIClientTestBase):
|
|
86
86
|
def test_get(self):
|
|
87
87
|
url = "test"
|
|
88
|
-
|
|
88
|
+
root_path = "root_path"
|
|
89
|
+
expected_url = f"http://localhost:9090/{root_path}/api/v1/{url}"
|
|
89
90
|
|
|
90
91
|
params = {"query": "ceilometer_image_size{publisher='localhost'}"}
|
|
91
92
|
expected_params = params
|
|
@@ -93,12 +94,14 @@ class PrometheusAPIClientTest(PrometheusAPIClientTestBase):
|
|
|
93
94
|
return_value = self.GoodResponse()
|
|
94
95
|
with mock.patch.object(requests.Session, 'get',
|
|
95
96
|
return_value=return_value) as m:
|
|
96
|
-
c = client.PrometheusAPIClient("localhost:9090"
|
|
97
|
+
c = client.PrometheusAPIClient("localhost:9090",
|
|
98
|
+
root_path=root_path)
|
|
97
99
|
c._get(url, params)
|
|
98
100
|
|
|
99
101
|
m.assert_called_with(expected_url,
|
|
100
102
|
params=expected_params,
|
|
101
|
-
headers={'Accept': 'application/json'
|
|
103
|
+
headers={'Accept': 'application/json',
|
|
104
|
+
'Accept-Encoding': 'identity'})
|
|
102
105
|
|
|
103
106
|
def test_get_error(self):
|
|
104
107
|
url = "test"
|
|
@@ -120,7 +123,7 @@ class PrometheusAPIClientTest(PrometheusAPIClientTestBase):
|
|
|
120
123
|
|
|
121
124
|
def test_post(self):
|
|
122
125
|
url = "test"
|
|
123
|
-
expected_url = "http://localhost:9090/api/v1/
|
|
126
|
+
expected_url = f"http://localhost:9090/api/v1/{url}"
|
|
124
127
|
|
|
125
128
|
params = {"query": "ceilometer_image_size{publisher='localhost'}"}
|
|
126
129
|
expected_params = params
|
|
@@ -17,11 +17,11 @@ from unittest import mock
|
|
|
17
17
|
import testtools
|
|
18
18
|
|
|
19
19
|
from observabilityclient import prometheus_client
|
|
20
|
+
from observabilityclient import rbac
|
|
20
21
|
from observabilityclient.tests.unit.test_prometheus_client import (
|
|
21
22
|
MetricListMatcher
|
|
22
23
|
)
|
|
23
24
|
from observabilityclient.v1 import python_api
|
|
24
|
-
from observabilityclient.v1 import rbac
|
|
25
25
|
|
|
26
26
|
|
|
27
27
|
class QueryManagerTest(testtools.TestCase):
|
|
@@ -31,9 +31,8 @@ class QueryManagerTest(testtools.TestCase):
|
|
|
31
31
|
prom_client = prometheus_client.PrometheusAPIClient("somehost")
|
|
32
32
|
self.client.prometheus_client = prom_client
|
|
33
33
|
|
|
34
|
-
self.rbac = mock.Mock(wraps=rbac.
|
|
35
|
-
self.rbac.
|
|
36
|
-
self.rbac.rbac_init_succesful = True
|
|
34
|
+
self.rbac = mock.Mock(wraps=rbac.PromQLRbac(prom_client, mock.Mock()))
|
|
35
|
+
self.rbac.labels = {'project': 'project_id'}
|
|
37
36
|
|
|
38
37
|
self.manager = python_api.QueryManager(self.client)
|
|
39
38
|
|
|
@@ -41,16 +40,16 @@ class QueryManagerTest(testtools.TestCase):
|
|
|
41
40
|
self.client.query = self.manager
|
|
42
41
|
|
|
43
42
|
def test_list(self):
|
|
44
|
-
|
|
43
|
+
returned_data = {'data': ['metric1', 'test42', 'abc2']}
|
|
45
44
|
expected = ['abc2', 'metric1', 'test42']
|
|
46
45
|
|
|
47
46
|
with mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
|
|
48
|
-
return_value=
|
|
47
|
+
return_value=returned_data):
|
|
49
48
|
ret1 = self.manager.list()
|
|
50
|
-
|
|
49
|
+
self.assertEqual(expected, ret1)
|
|
51
50
|
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
ret2 = self.manager.list(disable_rbac=True)
|
|
52
|
+
self.assertEqual(expected, ret2)
|
|
54
53
|
|
|
55
54
|
def test_show(self):
|
|
56
55
|
query = 'some_metric'
|
|
@@ -65,21 +64,23 @@ class QueryManagerTest(testtools.TestCase):
|
|
|
65
64
|
}
|
|
66
65
|
expected = [prometheus_client.PrometheusMetric(returned_by_prom)]
|
|
67
66
|
expected_matcher = MetricListMatcher(expected)
|
|
67
|
+
|
|
68
68
|
with mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
|
|
69
69
|
return_value=returned_by_prom):
|
|
70
|
-
ret1 = self.manager.show(query)
|
|
71
|
-
self.rbac.append_rbac.assert_called_with(query,
|
|
72
|
-
disable_rbac=False)
|
|
73
|
-
|
|
74
70
|
ret2 = self.manager.show(query, disable_rbac=True)
|
|
75
|
-
self.rbac.
|
|
76
|
-
|
|
71
|
+
self.rbac.append_rbac_labels.assert_not_called()
|
|
72
|
+
|
|
73
|
+
with mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
|
|
74
|
+
return_value=returned_by_prom):
|
|
75
|
+
ret1 = self.manager.show(query)
|
|
76
|
+
self.rbac.append_rbac_labels.assert_not_called()
|
|
77
77
|
|
|
78
78
|
self.assertThat(ret1, expected_matcher)
|
|
79
79
|
self.assertThat(ret2, expected_matcher)
|
|
80
80
|
|
|
81
81
|
def test_query(self):
|
|
82
|
-
|
|
82
|
+
queried_metric_name = 'some_metric'
|
|
83
|
+
query = queried_metric_name
|
|
83
84
|
returned_by_prom = {
|
|
84
85
|
'data': {
|
|
85
86
|
'resultType': 'non-vector'
|
|
@@ -92,14 +93,15 @@ class QueryManagerTest(testtools.TestCase):
|
|
|
92
93
|
expected = [prometheus_client.PrometheusMetric(returned_by_prom)]
|
|
93
94
|
expected_matcher = MetricListMatcher(expected)
|
|
94
95
|
with mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
|
|
95
|
-
return_value=returned_by_prom)
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
96
|
+
return_value=returned_by_prom), \
|
|
97
|
+
mock.patch.object(python_api.QueryManager, 'list',
|
|
98
|
+
return_value=queried_metric_name):
|
|
99
|
+
|
|
100
|
+
ret1 = self.manager.query(query, disable_rbac=True)
|
|
101
|
+
self.rbac.modify_query.assert_not_called()
|
|
99
102
|
|
|
100
|
-
ret2 = self.manager.query(query
|
|
101
|
-
self.rbac.
|
|
102
|
-
disable_rbac=True)
|
|
103
|
+
ret2 = self.manager.query(query)
|
|
104
|
+
self.rbac.modify_query.assert_not_called()
|
|
103
105
|
|
|
104
106
|
self.assertThat(ret1, expected_matcher)
|
|
105
107
|
self.assertThat(ret2, expected_matcher)
|
|
@@ -14,62 +14,67 @@
|
|
|
14
14
|
|
|
15
15
|
from unittest import mock
|
|
16
16
|
|
|
17
|
-
from keystoneauth1.exceptions.auth_plugins import MissingAuthPlugin
|
|
18
|
-
from keystoneauth1 import session
|
|
19
17
|
import testtools
|
|
20
18
|
|
|
21
|
-
from observabilityclient
|
|
19
|
+
from observabilityclient import rbac
|
|
22
20
|
|
|
23
21
|
|
|
24
|
-
class
|
|
22
|
+
class PromQLRbacTest(testtools.TestCase):
|
|
25
23
|
def setUp(self):
|
|
26
|
-
super(
|
|
27
|
-
self.
|
|
28
|
-
self.rbac
|
|
29
|
-
self.rbac.
|
|
30
|
-
"project":
|
|
24
|
+
super(PromQLRbacTest, self).setUp()
|
|
25
|
+
self.project_id = "project123"
|
|
26
|
+
self.rbac = rbac.PromQLRbac(mock.Mock(), mock.Mock())
|
|
27
|
+
self.rbac.labels = {
|
|
28
|
+
"project": "project123"
|
|
31
29
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
def test_constructor_error(self):
|
|
43
|
-
with mock.patch.object(session.Session, 'get_project_id',
|
|
44
|
-
side_effect=MissingAuthPlugin()):
|
|
45
|
-
r = rbac.Rbac("client", session.Session(), False)
|
|
46
|
-
self.assertIsNone(r.project_id)
|
|
47
|
-
|
|
48
|
-
def test_enrich_query(self):
|
|
49
|
-
test_cases = [
|
|
30
|
+
self.rbac.client.label_values = lambda label: [
|
|
31
|
+
'test_query',
|
|
32
|
+
'cpu_temp_celsius',
|
|
33
|
+
'http_requests',
|
|
34
|
+
'test:query:with:colon:',
|
|
35
|
+
'test_query_with_digit1',
|
|
36
|
+
'method_code:http_errors:rate5m',
|
|
37
|
+
'method:http_requests:rate5m'
|
|
38
|
+
]
|
|
39
|
+
self.test_cases = [
|
|
50
40
|
(
|
|
51
41
|
"test_query",
|
|
52
|
-
f"test_query{{project='{self.
|
|
42
|
+
f"test_query{{project='{self.project_id}'}}"
|
|
53
43
|
), (
|
|
54
44
|
"test_query{somelabel='value'}",
|
|
55
45
|
|
|
56
46
|
(f"test_query{{somelabel='value', "
|
|
57
|
-
f"project='{self.
|
|
47
|
+
f"project='{self.project_id}'}}")
|
|
58
48
|
), (
|
|
59
49
|
"test_query{somelabel='value', label2='value2'}",
|
|
60
50
|
|
|
61
51
|
(f"test_query{{somelabel='value', label2='value2', "
|
|
62
|
-
f"project='{self.
|
|
52
|
+
f"project='{self.project_id}'}}")
|
|
53
|
+
), (
|
|
54
|
+
"test_query{somelabel='unicode{}{ \t/-_#~$&%\\'}",
|
|
55
|
+
|
|
56
|
+
(f"test_query{{somelabel='unicode{{}}{{ \t/-_#~$&%\\', "
|
|
57
|
+
f"project='{self.project_id}'}}")
|
|
63
58
|
), (
|
|
64
|
-
"test_query{somelabel='
|
|
59
|
+
"test_query{somelabel='s p a c e'}",
|
|
65
60
|
|
|
66
|
-
(f"test_query{{somelabel='
|
|
67
|
-
f"project='{self.
|
|
61
|
+
(f"test_query{{somelabel='s p a c e', "
|
|
62
|
+
f"project='{self.project_id}'}}")
|
|
63
|
+
), (
|
|
64
|
+
"test_query{somelabel='doublequotes\"'}",
|
|
65
|
+
|
|
66
|
+
(f"test_query{{somelabel='doublequotes\"', "
|
|
67
|
+
f"project='{self.project_id}'}}")
|
|
68
|
+
), (
|
|
69
|
+
'test_query{somelabel="singlequotes\'"}',
|
|
70
|
+
|
|
71
|
+
(f'test_query{{somelabel="singlequotes\'", '
|
|
72
|
+
f'project=\'{self.project_id}\'}}')
|
|
68
73
|
), (
|
|
69
74
|
"test_query{doesnt_match_regex!~'regex'}",
|
|
70
75
|
|
|
71
76
|
(f"test_query{{doesnt_match_regex!~'regex', "
|
|
72
|
-
f"project='{self.
|
|
77
|
+
f"project='{self.project_id}'}}")
|
|
73
78
|
), (
|
|
74
79
|
"delta(cpu_temp_celsius{host='zeus'}[2h]) - "
|
|
75
80
|
"sum(http_requests) + "
|
|
@@ -77,69 +82,107 @@ class RbacTest(testtools.TestCase):
|
|
|
77
82
|
"sum(http_requests{or_regex=~'smth1|something2|3'})",
|
|
78
83
|
|
|
79
84
|
(f"delta(cpu_temp_celsius{{host='zeus', "
|
|
80
|
-
f"project='{self.
|
|
85
|
+
f"project='{self.project_id}'}}[2h]) - "
|
|
81
86
|
f"sum(http_requests"
|
|
82
|
-
f"{{project='{self.
|
|
87
|
+
f"{{project='{self.project_id}'}}) + "
|
|
83
88
|
f"sum(http_requests{{instance=~'.*', "
|
|
84
|
-
f"project='{self.
|
|
89
|
+
f"project='{self.project_id}'}}) + "
|
|
85
90
|
f"sum(http_requests{{or_regex=~'smth1|something2|3', "
|
|
86
|
-
f"project='{self.
|
|
87
|
-
)
|
|
88
|
-
|
|
91
|
+
f"project='{self.project_id}'}})")
|
|
92
|
+
), (
|
|
93
|
+
"round(test_query{label='something'},5)",
|
|
89
94
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
95
|
+
(f"round(test_query{{label='something', "
|
|
96
|
+
f"project='{self.project_id}'}},5)")
|
|
97
|
+
), (
|
|
98
|
+
"sum by (foo) (test_query{label_1='baz'})",
|
|
93
99
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
100
|
+
(f"sum by (foo) (test_query{{label_1='baz', "
|
|
101
|
+
f"project='{self.project_id}'}})")
|
|
102
|
+
), (
|
|
103
|
+
"test_query{} + avg without (application, group) "
|
|
104
|
+
"(test:query:with:colon:{label='baz'})",
|
|
97
105
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
"test_query"
|
|
106
|
+
(f"test_query{{project='{self.project_id}'}} + "
|
|
107
|
+
f"avg without (application, group) "
|
|
108
|
+
f"(test:query:with:colon:{{label='baz', "
|
|
109
|
+
f"project='{self.project_id}'}})")
|
|
103
110
|
), (
|
|
104
|
-
"test_query{
|
|
105
|
-
"
|
|
111
|
+
"test_query{label1='foo'} + on (label1,label2) "
|
|
112
|
+
"avg by (label3) (test_query_with_digit1{label='baz',"
|
|
113
|
+
"label1='foo',label2='bar'})",
|
|
114
|
+
|
|
115
|
+
(f"test_query{{label1='foo', "
|
|
116
|
+
f"project='{self.project_id}'}} "
|
|
117
|
+
f"+ on (label1,label2) avg by (label3) "
|
|
118
|
+
f"(test_query_with_digit1{{label='baz',"
|
|
119
|
+
f"label1='foo',label2='bar', "
|
|
120
|
+
f"project='{self.project_id}'}})")
|
|
106
121
|
), (
|
|
107
|
-
"
|
|
108
|
-
|
|
122
|
+
"{label='no-metric'}",
|
|
123
|
+
|
|
124
|
+
(f"{{label='no-metric', "
|
|
125
|
+
f"project='{self.project_id}'}}")
|
|
109
126
|
), (
|
|
110
|
-
"
|
|
111
|
-
"
|
|
127
|
+
"http_requests{environment=~"
|
|
128
|
+
"'staging|testing|development',method!='GET'}",
|
|
129
|
+
|
|
130
|
+
(f"http_requests{{environment=~"
|
|
131
|
+
f"'staging|testing|development',method!='GET', "
|
|
132
|
+
f"project='{self.project_id}'}}")
|
|
112
133
|
), (
|
|
113
|
-
"
|
|
114
|
-
|
|
134
|
+
"http_requests{replica!='rep-a',replica=~'rep.*'}",
|
|
135
|
+
|
|
136
|
+
(f"http_requests{{replica!='rep-a',replica=~'rep.*', "
|
|
137
|
+
f"project='{self.project_id}'}}")
|
|
115
138
|
), (
|
|
116
|
-
"
|
|
117
|
-
"sum(http_requests) + "
|
|
118
|
-
"sum(http_requests{instance=~'.*'}) + "
|
|
119
|
-
"sum(http_requests{or_regex=~'smth1|something2|3'})",
|
|
139
|
+
"{__name__=~'job:.*'}",
|
|
120
140
|
|
|
121
|
-
"
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
"
|
|
141
|
+
(f"{{__name__=~'job:.*', "
|
|
142
|
+
f"project='{self.project_id}'}}")
|
|
143
|
+
), (
|
|
144
|
+
"http_requests offset 5m",
|
|
145
|
+
|
|
146
|
+
(f"http_requests"
|
|
147
|
+
f"{{project='{self.project_id}'}} "
|
|
148
|
+
f"offset 5m")
|
|
149
|
+
), (
|
|
150
|
+
"rate(http_requests[5m] offset -1w)",
|
|
151
|
+
|
|
152
|
+
(f"rate(http_requests"
|
|
153
|
+
f"{{project='{self.project_id}'}}"
|
|
154
|
+
f"[5m] offset -1w)")
|
|
155
|
+
), (
|
|
156
|
+
"http_requests @ 1609746000",
|
|
157
|
+
|
|
158
|
+
(f"http_requests"
|
|
159
|
+
f"{{project='{self.project_id}'}} "
|
|
160
|
+
f"@ 1609746000")
|
|
161
|
+
), (
|
|
162
|
+
"histogram_quantile(0.9, sum by (le) "
|
|
163
|
+
"(rate(http_requests[10m])))",
|
|
164
|
+
|
|
165
|
+
(f"histogram_quantile(0.9, sum by (le) "
|
|
166
|
+
f"(rate(http_requests"
|
|
167
|
+
f"{{project='{self.project_id}'}}"
|
|
168
|
+
f"[10m])))"
|
|
169
|
+
)
|
|
125
170
|
)
|
|
126
171
|
]
|
|
127
172
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
self.assertEqual(ret, query)
|
|
173
|
+
def test_constructor(self):
|
|
174
|
+
r = rbac.PromQLRbac("client", "123")
|
|
175
|
+
self.assertEqual(r.labels, {
|
|
176
|
+
"project": "123"
|
|
177
|
+
})
|
|
134
178
|
|
|
135
|
-
def
|
|
136
|
-
query
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
self.assertEqual(ret, expected)
|
|
179
|
+
def test_modify_query(self):
|
|
180
|
+
for query, expected in self.test_cases:
|
|
181
|
+
ret = self.rbac.modify_query(query)
|
|
182
|
+
self.assertEqual(expected, ret)
|
|
140
183
|
|
|
141
|
-
def
|
|
184
|
+
def test_append_rbac_labels(self):
|
|
142
185
|
query = "test_query"
|
|
143
|
-
expected = query
|
|
144
|
-
ret = self.rbac.
|
|
145
|
-
self.assertEqual(
|
|
186
|
+
expected = f"{query}{{project='{self.project_id}'}}"
|
|
187
|
+
ret = self.rbac.append_rbac_labels(query)
|
|
188
|
+
self.assertEqual(expected, ret)
|
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
import os
|
|
16
16
|
from unittest import mock
|
|
17
17
|
|
|
18
|
+
from keystoneauth1 import adapter
|
|
19
|
+
from keystoneauth1 import session
|
|
18
20
|
import testtools
|
|
19
21
|
|
|
20
22
|
from observabilityclient import prometheus_client
|
|
@@ -55,19 +57,20 @@ class GetPrometheusClientTest(testtools.TestCase):
|
|
|
55
57
|
mock.patch.object(prometheus_client.PrometheusAPIClient,
|
|
56
58
|
"__init__", return_value=None) as m:
|
|
57
59
|
metric_utils.get_prometheus_client()
|
|
58
|
-
m.assert_called_with("somehost:1234")
|
|
60
|
+
m.assert_called_with("somehost:1234", None, "")
|
|
59
61
|
|
|
60
|
-
def
|
|
61
|
-
with mock.patch.dict(os.environ,
|
|
62
|
+
def test_get_prometheus_client_env_override(self):
|
|
63
|
+
with mock.patch.dict(os.environ,
|
|
64
|
+
{'PROMETHEUS_HOST': 'env_override'}), \
|
|
62
65
|
mock.patch.object(metric_utils, 'get_config_file',
|
|
63
66
|
return_value=self.config_file), \
|
|
64
67
|
mock.patch.object(prometheus_client.PrometheusAPIClient,
|
|
65
68
|
"__init__", return_value=None) as m:
|
|
66
69
|
metric_utils.get_prometheus_client()
|
|
67
|
-
m.assert_called_with("
|
|
70
|
+
m.assert_called_with("env_override:1234", None, "")
|
|
68
71
|
|
|
69
72
|
def test_get_prometheus_client_no_config_file(self):
|
|
70
|
-
patched_env = {'PROMETHEUS_HOST': '
|
|
73
|
+
patched_env = {'PROMETHEUS_HOST': 'env_override',
|
|
71
74
|
'PROMETHEUS_PORT': 'env_port'}
|
|
72
75
|
with mock.patch.dict(os.environ, patched_env), \
|
|
73
76
|
mock.patch.object(metric_utils, 'get_config_file',
|
|
@@ -75,7 +78,19 @@ class GetPrometheusClientTest(testtools.TestCase):
|
|
|
75
78
|
mock.patch.object(prometheus_client.PrometheusAPIClient,
|
|
76
79
|
"__init__", return_value=None) as m:
|
|
77
80
|
metric_utils.get_prometheus_client()
|
|
78
|
-
m.assert_called_with("
|
|
81
|
+
m.assert_called_with("env_override:env_port", None, "")
|
|
82
|
+
|
|
83
|
+
def test_get_prometheus_client_prefix_in_env_variable(self):
|
|
84
|
+
patched_env = {'PROMETHEUS_HOST': 'env_override',
|
|
85
|
+
'PROMETHEUS_PORT': 'env_port',
|
|
86
|
+
'PROMETHEUS_ROOT_PATH': 'root_path_env'}
|
|
87
|
+
with mock.patch.dict(os.environ, patched_env), \
|
|
88
|
+
mock.patch.object(metric_utils, 'get_config_file',
|
|
89
|
+
return_value=None), \
|
|
90
|
+
mock.patch.object(prometheus_client.PrometheusAPIClient,
|
|
91
|
+
"__init__", return_value=None) as m:
|
|
92
|
+
metric_utils.get_prometheus_client()
|
|
93
|
+
m.assert_called_with("env_override:env_port", None, "root_path_env")
|
|
79
94
|
|
|
80
95
|
def test_get_prometheus_client_missing_configuration(self):
|
|
81
96
|
with mock.patch.dict(os.environ, {}), \
|
|
@@ -86,6 +101,62 @@ class GetPrometheusClientTest(testtools.TestCase):
|
|
|
86
101
|
self.assertRaises(metric_utils.ConfigurationError,
|
|
87
102
|
metric_utils.get_prometheus_client)
|
|
88
103
|
|
|
104
|
+
def test_get_prometheus_client_from_keystone_http(self):
|
|
105
|
+
prometheus_endpoint = "http://localhost:1234/prometheus"
|
|
106
|
+
keystone_session = session.Session()
|
|
107
|
+
with mock.patch.dict(os.environ, {}), \
|
|
108
|
+
mock.patch.object(metric_utils, 'get_config_file',
|
|
109
|
+
return_value=None), \
|
|
110
|
+
mock.patch.object(adapter.Adapter, 'get_endpoint',
|
|
111
|
+
return_value=prometheus_endpoint), \
|
|
112
|
+
mock.patch.object(prometheus_client.PrometheusAPIClient,
|
|
113
|
+
"__init__", return_value=None) as init_m, \
|
|
114
|
+
mock.patch.object(prometheus_client.PrometheusAPIClient,
|
|
115
|
+
"set_ca_cert") as ca_m:
|
|
116
|
+
metric_utils.get_prometheus_client(keystone_session)
|
|
117
|
+
init_m.assert_called_with(
|
|
118
|
+
"localhost:1234", keystone_session, "prometheus"
|
|
119
|
+
)
|
|
120
|
+
ca_m.assert_not_called()
|
|
121
|
+
|
|
122
|
+
def test_get_prometheus_client_from_keystone_https(self):
|
|
123
|
+
prometheus_endpoint = "https://localhost:1234/prometheus"
|
|
124
|
+
keystone_session = session.Session()
|
|
125
|
+
with mock.patch.dict(os.environ, {}), \
|
|
126
|
+
mock.patch.object(metric_utils, 'get_config_file',
|
|
127
|
+
return_value=None), \
|
|
128
|
+
mock.patch.object(adapter.Adapter, 'get_endpoint',
|
|
129
|
+
return_value=prometheus_endpoint), \
|
|
130
|
+
mock.patch.object(prometheus_client.PrometheusAPIClient,
|
|
131
|
+
"__init__", return_value=None) as init_m, \
|
|
132
|
+
mock.patch.object(prometheus_client.PrometheusAPIClient,
|
|
133
|
+
"set_ca_cert") as ca_m:
|
|
134
|
+
metric_utils.get_prometheus_client(keystone_session)
|
|
135
|
+
init_m.assert_called_with(
|
|
136
|
+
"localhost:1234", keystone_session, "prometheus"
|
|
137
|
+
)
|
|
138
|
+
ca_m.assert_called_with(True)
|
|
139
|
+
|
|
140
|
+
def test_get_prometheus_client_from_keystone_custom_ca(self):
|
|
141
|
+
prometheus_endpoint = "https://localhost:1234/prometheus"
|
|
142
|
+
keystone_session = session.Session()
|
|
143
|
+
config_data = 'ca_cert: "ca/path"'
|
|
144
|
+
config_file = mock.mock_open(read_data=config_data)("name", 'r')
|
|
145
|
+
with mock.patch.dict(os.environ, {}), \
|
|
146
|
+
mock.patch.object(metric_utils, 'get_config_file',
|
|
147
|
+
return_value=config_file), \
|
|
148
|
+
mock.patch.object(adapter.Adapter, 'get_endpoint',
|
|
149
|
+
return_value=prometheus_endpoint), \
|
|
150
|
+
mock.patch.object(prometheus_client.PrometheusAPIClient,
|
|
151
|
+
"__init__", return_value=None) as init_m, \
|
|
152
|
+
mock.patch.object(prometheus_client.PrometheusAPIClient,
|
|
153
|
+
"set_ca_cert") as ca_m:
|
|
154
|
+
metric_utils.get_prometheus_client(keystone_session)
|
|
155
|
+
init_m.assert_called_with(
|
|
156
|
+
"localhost:1234", keystone_session, "prometheus"
|
|
157
|
+
)
|
|
158
|
+
ca_m.assert_called_with("ca/path")
|
|
159
|
+
|
|
89
160
|
|
|
90
161
|
class FormatLabelsTest(testtools.TestCase):
|
|
91
162
|
def setUp(self):
|
|
@@ -14,14 +14,20 @@
|
|
|
14
14
|
|
|
15
15
|
import logging
|
|
16
16
|
import os
|
|
17
|
+
from urllib import parse
|
|
17
18
|
|
|
19
|
+
from keystoneauth1 import adapter
|
|
20
|
+
from keystoneauth1.exceptions import catalog as keystone_exception
|
|
18
21
|
import yaml
|
|
19
22
|
|
|
20
23
|
from observabilityclient.prometheus_client import PrometheusAPIClient
|
|
21
24
|
|
|
22
25
|
|
|
23
|
-
DEFAULT_CONFIG_LOCATIONS =
|
|
24
|
-
|
|
26
|
+
DEFAULT_CONFIG_LOCATIONS = (
|
|
27
|
+
[os.path.join(os.environ["HOME"], ".config/openstack/"), "/etc/openstack/"]
|
|
28
|
+
if "HOME" in os.environ
|
|
29
|
+
else ["/etc/openstack/"]
|
|
30
|
+
)
|
|
25
31
|
CONFIG_FILE_NAME = "prometheus.yaml"
|
|
26
32
|
LOG = logging.getLogger(__name__)
|
|
27
33
|
|
|
@@ -42,10 +48,11 @@ def get_config_file():
|
|
|
42
48
|
return None
|
|
43
49
|
|
|
44
50
|
|
|
45
|
-
def get_prometheus_client():
|
|
51
|
+
def get_prometheus_client(session=None, adapter_options={}):
|
|
46
52
|
host = None
|
|
47
53
|
port = None
|
|
48
54
|
ca_cert = None
|
|
55
|
+
root_path = ""
|
|
49
56
|
conf_file = get_config_file()
|
|
50
57
|
if conf_file is not None:
|
|
51
58
|
conf = yaml.safe_load(conf_file)
|
|
@@ -55,8 +62,32 @@ def get_prometheus_client():
|
|
|
55
62
|
port = conf['port']
|
|
56
63
|
if 'ca_cert' in conf:
|
|
57
64
|
ca_cert = conf['ca_cert']
|
|
65
|
+
if 'root_path' in conf:
|
|
66
|
+
root_path = conf['root_path']
|
|
58
67
|
conf_file.close()
|
|
59
68
|
|
|
69
|
+
if session is not None and (host is None or port is None):
|
|
70
|
+
try:
|
|
71
|
+
endpoint = adapter.Adapter(
|
|
72
|
+
session=session, **adapter_options
|
|
73
|
+
).get_endpoint()
|
|
74
|
+
parsed_url = parse.urlparse(endpoint)
|
|
75
|
+
host = parsed_url.hostname
|
|
76
|
+
port = parsed_url.port if parsed_url.port is not None else 80
|
|
77
|
+
root_path = parsed_url.path.strip('/')
|
|
78
|
+
if parsed_url.scheme == "https" and ca_cert is None:
|
|
79
|
+
# NOTE(jwysogla): Use the default CA certs if the scheme
|
|
80
|
+
# is https, but keep the original value if already set,
|
|
81
|
+
# so that a custom certificate can be set in the config
|
|
82
|
+
# file, while the endpoint is retrieved from keystone.
|
|
83
|
+
ca_cert = True
|
|
84
|
+
except keystone_exception.EndpointNotFound:
|
|
85
|
+
# NOTE(jwysogla): Don't do anything here. It's still possible
|
|
86
|
+
# to get the correct endpoint configuration from the env vars.
|
|
87
|
+
# If that doesn't work, the same error message is part of the
|
|
88
|
+
# exception raised below.
|
|
89
|
+
pass
|
|
90
|
+
|
|
60
91
|
# NOTE(jwysogla): We allow to overide the prometheus.yaml by
|
|
61
92
|
# the environment variables
|
|
62
93
|
if 'PROMETHEUS_HOST' in os.environ:
|
|
@@ -65,10 +96,13 @@ def get_prometheus_client():
|
|
|
65
96
|
port = os.environ['PROMETHEUS_PORT']
|
|
66
97
|
if 'PROMETHEUS_CA_CERT' in os.environ:
|
|
67
98
|
ca_cert = os.environ['PROMETHEUS_CA_CERT']
|
|
99
|
+
if 'PROMETHEUS_ROOT_PATH' in os.environ:
|
|
100
|
+
root_path = os.environ['PROMETHEUS_ROOT_PATH']
|
|
68
101
|
if host is None or port is None:
|
|
69
102
|
raise ConfigurationError("Can't find prometheus host and "
|
|
70
|
-
"port configuration
|
|
71
|
-
|
|
103
|
+
"port configuration and endpoint for service"
|
|
104
|
+
"prometheus not found.")
|
|
105
|
+
client = PrometheusAPIClient(f"{host}:{port}", session, root_path)
|
|
72
106
|
if ca_cert is not None:
|
|
73
107
|
client.set_ca_cert(ca_cert)
|
|
74
108
|
return client
|
observabilityclient/v1/base.py
CHANGED
|
@@ -13,20 +13,39 @@
|
|
|
13
13
|
# under the License.
|
|
14
14
|
#
|
|
15
15
|
|
|
16
|
+
import argparse
|
|
17
|
+
import sys
|
|
18
|
+
|
|
16
19
|
from osc_lib.command import command
|
|
17
20
|
|
|
18
21
|
from observabilityclient.i18n import _
|
|
19
22
|
|
|
20
23
|
|
|
24
|
+
class DeprecatedNOOPAction(argparse.Action):
|
|
25
|
+
def __init__(self, *args, **kwargs):
|
|
26
|
+
kwargs['help'] = f"[DEPRECATED] {kwargs['help']}" \
|
|
27
|
+
if 'help' in kwargs else \
|
|
28
|
+
"[DEPRECATED]"
|
|
29
|
+
kwargs['required'] = False
|
|
30
|
+
kwargs['nargs'] = 0
|
|
31
|
+
super().__init__(*args, **kwargs)
|
|
32
|
+
|
|
33
|
+
def __call__(self, parser, namespace, values, option_string=None):
|
|
34
|
+
if option_string is not None:
|
|
35
|
+
print(f"Note: {option_string} is deprecated option, "
|
|
36
|
+
"has no effect and is a subject for removal "
|
|
37
|
+
"in future OpenStack release.", file=sys.stderr)
|
|
38
|
+
|
|
39
|
+
|
|
21
40
|
class ObservabilityBaseCommand(command.Command):
|
|
22
41
|
"""Base class for metric commands."""
|
|
23
42
|
|
|
24
43
|
def get_parser(self, prog_name):
|
|
25
44
|
parser = super().get_parser(prog_name)
|
|
26
|
-
# TODO(jwysogla): Should this be restricted somehow?
|
|
27
45
|
parser.add_argument(
|
|
28
46
|
'--disable-rbac',
|
|
29
|
-
action=
|
|
47
|
+
action=DeprecatedNOOPAction,
|
|
48
|
+
default=True,
|
|
30
49
|
help=_("Disable rbac injection")
|
|
31
50
|
)
|
|
32
51
|
return parser
|
observabilityclient/v1/client.py
CHANGED
|
@@ -14,9 +14,9 @@
|
|
|
14
14
|
|
|
15
15
|
import keystoneauth1.session
|
|
16
16
|
|
|
17
|
+
from observabilityclient import rbac
|
|
17
18
|
from observabilityclient.utils.metric_utils import get_prometheus_client
|
|
18
19
|
from observabilityclient.v1 import python_api
|
|
19
|
-
from observabilityclient.v1 import rbac
|
|
20
20
|
|
|
21
21
|
|
|
22
22
|
class Client(object):
|
|
@@ -28,7 +28,7 @@ class Client(object):
|
|
|
28
28
|
session_options = session_options or {}
|
|
29
29
|
adapter_options = adapter_options or {}
|
|
30
30
|
|
|
31
|
-
adapter_options.setdefault('service_type', "
|
|
31
|
+
adapter_options.setdefault('service_type', "prometheus")
|
|
32
32
|
|
|
33
33
|
if session is None:
|
|
34
34
|
session = keystoneauth1.session.Session(**session_options)
|
|
@@ -38,6 +38,11 @@ class Client(object):
|
|
|
38
38
|
|
|
39
39
|
self.session = session
|
|
40
40
|
|
|
41
|
-
self.prometheus_client = get_prometheus_client(
|
|
41
|
+
self.prometheus_client = get_prometheus_client(
|
|
42
|
+
session, adapter_options
|
|
43
|
+
)
|
|
42
44
|
self.query = python_api.QueryManager(self)
|
|
43
|
-
self.rbac = rbac.
|
|
45
|
+
self.rbac = rbac.PromQLRbac(
|
|
46
|
+
self.prometheus_client,
|
|
47
|
+
self.session.get_project_id()
|
|
48
|
+
)
|
|
@@ -17,35 +17,38 @@ from observabilityclient.v1 import base
|
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
class QueryManager(base.Manager):
|
|
20
|
-
def list(self, disable_rbac=
|
|
20
|
+
def list(self, disable_rbac=True):
|
|
21
21
|
"""List metric names.
|
|
22
22
|
|
|
23
23
|
:param disable_rbac: Disables rbac injection if set to True
|
|
24
24
|
:type disable_rbac: boolean
|
|
25
25
|
"""
|
|
26
|
-
if disable_rbac
|
|
26
|
+
if disable_rbac:
|
|
27
27
|
metric_names = self.prom.label_values("__name__")
|
|
28
28
|
return sorted(metric_names)
|
|
29
29
|
else:
|
|
30
|
-
match = f"{{{format_labels(self.client.rbac.
|
|
30
|
+
match = f"{{{format_labels(self.client.rbac.labels)}}}"
|
|
31
31
|
metrics = self.prom.series(match)
|
|
32
32
|
if metrics == []:
|
|
33
33
|
return []
|
|
34
34
|
unique_metric_names = list(set([m['__name__'] for m in metrics]))
|
|
35
35
|
return sorted(unique_metric_names)
|
|
36
36
|
|
|
37
|
-
def show(self, name, disable_rbac=
|
|
37
|
+
def show(self, name, disable_rbac=True):
|
|
38
38
|
"""Show current values for metrics of a specified name.
|
|
39
39
|
|
|
40
40
|
:param disable_rbac: Disables rbac injection if set to True
|
|
41
41
|
:type disable_rbac: boolean
|
|
42
42
|
"""
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
query = ""
|
|
44
|
+
if disable_rbac:
|
|
45
|
+
query = name
|
|
46
|
+
else:
|
|
47
|
+
query = self.client.rbac.append_rbac_labels(name)
|
|
48
|
+
last_metric_query = f"last_over_time({query}[5m])"
|
|
46
49
|
return self.prom.query(last_metric_query)
|
|
47
50
|
|
|
48
|
-
def query(self, query, disable_rbac=
|
|
51
|
+
def query(self, query, disable_rbac=True):
|
|
49
52
|
"""Send a query to prometheus.
|
|
50
53
|
|
|
51
54
|
The query can be any PromQL query. Labels for enforcing
|
|
@@ -63,7 +66,8 @@ class QueryManager(base.Manager):
|
|
|
63
66
|
:param disable_rbac: Disables rbac injection if set to True
|
|
64
67
|
:type disable_rbac: boolean
|
|
65
68
|
"""
|
|
66
|
-
|
|
69
|
+
if not disable_rbac:
|
|
70
|
+
query = self.client.rbac.modify_query(query)
|
|
67
71
|
return self.prom.query(query)
|
|
68
72
|
|
|
69
73
|
def delete(self, matches, start=None, end=None):
|
{python_observabilityclient-0.4.0.dist-info → python_observabilityclient-1.0.0.dist-info}/AUTHORS
RENAMED
|
@@ -9,4 +9,6 @@ Leif Madsen <lmadsen@redhat.com>
|
|
|
9
9
|
Marihan Girgis mgirgisf@redhat.com
|
|
10
10
|
Martin Magr <mmagr@redhat.com>
|
|
11
11
|
Martin Mágr <mmagr@redhat.com>
|
|
12
|
+
OpenStack Release Bot <infra-root@openstack.org>
|
|
12
13
|
Takashi Kajinami <kajinamit@oss.nttdata.com>
|
|
14
|
+
Victoria Martinez de la Cruz <victoria@redhat.com>
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
observabilityclient/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
observabilityclient/client.py,sha256=cfRb816p6Ips9t2cdWvmSJ1uGrtTEHXQnctoExrLA5A,828
|
|
3
|
+
observabilityclient/i18n.py,sha256=PFo6BaOfyGQD5q7L3FunYJ8d4mwbRszG8wOrJIjki_4,790
|
|
4
|
+
observabilityclient/plugin.py,sha256=hyxENjNO6NLrH6G4muyAeEEtFTBa9d5OozjwpzNpJVA,2262
|
|
5
|
+
observabilityclient/prometheus_client.py,sha256=yn0cOj08CUYLNydyh3sc1vTPGwplxZeMiyNN4l52aoY,7457
|
|
6
|
+
observabilityclient/rbac.py,sha256=Rw2jyEl-xGb2U6wi7mDjJxTR9kGpIt8xOKOUOFvjq1Q,6327
|
|
7
|
+
observabilityclient/tests/functional/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
observabilityclient/tests/functional/base.py,sha256=SXbPiksFH7aER-zZjIWxz5nwCqq5f1MYWth1tlFLwcs,6523
|
|
9
|
+
observabilityclient/tests/functional/test_cli.py,sha256=jwhgt8w0gq1CdDoaDKLV8SB_hQRmgnbMw9rRIyfcZLo,3944
|
|
10
|
+
observabilityclient/tests/functional/test_python_api.py,sha256=5TLFboJ7B__UZ9zfc6bVSxPTzV9KnnrwluTqLwnTZLs,3308
|
|
11
|
+
observabilityclient/tests/unit/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
|
+
observabilityclient/tests/unit/test_cli.py,sha256=N19Yo-PIdhlR4-tKQCKbtoetbknxZwl10zsEQSA1Leo,6191
|
|
13
|
+
observabilityclient/tests/unit/test_prometheus_client.py,sha256=iyAypBArQE2e7WGORwhelpoKdNAK1UD2SqZcs6p6ToQ,18982
|
|
14
|
+
observabilityclient/tests/unit/test_python_api.py,sha256=gMUcsvQISESY7p3NWsuoHk07I0phc74c-JC2vKRKfBI,4741
|
|
15
|
+
observabilityclient/tests/unit/test_rbac.py,sha256=9kntOcGUwz9uzG2S4uZUozOl_BMw7I9b0W8FCoaNvm4,6961
|
|
16
|
+
observabilityclient/tests/unit/test_utils.py,sha256=6Y1sgYk-5bJeS0OEUUnjNpTsAbiPYY8zBn_BWwvYoec,11258
|
|
17
|
+
observabilityclient/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
18
|
+
observabilityclient/utils/metric_utils.py,sha256=Rn4DeaOHgwW0WFJPcwjBqyYreQRYkzSAXFIKfMV2jew,5051
|
|
19
|
+
observabilityclient/v1/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
20
|
+
observabilityclient/v1/base.py,sha256=keRgOIe7_yvE57fJRZ5O6s0GK6N4CqbGwhw24CmiAGE,2913
|
|
21
|
+
observabilityclient/v1/cli.py,sha256=ln7GsnbW95n90oFGw4zLiwHDjY0YHBxkRBzID-j6ZmA,4028
|
|
22
|
+
observabilityclient/v1/client.py,sha256=J0BWVJY8-IlsPTregtZ-w5MwSqzpVdE4aBd4EO8iDS8,1751
|
|
23
|
+
observabilityclient/v1/python_api.py,sha256=niW0NUWKK-Mdli72gWpP_tjGYEFr4RXc5J3giBkIkuA,3891
|
|
24
|
+
python_observabilityclient-1.0.0.dist-info/AUTHORS,sha256=uF42zVMEuwXhMW7eHDIzCpnu-RfapejbBNapd1-nRmc,532
|
|
25
|
+
python_observabilityclient-1.0.0.dist-info/LICENSE,sha256=qyh9xnj_IPtYWzClm5mZxCWRKTr2eUP_iLcU28DIvCw,11343
|
|
26
|
+
python_observabilityclient-1.0.0.dist-info/METADATA,sha256=mspVnIVKjwTxTnwgWfDF1wxng_sXYawjF4uGEOlJJ20,2998
|
|
27
|
+
python_observabilityclient-1.0.0.dist-info/WHEEL,sha256=iAkIy5fosb7FzIOwONchHf19Qu7_1wCWyFNR5gu9nU0,91
|
|
28
|
+
python_observabilityclient-1.0.0.dist-info/entry_points.txt,sha256=15rmai80pMRfBKZrk6yPNrRkAs2h63bwlcbQKq9fax4,424
|
|
29
|
+
python_observabilityclient-1.0.0.dist-info/pbr.json,sha256=YK4B_isOYwbQEIqmRFy2GYseqdIngtkBMAKKtlBB4BU,46
|
|
30
|
+
python_observabilityclient-1.0.0.dist-info/top_level.txt,sha256=Lf7Fwm3hfK-X1JNRy19rVlrMUOyQShBBPwsCU3pz6yw,20
|
|
31
|
+
python_observabilityclient-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"git_version": "21e4885", "is_release": true}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
observabilityclient/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
observabilityclient/client.py,sha256=cfRb816p6Ips9t2cdWvmSJ1uGrtTEHXQnctoExrLA5A,828
|
|
3
|
-
observabilityclient/i18n.py,sha256=PFo6BaOfyGQD5q7L3FunYJ8d4mwbRszG8wOrJIjki_4,790
|
|
4
|
-
observabilityclient/plugin.py,sha256=hyxENjNO6NLrH6G4muyAeEEtFTBa9d5OozjwpzNpJVA,2262
|
|
5
|
-
observabilityclient/prometheus_client.py,sha256=NcZAJRZzcuNFVJGdROwZBdwzpaV98_Ywcw29QBf7g18,7052
|
|
6
|
-
observabilityclient/tests/functional/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
-
observabilityclient/tests/functional/base.py,sha256=SXbPiksFH7aER-zZjIWxz5nwCqq5f1MYWth1tlFLwcs,6523
|
|
8
|
-
observabilityclient/tests/functional/test_cli.py,sha256=QqU8BCGUnh4IlwnGr6hdDHjHp7TIM0jasDjReyZpz-Y,4856
|
|
9
|
-
observabilityclient/tests/functional/test_python_api.py,sha256=NJBqBTEW_POoxI8CvMlNhWhDNAU3sQVKjRdXM2NboeU,3310
|
|
10
|
-
observabilityclient/tests/unit/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
-
observabilityclient/tests/unit/test_cli.py,sha256=TMKjb0IXwbMkm6frvfTqClOa5ZGGWKywyheyTeWl3i8,5964
|
|
12
|
-
observabilityclient/tests/unit/test_prometheus_client.py,sha256=oPD5AALoyR0haQ8u0RYEasFSicKjT1RW9g_sFYCtLNM,18801
|
|
13
|
-
observabilityclient/tests/unit/test_python_api.py,sha256=xOKCdLH52AF9Vf7tu4ja-O6zsC-FEH7Y-DJQ7x8jzy4,4760
|
|
14
|
-
observabilityclient/tests/unit/test_rbac.py,sha256=PxBlvSGqGVFBSjQB1MGcyuBX5qcv6peQKSL5R_Aid9c,5692
|
|
15
|
-
observabilityclient/tests/unit/test_utils.py,sha256=-sYsiYTuvcb71SX2KewrxkPinMEGLzUi0wnfBHsOIOA,7387
|
|
16
|
-
observabilityclient/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
17
|
-
observabilityclient/utils/metric_utils.py,sha256=K_G23krVIENNSPNsj2w9AkmCk2MDum9K3DkWNQl3Bro,3425
|
|
18
|
-
observabilityclient/v1/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
19
|
-
observabilityclient/v1/base.py,sha256=2AMQU8WKbERvfoiRdOPIlogzH1ds_Son4Gr7fjGdGQ4,2260
|
|
20
|
-
observabilityclient/v1/cli.py,sha256=ln7GsnbW95n90oFGw4zLiwHDjY0YHBxkRBzID-j6ZmA,4028
|
|
21
|
-
observabilityclient/v1/client.py,sha256=q5bH-N9uCWi9boVVYhLv6M7NYLxMnmgGBupg2uqvgRo,1643
|
|
22
|
-
observabilityclient/v1/python_api.py,sha256=nTsaasNBMVNrGW5CNSf7-o2oOKsea_u38GEbs2-8Gkw,3916
|
|
23
|
-
observabilityclient/v1/rbac.py,sha256=cnscaSzcbY7AEcLHcjpXBeHHZ12k5-hYmVwXsMTBSjQ,6115
|
|
24
|
-
python_observabilityclient-0.4.0.dist-info/AUTHORS,sha256=zWBYZ8rVSwsa_gRSTSrH1_WEtElkJl2LO7nWANcVao0,432
|
|
25
|
-
python_observabilityclient-0.4.0.dist-info/LICENSE,sha256=qyh9xnj_IPtYWzClm5mZxCWRKTr2eUP_iLcU28DIvCw,11343
|
|
26
|
-
python_observabilityclient-0.4.0.dist-info/METADATA,sha256=qqBvE_I-2DBK1SWhWWo01QBvXzbISkpmeVT304ANO5M,2998
|
|
27
|
-
python_observabilityclient-0.4.0.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
|
|
28
|
-
python_observabilityclient-0.4.0.dist-info/entry_points.txt,sha256=15rmai80pMRfBKZrk6yPNrRkAs2h63bwlcbQKq9fax4,424
|
|
29
|
-
python_observabilityclient-0.4.0.dist-info/pbr.json,sha256=pdbs7sVnVCndbMaEPpoTrrs2jQhgqgrBFuTvBUaC--o,46
|
|
30
|
-
python_observabilityclient-0.4.0.dist-info/top_level.txt,sha256=Lf7Fwm3hfK-X1JNRy19rVlrMUOyQShBBPwsCU3pz6yw,20
|
|
31
|
-
python_observabilityclient-0.4.0.dist-info/RECORD,,
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"git_version": "df6c253", "is_release": true}
|
{python_observabilityclient-0.4.0.dist-info → python_observabilityclient-1.0.0.dist-info}/LICENSE
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|