internal/telemetry: extract units to their own package

This makes the code read slightly better, and more closely
aligns with the open telemetry code.

Change-Id: I87eaf7d08b802f7862f896f2654456ee6a7681e3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/190404
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
diff --git a/internal/lsp/telemetry/telemetry.go b/internal/lsp/telemetry/telemetry.go
index e6d6ae5..a92e25b 100644
--- a/internal/lsp/telemetry/telemetry.go
+++ b/internal/lsp/telemetry/telemetry.go
@@ -9,6 +9,7 @@
 import (
 	"golang.org/x/tools/internal/telemetry/stats"
 	"golang.org/x/tools/internal/telemetry/tag"
+	"golang.org/x/tools/internal/telemetry/unit"
 )
 
 const (
@@ -24,10 +25,10 @@
 
 var (
 	// create the stats we measure
-	Started       = stats.Int64("started", "Count of started RPCs.", stats.UnitDimensionless)
-	ReceivedBytes = stats.Int64("received_bytes", "Bytes received.", stats.UnitBytes)
-	SentBytes     = stats.Int64("sent_bytes", "Bytes sent.", stats.UnitBytes)
-	Latency       = stats.Float64("latency_ms", "Elapsed time in milliseconds", stats.UnitMilliseconds)
+	Started       = stats.Int64("started", "Count of started RPCs.", unit.Dimensionless)
+	ReceivedBytes = stats.Int64("received_bytes", "Bytes received.", unit.Bytes)
+	SentBytes     = stats.Int64("sent_bytes", "Bytes sent.", unit.Bytes)
+	Latency       = stats.Float64("latency_ms", "Elapsed time in milliseconds", unit.Milliseconds)
 )
 
 const (
diff --git a/internal/telemetry/stats/stats.go b/internal/telemetry/stats/stats.go
index 6da3ed3..807944f 100644
--- a/internal/telemetry/stats/stats.go
+++ b/internal/telemetry/stats/stats.go
@@ -9,13 +9,15 @@
 
 import (
 	"context"
+
+	"golang.org/x/tools/internal/telemetry/unit"
 )
 
 // Int64Measure is used to record integer values.
 type Int64Measure struct {
 	name        string
 	description string
-	unit        Unit
+	unit        unit.Unit
 	subscribers []Int64Subscriber
 }
 
@@ -23,7 +25,7 @@
 type Float64Measure struct {
 	name        string
 	description string
-	unit        Unit
+	unit        unit.Unit
 	subscribers []Float64Subscriber
 }
 
@@ -35,21 +37,8 @@
 // floating point statistic events.
 type Float64Subscriber func(context.Context, *Float64Measure, float64)
 
-// Unit is used to specify the units for a given measure.
-// This is can used for display purposes.
-type Unit int
-
-const (
-	// UnitDimensionless indicates that a measure has no specified units.
-	UnitDimensionless = Unit(iota)
-	// UnitBytes indicates that that a measure is recording number of bytes.
-	UnitBytes
-	// UnitMilliseconds indicates that a measure is recording a duration in milliseconds.
-	UnitMilliseconds
-)
-
 // Int64 creates a new Int64Measure and prepares it for use.
-func Int64(name string, description string, unit Unit) *Int64Measure {
+func Int64(name string, description string, unit unit.Unit) *Int64Measure {
 	return &Int64Measure{
 		name:        name,
 		description: description,
@@ -58,7 +47,7 @@
 }
 
 // Float64 creates a new Float64Measure and prepares it for use.
-func Float64(name string, description string, unit Unit) *Float64Measure {
+func Float64(name string, description string, unit unit.Unit) *Float64Measure {
 	return &Float64Measure{
 		name:        name,
 		description: description,
@@ -73,7 +62,7 @@
 func (m *Int64Measure) Description() string { return m.description }
 
 // Unit returns the units this measure was given on construction.
-func (m *Int64Measure) Unit() Unit { return m.unit }
+func (m *Int64Measure) Unit() unit.Unit { return m.unit }
 
 // Subscribe adds a new subscriber to this measure.
 func (m *Int64Measure) Subscribe(s Int64Subscriber) { m.subscribers = append(m.subscribers, s) }
@@ -92,7 +81,7 @@
 func (m *Float64Measure) Description() string { return m.description }
 
 // Unit returns the units this measure was given on construction.
-func (m *Float64Measure) Unit() Unit { return m.unit }
+func (m *Float64Measure) Unit() unit.Unit { return m.unit }
 
 // Subscribe adds a new subscriber to this measure.
 func (m *Float64Measure) Subscribe(s Float64Subscriber) { m.subscribers = append(m.subscribers, s) }
diff --git a/internal/telemetry/unit/unit.go b/internal/telemetry/unit/unit.go
new file mode 100644
index 0000000..a904a28
--- /dev/null
+++ b/internal/telemetry/unit/unit.go
@@ -0,0 +1,19 @@
+// Copyright 2019 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 unit holds the definitions for the units you can use in telemetry.
+package unit
+
+// Unit is used to specify the units for a given measure.
+// This is can used for display purposes.
+type Unit string
+
+const (
+	// UnitDimensionless indicates that a measure has no specified units.
+	Dimensionless = "1"
+	// UnitBytes indicates that that a measure is recording number of bytes.
+	Bytes = "By"
+	// UnitMilliseconds indicates that a measure is recording a duration in milliseconds.
+	Milliseconds = "ms"
+)