internal/worker: move metrics to separate file

Code motion and a new function.

Change-Id: I1d33660ae37b1fad1fe492af5653caae9ca4f179
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/278613
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/internal/worker/metrics.go b/internal/worker/metrics.go
new file mode 100644
index 0000000..d6812cd
--- /dev/null
+++ b/internal/worker/metrics.go
@@ -0,0 +1,39 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package worker
+
+import (
+	"context"
+	"strconv"
+
+	"go.opencensus.io/stats"
+	"go.opencensus.io/stats/view"
+	"go.opencensus.io/tag"
+)
+
+var (
+	// keyEnqueueStatus is a census tag used to keep track of the status
+	// of the modules being enqueued.
+	keyEnqueueStatus = tag.MustNewKey("enqueue.status")
+	enqueueStatus    = stats.Int64(
+		"go-discovery/worker_enqueue_count",
+		"The status of a module version enqueued to Cloud Tasks.",
+		stats.UnitDimensionless,
+	)
+	// EnqueueResponseCount counts worker enqueue responses by response type.
+	EnqueueResponseCount = &view.View{
+		Name:        "go-discovery/worker-enqueue/count",
+		Measure:     enqueueStatus,
+		Aggregation: view.Count(),
+		Description: "Worker enqueue request count",
+		TagKeys:     []tag.Key{keyEnqueueStatus},
+	}
+)
+
+func recordEnqueue(ctx context.Context, status int) {
+	stats.RecordWithTags(ctx,
+		[]tag.Mutator{tag.Upsert(keyEnqueueStatus, strconv.Itoa(status))},
+		enqueueStatus.M(int64(status)))
+}
diff --git a/internal/worker/server.go b/internal/worker/server.go
index 9723b97..d2bf348 100644
--- a/internal/worker/server.go
+++ b/internal/worker/server.go
@@ -22,9 +22,6 @@
 	"cloud.google.com/go/errorreporting"
 	"github.com/go-redis/redis/v8"
 	"github.com/google/safehtml/template"
-	"go.opencensus.io/stats"
-	"go.opencensus.io/stats/view"
-	"go.opencensus.io/tag"
 	"go.opencensus.io/trace"
 	"golang.org/x/pkgsite/internal"
 	"golang.org/x/pkgsite/internal/config"
@@ -342,25 +339,6 @@
 	return nil
 }
 
-var (
-	// keyEnqueueStatus is a census tag used to keep track of the status
-	// of the modules being enqueued.
-	keyEnqueueStatus = tag.MustNewKey("enqueue.status")
-	enqueueStatus    = stats.Int64(
-		"go-discovery/worker_enqueue_count",
-		"The status of a module version enqueued to Cloud Tasks.",
-		stats.UnitDimensionless,
-	)
-	// EnqueueResponseCount counts worker enqueue responses by response type.
-	EnqueueResponseCount = &view.View{
-		Name:        "go-discovery/worker-enqueue/count",
-		Measure:     enqueueStatus,
-		Aggregation: view.Count(),
-		Description: "Worker enqueue request count",
-		TagKeys:     []tag.Key{keyEnqueueStatus},
-	}
-)
-
 // handleEnqueue queries the module_version_states table for the next batch of
 // module versions to process, and enqueues them for processing. Note that this
 // may cause duplicate processing.
@@ -399,9 +377,7 @@
 				nErrors++
 			} else if enqueued {
 				nEnqueued++
-				stats.RecordWithTags(r.Context(),
-					[]tag.Mutator{tag.Upsert(keyEnqueueStatus, strconv.Itoa(m.Status))},
-					enqueueStatus.M(int64(m.Status)))
+				recordEnqueue(r.Context(), m.Status)
 			}
 			mu.Unlock()
 		}()