-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: add default time range filter for ListLogEntries API #304
fix: add default time range filter for ListLogEntries API #304
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 <[email protected]>
…ggingImplTest.java Co-authored-by: yoshi-code-bot <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions could not be made:
- google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
- lines 39-39
- lines 638-641
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions could not be made:
- google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
- lines 39-39
- lines 638-641
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions could not be made:
- google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java
- lines 39-39
- lines 638-641
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
if (filter != null) { | ||
if (!filter.contains("timestamp")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should try filter.lower(). "TIMESTAMP" is also allowed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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(); |
if (filter != null) { | ||
if (!filter.toLowerCase().contains("timestamp")) { | ||
filter = String.format("%s AND %s", filter, defaultTimestampFilterCreator.createDefaultTimestampFilter()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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\""; | |
} | |
}; |
@@ -1744,11 +1756,17 @@ public void testListLogEntriesNextPage() throws ExecutionException, InterruptedE | |||
String cursor1 = "cursor"; | |||
EasyMock.replay(rpcFactoryMock); | |||
logging = options.getService(); | |||
|
|||
String defaultTimeFilter = LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String defaultTimeFilter = LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter(); | |
String defaultTimeFilter = | |
LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter(); |
@@ -1809,7 +1831,8 @@ public void testListLogEntriesWithOptions() { | |||
ListLogEntriesRequest.newBuilder() | |||
.addResourceNames(PROJECT_PB) | |||
.setOrderBy("timestamp desc") | |||
.setFilter("logName:syslog") | |||
.setFilter( | |||
String.format("logName:syslog AND %s", LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String.format("logName:syslog AND %s", LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter())) | |
String.format( | |
"logName:syslog AND %s", | |
LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter())) |
@@ -1915,11 +1948,12 @@ public void testListLogEntriesAsyncWithOptions() throws ExecutionException, Inte | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 | |
} |
ListLogEntriesRequest request = | ||
ListLogEntriesRequest.newBuilder() | ||
.addResourceNames(PROJECT_PB) | ||
.setOrderBy("timestamp desc") | ||
.setFilter("logName:syslog") | ||
.setFilter(filter) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 ☕️