From 7dcd565d20d2ab0fdd319fb4570f94a2c03e76c2 Mon Sep 17 00:00:00 2001 From: abhishekacharya200 Date: Fri, 16 Oct 2026 15:36:59 +0545 Subject: [PATCH] fix(logging): only skip the default 24 hour filter for timestamp restrictions list_entries skipped its default timestamp filter whenever the substring 'timestamp' appeared anywhere in the filter, such as in jsonPayload.timestamp_ms or a text search. Only skip it when the filter compares the top-level timestamp or receiveTimestamp field. Fixes #17338 --- .../google/cloud/logging_v2/_helpers.py | 9 +++++- .../tests/unit/test__helpers.py | 28 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/google-cloud-logging/google/cloud/logging_v2/_helpers.py b/packages/google-cloud-logging/google/cloud/logging_v2/_helpers.py index c3cbcc9c013b..128ef5d139ee 100644 --- a/packages/google-cloud-logging/google/cloud/logging_v2/_helpers.py +++ b/packages/google-cloud-logging/google/cloud/logging_v2/_helpers.py @@ -15,6 +15,7 @@ """Common logging helpers.""" import logging +import re from datetime import datetime, timedelta, timezone import requests @@ -56,6 +57,12 @@ class LogSeverity(object): _TIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%f%z" """Time format for timestamps used in API""" +_TIME_RESTRICTION_RE = re.compile( + r"(?=!:]", re.IGNORECASE +) +"""Matches a comparison on the top-level ``timestamp`` or ``receiveTimestamp`` +field, but not on fields like ``jsonPayload.timestamp`` or ``timestamp_ms``.""" + METADATA_URL = "http://metadata.google.internal./computeMetadata/v1/" METADATA_HEADERS = {"Metadata-Flavor": "Google"} @@ -146,6 +153,6 @@ def _add_defaults_to_filter(filter_): time_filter = f'timestamp>="{yesterday.strftime(_TIME_FORMAT)}"' if filter_ is None: filter_ = time_filter - elif "timestamp" not in filter_.lower(): + elif not _TIME_RESTRICTION_RE.search(filter_): filter_ = f"{filter_} AND {time_filter}" return filter_ diff --git a/packages/google-cloud-logging/tests/unit/test__helpers.py b/packages/google-cloud-logging/tests/unit/test__helpers.py index 706cf8e17cbd..5b01b72848f7 100644 --- a/packages/google-cloud-logging/tests/unit/test__helpers.py +++ b/packages/google-cloud-logging/tests/unit/test__helpers.py @@ -215,6 +215,34 @@ def test_filter_defaults_capitalized_timestamp(self): out_filter = self._add_defaults_to_filter(in_filter) self.assertEqual(in_filter, out_filter) + def test_filter_defaults_user_time_restriction(self): + """Any comparison on timestamp or receiveTimestamp skips the default""" + test_inputs = [ + 'timestamp >= "2020-10-13T21:00:00Z"', + 'severity>=ERROR AND timestamp<"2020-10-13"', + 'NOT timestamp:"2020-10-13"', + 'receiveTimestamp>"2020-10-13"', + ] + for in_filter in test_inputs: + out_filter = self._add_defaults_to_filter(in_filter) + self.assertEqual(in_filter, out_filter) + + def test_filter_defaults_timestamp_substring(self): + """Mentioning timestamp elsewhere should still add the default""" + test_inputs = [ + "jsonPayload.timestamp_ms>0", + 'jsonPayload.timestamp>="2020-10-13"', + 'textPayload:"timestamp"', + 'labels.event_timestamp="x"', + ] + for in_filter in test_inputs: + out_filter = self._add_defaults_to_filter(in_filter) + timestamp = datetime.strptime( + out_filter, in_filter + ' AND timestamp>="' + self._time_format() + '"' + ) + yesterday = datetime.now(timezone.utc) - timedelta(days=1) + self.assertLess(yesterday - timestamp, timedelta(minutes=1)) + class EntryMock(object): def __init__(self):