Repository: census-ecosystem/opencensus-go-exporter-stackdriver
Pull request #138 (2019-04-19 20:39:14)

fix golint and add Makefile.


Patch:
diff --git a/.gitignore b/.gitignore
index dd3ca63..c0ee15b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
 /vendor/
 /.idea/
+*.iml
+
 
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..2e11d22
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,95 @@
+# TODO: Fix this on windows.
+ALL_SRC := $(shell find . -name '*.go' \
+								-not -path './vendor/*' \
+								-not -path '*/gen-go/*' \
+								-type f | sort)
+ALL_PKGS := $(shell go list $(sort $(dir $(ALL_SRC))))
+
+GOTEST_OPT?=-v -race -timeout 30s
+GOTEST_OPT_WITH_COVERAGE = $(GOTEST_OPT) -coverprofile=coverage.txt -covermode=atomic
+GOTEST=go test
+GOFMT=gofmt
+GOLINT=golint
+GOVET=go vet
+EMBEDMD=embedmd
+# TODO decide if we need to change these names.
+README_FILES := $(shell find . -name '*README.md' | sort | tr '\n' ' ')
+
+
+.DEFAULT_GOAL := fmt-lint-vet-embedmd-test
+
+.PHONY: fmt-lint-vet-embedmd-test
+fmt-lint-vet-embedmd-test: fmt lint vet embedmd test
+
+# TODO enable test-with-coverage in tavis
+.PHONY: travis-ci
+travis-ci: fmt lint vet embedmd test test-386
+
+all-pkgs:
+	@echo $(ALL_PKGS) | tr ' ' '\n' | sort
+
+all-srcs:
+	@echo $(ALL_SRC) | tr ' ' '\n' | sort
+
+.PHONY: test
+test:
+	$(GOTEST) $(GOTEST_OPT) $(ALL_PKGS)
+
+.PHONY: test-386
+test-386:
+	GOARCH=386 $(GOTEST) -v -timeout 30s $(ALL_PKGS)
+
+.PHONY: test-with-coverage
+test-with-coverage:
+	$(GOTEST) $(GOTEST_OPT_WITH_COVERAGE) $(ALL_PKGS)
+
+.PHONY: fmt
+fmt:
+	@FMTOUT=`$(GOFMT) -s -l $(ALL_SRC) 2>&1`; \
+	if [ "$$FMTOUT" ]; then \
+		echo "$(GOFMT) FAILED => gofmt the following files:\n"; \
+		echo "$$FMTOUT\n"; \
+		exit 1; \
+	else \
+	    echo "Fmt finished successfully"; \
+	fi
+
+.PHONY: lint
+lint:
+	@LINTOUT=`$(GOLINT) $(ALL_PKGS) 2>&1`; \
+	if [ "$$LINTOUT" ]; then \
+		echo "$(GOLINT) FAILED => clean the following lint errors:\n"; \
+		echo "$$LINTOUT\n"; \
+		exit 1; \
+	else \
+	    echo "Lint finished successfully"; \
+	fi
+
+.PHONY: vet
+vet:
+    # TODO: Understand why go vet downloads "github.com/google/go-cmp v0.2.0"
+	@VETOUT=`$(GOVET) ./... | grep -v "go: downloading" 2>&1`; \
+	if [ "$$VETOUT" ]; then \
+		echo "$(GOVET) FAILED => go vet the following files:\n"; \
+		echo "$$VETOUT\n"; \
+		exit 1; \
+	else \
+	    echo "Vet finished successfully"; \
+	fi
+	
+.PHONY: embedmd
+embedmd:
+	@EMBEDMDOUT=`$(EMBEDMD) -d $(README_FILES) 2>&1`; \
+	if [ "$$EMBEDMDOUT" ]; then \
+		echo "$(EMBEDMD) FAILED => embedmd the following files:\n"; \
+		echo "$$EMBEDMDOUT\n"; \
+		exit 1; \
+	else \
+	    echo "Embedmd finished successfully"; \
+	fi
+
+.PHONY: install-tools
+install-tools:
+	go get -u golang.org/x/tools/cmd/cover
+	go get -u golang.org/x/lint/golint
+	go get -u github.com/rakyll/embedmd
diff --git a/resource.go b/resource.go
index 9576931..6ddcc7e 100644
--- a/resource.go
+++ b/resource.go
@@ -85,7 +85,7 @@ var resourceMappings = []resourceMap{
 	},
 }
 
-func DefaultMapResource(res *resource.Resource) *monitoredrespb.MonitoredResource {
+func defaultMapResource(res *resource.Resource) *monitoredrespb.MonitoredResource {
 Outer:
 	for _, rm := range resourceMappings {
 		if res.Type != rm.srcType {
diff --git a/stackdriver.go b/stackdriver.go
index 9c77eea..7672a3e 100644
--- a/stackdriver.go
+++ b/stackdriver.go
@@ -182,7 +182,7 @@ type Options struct {
 
 	// MapResource converts a OpenCensus resource to a Stackdriver monitored resource.
 	//
-	// If this field is unset, DefaultMapResource will be used which encodes a set of default
+	// If this field is unset, defaultMapResource will be used which encodes a set of default
 	// conversions from auto-detected resources to well-known Stackdriver monitored resources.
 	MapResource func(*resource.Resource) *monitoredrespb.MonitoredResource
 
@@ -308,7 +308,7 @@ func NewExporter(o Options) (*Exporter, error) {
 		o.Resource = convertMonitoredResourceToPB(o.MonitoredResource)
 	}
 	if o.MapResource == nil {
-		o.MapResource = DefaultMapResource
+		o.MapResource = defaultMapResource
 	}
 	if o.ResourceDetector != nil {
 		// For backwards-compatibility we still respect the deprecated resource field.
diff --git a/stats.go b/stats.go
index 4d67b0f..47f54ce 100644
--- a/stats.go
+++ b/stats.go
@@ -157,15 +157,14 @@ func (e *statsExporter) getMonitoredResource(v *view.View, tags []tag.Tag) ([]ta
 	if get := e.o.GetMonitoredResource; get != nil {
 		newTags, mr := get(v, tags)
 		return newTags, convertMonitoredResourceToPB(mr)
-	} else {
-		resource := e.o.Resource
-		if resource == nil {
-			resource = &monitoredrespb.MonitoredResource{
-				Type: "global",
-			}
+	}
+	resource := e.o.Resource
+	if resource == nil {
+		resource = &monitoredrespb.MonitoredResource{
+			Type: "global",
 		}
-		return tags, resource
 	}
+	return tags, resource
 }
 
 // ExportView exports to the Stackdriver Monitoring if view data
@@ -239,17 +238,17 @@ func (e *statsExporter) uploadStats(vds []*view.Data) error {
 	return nil
 }
 
-func (se *statsExporter) makeReq(vds []*view.Data, limit int) []*monitoringpb.CreateTimeSeriesRequest {
+func (e *statsExporter) makeReq(vds []*view.Data, limit int) []*monitoringpb.CreateTimeSeriesRequest {
 	var reqs []*monitoringpb.CreateTimeSeriesRequest
 
 	var allTimeSeries []*monitoringpb.TimeSeries
 	for _, vd := range vds {
 		for _, row := range vd.Rows {
-			tags, resource := se.getMonitoredResource(vd.View, append([]tag.Tag(nil), row.Tags...))
+			tags, resource := e.getMonitoredResource(vd.View, append([]tag.Tag(nil), row.Tags...))
 			ts := &monitoringpb.TimeSeries{
 				Metric: &metricpb.Metric{
-					Type:   se.metricType(vd.View),
-					Labels: newLabels(se.defaultLabels, tags),
+					Type:   e.metricType(vd.View),
+					Labels: newLabels(e.defaultLabels, tags),
 				},
 				Resource: resource,
 				Points:   []*monitoringpb.Point{newPoint(vd.View, row, vd.Start, vd.End)},
@@ -262,14 +261,14 @@ func (se *statsExporter) makeReq(vds []*view.Data, limit int) []*monitoringpb.Cr
 	for _, ts := range allTimeSeries {
 		timeSeries = append(timeSeries, ts)
 		if len(timeSeries) == limit {
-			ctsreql := se.combineTimeSeriesToCreateTimeSeriesRequest(timeSeries)
+			ctsreql := e.combineTimeSeriesToCreateTimeSeriesRequest(timeSeries)
 			reqs = append(reqs, ctsreql...)
 			timeSeries = timeSeries[:0]
 		}
 	}
 
 	if len(timeSeries) > 0 {
-		ctsreql := se.combineTimeSeriesToCreateTimeSeriesRequest(timeSeries)
+		ctsreql := e.combineTimeSeriesToCreateTimeSeriesRequest(timeSeries)
 		reqs = append(reqs, ctsreql...)
 	}
 	return reqs
@@ -514,9 +513,8 @@ func addZeroBoundOnCondition(insert bool, bounds ...float64) []float64 {
 func (e *statsExporter) metricType(v *view.View) string {
 	if formatter := e.o.GetMetricType; formatter != nil {
 		return formatter(v)
-	} else {
-		return path.Join("custom.googleapis.com", "opencensus", v.Name)
 	}
+	return path.Join("custom.googleapis.com", "opencensus", v.Name)
 }
 
 func newLabels(defaults map[string]labelValue, tags []tag.Tag) map[string]string {


Test patch:
diff --git a/internal/testpb/impl.go b/internal/testpb/impl.go
index 24533af..f977cd0 100644
--- a/internal/testpb/impl.go
+++ b/internal/testpb/impl.go
@@ -62,6 +62,9 @@ func (s *testServer) Multiple(stream Foo_MultipleServer) error {
 	}
 }
 
+// NewTestClient is used for internal testing only. It creates a
+// grpc server and client. It returns client and cleanup function
+// to close the connection and gracefully stop the server.
 func NewTestClient(l *testing.T) (client FooClient, cleanup func()) {
 	// initialize server
 	listener, err := net.Listen("tcp", "localhost:0")
diff --git a/metrics_test_utils.go b/metrics_test_utils.go
index 045621e..bc7a1ff 100644
--- a/metrics_test_utils.go
+++ b/metrics_test_utils.go
@@ -33,9 +33,8 @@ import (
 func timestampToTime(ts *timestamp.Timestamp) time.Time {
 	if ts == nil {
 		return time.Unix(0, 0).UTC()
-	} else {
-		return time.Unix(ts.Seconds, int64(ts.Nanos)).UTC()
 	}
+	return time.Unix(ts.Seconds, int64(ts.Nanos)).UTC()
 }
 
 func cmpResource(got, want *monitoredrespb.MonitoredResource) string {
diff --git a/resource_test.go b/resource_test.go
index 70bc4c5..01fdce7 100644
--- a/resource_test.go
+++ b/resource_test.go
@@ -64,7 +64,7 @@ func TestDefaultMapResource(t *testing.T) {
 	}
 	for i, c := range cases {
 		t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) {
-			got := DefaultMapResource(c.input)
+			got := defaultMapResource(c.input)
 			if diff := cmp.Diff(got, c.want); diff != "" {
 				t.Errorf("Values differ -got +want: %s", diff)
 			}