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

Enable test coverage. #200

Merged
merged 8 commits into from
Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/vendor/
# GoLand IDEA
/.idea/
*.iml

# VS Code
.vscode

# Coverage
coverage.txt
coverage.html
20 changes: 8 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
language: go

go:
- 1.11.x
- 1.12.x

go_import_path: contrib.go.opencensus.io/exporter/stackdriver

env:
global:
GO111MODULE=on

before_script:
- GO_FILES=$(find . -iname '*.go' | grep -v /vendor/) # All the .go files, excluding vendor/ if any
- PKGS=$(go list ./... | grep -v /vendor/) # All the import paths, excluding vendor/ if any
install:
- go mod download
- make install-tools

script:
- go build ./... # Ensure dependency updates don't break build
- if [ -n "$(gofmt -s -l $GO_FILES)" ]; then echo "gofmt the following files:"; gofmt -s -l $GO_FILES; exit 1; fi
- go vet ./...
- go test -v -race $PKGS # Run all the tests with the race detector enabled
- GO111MODULE=off go get -t ./...
- GO111MODULE=off go build ./...
- GO111MODULE=off go test -v -race $PKGS # Make sure tests still pass when not using Go modules.
- 'if [[ $TRAVIS_GO_VERSION = 1.8* ]]; then ! golint ./... | grep -vE "(_mock|_string|\.pb)\.go:"; fi'
- make travis-ci

after_success:
- bash <(curl -s https://codecov.io/bash)
48 changes: 39 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ GOTEST_OPT_WITH_COVERAGE = $(GOTEST_OPT) -coverprofile=coverage.txt -covermode=a
GOTEST=go test
GOFMT=gofmt
GOLINT=golint
GOIMPORTS=goimports
GOVET=go vet
EMBEDMD=embedmd
STATICCHECK=staticcheck
# TODO decide if we need to change these names.
README_FILES := $(shell find . -name '*README.md' | sort | tr '\n' ' ')

.DEFAULT_GOAL := defaul-goal

.DEFAULT_GOAL := fmt-lint-vet-embedmd-test
.PHONY: defaul-goal
defaul-goal: fmt lint vet embedmd goimports staticcheck test

.PHONY: fmt-lint-vet-embedmd-test
fmt-lint-vet-embedmd-test: fmt lint vet embedmd test

# TODO enable test-with-coverage in tavis
# TODO: enable test-with-cover when find out why "scripts/check-test-files.sh: 4: set: Illegal option -o pipefail"
.PHONY: travis-ci
travis-ci: fmt lint vet embedmd test test-386
travis-ci: fmt lint vet embedmd goimports staticcheck test test-386 test-with-coverage

all-pkgs:
@echo $(ALL_PKGS) | tr ' ' '\n' | sort
Expand All @@ -41,7 +42,19 @@ test-386:

.PHONY: test-with-coverage
test-with-coverage:
@echo pre-compiling tests
@time go test -i $(ALL_PKGS)
$(GOTEST) $(GOTEST_OPT_WITH_COVERAGE) $(ALL_PKGS)
go tool cover -html=coverage.txt -o coverage.html

.PHONY: test-with-cover
test-with-cover:
@echo Verifying that all packages have test files to count in coverage
@scripts/check-test-files.sh $(subst contrib.go.opencensus.io/exporter/stackdriver,./,$(ALL_PKGS))
@echo pre-compiling tests
@time go test -i $(ALL_PKGS)
$(GOTEST) $(GOTEST_OPT_WITH_COVERAGE) $(ALL_PKGS)
go tool cover -html=coverage.txt -o coverage.html

.PHONY: fmt
fmt:
Expand Down Expand Up @@ -88,8 +101,25 @@ embedmd:
echo "Embedmd finished successfully"; \
fi

.PHONY: goimports
goimports:
@IMPORTSOUT=`$(GOIMPORTS) -d . 2>&1`; \
if [ "$$IMPORTSOUT" ]; then \
echo "$(GOIMPORTS) FAILED => fix the following goimports errors:\n"; \
echo "$$IMPORTSOUT\n"; \
exit 1; \
else \
echo "Goimports finished successfully"; \
fi

.PHONY: staticcheck
staticcheck:
$(STATICCHECK) ./...

.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
GO111MODULE=on go install \
golang.org/x/lint/golint \
golang.org/x/tools/cmd/goimports \
github.com/rakyll/embedmd \
honnef.co/go/tools/cmd/staticcheck
4 changes: 2 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func Example_defaults() {

// Subscribe views to see stats in Stackdriver Monitoring.
if err := view.Register(
ochttp.ClientLatencyView,
ochttp.ClientResponseBytesView,
ochttp.ClientRoundtripLatencyDistribution,
ochttp.ClientReceivedBytesDistribution,
); err != nil {
log.Fatal(err)
}
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ cloud.google.com/go v0.38.0 h1:ROfEUZz+Gh5pa62DJWXSaonyu3StP6EA6lPEXPI6mCo=
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
cloud.google.com/go v0.43.0 h1:banaiRPAM8kUVYneOSkhgcDsLzEvL25FinuiSZaH/2w=
cloud.google.com/go v0.43.0/go.mod h1:BOSR3VbTLkk6FDC/TcffxP4NF/FFBGA5ku+jvKOP7pg=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/aws/aws-sdk-go v1.22.1 h1://WJvJi9iq/i5TWHuK3hIC23xCZYH7Qv7SIN2vZVqxY=
github.com/aws/aws-sdk-go v1.22.1/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/census-instrumentation/opencensus-proto v0.2.1 h1:glEXhBS5PSLLv4IXzLA5yPRVX4bilULVyxxbrfOtDAk=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -27,6 +29,8 @@ github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/addlicense v0.0.0-20190510175307-22550fa7c1b0 h1:ydbHzabf84uucKri5fcfiqYxGg+rYgP/zQfLLN8lyP0=
github.com/google/addlicense v0.0.0-20190510175307-22550fa7c1b0/go.mod h1:QtPG26W17m+OIQgE6gQ24gC1M6pUaMBAbFrTIDtwG/E=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
Expand All @@ -53,6 +57,8 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rakyll/embedmd v0.0.0-20171029212350-c8060a0752a2 h1:1jfy6i1g66ijpffgfaF/7pIFYZnSZzvo9P9DFkFmRIM=
github.com/rakyll/embedmd v0.0.0-20171029212350-c8060a0752a2/go.mod h1:7jOTMgqac46PZcF54q6l2hkLEG8op93fZu61KmxWDV4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand All @@ -68,6 +74,7 @@ golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTk
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190409202823-959b441ac422 h1:QzoH/1pFpZguR8NrRHLcO6jKqfv2zpuSqZLgdm7ZmjI=
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -127,6 +134,7 @@ golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBn
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0 h1:Dh6fw+p6FyRl5x/FvNswO1ji0lIGzm3KP8Y9VkS9PTE=
golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
google.golang.org/api v0.4.0 h1:KKgc1aqhV8wDPbDzlDtpvyjZFY3vjz85FP7p4wcQUyI=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
Expand Down Expand Up @@ -159,5 +167,6 @@ google.golang.org/grpc v1.22.1/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyac
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc h1:/hemPrYIhOhy8zYrNj+069zDB68us2sMGsfkFJO0iZs=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
1 change: 1 addition & 0 deletions internal/testpb/.nocover
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FIXME: Autogenerated and some utils.
11 changes: 7 additions & 4 deletions internal/testpb/test.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ directly to Stackdriver Metrics.

import (
"context"
"errors"
"fmt"

"github.com/golang/protobuf/proto"
Expand All @@ -39,11 +38,6 @@ import (
"go.opencensus.io/resource"
)

var (
errLableExtraction = errors.New("error extracting labels")
errUnspecifiedMetricKind = errors.New("metric kind is unpsecified")
)

const (
exemplarAttachmentTypeString = "type.googleapis.com/google.protobuf.StringValue"
exemplarAttachmentTypeSpanCtx = "type.googleapis.com/google.monitoring.v3.SpanContext"
Expand Down Expand Up @@ -74,7 +68,7 @@ func (se *statsExporter) handleMetricsUpload(metrics []*metricdata.Metric) {
}

func (se *statsExporter) uploadMetrics(metrics []*metricdata.Metric) error {
ctx, cancel := se.o.newContextWithTimeout()
ctx, cancel := newContextWithTimeout(se.o.Context, se.o.Timeout)
defer cancel()

ctx, span := trace.StartSpan(
Expand Down Expand Up @@ -182,7 +176,7 @@ func metricLabelsToTsLabels(defaults map[string]labelValue, labelKeys []metricda

// Perform this sanity check now.
if len(labelKeys) != len(labelValues) {
return labels, fmt.Errorf("Length mismatch: len(labelKeys)=%d len(labelValues)=%d", len(labelKeys), len(labelValues))
return labels, fmt.Errorf("length mismatch: len(labelKeys)=%d len(labelValues)=%d", len(labelKeys), len(labelValues))
}

for i, labelKey := range labelKeys {
Expand Down Expand Up @@ -461,11 +455,9 @@ func metricExemplarToPbExemplar(exemplar *metricdata.Exemplar, projectID string)
func attachmentsToPbAttachments(attachments metricdata.Attachments, projectID string) []*any.Any {
var pbAttachments []*any.Any
for _, v := range attachments {
switch v.(type) {
case trace.SpanContext:
spanCtx, _ := v.(trace.SpanContext)
if spanCtx, succ := v.(trace.SpanContext); succ {
pbAttachments = append(pbAttachments, toPbSpanCtxAttachment(spanCtx, projectID))
default:
} else {
// Treat everything else as plain string for now.
// TODO(songy23): add support for dropped label attachments.
pbAttachments = append(pbAttachments, toPbStringAttachment(v))
Expand Down
14 changes: 7 additions & 7 deletions metrics_batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ import (
)

type metricsBatcher struct {
projectID string
allReqs []*monitoringpb.CreateTimeSeriesRequest
allTss []*monitoringpb.TimeSeries
allErrs []error
projectName string
allReqs []*monitoringpb.CreateTimeSeriesRequest
allTss []*monitoringpb.TimeSeries
allErrs []error
// Counts all dropped TimeSeries by this exporter.
droppedTimeSeries int
}

func newMetricsBatcher(projectID string) *metricsBatcher {
return &metricsBatcher{
projectID: projectID,
projectName: fmt.Sprintf("projects/%s", projectID),
allTss: make([]*monitoringpb.TimeSeries, 0, maxTimeSeriesPerUpload),
droppedTimeSeries: 0,
}
Expand All @@ -49,7 +49,7 @@ func (mb *metricsBatcher) addTimeSeries(ts *monitoringpb.TimeSeries) {
mb.allTss = append(mb.allTss, ts)
if len(mb.allTss) == maxTimeSeriesPerUpload {
mb.allReqs = append(mb.allReqs, &monitoringpb.CreateTimeSeriesRequest{
Name: monitoring.MetricProjectPath(mb.projectID),
Name: mb.projectName,
TimeSeries: mb.allTss,
})
mb.allTss = make([]*monitoringpb.TimeSeries, 0, maxTimeSeriesPerUpload)
Expand All @@ -60,7 +60,7 @@ func (mb *metricsBatcher) export(ctx context.Context, mc *monitoring.MetricClien
// Last batch, if any.
if len(mb.allTss) > 0 {
mb.allReqs = append(mb.allReqs, &monitoringpb.CreateTimeSeriesRequest{
Name: monitoring.MetricProjectPath(mb.projectID),
Name: mb.projectName,
TimeSeries: mb.allTss,
})
}
Expand Down
4 changes: 2 additions & 2 deletions metrics_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (se *statsExporter) PushMetricsProto(ctx context.Context, node *commonpb.No
return 0, errNilMetricOrMetricDescriptor
}

ctx, cancel := se.o.newContextWithTimeout()
ctx, cancel := newContextWithTimeout(ctx, se.o.Timeout)
defer cancel()

// Caches the resources seen so far
Expand Down Expand Up @@ -284,7 +284,7 @@ func labelsPerTimeSeries(defaults map[string]labelValue, labelKeys []string, lab

// Perform this sanity check now.
if len(labelKeys) != len(labelValues) {
return labels, fmt.Errorf("Length mismatch: len(labelKeys)=%d len(labelValues)=%d", len(labelKeys), len(labelValues))
return labels, fmt.Errorf("length mismatch: len(labelKeys)=%d len(labelValues)=%d", len(labelKeys), len(labelValues))
}

for i, labelKey := range labelKeys {
Expand Down
6 changes: 3 additions & 3 deletions metrics_proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ package stackdriver

import (
"context"
"fmt"
"strings"
"testing"

monitoring "cloud.google.com/go/monitoring/apiv3"
resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
"github.com/golang/protobuf/ptypes/timestamp"
distributionpb "google.golang.org/genproto/googleapis/api/distribution"
Expand Down Expand Up @@ -636,7 +636,7 @@ func TestCombineTimeSeriesAndDeduplication(t *testing.T) {
},
want: []*monitoringpb.CreateTimeSeriesRequest{
{
Name: monitoring.MetricProjectPath(se.o.ProjectID),
Name: fmt.Sprintf("projects/%s", se.o.ProjectID),
TimeSeries: []*monitoringpb.TimeSeries{
{
Metric: &googlemetricpb.Metric{
Expand Down Expand Up @@ -667,7 +667,7 @@ func TestCombineTimeSeriesAndDeduplication(t *testing.T) {
},
},
{
Name: monitoring.MetricProjectPath(se.o.ProjectID),
Name: fmt.Sprintf("projects/%s", se.o.ProjectID),
TimeSeries: []*monitoringpb.TimeSeries{
{
Metric: &googlemetricpb.Metric{
Expand Down
8 changes: 6 additions & 2 deletions monitoredresource/aws_identity_doc_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ type awsIdentityDocument struct {
// This is only done once.
func retrieveAWSIdentityDocument() *awsIdentityDocument {
awsIdentityDoc := awsIdentityDocument{}
c := ec2metadata.New(session.New())
if c.Available() == false {
sesion, err := session.NewSession()
if err != nil {
return nil
}
c := ec2metadata.New(sesion)
if !c.Available() {
return nil
}
ec2InstanceIdentifyDocument, err := c.GetInstanceIdentityDocument()
Expand Down
2 changes: 1 addition & 1 deletion monitoredresource/gcp_metadata_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strings"

"cloud.google.com/go/compute/metadata"
"cloud.google.com/go/container/apiv1"
container "cloud.google.com/go/container/apiv1"
containerpb "google.golang.org/genproto/googleapis/container/v1"
)

Expand Down
7 changes: 0 additions & 7 deletions resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ import (
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
)

type resourceMap struct {
// Mapping from the input resource type to the monitored resource type in Stackdriver.
srcType, dstType string
// Mapping from Stackdriver monitored resource label to an OpenCensus resource label.
labels map[string]string
}

// Resource labels that are generally internal to the exporter.
// Consider exposing these labels and a type identifier in the future to allow
// for customization.
Expand Down
Loading