gopls/internal/lsp: add tracing instrumentation for all Server methods

While debugging the gopls/internal/lsp tests, I noticed that I was not
seeing useful user defined tasks in the trace viewer. The reasons for
this were that (1) we were disabling all instrumentation of the event
package, and (2) we didn't have top-level tasks for each jsonrcp2
method, because the tests call into the Server directly.

Fix this by re-enabling the exporter in gopls/internal/lsp tests
(using a debug.Instance to suppress logging to stderr), and by
instrumenting all server methods with a top-level span. Some were
already instrumented, but it was inconsistent. Another advantage to
instrumenting server methods in addition to jsonrpc2 methods, is that
they can add labels specific to the request, such as the text document
URI.

Also add some instrumentation of key snapshot methods.

Change-Id: I992a6a86b175b766e6cbff8c2f2c4a5a35b5d0cf
Reviewed-on: https://go-review.googlesource.com/c/tools/+/480198
Reviewed-by: Peter Weinberger <pjw@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
diff --git a/gopls/internal/lsp/cache/snapshot.go b/gopls/internal/lsp/cache/snapshot.go
index 7771ce9..5e63ed9 100644
--- a/gopls/internal/lsp/cache/snapshot.go
+++ b/gopls/internal/lsp/cache/snapshot.go
@@ -657,6 +657,9 @@
 )
 
 func (s *snapshot) PackageDiagnostics(ctx context.Context, ids ...PackageID) (map[span.URI][]*source.Diagnostic, error) {
+	ctx, done := event.Start(ctx, "cache.snapshot.PackageDiagnostics")
+	defer done()
+
 	var mu sync.Mutex
 	perFile := make(map[span.URI][]*source.Diagnostic)
 	collect := func(diags []*source.Diagnostic) {
@@ -685,6 +688,9 @@
 }
 
 func (s *snapshot) References(ctx context.Context, ids ...PackageID) ([]source.XrefIndex, error) {
+	ctx, done := event.Start(ctx, "cache.snapshot.References")
+	defer done()
+
 	indexes := make([]source.XrefIndex, len(ids))
 	pre := func(i int, ph *packageHandle) bool {
 		data, err := filecache.Get(xrefsKind, ph.key)
@@ -713,6 +719,9 @@
 }
 
 func (s *snapshot) MethodSets(ctx context.Context, ids ...PackageID) ([]*methodsets.Index, error) {
+	ctx, done := event.Start(ctx, "cache.snapshot.MethodSets")
+	defer done()
+
 	indexes := make([]*methodsets.Index, len(ids))
 	pre := func(i int, ph *packageHandle) bool {
 		data, err := filecache.Get(methodSetsKind, ph.key)
@@ -731,6 +740,9 @@
 }
 
 func (s *snapshot) MetadataForFile(ctx context.Context, uri span.URI) ([]*source.Metadata, error) {
+	ctx, done := event.Start(ctx, "cache.snapshot.MetadataForFile")
+	defer done()
+
 	s.mu.Lock()
 
 	// Start with the set of package associations derived from the last load.
@@ -1689,7 +1701,7 @@
 }
 
 func (s *snapshot) clone(ctx, bgCtx context.Context, changes map[span.URI]*fileChange, forceReloadMetadata bool) (*snapshot, func()) {
-	ctx, done := event.Start(ctx, "snapshot.clone")
+	ctx, done := event.Start(ctx, "cache.snapshot.clone")
 	defer done()
 
 	reinit := false