internal/fetch: add a metric for number of packages successfully fetched

This should be a smoother metric than the number of fetched modules,
since processing time per package should have a much smaller range
than time per module.

Change-Id: I00f5ba83fcc4fe3c606883bd644e570b502c9090
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/257243
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
diff --git a/cmd/worker/main.go b/cmd/worker/main.go
index 992f42a..41ecfba 100644
--- a/cmd/worker/main.go
+++ b/cmd/worker/main.go
@@ -129,7 +129,8 @@
 		worker.EnqueueResponseCount,
 		fetch.FetchLatencyDistribution,
 		fetch.FetchResponseCount,
-		fetch.SheddedFetchCount)
+		fetch.SheddedFetchCount,
+		fetch.FetchPackageCount)
 	if err := dcensus.Init(cfg, views...); err != nil {
 		log.Fatal(ctx, err)
 	}
diff --git a/internal/fetch/fetch.go b/internal/fetch/fetch.go
index c0ada85..242a4b7 100644
--- a/internal/fetch/fetch.go
+++ b/internal/fetch/fetch.go
@@ -47,6 +47,11 @@
 		"Count of shedded fetches.",
 		stats.UnitDimensionless,
 	)
+	fetchedPackages = stats.Int64(
+		"go-discovery/worker/fetch-package-count",
+		"Count of successfully fetched packages.",
+		stats.UnitDimensionless,
+	)
 
 	// FetchLatencyDistribution aggregates frontend fetch request
 	// latency by status code. It does not count shedded requests.
@@ -65,7 +70,13 @@
 		Description: "Fetch request count by result status",
 		TagKeys:     []tag.Key{dcensus.KeyStatus},
 	}
-
+	// FetchPackageCount counts how many packages were sucessfully fetched.
+	FetchPackageCount = &view.View{
+		Name:        "go-discovery/worker/fetch-package-count",
+		Measure:     fetchedPackages,
+		Aggregation: view.Count(),
+		Description: "Count of packages successfully fetched",
+	}
 	// SheddedFetchCount counts the number of fetches that were shedded.
 	SheddedFetchCount = &view.View{
 		Name:        "go-discovery/worker/fetch-shedded",
@@ -113,6 +124,9 @@
 		}
 		latency := float64(time.Since(start).Milliseconds())
 		dcensus.RecordWithTag(ctx, dcensus.KeyStatus, strconv.Itoa(fr.Status), fetchLatency.M(latency))
+		if fr.Status < 300 {
+			stats.Record(ctx, fetchedPackages.M(int64(len(fr.PackageVersionStates))))
+		}
 		log.Debugf(ctx, "memory after fetch of %s@%s: %dM", modulePath, requestedVersion, allocMeg())
 	}()