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

Commit

Permalink
feat: add "not equal" support to the query filter (#11)
Browse files Browse the repository at this point in the history
* feat(monitoring): Add "not equal" support to the query filter

* chore: blacken

Co-authored-by: Bu Sun Kim <busunkim@google.com>
  • Loading branch information
qingling128 and busunkim96 committed Aug 20, 2020
1 parent ea5c2d8 commit e293f7f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
24 changes: 21 additions & 3 deletions google/cloud/monitoring_v3/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ def select_projects(self, *args):
def select_resources(self, *args, **kwargs):
"""Copy the query and add filtering by resource labels.
See more documentation at: https://cloud.google.com/monitoring/api/v3/filters#comparisons.
Examples::
query = query.select_resources(zone='us-central1-a')
Expand Down Expand Up @@ -273,8 +275,14 @@ def select_metrics(self, *args, **kwargs):
metric.label.<label> = "<value>"
However, by adding ``"_prefix"`` or ``"_suffix"`` to the keyword,
you can specify a partial match.
However, by adding ``"_notequal"`` to the keyword, you can inequality:
``<label>_notequal=<value>`` generates::
metric.label.<label> != <value>
By adding ``"_prefix"`` or ``"_suffix"`` to the keyword, you can specify
a partial match.
``<label>_prefix=<value>`` generates::
Expand Down Expand Up @@ -596,7 +604,15 @@ def _build_label_filter(category, *args, **kwargs):

suffix = None
if key.endswith(
("_prefix", "_suffix", "_greater", "_greaterequal", "_less", "_lessequal")
(
"_prefix",
"_suffix",
"_greater",
"_greaterequal",
"_less",
"_lessequal",
"_notequal",
)
):
key, suffix = key.rsplit("_", 1)

Expand All @@ -617,6 +633,8 @@ def _build_label_filter(category, *args, **kwargs):
term = "{key} < {value}"
elif suffix == "lessequal":
term = "{key} <= {value}"
elif suffix == "notequal":
term = "{key} != {value}"
else:
term = '{key} = "{value}"'

Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,11 @@ def test_metric_labels(self):
)
self.assertEqual(actual, expected)

def test_metric_label_response_code_not_equal(self):
actual = self._call_fut("metric", response_code_notequal=200)
expected = "metric.label.response_code != 200"
self.assertEqual(actual, expected)

def test_metric_label_response_code_greater_less(self):
actual = self._call_fut(
"metric", response_code_greater=500, response_code_less=600
Expand Down

0 comments on commit e293f7f

Please sign in to comment.