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

feat: add "not equal" support to the query filter #11

Merged
merged 2 commits into from Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 21 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:
busunkim96 marked this conversation as resolved.
Show resolved Hide resolved

``<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
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