diff --git a/.forgejo/workflows/dco.yml b/.forgejo/workflows/dco.yml new file mode 100644 index 0000000..9aa0d31 --- /dev/null +++ b/.forgejo/workflows/dco.yml @@ -0,0 +1,21 @@ +name: DCO action +on: [pull_request] + +jobs: + dco: + name: DCO + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup Go + uses: actions/setup-go@v3 + with: + go-version: '1.21' + + - name: Run commit format checker + uses: https://git.frostfs.info/TrueCloudLab/dco-go@v3 + with: + from: 'origin/${{ github.event.pull_request.base.ref }}' diff --git a/.forgejo/workflows/tests.yml b/.forgejo/workflows/tests.yml new file mode 100644 index 0000000..f66a2c4 --- /dev/null +++ b/.forgejo/workflows/tests.yml @@ -0,0 +1,73 @@ +name: Tests and linters +on: [pull_request] + +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: '1.21' + cache: true + + - name: Install linters + run: make lint-install + + - name: Run linters + run: make lint + + tests: + name: Tests + runs-on: ubuntu-latest + strategy: + matrix: + go_versions: [ '1.20', '1.21' ] + fail-fast: false + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: '${{ matrix.go_versions }}' + cache: true + + - name: Run tests + run: make test + + tests-race: + name: Tests with -race + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: '1.21' + cache: true + + - name: Run tests + run: go test ./... -count=1 -race + + staticcheck: + name: Staticcheck + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: '1.21' + cache: true + + - name: Install staticcheck + run: make staticcheck-install + + - name: Run staticcheck + run: make staticcheck-run diff --git a/.golangci.yml b/.golangci.yml index fae355a..87c612b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -52,7 +52,7 @@ linters: - durationcheck - exhaustive - exportloopref - - gofmt + - gofumpt - goimports - misspell - predeclared diff --git a/Makefile b/Makefile old mode 100644 new mode 100755 index 0caf248..e56794c --- a/Makefile +++ b/Makefile @@ -1,14 +1,63 @@ #!/usr/bin/make -f SHELL = bash +TRUECLOUDLAB_LINT_VERSION ?= 0.0.2 +TMP_DIR := .cache +OUTPUT_LINT_DIR ?= $(shell pwd)/bin +LINT_VERSION ?= 1.55.1 +LINT_DIR = $(OUTPUT_LINT_DIR)/golangci-lint-$(LINT_VERSION)-v$(TRUECLOUDLAB_LINT_VERSION) + +# Run all code formatters +fmts: fmt imports + +# Reformat code +fmt: + @echo "⇒ Processing gofmt check" + @gofumpt -s -w . + +# Reformat imports +imports: + @echo "⇒ Processing goimports check" + @goimports -w . + # Run Unit Test with go test test: + @echo "⇒ Running go test" @go test ./... -count=1 +# Activate pre-commit hooks +pre-commit: + pre-commit install -t pre-commit -t commit-msg + +# Deactivate pre-commit hooks +unpre-commit: + pre-commit uninstall -t pre-commit -t commit-msg + +pre-commit-run: + @pre-commit run -a --hook-stage manual + +# Install linters +lint-install: + @mkdir -p $(TMP_DIR) + @rm -rf $(TMP_DIR)/linters + @git -c advice.detachedHead=false clone --branch v$(TRUECLOUDLAB_LINT_VERSION) https://git.frostfs.info/TrueCloudLab/linters.git $(TMP_DIR)/linters + @make -C $(TMP_DIR)/linters lib CGO_ENABLED=1 OUT_DIR=$(OUTPUT_LINT_DIR) + @rm -rf $(TMP_DIR)/linters + @rmdir $(TMP_DIR) 2>/dev/null || true + @CGO_ENABLED=1 GOBIN=$(LINT_DIR) go install github.com/golangci/golangci-lint/cmd/golangci-lint@v$(LINT_VERSION) + # Run linters lint: - @golangci-lint --timeout=5m run + @if [ ! -d "$(LINT_DIR)" ]; then \ + echo "Run make lint-install"; \ + exit 1; \ + fi + @$(LINT_DIR)/golangci-lint run + +# Install staticcheck +staticcheck-install: + @go install honnef.co/go/tools/cmd/staticcheck@latest # Run staticcheck -staticcheck: - @staticcheck ./... \ No newline at end of file +staticcheck-run: + @staticcheck ./... diff --git a/logging/lokicore/loki/README.md b/logging/lokicore/loki/README.md index e130b46..18d9f3b 100644 --- a/logging/lokicore/loki/README.md +++ b/logging/lokicore/loki/README.md @@ -2,7 +2,7 @@ A simple asynchronous client in Go for sending logs to Loki. -## Usage +## Usage ```go package main @@ -29,4 +29,4 @@ func main() { loki.Send("log message", time.Now()) } -``` \ No newline at end of file +``` diff --git a/logging/lokicore/loki/example/main.go b/logging/lokicore/loki/example/main.go index 52051d3..57dca26 100644 --- a/logging/lokicore/loki/example/main.go +++ b/logging/lokicore/loki/example/main.go @@ -12,8 +12,10 @@ import ( var wg sync.WaitGroup -const countMsgGroup = 100 -const countMsg = 500000 +const ( + countMsgGroup = 100 + countMsg = 500000 +) func send(loki *loki.Client) { wg.Add(1) diff --git a/logging/lokicore/loki/send.go b/logging/lokicore/loki/send.go index de0e09c..293cf7c 100644 --- a/logging/lokicore/loki/send.go +++ b/logging/lokicore/loki/send.go @@ -35,7 +35,6 @@ func (client *Client) sendLogs(entries []logEntry) { func (client *Client) sendRequest(method, url string, ctype string, reqBody []byte) { req, err := http.NewRequest(method, url, bytes.NewBuffer(reqBody)) - if err != nil { return } diff --git a/logging/lokicore/loki/setup.go b/logging/lokicore/loki/setup.go index 5c7c77d..48b0449 100644 --- a/logging/lokicore/loki/setup.go +++ b/logging/lokicore/loki/setup.go @@ -43,9 +43,9 @@ type Config struct { // E.g. localhost:3100/api/prom/push. Endpoint string Labels map[string]string - //Maximum message buffering time. + // Maximum message buffering time. BatchWait time.Duration - //Maximum number of messages in the queue. + // Maximum number of messages in the queue. BatchEntriesNumber int } diff --git a/tracing/examples/grpc/main.go b/tracing/examples/grpc/main.go index 37aff9d..7aa0789 100644 --- a/tracing/examples/grpc/main.go +++ b/tracing/examples/grpc/main.go @@ -97,7 +97,6 @@ func main() { resp, err := client.Echo(ctx, &srv.Request{ Value: "Hello!", }) - if err != nil { log.Fatalf("failed to get response: %v", err) } diff --git a/tracing/examples/grpc/server/server.proto b/tracing/examples/grpc/server/server.proto index 3a4e419..e6ab96d 100644 --- a/tracing/examples/grpc/server/server.proto +++ b/tracing/examples/grpc/server/server.proto @@ -14,4 +14,4 @@ message Request { message Response { string value = 1; -} \ No newline at end of file +} diff --git a/tracing/propagator.go b/tracing/propagator.go index 4b7c70a..37b4a77 100644 --- a/tracing/propagator.go +++ b/tracing/propagator.go @@ -70,7 +70,7 @@ func (p *propagator) Extract(ctx context.Context, carrier propagation.TextMapCar } if traceIDDefined != spanIDDefined { - return ctx //traceID + spanID must be defined OR no traceID and no spanID + return ctx // traceID + spanID must be defined OR no traceID and no spanID } flagsStr := carrier.Get(flagsHeader) diff --git a/tracing/propagator_test.go b/tracing/propagator_test.go index fc81e48..06a58c3 100644 --- a/tracing/propagator_test.go +++ b/tracing/propagator_test.go @@ -124,7 +124,6 @@ func TestPropagator_Inject(t *testing.T) { require.Equal(t, spanIDHex, c.Values[spanIDHeader], "unexpected span id") require.Equal(t, "0", c.Values[flagsHeader], "unexpected flags") }) - } func TestPropagator_Extract(t *testing.T) {