Skip to content

Commit

Permalink
Merge pull request #18 from ex0rcist/iter20
Browse files Browse the repository at this point in the history
Инкремент №20
  • Loading branch information
ex0rcist authored Nov 7, 2024
2 parents 08eb4a0 + 33fb865 commit 92cc8b1
Show file tree
Hide file tree
Showing 39 changed files with 1,545 additions and 557 deletions.
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
5 changes: 3 additions & 2 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"fmt"

"github.com/ex0rcist/metflix/internal/agent"
"github.com/ex0rcist/metflix/internal/logging"
)
Expand All @@ -13,8 +15,7 @@ var (

func main() {
logging.Setup()

logging.LogInfoF("starting agent v%s [%s, #%s]...", buildVersion, buildDate, buildCommit)
fmt.Printf("Build version: %s\nBuild date: %s\nBuild commit: %s\n", buildVersion, buildDate, buildCommit)

agnt, err := agent.New()
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"fmt"

"github.com/ex0rcist/metflix/internal/logging"
"github.com/ex0rcist/metflix/internal/server"
)
Expand All @@ -13,7 +15,7 @@ var (

func main() {
logging.Setup()
logging.LogInfoF("starting server v%s [%s, #%s]...", buildVersion, buildDate, buildCommit)
fmt.Printf("Build version: %s\nBuild date: %s\nBuild commit: %s\n", buildVersion, buildDate, buildCommit)

srv, err := server.New()
if err != nil {
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.
696 changes: 514 additions & 182 deletions coverage.html

Large diffs are not rendered by default.

618 changes: 340 additions & 278 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

0 comments on commit 92cc8b1

Please sign in to comment.