This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
generated from yandex-praktikum/go-musthave-devops-tpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
202 lines (165 loc) · 4.37 KB
/
agent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package config
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/alkurbatov/metrics-collector/internal/entity"
"github.com/alkurbatov/metrics-collector/internal/security"
"github.com/caarlos0/env/v6"
"github.com/rs/zerolog/log"
flag "github.com/spf13/pflag"
)
type Agent struct {
PollInterval time.Duration `env:"POLL_INTERVAL" json:"poll_interval"`
ReportInterval time.Duration `env:"REPORT_INTERVAL" json:"report_interval"`
Address entity.NetAddress `env:"ADDRESS" json:"address"`
Transport string `env:"TRANSPORT" json:"transport"`
Secret security.Secret `env:"KEY" json:"key"`
PublicKeyPath entity.FilePath `env:"CRYPTO_KEY" json:"crypto_key"`
PollTimeout time.Duration `json:"-"`
ExportTimeout time.Duration `json:"-"`
Debug bool `env:"DEBUG" json:"debug"`
}
func NewAgent() *Agent {
return &Agent{
Address: "0.0.0.0:8080",
Transport: entity.TransportHTTP,
ReportInterval: 10 * time.Second,
PollInterval: 2 * time.Second,
Secret: "",
PublicKeyPath: "",
PollTimeout: 2 * time.Second,
ExportTimeout: 4 * time.Second,
Debug: false,
}
}
func (c *Agent) Parse() error {
address := c.Address
flag.VarP(
&address,
"address",
"a",
"address:port of metrics collector",
)
transport := flag.StringP(
"transport",
"t",
c.Transport,
"type of transport used to export metrics to the server (http or grpc)",
)
reportInterval := flag.DurationP(
"report-interval",
"r",
c.ReportInterval,
"metrics report interval in seconds",
)
pollInterval := flag.DurationP(
"poll-interval",
"p",
c.PollInterval,
"metrics poll interval in seconds",
)
secret := c.Secret
flag.VarP(
&secret,
"key",
"k",
"secret key for signature generation",
)
keyPath := c.PublicKeyPath
flag.VarP(
&keyPath,
"crypto-key",
"e",
"path to public key (stored in PEM format) to encrypt agent -> server communications",
)
debug := flag.BoolP(
"debug",
"g",
c.Debug,
"enable verbose logging",
)
configPath := entity.FilePath("")
flag.VarP(
&configPath,
"config",
"c",
"path to configuration file in JSON format",
)
flag.Parse()
if len(configPath) != 0 {
if err := LoadFromFile(configPath, c); err != nil {
return err
}
}
flag.Visit(func(f *flag.Flag) {
switch f.Name {
case "address":
c.Address = address
case "transport":
c.Transport = *transport
case "report-interval":
c.ReportInterval = *reportInterval
case "poll-interval":
c.PollInterval = *pollInterval
case "key":
c.Secret = secret
case "crypto-key":
c.PublicKeyPath = keyPath
case "debug":
c.Debug = *debug
}
})
err := env.Parse(c)
if err != nil {
log.Fatal().Err(err).Msg("")
}
c.Transport = strings.ToLower(c.Transport)
return nil
}
func (c Agent) String() string {
var sb strings.Builder
sb.WriteString("Configuration:\n")
sb.WriteString(fmt.Sprintf("\t\tPoll interval: %s\n", c.PollInterval))
sb.WriteString(fmt.Sprintf("\t\tReport interval: %s\n", c.ReportInterval))
sb.WriteString(fmt.Sprintf("\t\tCollector address: %s\n", c.Address))
sb.WriteString(fmt.Sprintf("\t\tTransport: %s\n", c.Transport))
if len(c.Secret) > 0 {
sb.WriteString(fmt.Sprintf("\t\tSecret key: %s\n", c.Secret))
}
if len(c.PublicKeyPath) > 0 {
sb.WriteString(fmt.Sprintf("\t\tPublic key path: %s\n", c.PublicKeyPath))
}
sb.WriteString(fmt.Sprintf("\t\tPollTimeout: %fs\n", c.PollTimeout.Seconds()))
sb.WriteString(fmt.Sprintf("\t\tExportTimeout: %fs\n", c.ExportTimeout.Seconds()))
sb.WriteString(fmt.Sprintf("\t\tDebug: %t", c.Debug))
return sb.String()
}
func (c *Agent) UnmarshalJSON(data []byte) error {
type Alias Agent
aux := &struct {
PollInterval string `json:"poll_interval"`
ReportInterval string `json:"report_interval"`
*Alias
}{
Alias: (*Alias)(c),
}
if err := json.Unmarshal(data, &aux); err != nil {
return fmt.Errorf("agent - UnmarshalJSON - json.Unmarshal: %w", err)
}
var err error
if len(aux.ReportInterval) != 0 {
c.ReportInterval, err = time.ParseDuration(aux.ReportInterval)
if err != nil {
return fmt.Errorf("agent - UnmarshalJSON - time.ParseDuration: %w", err)
}
}
if len(aux.PollInterval) != 0 {
c.PollInterval, err = time.ParseDuration(aux.PollInterval)
if err != nil {
return fmt.Errorf("agent - UnmarshalJSON - time.ParseDuration: %w", err)
}
}
return nil
}