Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add default time range filter for ListLogEntries API (#304)
* feat!: listLogEntries now appends default 24h filter BREAKING CHANGE: previously if the user called listLogEntries API with no parameters, the query would try to retrieve all log entries, taking up time and eventually crashing on projects with high volume logs. The new behavior complies with gcloud: if no timestamp filter was specified, it appends a filter covering last 24 hours.
- Loading branch information
Simon Zeltser
committed
Nov 11, 2020
1 parent
e97dda7
commit c2f40df
Showing
5 changed files
with
206 additions
and
28 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
google-cloud-logging/src/main/java/com/google/cloud/logging/ITimestampDefaultFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.logging; | ||
|
||
/** | ||
* 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 | ||
* | ||
* @return The filter using timestamp field | ||
*/ | ||
String createDefaultTimestampFilter(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
google-cloud-logging/src/main/java/com/google/cloud/logging/TimestampDefaultFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.logging; | ||
|
||
import java.text.DateFormat; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.util.TimeZone; | ||
|
||
public class TimestampDefaultFilter implements ITimestampDefaultFilter { | ||
@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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.