Skip to content

Commit b32eb82

Browse files
authored
fix(logging): Entries has a 24H default filter (#3120)
* feat(logging): List entries has a 24H default filter
1 parent 733c72a commit b32eb82

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

logging/logadmin/logadmin.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ func (rn resourceNames) set(r *logpb.ListLogEntriesRequest) {
208208
// "projects/PROJECT-ID/logs/LOG-ID". Forward slashes in LOG-ID must be
209209
// replaced by %2F before calling Filter.
210210
//
211-
// Timestamps in the filter string must be written in RFC 3339 format. See the
212-
// timestamp example.
211+
// Timestamps in the filter string must be written in RFC 3339 format. By default,
212+
// timestamp filters for the past 24 hours.
213213
func Filter(f string) EntriesOption { return filter(f) }
214214

215215
type filter string
@@ -245,9 +245,25 @@ func listLogEntriesRequest(parent string, opts []EntriesOption) *logpb.ListLogEn
245245
for _, opt := range opts {
246246
opt.set(req)
247247
}
248+
req.Filter = defaultTimestampFilter(req.Filter)
248249
return req
249250
}
250251

252+
// defaultTimestampFilter returns a timestamp filter that looks back 24 hours in the past.
253+
// This default setting is consistent with documentation. Note: user filters containing 'timestamp'
254+
// substring disables this default timestamp filter, e.g. `textPayload: "timestamp"`
255+
func defaultTimestampFilter(filter string) string {
256+
dayAgo := time.Now().Add(-24 * time.Hour).UTC()
257+
switch {
258+
case len(filter) == 0:
259+
return fmt.Sprintf(`timestamp >= "%s"`, dayAgo.Format(time.RFC3339))
260+
case !strings.Contains(strings.ToLower(filter), "timestamp"):
261+
return fmt.Sprintf(`%s AND timestamp >= "%s"`, filter, dayAgo.Format(time.RFC3339))
262+
default:
263+
return filter
264+
}
265+
}
266+
251267
// An EntryIterator iterates over log entries.
252268
type EntryIterator struct {
253269
it *vkit.LogEntryIterator

logging/logadmin/logadmin_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,27 +262,31 @@ func TestFromLogEntry(t *testing.T) {
262262
}
263263

264264
func TestListLogEntriesRequest(t *testing.T) {
265+
dayAgo := time.Now().Add(-24 * time.Hour).UTC().Format(time.RFC3339)
265266
for _, test := range []struct {
266267
opts []EntriesOption
267268
resourceNames []string
268269
filter string
269270
orderBy string
270271
}{
271-
// Default is client's project ID, empty filter and orderBy.
272-
{nil, []string{"projects/PROJECT_ID"}, "", ""},
272+
// Default is client's project ID, 24 hour lookback, and orderBy.
273+
{nil, []string{"projects/PROJECT_ID"}, `timestamp >= "` + dayAgo + `"`, ""},
274+
// Timestamp default does not override user's filter
275+
{[]EntriesOption{NewestFirst(), Filter(`timestamp > "2020-10-30T15:39:09Z"`)},
276+
[]string{"projects/PROJECT_ID"}, `timestamp > "2020-10-30T15:39:09Z"`, "timestamp desc"},
273277
{[]EntriesOption{NewestFirst(), Filter("f")},
274-
[]string{"projects/PROJECT_ID"}, "f", "timestamp desc"},
278+
[]string{"projects/PROJECT_ID"}, `f AND timestamp >= "` + dayAgo + `"`, "timestamp desc"},
275279
{[]EntriesOption{ProjectIDs([]string{"foo"})},
276-
[]string{"projects/foo"}, "", ""},
280+
[]string{"projects/foo"}, `timestamp >= "` + dayAgo + `"`, ""},
277281
{[]EntriesOption{ResourceNames([]string{"folders/F", "organizations/O"})},
278-
[]string{"folders/F", "organizations/O"}, "", ""},
282+
[]string{"folders/F", "organizations/O"}, `timestamp >= "` + dayAgo + `"`, ""},
279283
{[]EntriesOption{NewestFirst(), Filter("f"), ProjectIDs([]string{"foo"})},
280-
[]string{"projects/foo"}, "f", "timestamp desc"},
284+
[]string{"projects/foo"}, `f AND timestamp >= "` + dayAgo + `"`, "timestamp desc"},
281285
{[]EntriesOption{NewestFirst(), Filter("f"), ProjectIDs([]string{"foo"})},
282-
[]string{"projects/foo"}, "f", "timestamp desc"},
286+
[]string{"projects/foo"}, `f AND timestamp >= "` + dayAgo + `"`, "timestamp desc"},
283287
// If there are repeats, last one wins.
284288
{[]EntriesOption{NewestFirst(), Filter("no"), ProjectIDs([]string{"foo"}), Filter("f")},
285-
[]string{"projects/foo"}, "f", "timestamp desc"},
289+
[]string{"projects/foo"}, `f AND timestamp >= "` + dayAgo + `"`, "timestamp desc"},
286290
} {
287291
got := listLogEntriesRequest("projects/PROJECT_ID", test.opts)
288292
want := &logpb.ListLogEntriesRequest{

0 commit comments

Comments
 (0)