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

Commit

Permalink
feat(monitoring): Add "not equal" support to the query filter
Browse files Browse the repository at this point in the history
  • Loading branch information
qingling128 committed Jul 31, 2020
1 parent ea5c2d8 commit c51dc74
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
16 changes: 13 additions & 3 deletions google/cloud/monitoring_v3/query.py
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,7 @@ 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 +625,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
9 changes: 9 additions & 0 deletions tests/unit/test_query.py
Expand Up @@ -517,6 +517,15 @@ 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 c51dc74

Please sign in to comment.