Skip to content

Commit

Permalink
fix(logadmin): use consistent filter in paging example (#8221)
Browse files Browse the repository at this point in the history
Without supplying a `timestamp` in the `ListLogEntries.Filter`, the client by default sets one for the last twenty-four hours when the `EntryIterator` is created. Since this example creates a new `EntryIterator` on each request, a new `timestamp` of a slightly different twenty-four hour window is added. When the `Next Page` button is pressed, it reuses the `NextPageToken` from the previous call, but that was created for a different twenty-four hour window `Filter`. This creates a mismatch between the requested `Filter` and the `Filter` used to create the `NextPageToken`, resulting in an error.

To address this, we create the filter in the main function before server start up. In this filter, we set a timestamp **explicitly**. 

Note: I made this a `fix` commit because this is part of the GoDoc examples and we need this to appear as the latest release, otherwise no one will see it until another release comes along.

Fixes #8186.
  • Loading branch information
noahdietz authored Jul 6, 2023
1 parent 6c28f23 commit 9570159
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion logging/logadmin/example_paging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"html/template"
"log"
"net/http"
"time"

"cloud.google.com/go/logging"
"cloud.google.com/go/logging/logadmin"
Expand All @@ -31,6 +32,7 @@ import (
var (
client *logadmin.Client
projectID = flag.String("project-id", "", "ID of the project to use")
filter string
)

func ExampleClient_Entries_pagination() {
Expand All @@ -49,6 +51,15 @@ func ExampleClient_Entries_pagination() {
log.Fatalf("creating logging client: %v", err)
}

// Filter for logs of a specific name.
logName := fmt.Sprintf(`logName = "projects/%s/logs/testlog"`, *projectID)

// Filter for logs from the last twenty-four hours.
yesterday := time.Now().Add(-24 * time.Hour).UTC()
dayAgo := fmt.Sprintf("timestamp >= %q", yesterday.Format(time.RFC3339))

filter = fmt.Sprintf("%s AND %s", logName, dayAgo)

http.HandleFunc("/entries", handleEntries)
log.Print("listening on 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
Expand All @@ -67,7 +78,6 @@ var pageTemplate = template.Must(template.New("").Parse(`

func handleEntries(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
filter := fmt.Sprintf(`logName = "projects/%s/logs/testlog"`, *projectID)
it := client.Entries(ctx, logadmin.Filter(filter))
var entries []*logging.Entry
nextTok, err := iterator.NewPager(it, 5, r.URL.Query().Get("pageToken")).NextPage(&entries)
Expand Down

0 comments on commit 9570159

Please sign in to comment.