[GCE] Limit number of requested bot events

Requesting bot events is the biggest cause of deadline exceeded errors.
Since we only care about bot_terminate, which is among the bot's final
events, we can limit the number of events we request.

Bug: 897355
Change-Id: Ie314214602042d7ae7698b7fcd2754aedf4c3ec3
Reviewed-on: https://chromium-review.googlesource.com/c/infra/luci/luci-go/+/1590498
Reviewed-by: Andrii Shyshkalov <tandrii@chromium.org>
Commit-Queue: smut <smut@google.com>
diff --git a/gce/appengine/backend/bots.go b/gce/appengine/backend/bots.go
index f899bdb..d5683de 100644
--- a/gce/appengine/backend/bots.go
+++ b/gce/appengine/backend/bots.go
@@ -112,7 +112,11 @@
 		return destroyInstanceAsync(c, vm.ID, vm.URL)
 	}
 	srv := getSwarming(c, vm.Swarming).Bot
-	events, err := srv.Events(vm.Hostname).Context(c).Fields("items/event_type").Do()
+	// bot_terminate occurs when the bot starts the termination task and is normally followed
+	// by task_completed and bot_shutdown. A terminated bot has no further events. Responses
+	// also include the full set of dimensions when the event was recorded. Limit response size
+	// by fetching only recent events, and only the type of each.
+	events, err := srv.Events(vm.Hostname).Context(c).Fields("items/event_type").Limit(5).Do()
 	if err != nil {
 		if gerr, ok := err.(*googleapi.Error); ok {
 			logErrors(c, gerr)