Skip to content

Commit

Permalink
fix: make ListLogEntries use 24hr filter
Browse files Browse the repository at this point in the history
  • Loading branch information
simonz130 committed Oct 29, 2020
1 parent 024b73d commit 552997c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 16 deletions.
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 All @@ -48,7 +49,12 @@
import com.google.common.util.concurrent.Uninterruptibles;
import com.google.logging.v2.*;
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 @@ -631,6 +637,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());
}

return builder.build();
}

Expand Down Expand Up @@ -692,4 +703,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();
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();
}
}
Expand Up @@ -68,6 +68,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.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -1291,8 +1296,11 @@ 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();

List<LogEntry> entriesList = ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2);
ListLogEntriesResponse response =
ListLogEntriesResponse.newBuilder()
Expand All @@ -1312,12 +1320,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 request1 = 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 @@ -1353,7 +1365,10 @@ 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 @@ -1401,8 +1416,9 @@ public void testListLogEntriesAsync() throws ExecutionException, InterruptedExce
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();
List<LogEntry> entriesList = ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2);
ListLogEntriesResponse response =
ListLogEntriesResponse.newBuilder()
Expand All @@ -1422,13 +1438,17 @@ public void testListLogEntriesAsyncNextPage() {
String cursor1 = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogEntriesRequest request1 =
ListLogEntriesRequest.newBuilder().addResourceNames(PROJECT_PB).build();
ListLogEntriesRequest request2 =
ListLogEntriesRequest.newBuilder()
.addResourceNames(PROJECT_PB)
.setPageToken(cursor1)
.build();
ListLogEntriesRequest request1 = 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);
List<LogEntry> descriptorList2 = ImmutableList.of(LOG_ENTRY1);
ListLogEntriesResponse response1 =
Expand Down Expand Up @@ -1458,12 +1478,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 @@ -1584,4 +1606,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();
}
}

0 comments on commit 552997c

Please sign in to comment.