Skip to content

Commit

Permalink
fix(logging): Entries has a 24H default filter (#3120)
Browse files Browse the repository at this point in the history
* feat(logging): List entries has a 24H default filter
  • Loading branch information
0xSage committed Apr 7, 2021
1 parent 733c72a commit b32eb82
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
20 changes: 18 additions & 2 deletions logging/logadmin/logadmin.go
Expand Up @@ -208,8 +208,8 @@ func (rn resourceNames) set(r *logpb.ListLogEntriesRequest) {
// "projects/PROJECT-ID/logs/LOG-ID". Forward slashes in LOG-ID must be
// replaced by %2F before calling Filter.
//
// Timestamps in the filter string must be written in RFC 3339 format. See the
// timestamp example.
// Timestamps in the filter string must be written in RFC 3339 format. By default,
// timestamp filters for the past 24 hours.
func Filter(f string) EntriesOption { return filter(f) }

type filter string
Expand Down Expand Up @@ -245,9 +245,25 @@ func listLogEntriesRequest(parent string, opts []EntriesOption) *logpb.ListLogEn
for _, opt := range opts {
opt.set(req)
}
req.Filter = defaultTimestampFilter(req.Filter)
return req
}

// defaultTimestampFilter returns a timestamp filter that looks back 24 hours in the past.
// This default setting is consistent with documentation. Note: user filters containing 'timestamp'
// substring disables this default timestamp filter, e.g. `textPayload: "timestamp"`
func defaultTimestampFilter(filter string) string {
dayAgo := time.Now().Add(-24 * time.Hour).UTC()
switch {
case len(filter) == 0:
return fmt.Sprintf(`timestamp >= "%s"`, dayAgo.Format(time.RFC3339))
case !strings.Contains(strings.ToLower(filter), "timestamp"):
return fmt.Sprintf(`%s AND timestamp >= "%s"`, filter, dayAgo.Format(time.RFC3339))
default:
return filter
}
}

// An EntryIterator iterates over log entries.
type EntryIterator struct {
it *vkit.LogEntryIterator
Expand Down
20 changes: 12 additions & 8 deletions logging/logadmin/logadmin_test.go
Expand Up @@ -262,27 +262,31 @@ func TestFromLogEntry(t *testing.T) {
}

func TestListLogEntriesRequest(t *testing.T) {
dayAgo := time.Now().Add(-24 * time.Hour).UTC().Format(time.RFC3339)
for _, test := range []struct {
opts []EntriesOption
resourceNames []string
filter string
orderBy string
}{
// Default is client's project ID, empty filter and orderBy.
{nil, []string{"projects/PROJECT_ID"}, "", ""},
// Default is client's project ID, 24 hour lookback, and orderBy.
{nil, []string{"projects/PROJECT_ID"}, `timestamp >= "` + dayAgo + `"`, ""},
// Timestamp default does not override user's filter
{[]EntriesOption{NewestFirst(), Filter(`timestamp > "2020-10-30T15:39:09Z"`)},
[]string{"projects/PROJECT_ID"}, `timestamp > "2020-10-30T15:39:09Z"`, "timestamp desc"},
{[]EntriesOption{NewestFirst(), Filter("f")},
[]string{"projects/PROJECT_ID"}, "f", "timestamp desc"},
[]string{"projects/PROJECT_ID"}, `f AND timestamp >= "` + dayAgo + `"`, "timestamp desc"},
{[]EntriesOption{ProjectIDs([]string{"foo"})},
[]string{"projects/foo"}, "", ""},
[]string{"projects/foo"}, `timestamp >= "` + dayAgo + `"`, ""},
{[]EntriesOption{ResourceNames([]string{"folders/F", "organizations/O"})},
[]string{"folders/F", "organizations/O"}, "", ""},
[]string{"folders/F", "organizations/O"}, `timestamp >= "` + dayAgo + `"`, ""},
{[]EntriesOption{NewestFirst(), Filter("f"), ProjectIDs([]string{"foo"})},
[]string{"projects/foo"}, "f", "timestamp desc"},
[]string{"projects/foo"}, `f AND timestamp >= "` + dayAgo + `"`, "timestamp desc"},
{[]EntriesOption{NewestFirst(), Filter("f"), ProjectIDs([]string{"foo"})},
[]string{"projects/foo"}, "f", "timestamp desc"},
[]string{"projects/foo"}, `f AND timestamp >= "` + dayAgo + `"`, "timestamp desc"},
// If there are repeats, last one wins.
{[]EntriesOption{NewestFirst(), Filter("no"), ProjectIDs([]string{"foo"}), Filter("f")},
[]string{"projects/foo"}, "f", "timestamp desc"},
[]string{"projects/foo"}, `f AND timestamp >= "` + dayAgo + `"`, "timestamp desc"},
} {
got := listLogEntriesRequest("projects/PROJECT_ID", test.opts)
want := &logpb.ListLogEntriesRequest{
Expand Down

0 comments on commit b32eb82

Please sign in to comment.