fix: add default time range filter for ListLogEntries API#304
fix: add default time range filter for ListLogEntries API#304simonz130 merged 12 commits intogoogleapis:masterfrom
Conversation
yoshi-code-bot
left a comment
There was a problem hiding this comment.
Some suggestions could not be made:
- google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
- lines 39-39
- lines 639-641
- google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
- lines 1488-1488
google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
Codecov Report
@@ Coverage Diff @@
## master #304 +/- ##
============================================
+ Coverage 75.77% 75.85% +0.08%
- Complexity 636 640 +4
============================================
Files 43 44 +1
Lines 3863 3876 +13
Branches 280 281 +1
============================================
+ Hits 2927 2940 +13
Misses 772 772
Partials 164 164
Continue to review full report at Codecov.
|
yoshi-code-bot
left a comment
There was a problem hiding this comment.
Some suggestions could not be made:
- google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
- lines 39-39
- lines 639-641
- google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
- lines 1488-1488
google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
d69cdde to
552997c
Compare
yoshi-code-bot
left a comment
There was a problem hiding this comment.
Some suggestions could not be made:
- google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
- lines 39-39
- lines 639-641
- google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
- lines 1488-1488
google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
yoshi-code-bot
left a comment
There was a problem hiding this comment.
Some suggestions could not be made:
- google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
- lines 39-39
- lines 638-640
- google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
- lines 1491-1491
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
Outdated
Show resolved
Hide resolved
…ggingImplTest.java Co-authored-by: yoshi-code-bot <70984784+yoshi-code-bot@users.noreply.github.com>
…ggingImplTest.java Co-authored-by: yoshi-code-bot <70984784+yoshi-code-bot@users.noreply.github.com>
yoshi-code-bot
left a comment
There was a problem hiding this comment.
Some suggestions could not be made:
- google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
- lines 39-39
- lines 638-641
yoshi-code-bot
left a comment
There was a problem hiding this comment.
Some suggestions could not be made:
- google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
- lines 39-39
- lines 638-641
yoshi-code-bot
left a comment
There was a problem hiding this comment.
Some suggestions could not be made:
- google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
- lines 39-39
- lines 638-641
yoshi-code-bot
left a comment
There was a problem hiding this comment.
Some suggestions could not be made:
- google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
- lines 39-39
- lines 789-792
| builder.setFilter(filter); | ||
| } | ||
| else { | ||
| // If filter is not specified, default filter is looking back 24 hours in line with gcloud behavior | ||
| builder.setFilter(createDefaultTimeRangeFilter()); |
There was a problem hiding this comment.
| builder.setFilter(filter); | |
| } | |
| else { | |
| // If filter is not specified, default filter is looking back 24 hours in line with gcloud behavior | |
| builder.setFilter(createDefaultTimeRangeFilter()); | |
| builder.setFilter(filter); | |
| } else { | |
| // If filter is not specified, default filter is looking back 24 hours in line with gcloud | |
| // behavior |
google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
Show resolved
Hide resolved
| calendar.set(Calendar.HOUR_OF_DAY, 0); | ||
| calendar.set(Calendar.MINUTE, 0); | ||
| calendar.set(Calendar.SECOND, 0); | ||
| calendar.set(Calendar.MILLISECOND, 0); |
There was a problem hiding this comment.
This reads to me like "yesterday at midnight", not "24 hours ago". Is that right?
| } | ||
|
|
||
| private static Date yesterday() { | ||
| Calendar calendar = Calendar.getInstance(); |
There was a problem hiding this comment.
What timezone is used here? Is that accounted for?
google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
Outdated
Show resolved
Hide resolved
| // Make sure timestamp filter is either explicitly specified or we add a default time filter | ||
| // of 24 hours back to be inline with gcloud behavior for the same API | ||
| if (filter != null) { | ||
| if (!filter.contains("timestamp")) { |
There was a problem hiding this comment.
you should try filter.lower(). "TIMESTAMP" is also allowed
yoshi-code-bot
left a comment
There was a problem hiding this comment.
Some suggestions could not be made:
- google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java
- lines 80-81
- lines 1747-1747
| * Encapsulates implementation of default time filter. | ||
| * This is needed for testing since we can't mock static classes | ||
| * with EasyMock | ||
| */ | ||
| public interface ITimestampDefaultFilter { | ||
|
|
||
| /** | ||
| * Creates a default filter with timestamp to query Cloud Logging |
There was a problem hiding this comment.
| * Encapsulates implementation of default time filter. | |
| * This is needed for testing since we can't mock static classes | |
| * with EasyMock | |
| */ | |
| public interface ITimestampDefaultFilter { | |
| /** | |
| * Creates a default filter with timestamp to query Cloud Logging | |
| * Encapsulates implementation of default time filter. This is needed for testing since we can't | |
| * mock static classes with EasyMock | |
| /** | |
| * Creates a default filter with timestamp to query Cloud Logging | |
| * | |
| * @return The filter using timestamp field | |
| */ | |
| String createDefaultTimestampFilter(); |
| // of 24 hours back to be inline with gcloud behavior for the same API | ||
| if (filter != null) { | ||
| if (!filter.toLowerCase().contains("timestamp")) { | ||
| filter = String.format("%s AND %s", filter, defaultTimestampFilterCreator.createDefaultTimestampFilter()); |
There was a problem hiding this comment.
| filter = String.format("%s AND %s", filter, defaultTimestampFilterCreator.createDefaultTimestampFilter()); | |
| filter = | |
| String.format( | |
| "%s AND %s", filter, defaultTimestampFilterCreator.createDefaultTimestampFilter()); |
| listLogEntriesResponse.getEntriesList() == null | ||
| ? ImmutableList.<LogEntry>of() | ||
| : Lists.transform( |
There was a problem hiding this comment.
| listLogEntriesResponse.getEntriesList() == null | |
| ? ImmutableList.<LogEntry>of() | |
| : Lists.transform( | |
| listLogEntriesResponse.getEntriesList() == null | |
| listLogEntriesResponse.getEntriesList(), LogEntry.FROM_PB_FUNCTION); | |
| listLogEntriesResponse.getNextPageToken().equals("") |
| @Override | ||
| public String createDefaultTimestampFilter() { | ||
| DateFormat rfcDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); | ||
| rfcDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); | ||
| return "timestamp>=\"" + rfcDateFormat.format(yesterday()) + "\""; | ||
| } | ||
|
|
||
| private Date yesterday() { | ||
| TimeZone timeZone = TimeZone.getTimeZone("UTC"); | ||
| Calendar calendar = Calendar.getInstance(timeZone); | ||
| calendar.add(Calendar.DATE, -1); | ||
|
|
There was a problem hiding this comment.
| @Override | |
| public String createDefaultTimestampFilter() { | |
| DateFormat rfcDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); | |
| rfcDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); | |
| return "timestamp>=\"" + rfcDateFormat.format(yesterday()) + "\""; | |
| } | |
| private Date yesterday() { | |
| TimeZone timeZone = TimeZone.getTimeZone("UTC"); | |
| Calendar calendar = Calendar.getInstance(timeZone); | |
| calendar.add(Calendar.DATE, -1); | |
| @Override | |
| public String createDefaultTimestampFilter() { | |
| DateFormat rfcDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); | |
| rfcDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); | |
| return "timestamp>=\"" + rfcDateFormat.format(yesterday()) + "\""; | |
| } | |
| private Date yesterday() { | |
| TimeZone timeZone = TimeZone.getTimeZone("UTC"); | |
| Calendar calendar = Calendar.getInstance(timeZone); | |
| calendar.add(Calendar.DATE, -1); | |
| return calendar.getTime(); | |
| } |
| LoggingImpl.defaultTimestampFilterCreator = new ITimestampDefaultFilter() { | ||
| @Override | ||
| public String createDefaultTimestampFilter() { | ||
| return "timestamp>=\"2020-10-30T15:39:09Z\""; | ||
| } | ||
| }; |
There was a problem hiding this comment.
| LoggingImpl.defaultTimestampFilterCreator = new ITimestampDefaultFilter() { | |
| @Override | |
| public String createDefaultTimestampFilter() { | |
| return "timestamp>=\"2020-10-30T15:39:09Z\""; | |
| } | |
| }; | |
| LoggingImpl.defaultTimestampFilterCreator = | |
| new ITimestampDefaultFilter() { | |
| @Override | |
| public String createDefaultTimestampFilter() { | |
| return "timestamp>=\"2020-10-30T15:39:09Z\""; | |
| } | |
| }; |
| EasyMock.replay(rpcFactoryMock); | ||
| logging = options.getService(); | ||
|
|
||
| String defaultTimeFilter = LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter(); |
There was a problem hiding this comment.
| String defaultTimeFilter = LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter(); | |
| String defaultTimeFilter = | |
| LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter(); |
| .setOrderBy("timestamp desc") | ||
| .setFilter("logName:syslog") | ||
| .setFilter( | ||
| String.format("logName:syslog AND %s", LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter())) |
There was a problem hiding this comment.
| String.format("logName:syslog AND %s", LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter())) | |
| String.format( | |
| "logName:syslog AND %s", | |
| LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter())) |
| String cursor = "cursor"; | ||
| EasyMock.replay(rpcFactoryMock); | ||
| logging = options.getService(); | ||
| String filter = String.format("logName:syslog AND %s", LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter()); |
There was a problem hiding this comment.
| String filter = String.format("logName:syslog AND %s", LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter()); | |
| String filter = | |
| String.format( | |
| "logName:syslog AND %s", | |
| LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter()); |
| import org.junit.Test; | ||
|
|
||
| import javax.management.timer.Timer; | ||
| import java.text.DateFormat; | ||
| import java.text.SimpleDateFormat; | ||
| import java.util.Calendar; | ||
| import java.util.Date; | ||
| import java.util.TimeZone; | ||
|
|
||
| import static org.junit.Assert.assertTrue; | ||
| import static org.junit.Assert.fail; | ||
|
|
||
| public class TimestampDefaultFilterTest { | ||
|
|
||
| @Test | ||
| public void DefaultTimestampFilterTest() { | ||
| ITimestampDefaultFilter filter = new TimestampDefaultFilter(); | ||
|
|
||
| TimeZone timeZone = TimeZone.getTimeZone("UTC"); | ||
| Calendar calendar = Calendar.getInstance(timeZone); | ||
| calendar.add(Calendar.DATE, -1); | ||
| Date expected = calendar.getTime(); | ||
|
|
||
| // Timestamp filter exists | ||
| String defaultFilter = filter.createDefaultTimestampFilter(); | ||
| assertTrue(defaultFilter.contains("timestamp>=")); | ||
|
|
||
| // Time is last 24 hours |
There was a problem hiding this comment.
| import org.junit.Test; | |
| import javax.management.timer.Timer; | |
| import java.text.DateFormat; | |
| import java.text.SimpleDateFormat; | |
| import java.util.Calendar; | |
| import java.util.Date; | |
| import java.util.TimeZone; | |
| import static org.junit.Assert.assertTrue; | |
| import static org.junit.Assert.fail; | |
| public class TimestampDefaultFilterTest { | |
| @Test | |
| public void DefaultTimestampFilterTest() { | |
| ITimestampDefaultFilter filter = new TimestampDefaultFilter(); | |
| TimeZone timeZone = TimeZone.getTimeZone("UTC"); | |
| Calendar calendar = Calendar.getInstance(timeZone); | |
| calendar.add(Calendar.DATE, -1); | |
| Date expected = calendar.getTime(); | |
| // Timestamp filter exists | |
| String defaultFilter = filter.createDefaultTimestampFilter(); | |
| assertTrue(defaultFilter.contains("timestamp>=")); | |
| // Time is last 24 hours | |
| import static org.junit.Assert.assertTrue; | |
| import static org.junit.Assert.fail; | |
| import javax.management.timer.Timer; | |
| import org.junit.Test; | |
| @Test | |
| public void DefaultTimestampFilterTest() { | |
| ITimestampDefaultFilter filter = new TimestampDefaultFilter(); | |
| TimeZone timeZone = TimeZone.getTimeZone("UTC"); | |
| Calendar calendar = Calendar.getInstance(timeZone); | |
| calendar.add(Calendar.DATE, -1); | |
| Date expected = calendar.getTime(); | |
| // Timestamp filter exists | |
| String defaultFilter = filter.createDefaultTimestampFilter(); | |
| assertTrue(defaultFilter.contains("timestamp>=")); | |
| // Time is last 24 hours | |
| try { | |
| DateFormat rfcDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); | |
| rfcDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); | |
| Date actual = rfcDateFormat.parse(defaultFilter.substring(12, defaultFilter.length() - 1)); | |
| assertTrue(Math.abs(expected.getTime() - actual.getTime()) < Timer.ONE_MINUTE); | |
| } catch (java.text.ParseException ex) { | |
| fail(); // Just fail if exception is thrown | |
| } |
| .addResourceNames(PROJECT_PB) | ||
| .setOrderBy("timestamp desc") | ||
| .setFilter("logName:syslog") | ||
| .setFilter(filter) |
There was a problem hiding this comment.
I'm not sure if I'm reading these tests right, but they all seem to include a timestamp as part of the filter. I'd expect to see tests with no filter added to make sure it's added automatically, and tests with "timestamp" or "TIMESTAMP" in the string to make sure those are behaving as expected
Making the behavior of ListLogEntries API consistent with gcloud: if filter wasn't specified we now attach a default filter of last 24 hours. This will prevent calls from hanging when listing all logs from projects with high log volumes.
Fixes #303 ☕️