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
Expand Up @@ -36,6 +36,7 @@
import com.google.cloud.MonitoredResource;
import com.google.cloud.MonitoredResourceDescriptor;
import com.google.cloud.PageImpl;
import com.google.cloud.logging.Logging.EntryListOption.OptionType;
import com.google.cloud.logging.spi.v2.LoggingRpc;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
Expand Down Expand Up @@ -77,7 +78,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 @@ -782,6 +787,11 @@ static ListLogEntriesRequest listLogEntriesRequest(
if (filter != null) {
builder.setFilter(filter);
}
else {
// 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 +853,20 @@ public void close() throws Exception {
int getNumPendingWrites() {
return pendingWrites.size();
}

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();
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();
}
}
Expand Up @@ -76,6 +76,10 @@
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.Calendar;
import java.util.Date;
import com.google.protobuf.Timestamp;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -1723,8 +1727,12 @@ public void testListLogEntries() {
String cursor = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogEntriesRequest request =
ListLogEntriesRequest.newBuilder().addResourceNames(PROJECT_PB).build();
ListLogEntriesRequest request =
ListLogEntriesRequest.newBuilder()
.addResourceNames(PROJECT_PB)
.setFilter(createDefaultTimeRangeFilter())
.build();
simonz130 marked this conversation as resolved.
Show resolved Hide resolved

List<LogEntry> entriesList = ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2);
ListLogEntriesResponse response =
ListLogEntriesResponse.newBuilder()
Expand All @@ -1744,12 +1752,16 @@ public void testListLogEntriesNextPage() throws ExecutionException, InterruptedE
String cursor1 = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogEntriesRequest request1 =
ListLogEntriesRequest.newBuilder().addResourceNames(PROJECT_PB).build();
ListLogEntriesRequest request2 =
ListLogEntriesRequest request1 =
ListLogEntriesRequest.newBuilder()
simonz130 marked this conversation as resolved.
Show resolved Hide resolved
.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 Down Expand Up @@ -1834,7 +1850,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 +1874,14 @@ public void testListLogEntriesAsyncNextPage() {
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogEntriesRequest request1 =
ListLogEntriesRequest.newBuilder().addResourceNames(PROJECT_PB).build();
ListLogEntriesRequest request2 =
.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 +1913,14 @@ 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 Down Expand Up @@ -2016,4 +2041,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();
}
}