Skip to content

Commit 662a439

Browse files
fix: Retrieving logentries pagewise always results in an exception (#1220)
* fix: Retrieving logentries pagewise always results in an exception * Address PR comments * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent f4c346e commit 662a439

File tree

3 files changed

+56
-19
lines changed

3 files changed

+56
-19
lines changed

google-cloud-logging/src/main/java/com/google/cloud/logging/Logging.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,8 @@ ApiFuture<AsyncPage<MonitoredResourceDescriptor>> listMonitoredResourceDescripto
12031203
* {@link EntryListOption#pageToken(String)} to specify the page token from which to start listing
12041204
* entries. Use {@link EntryListOption#sortOrder(SortingField, SortingOrder)} to sort log entries
12051205
* according to your preferred order (default is most-recent last). Use {@link
1206-
* EntryListOption#filter(String)} to filter listed log entries.
1206+
* EntryListOption#filter(String)} to filter listed log entries. By default a 24 hour filter is
1207+
* applied.
12071208
*
12081209
* <p>Example of listing log entries for a specific log.
12091210
*
@@ -1231,7 +1232,8 @@ ApiFuture<AsyncPage<MonitoredResourceDescriptor>> listMonitoredResourceDescripto
12311232
* specify the page size. Use {@link EntryListOption#pageToken(String)} to specify the page token
12321233
* from which to start listing entries. Use {@link EntryListOption#sortOrder(SortingField,
12331234
* SortingOrder)} to sort log entries according to your preferred order (default is most-recent
1234-
* last). Use {@link EntryListOption#filter(String)} to filter listed log entries.
1235+
* last). Use {@link EntryListOption#filter(String)} to filter listed log entries. By default a 24
1236+
* hour filter is applied.
12351237
*
12361238
* <p>Example of asynchronously listing log entries for a specific log.
12371239
*

google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java

+36-17
Original file line numberDiff line numberDiff line change
@@ -1014,31 +1014,19 @@ static ListLogEntriesRequest listLogEntriesRequest(
10141014
if (orderBy != null) {
10151015
builder.setOrderBy(orderBy);
10161016
}
1017-
String filter = EntryListOption.OptionType.FILTER.get(options);
1018-
// Make sure timestamp filter is either explicitly specified or we add a default
1019-
// time filter
1020-
// of 24 hours back to be inline with gcloud behavior for the same API
1017+
String filter = generateFilter(EntryListOption.OptionType.FILTER.get(options));
10211018
if (filter != null) {
1022-
if (!Ascii.toLowerCase(filter).contains("timestamp")) {
1023-
filter =
1024-
String.format(
1025-
"%s AND %s", filter, defaultTimestampFilterCreator.createDefaultTimestampFilter());
1026-
}
10271019
builder.setFilter(filter);
1028-
} else {
1029-
// If filter is not specified, default filter is looking back 24 hours in line
1030-
// with gcloud
1031-
// behavior
1032-
builder.setFilter(defaultTimestampFilterCreator.createDefaultTimestampFilter());
10331020
}
1034-
10351021
return builder.build();
10361022
}
10371023

10381024
private static ApiFuture<AsyncPage<LogEntry>> listLogEntriesAsync(
10391025
final LoggingOptions serviceOptions, final Map<Option.OptionType, ?> options) {
1026+
// Make sure to set a filter option which later can be reused in subsequent calls
1027+
final Map<Option.OptionType, ?> updatedOptions = updateFilter(options);
10401028
final ListLogEntriesRequest request =
1041-
listLogEntriesRequest(serviceOptions.getProjectId(), options);
1029+
listLogEntriesRequest(serviceOptions.getProjectId(), updatedOptions);
10421030
ApiFuture<ListLogEntriesResponse> list = serviceOptions.getLoggingRpcV2().list(request);
10431031
return transform(
10441032
list,
@@ -1055,7 +1043,7 @@ public AsyncPage<LogEntry> apply(ListLogEntriesResponse listLogEntriesResponse)
10551043
? null
10561044
: listLogEntriesResponse.getNextPageToken();
10571045
return new AsyncPageImpl<>(
1058-
new LogEntryPageFetcher(serviceOptions, cursor, options), cursor, entries);
1046+
new LogEntryPageFetcher(serviceOptions, cursor, updatedOptions), cursor, entries);
10591047
}
10601048
});
10611049
}
@@ -1137,6 +1125,37 @@ public void close() throws Exception {
11371125
return optionMap;
11381126
}
11391127

1128+
static Map<Option.OptionType, ?> updateFilter(final Map<Option.OptionType, ?> options) {
1129+
// We should see if filter provided in otiopns have a timestamp parameter
1130+
// and if not, it should be added with further update of options map.
1131+
String existingFilter = EntryListOption.OptionType.FILTER.get(options);
1132+
String newFilter = generateFilter(existingFilter);
1133+
if (newFilter.equals(existingFilter)) {
1134+
return options;
1135+
}
1136+
// Update
1137+
Map<Option.OptionType, Object> optionsCopy = Maps.newHashMap(options);
1138+
optionsCopy.put(EntryListOption.OptionType.FILTER, newFilter);
1139+
return optionsCopy;
1140+
}
1141+
1142+
static String generateFilter(String filter) {
1143+
String newFilter = filter;
1144+
// Make sure timestamp filter is either explicitly specified or we add a default
1145+
// time filter of 24 hours back to be inline with gcloud behavior for the same API
1146+
if (newFilter != null) {
1147+
if (!Ascii.toLowerCase(filter).contains("timestamp")) {
1148+
newFilter =
1149+
String.format(
1150+
"%s AND %s",
1151+
newFilter, defaultTimestampFilterCreator.createDefaultTimestampFilter());
1152+
}
1153+
} else {
1154+
newFilter = defaultTimestampFilterCreator.createDefaultTimestampFilter();
1155+
}
1156+
return newFilter;
1157+
}
1158+
11401159
@VisibleForTesting
11411160
int getNumPendingWrites() {
11421161
return pendingWrites.size();

google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertFalse;
22+
import static org.junit.Assert.assertNotEquals;
23+
import static org.junit.Assert.assertTrue;
2124

2225
import com.google.cloud.MonitoredResource;
2326
import com.google.cloud.logging.Logging.EntryListOption;
@@ -27,6 +30,7 @@
2730
import com.google.cloud.logging.Logging.WriteOption;
2831
import com.google.common.collect.ImmutableMap;
2932
import com.google.logging.v2.ListLogEntriesRequest;
33+
import java.util.Map;
3034
import org.junit.Test;
3135
import org.junit.runner.RunWith;
3236
import org.junit.runners.JUnit4;
@@ -100,6 +104,18 @@ public void testEntryListOption() {
100104
"folders/test-folder");
101105
}
102106

107+
@Test
108+
public void testFilterUpdate() {
109+
Map<Option.OptionType, ?> options = LoggingImpl.optionMap(EntryListOption.filter(FILTER));
110+
assertThat((String) EntryListOption.OptionType.FILTER.get(options)).isEqualTo(FILTER);
111+
Map<Option.OptionType, ?> updated = LoggingImpl.updateFilter(options);
112+
assertTrue(((String) EntryListOption.OptionType.FILTER.get(updated)).contains("timestamp"));
113+
assertFalse(options == updated);
114+
assertNotEquals(EntryListOption.OptionType.FILTER.get(updated), FILTER);
115+
Map<Option.OptionType, ?> anotherUpdated = LoggingImpl.updateFilter(updated);
116+
assertTrue(anotherUpdated == updated);
117+
}
118+
103119
@Test
104120
public void testWriteOption() {
105121
WriteOption writeOption = WriteOption.labels(LABELS);

0 commit comments

Comments
 (0)