internal/frontend: add additional stats in serveUnitPage stack

Change-Id: I17c9eb093e9e5728477ab0b39c748cc5923d8590
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/311629
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
diff --git a/internal/frontend/latest_version.go b/internal/frontend/latest_version.go
index 32ced01..b12cc92 100644
--- a/internal/frontend/latest_version.go
+++ b/internal/frontend/latest_version.go
@@ -9,6 +9,7 @@
 
 	"golang.org/x/pkgsite/internal"
 	"golang.org/x/pkgsite/internal/log"
+	"golang.org/x/pkgsite/internal/middleware"
 )
 
 // GetLatestInfo returns various pieces of information about the latest
@@ -19,6 +20,8 @@
 // It returns empty strings on error.
 // It is intended to be used as an argument to middleware.LatestVersions.
 func (s *Server) GetLatestInfo(ctx context.Context, unitPath, modulePath string, latestUnitMeta *internal.UnitMeta) internal.LatestInfo {
+	defer middleware.ElapsedStat(ctx, "GetLatestInfo")()
+
 	// It is okay to use a different DataSource (DB connection) than the rest of the
 	// request, because this makes self-contained calls on the DB.
 	ds := s.getDataSource(ctx)
diff --git a/internal/frontend/unit.go b/internal/frontend/unit.go
index 61e88d9..b474d66 100644
--- a/internal/frontend/unit.go
+++ b/internal/frontend/unit.go
@@ -20,6 +20,7 @@
 	"golang.org/x/pkgsite/internal/cookie"
 	"golang.org/x/pkgsite/internal/derrors"
 	"golang.org/x/pkgsite/internal/log"
+	"golang.org/x/pkgsite/internal/middleware"
 	"golang.org/x/pkgsite/internal/stdlib"
 )
 
@@ -92,6 +93,7 @@
 func (s *Server) serveUnitPage(ctx context.Context, w http.ResponseWriter, r *http.Request,
 	ds internal.DataSource, info *urlPathInfo) (err error) {
 	defer derrors.Wrap(&err, "serveUnitPage(ctx, w, r, ds, %v)", info)
+	defer middleware.ElapsedStat(ctx, "serveUnitPage")()
 
 	tab := r.FormValue("tab")
 	if tab == "" {
diff --git a/internal/postgres/unit.go b/internal/postgres/unit.go
index ac16101..4c5b44c 100644
--- a/internal/postgres/unit.go
+++ b/internal/postgres/unit.go
@@ -36,7 +36,7 @@
 //       we do have: again first by module path length, then by version.
 func (db *DB) GetUnitMeta(ctx context.Context, fullPath, requestedModulePath, requestedVersion string) (_ *internal.UnitMeta, err error) {
 	defer derrors.WrapStack(&err, "DB.GetUnitMeta(ctx, %q, %q, %q)", fullPath, requestedModulePath, requestedVersion)
-	defer middleware.ElapsedStat(ctx, "GetUnitMeta")()
+	defer middleware.ElapsedStat(ctx, "DB.GetUnitMeta")()
 
 	modulePath := requestedModulePath
 	version := requestedVersion
diff --git a/internal/postgres/version.go b/internal/postgres/version.go
index 65519d0..d9d16ea 100644
--- a/internal/postgres/version.go
+++ b/internal/postgres/version.go
@@ -155,6 +155,7 @@
 // That can save a redundant call to GetUnitMeta here.
 func (db *DB) GetLatestInfo(ctx context.Context, unitPath, modulePath string, latestUnitMeta *internal.UnitMeta) (latest internal.LatestInfo, err error) {
 	defer derrors.WrapStack(&err, "DB.GetLatestInfo(ctx, %q, %q)", unitPath, modulePath)
+	defer middleware.ElapsedStat(ctx, "DB.GetLatestInfo")()
 
 	group, gctx := errgroup.WithContext(ctx)
 
@@ -199,6 +200,7 @@
 // it returns empty strings.
 func (db *DB) getLatestMajorVersion(ctx context.Context, fullPath, modulePath string) (modPath, pkgPath string, err error) {
 	defer derrors.WrapStack(&err, "DB.getLatestMajorVersion2(%q)", modulePath)
+	defer middleware.ElapsedStat(ctx, "DB.getLatestMajorVersion")()
 
 	// Collect all the non-deprecated module paths for the series that have at
 	// least one good version, along with that good version. A good version
@@ -276,6 +278,7 @@
 // unitExistsAtLatest reports whether unitPath exists at the latest version of modulePath.
 func (db *DB) unitExistsAtLatest(ctx context.Context, unitPath, modulePath string) (unitExists bool, err error) {
 	defer derrors.WrapStack(&err, "DB.unitExistsAtLatest(ctx, %q, %q)", unitPath, modulePath)
+	defer middleware.ElapsedStat(ctx, "DB.unitExistsAtLatest")()
 
 	// Find the latest version of the module path in the modules table.
 	var latestGoodVersion string