gopls/internal/bug: add gopls/bug telemetry counter

This counter (tentatively named 'gopls/bug') will track and report
the number of `report` calls.

Since bug.Report* can be called in a type loop, we increment
the stack counter only where this is the first exemplar, and
after getting out of the critical section.

Change-Id: I42ee385bd69bc148454a82c98f7d623d5c803907
Reviewed-on: https://go-review.googlesource.com/c/tools/+/513100
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/gopls/internal/bug/bug.go b/gopls/internal/bug/bug.go
index f72948b..7331ba8 100644
--- a/gopls/internal/bug/bug.go
+++ b/gopls/internal/bug/bug.go
@@ -18,6 +18,8 @@
 	"sort"
 	"sync"
 	"time"
+
+	"golang.org/x/telemetry/counter"
 )
 
 // PanicOnBugs controls whether to panic when bugs are reported.
@@ -63,6 +65,8 @@
 	report(description)
 }
 
+var bugReport = counter.NewStack("gopls/bug", 16)
+
 func report(description string) {
 	_, file, line, ok := runtime.Caller(2) // all exported reporting functions call report directly
 
@@ -84,17 +88,22 @@
 		AtTime:      time.Now(),
 	}
 
+	newBug := false
 	mu.Lock()
 	if _, ok := exemplars[key]; !ok {
 		if exemplars == nil {
 			exemplars = make(map[string]Bug)
 		}
 		exemplars[key] = bug // capture one exemplar per key
+		newBug = true
 	}
 	hh := handlers
 	handlers = nil
 	mu.Unlock()
 
+	if newBug {
+		bugReport.Inc()
+	}
 	// Call the handlers outside the critical section since a
 	// handler may itself fail and call bug.Report. Since handlers
 	// are one-shot, the inner call should be trivial.