Skip to content
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

Merged
merged 12 commits into from Nov 11, 2020

Conversation

simonz130
Copy link

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.

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #303 ☕️

@simonz130 simonz130 added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. priority: p2 Moderately-important priority. Fix may not be included in next release. api: logging Issues related to the googleapis/java-logging API. lang: java Issues specific to Java. labels Oct 29, 2020
@simonz130 simonz130 requested a review from a team as a code owner October 29, 2020 06:35
@simonz130 simonz130 requested a review from a team October 29, 2020 06:35
@simonz130 simonz130 requested a review from a team as a code owner October 29, 2020 06:35
@simonz130 simonz130 self-assigned this Oct 29, 2020
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Oct 29, 2020
Copy link

@yoshi-code-bot yoshi-code-bot left a 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

@codecov
Copy link

codecov bot commented Oct 29, 2020

Codecov Report

Merging #304 (a0e16a9) into master (983f106) will increase coverage by 0.08%.
The diff coverage is 89.47%.

Impacted file tree graph

@@             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              
Impacted Files Coverage Δ Complexity Δ
...ain/java/com/google/cloud/logging/LoggingImpl.java 89.85% <81.81%> (+0.14%) 92.00 <1.00> (+1.00)
...m/google/cloud/logging/TimestampDefaultFilter.java 100.00% <100.00%> (ø) 3.00 <3.00> (?)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 983f106...a0e16a9. Read the comment docs.

Copy link

@yoshi-code-bot yoshi-code-bot left a 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

Copy link

@yoshi-code-bot yoshi-code-bot left a 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

Copy link

@yoshi-code-bot yoshi-code-bot left a 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

Simon Zeltser and others added 2 commits November 2, 2020 09:28
…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>
Copy link

@yoshi-code-bot yoshi-code-bot left a 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

Copy link

@yoshi-code-bot yoshi-code-bot left a 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

Copy link

@yoshi-code-bot yoshi-code-bot left a 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

Copy link

@yoshi-code-bot yoshi-code-bot left a 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

Comment on lines 787 to 791
builder.setFilter(filter);
}
else {
// If filter is not specified, default filter is looking back 24 hours in line with gcloud behavior
builder.setFilter(createDefaultTimeRangeFilter());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

@simonz130 simonz130 added the kokoro:run Add this label to force Kokoro to re-run the tests. label Nov 10, 2020
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Copy link
Contributor

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();
Copy link
Contributor

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?

@yoshi-kokoro yoshi-kokoro removed the kokoro:run Add this label to force Kokoro to re-run the tests. label Nov 10, 2020
if (filter != null) {
if (!filter.contains("timestamp")) {
Copy link
Contributor

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

Copy link

@yoshi-code-bot yoshi-code-bot left a 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

Comment on lines 20 to 27
* 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
filter = String.format("%s AND %s", filter, defaultTimestampFilterCreator.createDefaultTimestampFilter());
filter =
String.format(
"%s AND %s", filter, defaultTimestampFilterCreator.createDefaultTimestampFilter());

Comment on lines 809 to 811
listLogEntriesResponse.getEntriesList() == null
? ImmutableList.<LogEntry>of()
: Lists.transform(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
listLogEntriesResponse.getEntriesList() == null
? ImmutableList.<LogEntry>of()
: Lists.transform(
listLogEntriesResponse.getEntriesList() == null
listLogEntriesResponse.getEntriesList(), LogEntry.FROM_PB_FUNCTION);
listLogEntriesResponse.getNextPageToken().equals("")

Comment on lines 26 to 37
@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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@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();
}

Comment on lines 195 to 200
LoggingImpl.defaultTimestampFilterCreator = new ITimestampDefaultFilter() {
@Override
public String createDefaultTimestampFilter() {
return "timestamp>=\"2020-10-30T15:39:09Z\"";
}
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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()))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
String filter = String.format("logName:syslog AND %s", LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter());
String filter =
String.format(
"logName:syslog AND %s",
LoggingImpl.defaultTimestampFilterCreator.createDefaultTimestampFilter());

Comment on lines 19 to 46
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
Copy link
Contributor

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

@simonz130 simonz130 merged commit c2f40df into googleapis:master Nov 11, 2020
@simonz130 simonz130 deleted the fixListLogsFlackiness branch November 11, 2020 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: logging Issues related to the googleapis/java-logging API. cla: yes This human has signed the Contributor License Agreement. lang: java Issues specific to Java. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make ListLogEntries by default return logs from last 24 hours
6 participants