generated from Yandex-Practicum/go-musthave-metrics-tpl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from ex0rcist/iter5
Инкремент №5
- Loading branch information
Showing
34 changed files
with
1,658 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
linters-settings: | ||
custom: | ||
statictest: | ||
path: ./plugin/statictest.so |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,28 @@ | ||
package main | ||
|
||
func main() {} | ||
import ( | ||
"github.com/ex0rcist/metflix/internal/agent" | ||
"github.com/ex0rcist/metflix/internal/logging" | ||
) | ||
|
||
func main() { | ||
logging.Setup() | ||
|
||
logging.LogInfo("starting agent...") | ||
|
||
agnt, err := agent.New() | ||
if err != nil { | ||
logging.LogFatal(err) | ||
} | ||
|
||
err = agnt.ParseFlags() | ||
if err != nil { | ||
logging.LogFatal(err) | ||
} | ||
|
||
logging.LogInfo(agnt.Config.String()) | ||
|
||
agnt.Run() | ||
|
||
logging.LogInfo("agent ready") | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,30 @@ | ||
package main | ||
|
||
func main() {} | ||
import ( | ||
"github.com/ex0rcist/metflix/internal/logging" | ||
"github.com/ex0rcist/metflix/internal/server" | ||
) | ||
|
||
func main() { | ||
logging.Setup() | ||
|
||
logging.LogInfo("starting server...") | ||
|
||
srv, err := server.New() | ||
if err != nil { | ||
logging.LogFatal(err) | ||
} | ||
|
||
err = srv.ParseFlags() | ||
if err != nil { | ||
logging.LogFatal(err) | ||
} | ||
|
||
logging.LogInfo(srv.Config.String()) | ||
logging.LogInfo("server ready") // TODO: must be after run? | ||
|
||
err = srv.Run() | ||
if err != nil { | ||
logging.LogFatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module github.com/ex0rcist/metflix | ||
|
||
go 1.22.4 | ||
|
||
require ( | ||
github.com/caarlos0/env/v11 v11.1.0 | ||
github.com/go-chi/chi/v5 v5.1.0 | ||
github.com/pkg/errors v0.9.1 | ||
github.com/rs/zerolog v1.33.0 | ||
github.com/spf13/pflag v1.0.5 | ||
github.com/stretchr/testify v1.9.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.19 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
golang.org/x/sys v0.21.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
github.com/caarlos0/env/v11 v11.1.0 h1:a5qZqieE9ZfzdvbbdhTalRrHT5vu/4V1/ad1Ka6frhI= | ||
github.com/caarlos0/env/v11 v11.1.0/go.mod h1:LwgkYk1kDvfGpHthrWWLof3Ny7PezzFwS4QrsJdHTMo= | ||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= | ||
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= | ||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= | ||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= | ||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= | ||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= | ||
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= | ||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= | ||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
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/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= | ||
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= | ||
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= | ||
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package agent | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/caarlos0/env/v11" | ||
"github.com/ex0rcist/metflix/internal/entities" | ||
"github.com/ex0rcist/metflix/internal/logging" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
type Agent struct { | ||
Config *Config | ||
Stats *Stats | ||
API *API | ||
|
||
wg sync.WaitGroup | ||
} | ||
|
||
type Config struct { | ||
Address entities.Address `env:"ADDRESS"` | ||
PollInterval int `env:"POLL_INTERVAL"` | ||
ReportInterval int `env:"REPORT_INTERVAL"` | ||
} | ||
|
||
func New() (*Agent, error) { | ||
config := &Config{ | ||
Address: "0.0.0.0:8080", | ||
PollInterval: 2, | ||
ReportInterval: 10, | ||
} | ||
|
||
stats := NewStats() | ||
api := NewAPI(&config.Address, nil) | ||
|
||
return &Agent{ | ||
Config: config, | ||
Stats: stats, | ||
API: api, | ||
}, nil | ||
} | ||
|
||
func (a *Agent) ParseFlags() error { | ||
address := a.Config.Address | ||
|
||
pflag.VarP(&address, "address", "a", "address:port for HTTP API requests") // HELP: "&"" because Set() has pointer receiver? | ||
|
||
pflag.IntVarP(&a.Config.PollInterval, "poll-interval", "p", a.Config.PollInterval, "interval (s) for polling stats") | ||
pflag.IntVarP(&a.Config.ReportInterval, "report-interval", "r", a.Config.ReportInterval, "interval (s) for polling stats") | ||
|
||
pflag.Parse() | ||
|
||
// because VarP gets non-pointer value, set it manually | ||
pflag.Visit(func(f *pflag.Flag) { | ||
switch f.Name { | ||
case "address": | ||
a.Config.Address = address | ||
} | ||
}) | ||
|
||
if err := env.Parse(a.Config); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (a *Agent) Run() { | ||
a.wg.Add(2) | ||
|
||
go a.startPolling() | ||
go a.startReporting() | ||
|
||
a.wg.Wait() | ||
} | ||
|
||
func (a *Agent) startPolling() { | ||
defer a.wg.Done() | ||
|
||
for { | ||
err := a.Stats.Poll() | ||
if err != nil { | ||
logging.LogError(err) | ||
} | ||
|
||
time.Sleep(intToDuration(a.Config.PollInterval)) | ||
} | ||
} | ||
|
||
func (a *Agent) startReporting() { | ||
defer a.wg.Done() | ||
|
||
for { | ||
time.Sleep(intToDuration(a.Config.ReportInterval)) | ||
|
||
a.reportStats() | ||
} | ||
} | ||
|
||
func (a *Agent) reportStats() { | ||
logging.LogInfo("reporting stats ... ") | ||
|
||
// agent continues polling while report is in progress, take snapshot? | ||
snapshot := *a.Stats | ||
|
||
a.API. | ||
Report("Alloc", snapshot.Runtime.Alloc). | ||
Report("BuckHashSys", snapshot.Runtime.BuckHashSys). | ||
Report("Frees", snapshot.Runtime.Frees). | ||
Report("GCCPUFraction", snapshot.Runtime.GCCPUFraction). | ||
Report("GCSys", snapshot.Runtime.GCSys). | ||
Report("HeapAlloc", snapshot.Runtime.HeapAlloc). | ||
Report("HeapIdle", snapshot.Runtime.HeapIdle). | ||
Report("HeapInuse", snapshot.Runtime.HeapInuse). | ||
Report("HeapObjects", snapshot.Runtime.HeapObjects). | ||
Report("HeapReleased", snapshot.Runtime.HeapReleased). | ||
Report("HeapSys", snapshot.Runtime.HeapSys). | ||
Report("LastGC", snapshot.Runtime.LastGC). | ||
Report("Lookups", snapshot.Runtime.Lookups). | ||
Report("MCacheInuse", snapshot.Runtime.MCacheInuse). | ||
Report("MCacheSys", snapshot.Runtime.MCacheSys). | ||
Report("MSpanInuse", snapshot.Runtime.MSpanInuse). | ||
Report("MSpanSys", snapshot.Runtime.MSpanSys). | ||
Report("Mallocs", snapshot.Runtime.Mallocs). | ||
Report("NextGC", snapshot.Runtime.NextGC). | ||
Report("NumForcedGC", snapshot.Runtime.NumForcedGC). | ||
Report("NumGC", snapshot.Runtime.NumGC). | ||
Report("OtherSys", snapshot.Runtime.OtherSys). | ||
Report("PauseTotalNs", snapshot.Runtime.PauseTotalNs). | ||
Report("StackInuse", snapshot.Runtime.StackInuse). | ||
Report("StackSys", snapshot.Runtime.StackSys). | ||
Report("Sys", snapshot.Runtime.Sys). | ||
Report("TotalAlloc", snapshot.Runtime.TotalAlloc) | ||
|
||
a.API. | ||
Report("RandomValue", snapshot.RandomValue) | ||
|
||
a.API. | ||
Report("PollCount", snapshot.PollCount) | ||
|
||
// because metrics.Counter adds value to itself | ||
a.Stats.PollCount -= snapshot.PollCount | ||
} | ||
|
||
func intToDuration(s int) time.Duration { | ||
return time.Duration(s) * time.Second | ||
} | ||
|
||
func (c Config) String() string { | ||
out := "agent config: " | ||
|
||
out += fmt.Sprintf("address=%v \t", c.Address) | ||
out += fmt.Sprintf("poll-interval=%v \t", c.PollInterval) | ||
out += fmt.Sprintf("report-interval=%v \t", c.ReportInterval) | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package agent | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRun(t *testing.T) { | ||
_, err := New() | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package agent | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/ex0rcist/metflix/internal/entities" | ||
"github.com/ex0rcist/metflix/internal/logging" | ||
"github.com/ex0rcist/metflix/internal/metrics" | ||
) | ||
|
||
type API struct { | ||
address *entities.Address | ||
httpClient *http.Client | ||
} | ||
|
||
func NewAPI(address *entities.Address, httpTransport http.RoundTripper) *API { | ||
if httpTransport == nil { | ||
httpTransport = http.DefaultTransport | ||
} | ||
|
||
client := &http.Client{ | ||
Timeout: 2 * time.Second, | ||
Transport: httpTransport, | ||
} | ||
|
||
return &API{ | ||
address: address, | ||
httpClient: client, | ||
} | ||
} | ||
|
||
func (c *API) Report(name string, metric metrics.Metric) *API { | ||
// todo: another transport? | ||
url := "http://" + c.address.String() + fmt.Sprintf("/update/%s/%s/%s", metric.Kind(), name, metric) | ||
|
||
req, err := http.NewRequest(http.MethodPost, url, http.NoBody) | ||
if err != nil { | ||
logging.LogError(err, "httpRequest error") | ||
} | ||
|
||
req.Header.Set("Content-Type", "text/plain") | ||
|
||
logging.LogInfo(fmt.Sprintf("sending POST to %v", url)) | ||
|
||
resp, err := c.httpClient.Do(req) | ||
|
||
if err != nil { | ||
logging.LogError(err, "httpClient error") | ||
return c | ||
} | ||
|
||
defer resp.Body.Close() | ||
respBody, err := io.ReadAll(resp.Body) // нужно прочитать ответ для keepalive? | ||
if err != nil { | ||
logging.LogError(entities.ErrMetricReport, "error reading response body") | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
logging.LogError(entities.ErrMetricReport, string(respBody)) | ||
} | ||
|
||
return c | ||
} |
Oops, something went wrong.