Repository: census-instrumentation/opencensus-go (go)

Issue:
stats: add UnregisterView and then remove *View.Unregister for parity of APIs
I was just reviewing https://github.com/census-instrumentation/opencensus-go/pull/249/files and noticed that the stats API has
stats.RegisterView
but no matching call to stats.UnregisterView

and that instead the caller has to do *View.Unregister(), of which there also isn't a match method *View.Register()

Notice that RegisterView and *View.UnregisterView look like the perfect match
https://github.com/census-instrumentation/opencensus-go/blob/86bea64327bd7d353334ff858a2e6675a3122b7b/stats/worker.go#L115-L122
and
https://github.com/census-instrumentation/opencensus-go/blob/86bea64327bd7d353334ff858a2e6675a3122b7b/stats/worker.go#L127-L134

I'd say let's deprecate/remove *View.Unregister and instead add the UnregisterView helper since
we already have *View.Subscribe and *View.Unsubscribe of which Subscribe and Unsubscribe logically use RegisterView and UnregisterView respectively.

Pull request:
Remove *View.Unregister, add stats.RegisterView for API consistency
Fixes #250

Patch:
diff --git a/README.md b/README.md
index ea679d6..0f12fba 100644
--- a/README.md
+++ b/README.md
@@ -204,7 +204,7 @@ Unregister view:
 
 [embedmd]:# (stats.go unregisterView)
 ```go
-if v.Unregister(); err != nil {
+if stats.UnregisterView(v); err != nil {
 	log.Fatal(err)
 }
 ```
diff --git a/internal/readme/stats.go b/internal/readme/stats.go
index 631716f..a3d7e9a 100644
--- a/internal/readme/stats.go
+++ b/internal/readme/stats.go
@@ -99,7 +99,7 @@ func statsExamples() {
 	_ = v
 
 	// START unregisterView
-	if v.Unregister(); err != nil {
+	if stats.UnregisterView(v); err != nil {
 		log.Fatal(err)
 	}
 	// END unregisterView
diff --git a/stats/worker.go b/stats/worker.go
index ad41fac..6750149 100644
--- a/stats/worker.go
+++ b/stats/worker.go
@@ -124,7 +124,7 @@ func RegisterView(v *View) error {
 // Unregister removes the previously registered view. It returns an error
 // if the view wasn't registered. All data collected and not reported for the
 // corresponding view will be lost. The view is automatically be unsubscribed.
-func (v *View) Unregister() error {
+func UnregisterView(v *View) error {
 	req := &unregisterViewReq{
 		v:   v,
 		err: make(chan error),


Test patch:
diff --git a/exporter/stats/stackdriver/stackdriver_test.go b/exporter/stats/stackdriver/stackdriver_test.go
index bdf68a4..0f4acde 100644
--- a/exporter/stats/stackdriver/stackdriver_test.go
+++ b/exporter/stats/stackdriver/stackdriver_test.go
@@ -53,7 +53,7 @@ func TestExporter_makeReq(t *testing.T) {
 	if err := stats.RegisterView(cumView); err != nil {
 		t.Fatal(err)
 	}
-	defer cumView.Unregister()
+	defer stats.UnregisterView(cumView)
 
 	distView, err := stats.NewView("distview", "desc", nil, m, stats.DistributionAggregation([]float64{2, 4, 7}), stats.Interval{})
 	if err != nil {
@@ -62,7 +62,7 @@ func TestExporter_makeReq(t *testing.T) {
 	if err := stats.RegisterView(distView); err != nil {
 		t.Fatal(err)
 	}
-	defer distView.Unregister()
+	defer stats.UnregisterView(distView)
 
 	start := time.Now()
 	end := start.Add(time.Minute)
@@ -338,7 +338,7 @@ func TestExporter_makeReq_batching(t *testing.T) {
 	if err := stats.RegisterView(view); err != nil {
 		t.Fatal(err)
 	}
-	defer view.Unregister()
+	defer stats.UnregisterView(view)
 
 	tests := []struct {
 		name      string
diff --git a/stats/worker_test.go b/stats/worker_test.go
index 24be1cb..fe3da50 100644
--- a/stats/worker_test.go
+++ b/stats/worker_test.go
@@ -442,9 +442,9 @@ func Test_Worker_ViewRegistration(t *testing.T) {
 
 		for _, unreg := range tc.unregs {
 			v := views[unreg.vID]
-			err := v.Unregister()
+			err := UnregisterView(v)
 			if (err != nil) != (unreg.err != nil) {
-				t.Errorf("%v: Unregister() = %v; want %v", tc.label, err, unreg.err)
+				t.Errorf("%v: UnregisterView() = %v; want %v", tc.label, err, unreg.err)
 			}
 		}
 
@@ -605,7 +605,7 @@ func Test_Worker_RecordFloat64(t *testing.T) {
 		}
 
 		for _, v := range tc.registrations {
-			if err := v.Unregister(); err != nil {
+			if err := UnregisterView(v); err != nil {
 				t.Fatalf("%v: Unregistering view %v errrored with %v; want no error", tc.label, v.Name(), err)
 			}
 		}