Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Инкремент №19 #17

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SERVER_VERSION ?= 0.1.0
BUILD_DATE ?= $(shell date +%F\ %H:%M:%S)
BUILD_COMMIT ?= $(shell git rev-parse --short HEAD)

build: agent server
build: agent server staticlint
.PHONY: build

agent: ## build agent
Expand All @@ -22,7 +22,7 @@ agent: ## build agent

server: ## build server
rm -rf $(API_DOCS)
swag init -g ./internal/httpserver/handlers.go --output $(API_DOCS)
swag init -g ./internal/httpserver/router.go --output docs/api

go build \
-ldflags "\
Expand All @@ -34,6 +34,10 @@ server: ## build server
cmd/$@/*.go
.PHONY: server

staticlint: ## build static lint
go build -o cmd/$@/$@ cmd/$@/*.go
.PHONY: staticlint

clean: ## remove build artifacts
rm -rf cmd/agent/agent cmd/server/server cmd/staticlint/staticlint
.PHONY: clean
Expand Down
8 changes: 8 additions & 0 deletions cmd/staticlint/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import "github.com/ex0rcist/metflix/pkg/staticlint"

func main() {
lint := staticlint.New()
lint.Run()
}
Binary file added cmd/staticlint/staticlint
Binary file not shown.
695 changes: 513 additions & 182 deletions coverage.html

Large diffs are not rendered by default.

458 changes: 260 additions & 198 deletions coverage.out.tmp

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ require (
github.com/go-openapi/swag v0.23.0
github.com/golang-migrate/migrate/v4 v4.17.1
github.com/jackc/pgx/v5 v5.6.0
github.com/jingyugao/rowserrcheck v1.1.1
github.com/kisielk/errcheck v1.8.0
github.com/klauspost/compress v1.15.11
github.com/rs/zerolog v1.33.0
github.com/satori/go.uuid v1.2.0
Expand All @@ -20,10 +22,14 @@ require (
github.com/stretchr/testify v1.9.0
github.com/swaggo/http-swagger v1.3.4
github.com/swaggo/swag v1.16.4
github.com/timakin/bodyclose v0.0.0-20241017074824-adbc21e6bf36
golang.org/x/sync v0.8.0
golang.org/x/tools v0.26.0
honnef.co/go/tools v0.5.1
)

require (
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -33,6 +39,8 @@ require (
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/spec v0.21.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gostaticanalysis/analysisutil v0.7.1 // indirect
github.com/gostaticanalysis/comment v1.4.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
Expand All @@ -56,11 +64,12 @@ require (
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.mongodb.org/mongo-driver v1.14.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/exp/typeparams v0.0.0-20231108232855-2478ac86f678 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/tools v0.26.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
79 changes: 77 additions & 2 deletions go.sum

Large diffs are not rendered by default.

26 changes: 15 additions & 11 deletions internal/agent/exporter/batch_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ex0rcist/metflix/internal/compression"
"github.com/ex0rcist/metflix/internal/entities"
"github.com/ex0rcist/metflix/internal/logging"
"github.com/ex0rcist/metflix/internal/retrier"
"github.com/ex0rcist/metflix/internal/services"
"github.com/ex0rcist/metflix/internal/utils"
"github.com/ex0rcist/metflix/pkg/metrics"
Expand Down Expand Up @@ -75,17 +76,15 @@ func (e *BatchExporter) Send() error {
return fmt.Errorf("cannot send empty buffer")
}

err := utils.NewRetrier(
delays := []time.Duration{1 * time.Second, 3 * time.Second, 5 * time.Second}

err := retrier.New(
func() error { return e.doSend() },
func(err error) bool {
_, ok := err.(entities.RetriableError)
return ok
},
[]time.Duration{
1 * time.Second,
3 * time.Second,
5 * time.Second,
},
retrier.WithDelays(delays),
).Run()

e.Reset()
Expand Down Expand Up @@ -136,10 +135,10 @@ func (e *BatchExporter) doSend() error {
req.Header.Set("X-Request-Id", requestID)

if e.signer != nil {
signature, err := e.signer.CalculateSignature(payload.Bytes())
if err != nil {
logging.LogErrorCtx(ctx, entities.ErrMetricReport, "error during signing", err.Error())
return err
signature, signErr := e.signer.CalculateSignature(payload.Bytes())
if signErr != nil {
logging.LogErrorCtx(ctx, entities.ErrMetricReport, "error during signing", signErr.Error())
return signErr
}

req.Header.Set("HashSHA256", signature)
Expand All @@ -153,7 +152,12 @@ func (e *BatchExporter) doSend() error {
return entities.RetriableError{Err: err, RetryAfter: 10 * time.Second}
}

defer resp.Body.Close()
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
logging.LogError(closeErr)
}
}()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
logging.LogErrorCtx(ctx, entities.ErrMetricReport, "error reading response body", err.Error())
Expand Down
26 changes: 15 additions & 11 deletions internal/agent/exporter/limited_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ex0rcist/metflix/internal/compression"
"github.com/ex0rcist/metflix/internal/entities"
"github.com/ex0rcist/metflix/internal/logging"
"github.com/ex0rcist/metflix/internal/retrier"
"github.com/ex0rcist/metflix/internal/services"
"github.com/ex0rcist/metflix/internal/utils"
"github.com/ex0rcist/metflix/pkg/metrics"
Expand Down Expand Up @@ -116,20 +117,18 @@ func (e *LimitedExporter) spawnWorkers(numWorkers int) {
}

func (e *LimitedExporter) worker(id int) {
delays := []time.Duration{1 * time.Second, 3 * time.Second, 5 * time.Second}

for mex := range e.jobs {
logging.LogDebugF("worker #%d started job", id)

err := utils.NewRetrier(
err := retrier.New(
func() error { return e.doSend(mex) },
func(err error) bool {
_, ok := err.(entities.RetriableError)
return ok
},
[]time.Duration{
1 * time.Second,
3 * time.Second,
5 * time.Second,
},
retrier.WithDelays(delays),
).Run()

if err != nil {
Expand Down Expand Up @@ -169,10 +168,10 @@ func (e *LimitedExporter) doSend(mex metrics.MetricExchange) error {
req.Header.Set("X-Request-Id", requestID)

if e.signer != nil {
signature, err := e.signer.CalculateSignature(payload.Bytes())
if err != nil {
logging.LogErrorCtx(ctx, entities.ErrMetricReport, "error during signing", err.Error())
return err
signature, signErr := e.signer.CalculateSignature(payload.Bytes())
if signErr != nil {
logging.LogErrorCtx(ctx, entities.ErrMetricReport, "error during signing", signErr.Error())
return signErr
}

req.Header.Set("HashSHA256", signature)
Expand All @@ -186,7 +185,12 @@ func (e *LimitedExporter) doSend(mex metrics.MetricExchange) error {
return entities.RetriableError{Err: err, RetryAfter: 10 * time.Second}
}

defer resp.Body.Close()
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
logging.LogError(closeErr)
}
}()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
logging.LogErrorCtx(ctx, entities.ErrMetricReport, "error reading response body", err.Error())
Expand Down
8 changes: 7 additions & 1 deletion internal/compression/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"testing"

"github.com/ex0rcist/metflix/internal/logging"
"github.com/klauspost/compress/gzip"
)

Expand All @@ -19,7 +20,12 @@ func TestPack_Success(t *testing.T) {
if err != nil {
t.Fatalf("expected no error creating gzip reader, got %v", err)
}
defer reader.Close()

defer func() {
if closeErr := reader.Close(); closeErr != nil {
logging.LogError(closeErr)
}
}()

unpackedData, err := io.ReadAll(reader)
if err != nil {
Expand Down
21 changes: 18 additions & 3 deletions internal/compression/compressor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http/httptest"
"testing"

"github.com/ex0rcist/metflix/internal/logging"
"github.com/klauspost/compress/gzip"
)

Expand All @@ -28,7 +29,12 @@ func TestCompressor_Write_SupportedContent(t *testing.T) {
compressor.Close()

resp := recorder.Result()
defer resp.Body.Close()

defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
logging.LogError(closeErr)
}
}()

if resp.Header.Get("Content-Encoding") != "gzip" {
t.Fatalf("expected Content-Encoding to be gzip, got %s", resp.Header.Get("Content-Encoding"))
Expand All @@ -38,7 +44,12 @@ func TestCompressor_Write_SupportedContent(t *testing.T) {
if err != nil {
t.Fatalf("expected no error creating gzip reader, got %v", err)
}
defer gr.Close()

defer func() {
if closeErr := gr.Close(); closeErr != nil {
logging.LogError(closeErr)
}
}()

uncompressedData := new(bytes.Buffer)
_, err = uncompressedData.ReadFrom(gr)
Expand Down Expand Up @@ -66,7 +77,11 @@ func TestCompressor_Write_UnsupportedContent(t *testing.T) {
}

resp := recorder.Result()
defer resp.Body.Close()
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
logging.LogError(closeErr)
}
}()

if resp.Header.Get("Content-Encoding") == "gzip" {
t.Fatalf("expected Content-Encoding to not be gzip")
Expand Down
12 changes: 10 additions & 2 deletions internal/compression/decompressor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/klauspost/compress/gzip"

"github.com/ex0rcist/metflix/internal/entities"
"github.com/ex0rcist/metflix/internal/logging"

"net/http"
"net/http/httptest"
Expand All @@ -24,7 +25,10 @@ func TestDecompressor_Decompress_SupportedEncoding(t *testing.T) {
t.Fatalf("expected no error on writer.Write(), got %v", err)
}

writer.Close()
err = writer.Close()
if err != nil {
logging.LogError(err)
}

req := httptest.NewRequest(http.MethodPost, "/", &buf)
req.Header.Set("Content-Encoding", "gzip")
Expand Down Expand Up @@ -95,7 +99,11 @@ func TestDecompressor_Close(t *testing.T) {
if err != nil {
t.Fatalf("expected no error on writer.Write, got %v", err)
}
writer.Close()

err = writer.Close()
if err != nil {
logging.LogError(err)
}

req := httptest.NewRequest(http.MethodPost, "/", &buf)
req.Header.Set("Content-Encoding", "gzip")
Expand Down
4 changes: 2 additions & 2 deletions internal/httpserver/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (r MetricResource) UpdateMetricJSON(rw http.ResponseWriter, req *http.Reque
}
}

// BatchUpdateMetricsJSON godoc
// UpdateMetricsBatch godoc
// @Tags Metrics
// @Router /updates [post]
// @Summary Push list of metrics data as JSON
Expand All @@ -189,7 +189,7 @@ func (r MetricResource) UpdateMetricJSON(rw http.ResponseWriter, req *http.Reque
// @Success 200 {object} []metrics.MetricExchange
// @Failure 400 {string} string http.StatusBadRequest
// @Failure 500 {string} string http.StatusInternalServerError
func (r MetricResource) BatchUpdateMetricsJSON(rw http.ResponseWriter, req *http.Request) {
func (r MetricResource) UpdateMetricsBatch(rw http.ResponseWriter, req *http.Request) {
ctx := req.Context()

records, err := parseJSONMetricsList(req)
Expand Down
7 changes: 6 additions & 1 deletion internal/httpserver/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/ex0rcist/metflix/internal/entities"
"github.com/ex0rcist/metflix/internal/logging"
"github.com/ex0rcist/metflix/internal/services"
"github.com/ex0rcist/metflix/internal/storage"
"github.com/ex0rcist/metflix/pkg/metrics"
Expand All @@ -29,7 +30,11 @@ func testRequest(t *testing.T, router http.Handler, method, path string, payload
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)

defer resp.Body.Close()
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
logging.LogError(closeErr)
}
}()

contentType := resp.Header.Get("Content-Type")

Expand Down
2 changes: 1 addition & 1 deletion internal/httpserver/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func registerMetricsEndpoints(storageService storage.StorageService, router *chi

router.Post("/update/{metricKind}/{metricName}/{metricValue}", resource.UpdateMetric)
router.Post("/update", resource.UpdateMetricJSON)
router.Post("/updates", resource.BatchUpdateMetricsJSON)
router.Post("/updates", resource.UpdateMetricsBatch)

router.Get("/value/{metricKind}/{metricName}", resource.GetMetric)
router.Post("/value", resource.GetMetricJSON)
Expand Down
Loading
Loading