diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 00000000..ef15a12c --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,22 @@ +name: Go +on: + push: + tags: + branches: + - master + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Build + run: make build + - name: Test + run: make test + - name: Shellcheck + run: make shell + - name: Lint + run: make lint + diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 00000000..4a754ade --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,27 @@ +run: + skip-files: + - ".*_test.go" + +linters: + disable-all: true + enable: + - dupl + - goconst + - gocyclo + - gofmt + - gosec + - ineffassign + - misspell + - nakedret + - prealloc + - revive + - unconvert + - unparam + - unused + - vet + - gosec + +issues: + exclude: + - G404 + - G114 diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index b7bbf1d1..00000000 --- a/.travis.yml +++ /dev/null @@ -1,18 +0,0 @@ -language: go - -sudo: required - -services: - - docker - -matrix: - # fail fast - fast_finish: true - include: - - go: 1.12.1 - -install: - - skip - -script: - - make ci diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..7e250572 --- /dev/null +++ b/Makefile @@ -0,0 +1,62 @@ +.DEFAULT_GOAL := ci + +GOOS ?= linux +GOARCH ?= amd64 + +out := target +package_dir := $(out)/pkg +cache := $(out)/.cache +docker_dir := /home/do-agent +shellscripts := $(shell find -type f -iname '*.sh' ! -path './repos/*' ! -path './vendor/*' ! -path './.git/*') +go_version := $(shell sed -En 's/^go[[:space:]]+([[:digit:].]+)$$/\1/p' go.mod) +print = @printf "\n:::::::::::::::: [$(shell date -u)] $@ ::::::::::::::::\n" + +go = \ + docker run --rm -i \ + -u "$(shell id -u):$(shell id -g)" \ + -e "GOOS=$(GOOS)" \ + -e "GOARCH=$(GOARCH)" \ + -e "GO111MODULE=on" \ + -e "GOFLAGS=-mod=vendor" \ + -e "GOCACHE=$(docker_dir)/target/.cache/go" \ + -v "$(CURDIR):$(docker_dir)" \ + -w "$(docker_dir)" \ + golang:$(go_version) \ + go + +shellcheck = \ + docker run --rm -i \ + -u "$(shell id -u):$(shell id -g)" \ + -v "$(CURDIR):$(docker_dir)" \ + -w "$(docker_dir)" \ + koalaman/shellcheck:v0.6.0 + +linter = \ + docker run --rm -i \ + -w "$(docker_dir)" \ + -e "GO111MODULE=on" \ + -e "GOFLAGS=-mod=vendor" \ + -v "$(CURDIR):$(docker_dir)" \ + golangci/golangci-lint:v1.50.1 + +clean: + $(print) + rm -rf ./target + +test: + $(print) + $(go) test -v ./... + +build: + $(print) + $(go) build -v ./... + +shell: + $(print) + $(shellcheck) $(shellscripts) + +lint: + $(print) + $(linter) golangci-lint run ./... + +ci: clean build test lint shell diff --git a/README.md b/README.md index c056c212..43586df6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # DigitalOcean Agent -[![Build Status](https://travis-ci.org/digitalocean/do-agent.svg?branch=master)](https://travis-ci.org/digitalocean/do-agent) [![Go Report Card](https://goreportcard.com/badge/github.com/digitalocean/do-agent)](https://goreportcard.com/report/github.com/digitalocean/do-agent) [![Coverage Status](https://coveralls.io/repos/github/digitalocean/do-agent/badge.svg?branch=master)](https://coveralls.io/github/digitalocean/do-agent?branch=master) diff --git a/internal/process/collector.go b/internal/process/collector.go index 47fb6b23..b270d30c 100644 --- a/internal/process/collector.go +++ b/internal/process/collector.go @@ -10,7 +10,6 @@ import ( type processCollector struct { collectFn func(chan<- prometheus.Metric) - cpuTotal *prometheus.Desc rss *prometheus.Desc } @@ -58,7 +57,7 @@ func (c *processCollector) processCollect(ch chan<- prometheus.Metric) { } for _, proc := range procs { - stat, err := proc.NewStat() + stat, err := proc.Stat() if err != nil { return } diff --git a/pkg/clients/roundtrippers/bearer_token_file.go b/pkg/clients/roundtrippers/bearer_token_file.go index 5fae0cee..32332f4b 100644 --- a/pkg/clients/roundtrippers/bearer_token_file.go +++ b/pkg/clients/roundtrippers/bearer_token_file.go @@ -2,8 +2,8 @@ package roundtrippers import ( "fmt" - "io/ioutil" "net/http" + "os" "strings" ) @@ -14,7 +14,7 @@ type bearerTokenFileRoundTripper struct { // RoundTrip implements http.RoundTripper's interface func (rt *bearerTokenFileRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { - t, err := ioutil.ReadFile(rt.tokenFile) + t, err := os.ReadFile(rt.tokenFile) if err != nil { return nil, fmt.Errorf("unable to read bearer token file %s: %s", rt.tokenFile, err) } diff --git a/pkg/clients/roundtrippers/bearer_token_file_test.go b/pkg/clients/roundtrippers/bearer_token_file_test.go index e252818a..fcc4d43e 100644 --- a/pkg/clients/roundtrippers/bearer_token_file_test.go +++ b/pkg/clients/roundtrippers/bearer_token_file_test.go @@ -32,9 +32,7 @@ func Test_bearerTokenFileRoundTripper_RoundTrip_Happy_Path(t *testing.T) { func Test_bearerTokenFileRoundTripper_RoundTrip_Missing_File(t *testing.T) { rt := NewBearerTokenFile(invalidTokenPath, http.DefaultTransport) - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - return - })) + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) defer ts.Close() _, err := rt.RoundTrip(httptest.NewRequest(http.MethodGet, ts.URL, nil)) diff --git a/pkg/clients/tsclient/client.go b/pkg/clients/tsclient/client.go index 07f13134..83131dcb 100644 --- a/pkg/clients/tsclient/client.go +++ b/pkg/clients/tsclient/client.go @@ -8,7 +8,6 @@ server. Wharf responds with a rate-limit value which the client must wait that many seconds or longer before submitting the next batch of metrics -- this is exposed via the WaitDuration() method. - */ package tsclient @@ -19,7 +18,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "math/rand" "net" "net/http" @@ -551,14 +549,14 @@ func (c *HTTPClient) httpGet(url, authToken string) (string, error) { return "", &UnexpectedHTTPStatusError{StatusCode: resp.StatusCode} } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return "", err } return string(body), nil } -//{"success":true,"frequency":60,"max_metrics":1000,"max_lfm":512} +// {"success":true,"frequency":60,"max_metrics":1000,"max_lfm":512} type sonarResponse struct { Success bool `json:"success"` FrequencySeconds int32 `json:"frequency"` diff --git a/pkg/clients/tsclient/structuredstream/structured_stream_writer.go b/pkg/clients/tsclient/structuredstream/structured_stream_writer.go index 8591d6e8..7a529d44 100644 --- a/pkg/clients/tsclient/structuredstream/structured_stream_writer.go +++ b/pkg/clients/tsclient/structuredstream/structured_stream_writer.go @@ -48,7 +48,7 @@ func (s *Writer) WriteUint16PrefixedString(x string) { // WriteUnixTime64UTC writes a time as unix epoch in UTC; sub-second accuracy is truncated func (s *Writer) WriteUnixTime64UTC(x time.Time) { - s.Write(int64(x.UTC().Unix())) + s.Write(x.UTC().Unix()) } // Error returns any errors the occurred since the writer was first constructed