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
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@
import com.google.logging.v2.WriteLogEntriesRequest;
import com.google.logging.v2.WriteLogEntriesResponse;
import com.google.protobuf.Empty;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -779,9 +783,19 @@ static ListLogEntriesRequest listLogEntriesRequest(
builder.setOrderBy(orderBy);
}
String filter = FILTER.get(options);
// 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")) {
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

filter = String.format("%s AND %s", filter, createDefaultTimeRangeFilter());
}
builder.setFilter(filter);
} else {
simonz130 marked this conversation as resolved.
Show resolved Hide resolved
// If filter is not specified, default filter is looking back 24 hours in line with gcloud
// behavior
builder.setFilter(createDefaultTimeRangeFilter());
simonz130 marked this conversation as resolved.
Show resolved Hide resolved
}

return builder.build();
}

Expand Down Expand Up @@ -843,4 +857,21 @@ public void close() throws Exception {
int getNumPendingWrites() {
return pendingWrites.size();
}

@VisibleForTesting
static String createDefaultTimeRangeFilter() {
DateFormat rfcDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
return "timestamp>=\"" + rfcDateFormat.format(yesterday()) + "\"";
}

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?

calendar.add(Calendar.DATE, -1);
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?


return calendar.getTime();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
import com.google.logging.v2.WriteLogEntriesResponse;
import com.google.protobuf.Empty;
import com.google.protobuf.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -1724,7 +1728,11 @@ public void testListLogEntries() {
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogEntriesRequest request =
ListLogEntriesRequest.newBuilder().addResourceNames(PROJECT_PB).build();
ListLogEntriesRequest.newBuilder()
.addResourceNames(PROJECT_PB)
.setFilter(createDefaultTimeRangeFilter())
.build();

List<LogEntry> entriesList = ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2);
ListLogEntriesResponse response =
ListLogEntriesResponse.newBuilder()
Expand All @@ -1745,11 +1753,15 @@ public void testListLogEntriesNextPage() throws ExecutionException, InterruptedE
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogEntriesRequest request1 =
ListLogEntriesRequest.newBuilder().addResourceNames(PROJECT_PB).build();
ListLogEntriesRequest.newBuilder()
.addResourceNames(PROJECT_PB)
.setFilter(createDefaultTimeRangeFilter())
.build();
ListLogEntriesRequest request2 =
ListLogEntriesRequest.newBuilder()
.addResourceNames(PROJECT_PB)
.setPageToken(cursor1)
.setFilter(createDefaultTimeRangeFilter())
.build();
List<LogEntry> descriptorList1 = ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2);
List<LogEntry> descriptorList2 = ImmutableList.of(LOG_ENTRY1);
Expand Down Expand Up @@ -1785,7 +1797,11 @@ public void testListLogEntriesEmpty() {
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogEntriesRequest request =
ListLogEntriesRequest.newBuilder().addResourceNames(PROJECT_PB).build();
ListLogEntriesRequest.newBuilder()
.addResourceNames(PROJECT_PB)
.setFilter(createDefaultTimeRangeFilter())
.build();

List<LogEntry> entriesList = ImmutableList.of();
ListLogEntriesResponse response =
ListLogEntriesResponse.newBuilder()
Expand All @@ -1809,7 +1825,8 @@ public void testListLogEntriesWithOptions() {
ListLogEntriesRequest.newBuilder()
.addResourceNames(PROJECT_PB)
.setOrderBy("timestamp desc")
.setFilter("logName:syslog")
.setFilter(
String.format("logName:syslog AND %s", LoggingImpl.createDefaultTimeRangeFilter()))
.build();
List<LogEntry> entriesList = ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2);
ListLogEntriesResponse response =
Expand All @@ -1834,7 +1851,10 @@ public void testListLogEntriesAsync() throws ExecutionException, InterruptedExce
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogEntriesRequest request =
ListLogEntriesRequest.newBuilder().addResourceNames(PROJECT_PB).build();
ListLogEntriesRequest.newBuilder()
.addResourceNames(PROJECT_PB)
.setFilter(createDefaultTimeRangeFilter())
.build();
List<LogEntry> entriesList = ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2);
ListLogEntriesResponse response =
ListLogEntriesResponse.newBuilder()
Expand All @@ -1855,10 +1875,14 @@ public void testListLogEntriesAsyncNextPage() {
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogEntriesRequest request1 =
ListLogEntriesRequest.newBuilder().addResourceNames(PROJECT_PB).build();
ListLogEntriesRequest.newBuilder()
.addResourceNames(PROJECT_PB)
.setFilter(createDefaultTimeRangeFilter())
.build();
ListLogEntriesRequest request2 =
ListLogEntriesRequest.newBuilder()
.addResourceNames(PROJECT_PB)
.setFilter(createDefaultTimeRangeFilter())
.setPageToken(cursor1)
.build();
List<LogEntry> descriptorList1 = ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2);
Expand Down Expand Up @@ -1890,12 +1914,15 @@ public void testListLogEntriesAsyncNextPage() {
}

@Test
public void testListLogEntriesAyncEmpty() throws ExecutionException, InterruptedException {
public void testListLogEntriesAsyncEmpty() throws ExecutionException, InterruptedException {
String cursor = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogEntriesRequest request =
ListLogEntriesRequest.newBuilder().addResourceNames(PROJECT_PB).build();
ListLogEntriesRequest.newBuilder()
.addResourceNames(PROJECT_PB)
.setFilter(createDefaultTimeRangeFilter())
.build();
List<LogEntry> entriesList = ImmutableList.of();
ListLogEntriesResponse response =
ListLogEntriesResponse.newBuilder()
Expand All @@ -1919,7 +1946,8 @@ public void testListLogEntriesAsyncWithOptions() throws ExecutionException, Inte
ListLogEntriesRequest.newBuilder()
.addResourceNames(PROJECT_PB)
.setOrderBy("timestamp desc")
.setFilter("logName:syslog")
.setFilter(
String.format("logName:syslog AND %s", LoggingImpl.createDefaultTimeRangeFilter()))
.build();
List<LogEntry> entriesList = ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2);
ListLogEntriesResponse response =
Expand Down Expand Up @@ -2016,4 +2044,20 @@ public void run() {
}
assertSame(0, exceptions.get());
}

private static String createDefaultTimeRangeFilter() {
DateFormat rfcDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
return "timestamp>=\"" + rfcDateFormat.format(yesterday()) + "\"";
}

private static Date yesterday() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

return calendar.getTime();
}
}