Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

Commit e293f7f

Browse files
feat: add "not equal" support to the query filter (#11)
* feat(monitoring): Add "not equal" support to the query filter * chore: blacken Co-authored-by: Bu Sun Kim <busunkim@google.com>
1 parent ea5c2d8 commit e293f7f

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

google/cloud/monitoring_v3/query.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ def select_projects(self, *args):
206206
def select_resources(self, *args, **kwargs):
207207
"""Copy the query and add filtering by resource labels.
208208
209+
See more documentation at: https://cloud.google.com/monitoring/api/v3/filters#comparisons.
210+
209211
Examples::
210212
211213
query = query.select_resources(zone='us-central1-a')
@@ -273,8 +275,14 @@ def select_metrics(self, *args, **kwargs):
273275
274276
metric.label.<label> = "<value>"
275277
276-
However, by adding ``"_prefix"`` or ``"_suffix"`` to the keyword,
277-
you can specify a partial match.
278+
However, by adding ``"_notequal"`` to the keyword, you can inequality:
279+
280+
``<label>_notequal=<value>`` generates::
281+
282+
metric.label.<label> != <value>
283+
284+
By adding ``"_prefix"`` or ``"_suffix"`` to the keyword, you can specify
285+
a partial match.
278286
279287
``<label>_prefix=<value>`` generates::
280288
@@ -596,7 +604,15 @@ def _build_label_filter(category, *args, **kwargs):
596604

597605
suffix = None
598606
if key.endswith(
599-
("_prefix", "_suffix", "_greater", "_greaterequal", "_less", "_lessequal")
607+
(
608+
"_prefix",
609+
"_suffix",
610+
"_greater",
611+
"_greaterequal",
612+
"_less",
613+
"_lessequal",
614+
"_notequal",
615+
)
600616
):
601617
key, suffix = key.rsplit("_", 1)
602618

@@ -617,6 +633,8 @@ def _build_label_filter(category, *args, **kwargs):
617633
term = "{key} < {value}"
618634
elif suffix == "lessequal":
619635
term = "{key} <= {value}"
636+
elif suffix == "notequal":
637+
term = "{key} != {value}"
620638
else:
621639
term = '{key} = "{value}"'
622640

tests/unit/test_query.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,11 @@ def test_metric_labels(self):
517517
)
518518
self.assertEqual(actual, expected)
519519

520+
def test_metric_label_response_code_not_equal(self):
521+
actual = self._call_fut("metric", response_code_notequal=200)
522+
expected = "metric.label.response_code != 200"
523+
self.assertEqual(actual, expected)
524+
520525
def test_metric_label_response_code_greater_less(self):
521526
actual = self._call_fut(
522527
"metric", response_code_greater=500, response_code_less=600

0 commit comments

Comments
 (0)