Skip to content

Commit 395a6ea

Browse files
authored
chore: Add docs, tests and optimization (#20)
* fix(agent): Add correct behavior during testing attempts for HTTP CLient * chore(agent): Add profiles * chore: Up Golang verison * docs: Add comments for godoc and examples * tests: Add tests for 40% cover
1 parent e897951 commit 395a6ea

File tree

22 files changed

+553
-101
lines changed

22 files changed

+553
-101
lines changed

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
golang 1.23.1
1+
golang 1.23.8

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ clean:
2222
rm -f $(BINARY_SERVER) $(BINARY_AGENT)
2323

2424
lint:
25-
go fmt ./...
25+
go fmt ./...
26+
goimports -w $(find . -name '*.go')

go.mod

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
11
module github.com/srg-bnd/observator
22

3-
go 1.23.0
4-
5-
toolchain go1.23.8
3+
go 1.23.8
64

75
require (
86
github.com/caarlos0/env/v11 v11.3.1
97
github.com/go-chi/chi v1.5.5
108
github.com/go-resty/resty/v2 v2.16.5
11-
github.com/jackc/pgx/v5 v5.7.4
9+
github.com/jackc/pgx/v5 v5.7.5
1210
github.com/pressly/goose/v3 v3.24.3
13-
github.com/shirou/gopsutil/v4 v4.25.5
11+
github.com/shirou/gopsutil/v4 v4.25.7
1412
github.com/stretchr/testify v1.10.0
1513
go.uber.org/zap v1.27.0
1614
)
1715

1816
require (
1917
github.com/davecgh/go-spew v1.1.1 // indirect
2018
github.com/ebitengine/purego v0.8.4 // indirect
21-
github.com/go-ole/go-ole v1.2.6 // indirect
19+
github.com/go-ole/go-ole v1.3.0 // indirect
2220
github.com/jackc/pgpassfile v1.0.0 // indirect
2321
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
2422
github.com/jackc/puddle/v2 v2.2.2 // indirect
25-
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
23+
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35 // indirect
2624
github.com/mfridman/interpolate v0.0.2 // indirect
2725
github.com/pmezard/go-difflib v1.0.0 // indirect
28-
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
26+
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
2927
github.com/sethvargo/go-retry v0.3.0 // indirect
30-
github.com/tklauser/go-sysconf v0.3.12 // indirect
31-
github.com/tklauser/numcpus v0.6.1 // indirect
28+
github.com/tklauser/go-sysconf v0.3.15 // indirect
29+
github.com/tklauser/numcpus v0.10.0 // indirect
3230
github.com/yusufpapurcu/wmi v1.2.4 // indirect
3331
go.uber.org/multierr v1.11.0 // indirect
34-
golang.org/x/crypto v0.38.0 // indirect
35-
golang.org/x/net v0.40.0 // indirect
36-
golang.org/x/sync v0.14.0 // indirect
37-
golang.org/x/sys v0.33.0 // indirect
38-
golang.org/x/text v0.25.0 // indirect
32+
golang.org/x/crypto v0.41.0 // indirect
33+
golang.org/x/net v0.43.0 // indirect
34+
golang.org/x/sync v0.16.0 // indirect
35+
golang.org/x/sys v0.35.0 // indirect
36+
golang.org/x/text v0.28.0 // indirect
3937
gopkg.in/yaml.v3 v3.0.1 // indirect
4038
)

go.sum

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ github.com/ebitengine/purego v0.8.4 h1:CF7LEKg5FFOsASUj0+QwaXf8Ht6TlFxg09+S9wz0o
99
github.com/ebitengine/purego v0.8.4/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
1010
github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE=
1111
github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw=
12-
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
1312
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
13+
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
14+
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
1415
github.com/go-resty/resty/v2 v2.16.5 h1:hBKqmWrr7uRc3euHVqmh1HTHcKn99Smr7o5spptdhTM=
1516
github.com/go-resty/resty/v2 v2.16.5/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA=
16-
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
1717
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
1818
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
1919
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
@@ -22,16 +22,16 @@ github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsI
2222
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
2323
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
2424
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
25-
github.com/jackc/pgx/v5 v5.7.4 h1:9wKznZrhWa2QiHL+NjTSPP6yjl3451BX3imWDnokYlg=
26-
github.com/jackc/pgx/v5 v5.7.4/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
25+
github.com/jackc/pgx/v5 v5.7.5 h1:JHGfMnQY+IEtGM63d+NGMjoRpysB2JBwDr5fsngwmJs=
26+
github.com/jackc/pgx/v5 v5.7.5/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M=
2727
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
2828
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
2929
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
3030
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
3131
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
3232
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
33-
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
34-
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
33+
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35 h1:PpXWgLPs+Fqr325bN2FD2ISlRRztXibcX6e8f5FR5Dc=
34+
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35/go.mod h1:autxFIvghDt3jPTLoqZ9OZ7s9qTGNAWmYCjVFWPX/zg=
3535
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
3636
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
3737
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
@@ -40,8 +40,8 @@ github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdh
4040
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
4141
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4242
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
43-
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
44-
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
43+
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=
44+
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
4545
github.com/pressly/goose/v3 v3.24.3 h1:DSWWNwwggVUsYZ0X2VitiAa9sKuqtBfe+Jr9zFGwWlM=
4646
github.com/pressly/goose/v3 v3.24.3/go.mod h1:v9zYL4xdViLHCUUJh/mhjnm6JrK7Eul8AS93IxiZM4E=
4747
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
@@ -50,17 +50,17 @@ github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0t
5050
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
5151
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
5252
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
53-
github.com/shirou/gopsutil/v4 v4.25.5 h1:rtd9piuSMGeU8g1RMXjZs9y9luK5BwtnG7dZaQUJAsc=
54-
github.com/shirou/gopsutil/v4 v4.25.5/go.mod h1:PfybzyydfZcN+JMMjkF6Zb8Mq1A/VcogFFg7hj50W9c=
53+
github.com/shirou/gopsutil/v4 v4.25.7 h1:bNb2JuqKuAu3tRlPv5piSmBZyMfecwQ+t/ILq+1JqVM=
54+
github.com/shirou/gopsutil/v4 v4.25.7/go.mod h1:XV/egmwJtd3ZQjBpJVY5kndsiOO4IRqy9TQnmm6VP7U=
5555
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5656
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
5757
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
5858
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
5959
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
60-
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
61-
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
62-
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
63-
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
60+
github.com/tklauser/go-sysconf v0.3.15 h1:VE89k0criAymJ/Os65CSn1IXaol+1wrsFHEB8Ol49K4=
61+
github.com/tklauser/go-sysconf v0.3.15/go.mod h1:Dmjwr6tYFIseJw7a3dRLJfsHAMXZ3nEnL/aZY+0IuI4=
62+
github.com/tklauser/numcpus v0.10.0 h1:18njr6LDBk1zuna922MgdjQuJFjrdppsZG60sHGfjso=
63+
github.com/tklauser/numcpus v0.10.0/go.mod h1:BiTKazU708GQTYF4mB+cmlpT2Is1gLk7XVuEeem8LsQ=
6464
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
6565
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
6666
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
@@ -69,25 +69,23 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
6969
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
7070
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
7171
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
72-
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
73-
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
72+
golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4=
73+
golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc=
7474
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6 h1:y5zboxd6LQAqYIhHnB48p0ByQ/GnQx2BE33L8BOHQkI=
7575
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6/go.mod h1:U6Lno4MTRCDY+Ba7aCcauB9T60gsv5s4ralQzP72ZoQ=
76-
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
77-
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
78-
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
79-
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
76+
golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE=
77+
golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
78+
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
79+
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
8080
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8181
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
82-
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
83-
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
84-
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
85-
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
86-
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
87-
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
82+
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
83+
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
84+
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
85+
golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
86+
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
8887
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
8988
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
90-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
9189
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9290
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
9391
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

internal/agent/agent.go

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
// Agent
1+
// Package agent provides implementation of monitoring agent responsible for data collection and reporting.
2+
//
3+
// The package contains core components for monitoring system:
4+
// - Agent structure representing main monitoring entity
5+
// - Methods for starting and managing monitoring processes
6+
// - Dependencies for data collection and reporting
7+
//
8+
// This package serves as a central component for monitoring operations,
9+
// coordinating data collection and reporting activities.
10+
//
11+
// Usage example:
12+
//
13+
// container := config.NewContainer()
14+
// agent := agent.NewAgent(container)
15+
// agent.Start(60, 300)
216
package agent
317

418
import (
@@ -11,26 +25,73 @@ import (
1125
"github.com/srg-bnd/observator/internal/agent/client"
1226
"github.com/srg-bnd/observator/internal/agent/poller"
1327
"github.com/srg-bnd/observator/internal/agent/reporter"
28+
"github.com/srg-bnd/observator/internal/shared/compressor"
1429
)
1530

16-
// Agent
31+
// Agent represents the main monitoring agent structure.
32+
//
33+
// The Agent struct is responsible for managing data collection and reporting processes.
34+
// It consists of two main components:
35+
// - Poller for collecting monitoring data
36+
// - Reporter for processing and sending collected data
37+
//
38+
// This structure serves as the core component for monitoring operations,
39+
// coordinating data collection and reporting activities.
1740
type Agent struct {
18-
poller *poller.Poller
41+
// Poller is responsible for collecting monitoring data from various sources
42+
poller *poller.Poller
43+
// Reporter is responsible for processing and sending collected data
1944
reporter *reporter.Reporter
2045
}
2146

22-
// Returns a new agent
47+
// NewAgent creates a new Agent instance configured with provided settings.
48+
//
49+
// The function initializes all necessary components for the agent to operate:
50+
// - Poller for data collection
51+
// - Reporter for data processing and reporting
52+
// - Client for communication with the server
53+
//
54+
// Parameters:
55+
// - container: configuration container containing all necessary settings and dependencies
56+
//
57+
// The configuration container should provide:
58+
// * Storage for data persistence
59+
// * WorkerPoolReporter for managing reporting workers
60+
// * Server address for client communication
61+
// * ChecksumService for data integrity verification
62+
// * Other required dependencies
63+
//
64+
// Returns a fully initialized Agent instance ready for operation.
2365
func NewAgent(container *config.Container) *Agent {
2466
return &Agent{
2567
poller: poller.NewPoller(container.Storage),
2668
reporter: reporter.NewReporter(
2769
container.Storage,
2870
container.WorkerPoolReporter,
29-
client.NewClient(container.ServerAddr, container.ChecksumService)),
71+
client.NewClient(
72+
container.ServerAddr,
73+
container.ChecksumService,
74+
compressor.NewCompressor(),
75+
)),
3076
}
3177
}
3278

33-
// Starts processes poller and reporter
79+
// Start launches the agent with specified polling and reporting intervals.
80+
//
81+
// The agent starts working in two modes:
82+
// - Data collection with a specified polling interval
83+
// - Sending collected data with a specified reporting interval
84+
//
85+
// The function creates a context with interrupt signal handling and launches:
86+
// 1. Data collector (poller) in a separate goroutine
87+
// 2. Reporting component (reporter) in the current thread
88+
//
89+
// Parameters:
90+
// - pollInterval: polling interval in seconds
91+
// - reportInterval: reporting interval in seconds
92+
//
93+
// Return values:
94+
// - error: nil in case of successful launch, error in case of failure
3495
func (a *Agent) Start(pollInterval int, reportInterval int) error {
3596
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
3697
defer stop()

internal/agent/client/client.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,24 @@ type ChecksumBehaviour interface {
2121
Sum(string) (string, error)
2222
}
2323

24+
type CompressorBehaviour interface {
25+
Compress([]byte) ([]byte, error)
26+
Decompress([]byte) ([]byte, error)
27+
}
28+
2429
// Client
2530
type Client struct {
2631
httpClient *resty.Client
2732
checksumService *services.Checksum
33+
compressor CompressorBehaviour
2834
}
2935

3036
// Returns a new client
31-
func NewClient(baseURL string, checksumService *services.Checksum) *Client {
37+
func NewClient(baseURL string, checksumService *services.Checksum, compress CompressorBehaviour) *Client {
3238
return &Client{
3339
httpClient: newHTTPClient(baseURL),
3440
checksumService: checksumService,
41+
compressor: compress,
3542
}
3643
}
3744

@@ -42,7 +49,7 @@ func (c *Client) SendMetrics(context context.Context, metrics []models.Metrics)
4249
return err
4350
}
4451

45-
compressedData, err := compress(data)
52+
compressedData, err := c.compressor.Compress(data)
4653
if err != nil {
4754
return err
4855
}
@@ -60,7 +67,7 @@ func (c *Client) SendMetrics(context context.Context, metrics []models.Metrics)
6067

6168
if strings.Contains(response.Header().Get("Accept-Encoding"), "gzip") {
6269
// TODO: use the results
63-
decompress(response.Body())
70+
c.compressor.Decompress(response.Body())
6471
}
6572

6673
return nil

internal/agent/client/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
func TestNewClient(t *testing.T) {
10-
client := NewClient(":8080", nil)
10+
client := NewClient(":8080", nil, nil)
1111
assert.IsType(t, client, &Client{})
1212
}
1313

internal/agent/client/helpers.go

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package client
22

33
import (
4-
"bytes"
5-
"compress/flate"
6-
"compress/gzip"
7-
"fmt"
84
"time"
95

106
"github.com/go-resty/resty/v2"
@@ -14,50 +10,21 @@ import (
1410
func newHTTPClient(baseURL string) *resty.Client {
1511
// Retriable errors
1612
repetitionIntervals := [3]int{1, 3, 5}
17-
currentRepetitionInterval := -1
1813

1914
return resty.New().
2015
SetBaseURL("http://"+baseURL).
2116
SetRetryCount(len(repetitionIntervals)).
2217
SetRetryAfter(func(c *resty.Client, r *resty.Response) (time.Duration, error) {
23-
currentRepetitionInterval++
24-
return time.Duration(repetitionIntervals[currentRepetitionInterval]) * time.Second, nil
18+
var currentRepetitionInterval int
19+
20+
return func() (time.Duration, error) {
21+
delay := time.Duration(repetitionIntervals[currentRepetitionInterval]) * time.Second
22+
currentRepetitionInterval++
23+
return delay, nil
24+
}()
2525
}).
2626
SetRetryMaxWaitTime(5*time.Second).
2727
SetHeader("Content-Type", "application/json").
2828
SetHeader("Accept-Encoding", "gzip").
2929
SetHeader("Content-Encoding", "gzip")
3030
}
31-
32-
// Compress data
33-
func compress(data []byte) ([]byte, error) {
34-
var b bytes.Buffer
35-
36-
w := gzip.NewWriter(&b)
37-
38-
_, err := w.Write(data)
39-
if err != nil {
40-
return nil, fmt.Errorf("failed write data to compress temporary buffer: %v", err)
41-
}
42-
43-
err = w.Close()
44-
if err != nil {
45-
return nil, fmt.Errorf("failed compress data: %v", err)
46-
}
47-
48-
return b.Bytes(), nil
49-
}
50-
51-
// Decompress data
52-
func decompress(compressedData []byte) ([]byte, error) {
53-
r := flate.NewReader(bytes.NewReader(compressedData))
54-
defer r.Close()
55-
56-
var b bytes.Buffer
57-
_, err := b.ReadFrom(r)
58-
if err != nil {
59-
return nil, fmt.Errorf("failed decompress data: %v", err)
60-
}
61-
62-
return b.Bytes(), nil
63-
}

0 commit comments

Comments
 (0)