Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 5cd96ff

Browse files
authored
fix golint and add Makefile. (#138)
1 parent 433871b commit 5cd96ff

8 files changed

Lines changed: 118 additions & 21 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/vendor/
22
/.idea/
3+
*.iml
4+
35

Makefile

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# TODO: Fix this on windows.
2+
ALL_SRC := $(shell find . -name '*.go' \
3+
-not -path './vendor/*' \
4+
-not -path '*/gen-go/*' \
5+
-type f | sort)
6+
ALL_PKGS := $(shell go list $(sort $(dir $(ALL_SRC))))
7+
8+
GOTEST_OPT?=-v -race -timeout 30s
9+
GOTEST_OPT_WITH_COVERAGE = $(GOTEST_OPT) -coverprofile=coverage.txt -covermode=atomic
10+
GOTEST=go test
11+
GOFMT=gofmt
12+
GOLINT=golint
13+
GOVET=go vet
14+
EMBEDMD=embedmd
15+
# TODO decide if we need to change these names.
16+
README_FILES := $(shell find . -name '*README.md' | sort | tr '\n' ' ')
17+
18+
19+
.DEFAULT_GOAL := fmt-lint-vet-embedmd-test
20+
21+
.PHONY: fmt-lint-vet-embedmd-test
22+
fmt-lint-vet-embedmd-test: fmt lint vet embedmd test
23+
24+
# TODO enable test-with-coverage in tavis
25+
.PHONY: travis-ci
26+
travis-ci: fmt lint vet embedmd test test-386
27+
28+
all-pkgs:
29+
@echo $(ALL_PKGS) | tr ' ' '\n' | sort
30+
31+
all-srcs:
32+
@echo $(ALL_SRC) | tr ' ' '\n' | sort
33+
34+
.PHONY: test
35+
test:
36+
$(GOTEST) $(GOTEST_OPT) $(ALL_PKGS)
37+
38+
.PHONY: test-386
39+
test-386:
40+
GOARCH=386 $(GOTEST) -v -timeout 30s $(ALL_PKGS)
41+
42+
.PHONY: test-with-coverage
43+
test-with-coverage:
44+
$(GOTEST) $(GOTEST_OPT_WITH_COVERAGE) $(ALL_PKGS)
45+
46+
.PHONY: fmt
47+
fmt:
48+
@FMTOUT=`$(GOFMT) -s -l $(ALL_SRC) 2>&1`; \
49+
if [ "$$FMTOUT" ]; then \
50+
echo "$(GOFMT) FAILED => gofmt the following files:\n"; \
51+
echo "$$FMTOUT\n"; \
52+
exit 1; \
53+
else \
54+
echo "Fmt finished successfully"; \
55+
fi
56+
57+
.PHONY: lint
58+
lint:
59+
@LINTOUT=`$(GOLINT) $(ALL_PKGS) 2>&1`; \
60+
if [ "$$LINTOUT" ]; then \
61+
echo "$(GOLINT) FAILED => clean the following lint errors:\n"; \
62+
echo "$$LINTOUT\n"; \
63+
exit 1; \
64+
else \
65+
echo "Lint finished successfully"; \
66+
fi
67+
68+
.PHONY: vet
69+
vet:
70+
# TODO: Understand why go vet downloads "github.com/google/go-cmp v0.2.0"
71+
@VETOUT=`$(GOVET) ./... | grep -v "go: downloading" 2>&1`; \
72+
if [ "$$VETOUT" ]; then \
73+
echo "$(GOVET) FAILED => go vet the following files:\n"; \
74+
echo "$$VETOUT\n"; \
75+
exit 1; \
76+
else \
77+
echo "Vet finished successfully"; \
78+
fi
79+
80+
.PHONY: embedmd
81+
embedmd:
82+
@EMBEDMDOUT=`$(EMBEDMD) -d $(README_FILES) 2>&1`; \
83+
if [ "$$EMBEDMDOUT" ]; then \
84+
echo "$(EMBEDMD) FAILED => embedmd the following files:\n"; \
85+
echo "$$EMBEDMDOUT\n"; \
86+
exit 1; \
87+
else \
88+
echo "Embedmd finished successfully"; \
89+
fi
90+
91+
.PHONY: install-tools
92+
install-tools:
93+
go get -u golang.org/x/tools/cmd/cover
94+
go get -u golang.org/x/lint/golint
95+
go get -u github.com/rakyll/embedmd

internal/testpb/impl.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func (s *testServer) Multiple(stream Foo_MultipleServer) error {
6262
}
6363
}
6464

65+
// NewTestClient is used for internal testing only. It creates a
66+
// grpc server and client. It returns client and cleanup function
67+
// to close the connection and gracefully stop the server.
6568
func NewTestClient(l *testing.T) (client FooClient, cleanup func()) {
6669
// initialize server
6770
listener, err := net.Listen("tcp", "localhost:0")

metrics_test_utils.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ import (
3333
func timestampToTime(ts *timestamp.Timestamp) time.Time {
3434
if ts == nil {
3535
return time.Unix(0, 0).UTC()
36-
} else {
37-
return time.Unix(ts.Seconds, int64(ts.Nanos)).UTC()
3836
}
37+
return time.Unix(ts.Seconds, int64(ts.Nanos)).UTC()
3938
}
4039

4140
func cmpResource(got, want *monitoredrespb.MonitoredResource) string {

resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ var resourceMappings = []resourceMap{
8585
},
8686
}
8787

88-
func DefaultMapResource(res *resource.Resource) *monitoredrespb.MonitoredResource {
88+
func defaultMapResource(res *resource.Resource) *monitoredrespb.MonitoredResource {
8989
Outer:
9090
for _, rm := range resourceMappings {
9191
if res.Type != rm.srcType {

resource_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestDefaultMapResource(t *testing.T) {
6464
}
6565
for i, c := range cases {
6666
t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) {
67-
got := DefaultMapResource(c.input)
67+
got := defaultMapResource(c.input)
6868
if diff := cmp.Diff(got, c.want); diff != "" {
6969
t.Errorf("Values differ -got +want: %s", diff)
7070
}

stackdriver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ type Options struct {
182182

183183
// MapResource converts a OpenCensus resource to a Stackdriver monitored resource.
184184
//
185-
// If this field is unset, DefaultMapResource will be used which encodes a set of default
185+
// If this field is unset, defaultMapResource will be used which encodes a set of default
186186
// conversions from auto-detected resources to well-known Stackdriver monitored resources.
187187
MapResource func(*resource.Resource) *monitoredrespb.MonitoredResource
188188

@@ -308,7 +308,7 @@ func NewExporter(o Options) (*Exporter, error) {
308308
o.Resource = convertMonitoredResourceToPB(o.MonitoredResource)
309309
}
310310
if o.MapResource == nil {
311-
o.MapResource = DefaultMapResource
311+
o.MapResource = defaultMapResource
312312
}
313313
if o.ResourceDetector != nil {
314314
// For backwards-compatibility we still respect the deprecated resource field.

stats.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,14 @@ func (e *statsExporter) getMonitoredResource(v *view.View, tags []tag.Tag) ([]ta
157157
if get := e.o.GetMonitoredResource; get != nil {
158158
newTags, mr := get(v, tags)
159159
return newTags, convertMonitoredResourceToPB(mr)
160-
} else {
161-
resource := e.o.Resource
162-
if resource == nil {
163-
resource = &monitoredrespb.MonitoredResource{
164-
Type: "global",
165-
}
160+
}
161+
resource := e.o.Resource
162+
if resource == nil {
163+
resource = &monitoredrespb.MonitoredResource{
164+
Type: "global",
166165
}
167-
return tags, resource
168166
}
167+
return tags, resource
169168
}
170169

171170
// ExportView exports to the Stackdriver Monitoring if view data
@@ -239,17 +238,17 @@ func (e *statsExporter) uploadStats(vds []*view.Data) error {
239238
return nil
240239
}
241240

242-
func (se *statsExporter) makeReq(vds []*view.Data, limit int) []*monitoringpb.CreateTimeSeriesRequest {
241+
func (e *statsExporter) makeReq(vds []*view.Data, limit int) []*monitoringpb.CreateTimeSeriesRequest {
243242
var reqs []*monitoringpb.CreateTimeSeriesRequest
244243

245244
var allTimeSeries []*monitoringpb.TimeSeries
246245
for _, vd := range vds {
247246
for _, row := range vd.Rows {
248-
tags, resource := se.getMonitoredResource(vd.View, append([]tag.Tag(nil), row.Tags...))
247+
tags, resource := e.getMonitoredResource(vd.View, append([]tag.Tag(nil), row.Tags...))
249248
ts := &monitoringpb.TimeSeries{
250249
Metric: &metricpb.Metric{
251-
Type: se.metricType(vd.View),
252-
Labels: newLabels(se.defaultLabels, tags),
250+
Type: e.metricType(vd.View),
251+
Labels: newLabels(e.defaultLabels, tags),
253252
},
254253
Resource: resource,
255254
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
262261
for _, ts := range allTimeSeries {
263262
timeSeries = append(timeSeries, ts)
264263
if len(timeSeries) == limit {
265-
ctsreql := se.combineTimeSeriesToCreateTimeSeriesRequest(timeSeries)
264+
ctsreql := e.combineTimeSeriesToCreateTimeSeriesRequest(timeSeries)
266265
reqs = append(reqs, ctsreql...)
267266
timeSeries = timeSeries[:0]
268267
}
269268
}
270269

271270
if len(timeSeries) > 0 {
272-
ctsreql := se.combineTimeSeriesToCreateTimeSeriesRequest(timeSeries)
271+
ctsreql := e.combineTimeSeriesToCreateTimeSeriesRequest(timeSeries)
273272
reqs = append(reqs, ctsreql...)
274273
}
275274
return reqs
@@ -514,9 +513,8 @@ func addZeroBoundOnCondition(insert bool, bounds ...float64) []float64 {
514513
func (e *statsExporter) metricType(v *view.View) string {
515514
if formatter := e.o.GetMetricType; formatter != nil {
516515
return formatter(v)
517-
} else {
518-
return path.Join("custom.googleapis.com", "opencensus", v.Name)
519516
}
517+
return path.Join("custom.googleapis.com", "opencensus", v.Name)
520518
}
521519

522520
func newLabels(defaults map[string]labelValue, tags []tag.Tag) map[string]string {

0 commit comments

Comments
 (0)