Linting fixes.
This commit is contained in:
parent
16be332d38
commit
26ce90bb84
@ -12,8 +12,8 @@ linters:
|
|||||||
linters-settings:
|
linters-settings:
|
||||||
lll:
|
lll:
|
||||||
line-length: 120
|
line-length: 120
|
||||||
gocyclo:
|
cyclop:
|
||||||
min-complexity: 40
|
max-complexity: 40
|
||||||
gocognit:
|
gocognit:
|
||||||
min-complexity: 40
|
min-complexity: 40
|
||||||
funlen:
|
funlen:
|
||||||
|
@ -25,6 +25,9 @@ var (
|
|||||||
output = flag.String("output", "json", "Output format. Can be 'json' or 'plain-by-line'.")
|
output = flag.String("output", "json", "Output format. Can be 'json' or 'plain-by-line'.")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// This function uses fmt.Println to print lines without timestamps to make it easy
|
||||||
|
// to parse output, so:
|
||||||
|
// nolint:forbidigo
|
||||||
func main() {
|
func main() {
|
||||||
config := configuration.NewConfig()
|
config := configuration.NewConfig()
|
||||||
|
|
||||||
@ -83,17 +86,17 @@ func main() {
|
|||||||
Timeout: *metricatorTimeout,
|
Timeout: *metricatorTimeout,
|
||||||
}
|
}
|
||||||
|
|
||||||
c := client.NewClient(clientConfig, logger)
|
clnt := client.NewClient(clientConfig, logger)
|
||||||
|
|
||||||
var data interface{}
|
var data interface{}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case *appsList:
|
case *appsList:
|
||||||
data = c.GetAppsList()
|
data = clnt.GetAppsList()
|
||||||
case *metricsList:
|
case *metricsList:
|
||||||
data = c.GetMetricsList(*application)
|
data = clnt.GetMetricsList(*application)
|
||||||
case *metric != "":
|
case *metric != "":
|
||||||
data = c.GetMetric(*application, *metric)
|
data = clnt.GetMetric(*application, *metric)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch *output {
|
switch *output {
|
||||||
|
@ -29,8 +29,7 @@ func main() {
|
|||||||
// Parse configuration.
|
// Parse configuration.
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
err := config.Parse()
|
if err := config.Parse(); err != nil {
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("Failed to parse configuration:", err.Error())
|
log.Fatalln("Failed to parse configuration:", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,16 +28,18 @@ type Application struct {
|
|||||||
|
|
||||||
// NewApplication creates new application.
|
// NewApplication creates new application.
|
||||||
func NewApplication(ctx context.Context, name string, config *Config, logger *logger.Logger) *Application {
|
func NewApplication(ctx context.Context, name string, config *Config, logger *logger.Logger) *Application {
|
||||||
a := &Application{
|
// Some variables are initialized in initialize() function.
|
||||||
|
// nolint:exhaustivestruct
|
||||||
|
app := &Application{
|
||||||
config: config,
|
config: config,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
doneChan: make(chan struct{}),
|
doneChan: make(chan struct{}),
|
||||||
logger: logger,
|
logger: logger,
|
||||||
name: name,
|
name: name,
|
||||||
}
|
}
|
||||||
a.initialize()
|
app.initialize()
|
||||||
|
|
||||||
return a
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDoneChan returns a channel which should be used to block execution until
|
// GetDoneChan returns a channel which should be used to block execution until
|
||||||
|
@ -11,5 +11,6 @@ type Config struct {
|
|||||||
Endpoint string `yaml:"endpoint"`
|
Endpoint string `yaml:"endpoint"`
|
||||||
// TimeBetweenRequests is a minimal amount of time which should pass
|
// TimeBetweenRequests is a minimal amount of time which should pass
|
||||||
// between requests.
|
// between requests.
|
||||||
|
// nolint:tagliatelle
|
||||||
TimeBetweenRequests time.Duration `yaml:"time_between_requests"`
|
TimeBetweenRequests time.Duration `yaml:"time_between_requests"`
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,9 @@ func (a *Application) fetch() {
|
|||||||
// Do not do anything if fetching is running.
|
// Do not do anything if fetching is running.
|
||||||
// ToDo: maybe another approach?
|
// ToDo: maybe another approach?
|
||||||
a.fetchIsRunningMutex.RLock()
|
a.fetchIsRunningMutex.RLock()
|
||||||
|
// This is an optimization to avoid excessive waiting when using Lock().
|
||||||
|
// Most of time application will wait between fetches.
|
||||||
|
// nolint:ifshort
|
||||||
isFetching := a.fetchIsRunning
|
isFetching := a.fetchIsRunning
|
||||||
a.fetchIsRunningMutex.RUnlock()
|
a.fetchIsRunningMutex.RUnlock()
|
||||||
|
|
||||||
|
@ -143,8 +143,8 @@ func (a *Application) getParametersForPrometheusMetric(line string) []string {
|
|||||||
paramNameFinished, paramValueStarted, paramValueFinished bool
|
paramNameFinished, paramValueStarted, paramValueFinished bool
|
||||||
)
|
)
|
||||||
|
|
||||||
for _, r := range valuesString {
|
for _, runeChar := range valuesString {
|
||||||
if paramValueFinished && string(r) == "," {
|
if paramValueFinished && string(runeChar) == "," {
|
||||||
params = append(params, paramName+":"+paramValue)
|
params = append(params, paramName+":"+paramValue)
|
||||||
paramName, paramValue = "", ""
|
paramName, paramValue = "", ""
|
||||||
paramNameFinished, paramValueStarted, paramValueFinished = false, false, false
|
paramNameFinished, paramValueStarted, paramValueFinished = false, false, false
|
||||||
@ -156,8 +156,8 @@ func (a *Application) getParametersForPrometheusMetric(line string) []string {
|
|||||||
// "deeply nested"? I think not. So:
|
// "deeply nested"? I think not. So:
|
||||||
// nolint:nestif
|
// nolint:nestif
|
||||||
if !paramNameFinished {
|
if !paramNameFinished {
|
||||||
if string(r) != "=" {
|
if string(runeChar) != "=" {
|
||||||
paramName += string(r)
|
paramName += string(runeChar)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
@ -166,19 +166,19 @@ func (a *Application) getParametersForPrometheusMetric(line string) []string {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if string(r) == "\"" && !paramValueStarted {
|
if string(runeChar) == "\"" && !paramValueStarted {
|
||||||
paramValueStarted = true
|
paramValueStarted = true
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if paramValueStarted && string(r) != "\"" {
|
if paramValueStarted && string(runeChar) != "\"" {
|
||||||
paramValue += string(r)
|
paramValue += string(runeChar)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if paramValueStarted && string(r) == "\"" {
|
if paramValueStarted && string(runeChar) == "\"" {
|
||||||
paramValueFinished = true
|
paramValueFinished = true
|
||||||
|
|
||||||
continue
|
continue
|
||||||
|
@ -21,16 +21,18 @@ var (
|
|||||||
|
|
||||||
// Config is an application's configuration.
|
// Config is an application's configuration.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
configPath string
|
|
||||||
// Applications describes configuration for remote application's endpoints.
|
// Applications describes configuration for remote application's endpoints.
|
||||||
// Key is an application's name.
|
// Key is an application's name.
|
||||||
Applications map[string]*application.Config `yaml:"applications"`
|
Applications map[string]*application.Config `yaml:"applications"`
|
||||||
// Logger is a logging configuration.
|
// Logger is a logging configuration.
|
||||||
Logger *logger.Config `yaml:"logger"`
|
Logger *logger.Config `yaml:"logger"`
|
||||||
|
configPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConfig returns new configuration.
|
// NewConfig returns new configuration.
|
||||||
func NewConfig() *Config {
|
func NewConfig() *Config {
|
||||||
|
// Fields are initialized when parsing YAML file.
|
||||||
|
// nolint:exhaustivestruct
|
||||||
c := &Config{}
|
c := &Config{}
|
||||||
c.initialize()
|
c.initialize()
|
||||||
|
|
||||||
|
@ -48,14 +48,14 @@ func (h *handler) getAppsList() ([]byte, error) {
|
|||||||
|
|
||||||
// Gets request information from URL. Returns a structure with filled request
|
// Gets request information from URL. Returns a structure with filled request
|
||||||
// info and error if it occurs.
|
// info and error if it occurs.
|
||||||
func (h *handler) getRequestInfo(r *http.Request) (*models.RequestInfo, error) {
|
func (h *handler) getRequestInfo(req *http.Request) (*models.RequestInfo, error) {
|
||||||
// Request isn't for API or isn't versioned.
|
// Request isn't for API or isn't versioned.
|
||||||
if !strings.HasPrefix(r.URL.Path, "/api/v") {
|
if !strings.HasPrefix(req.URL.Path, "/api/v") {
|
||||||
return nil, errInvalidPath
|
return nil, errInvalidPath
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: first element will always be empty!
|
// Note: first element will always be empty!
|
||||||
pathSplitted := strings.Split(r.URL.Path, "/")
|
pathSplitted := strings.Split(req.URL.Path, "/")
|
||||||
|
|
||||||
// Request is for API but not enough items in URL was passed.
|
// Request is for API but not enough items in URL was passed.
|
||||||
if len(pathSplitted) < 4 {
|
if len(pathSplitted) < 4 {
|
||||||
@ -119,20 +119,20 @@ func (h *handler) register(appName string, hndl common.HTTPHandlerFunc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ServeHTTP handles every HTTP request.
|
// ServeHTTP handles every HTTP request.
|
||||||
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *handler) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
requestDuration := time.Since(startTime)
|
requestDuration := time.Since(startTime)
|
||||||
|
|
||||||
log.Printf("[HTTP Request] from %s to %s, duration %.4fs\n", r.RemoteAddr, r.URL.Path, requestDuration.Seconds())
|
log.Printf("[HTTP Request] from %s to %s, duration %.4fs\n", req.RemoteAddr, req.URL.Path, requestDuration.Seconds())
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Validate request and extract needed info.
|
// Validate request and extract needed info.
|
||||||
rInfo, err := h.getRequestInfo(r)
|
rInfo, err := h.getRequestInfo(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
writer.WriteHeader(http.StatusBadRequest)
|
||||||
_, _ = w.Write([]byte("400 bad request - " + err.Error()))
|
_, _ = writer.Write([]byte("400 bad request - " + err.Error()))
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -144,14 +144,14 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
case "apps_list":
|
case "apps_list":
|
||||||
appsList, err := h.getAppsList()
|
appsList, err := h.getAppsList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
writer.WriteHeader(http.StatusBadRequest)
|
||||||
_, _ = w.Write([]byte("400 bad request - " + err.Error()))
|
_, _ = writer.Write([]byte("400 bad request - " + err.Error()))
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
writer.WriteHeader(http.StatusOK)
|
||||||
_, _ = w.Write(appsList)
|
_, _ = writer.Write(appsList)
|
||||||
|
|
||||||
return
|
return
|
||||||
case "info":
|
case "info":
|
||||||
@ -169,15 +169,15 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
infoBytes, _ := json.Marshal(infoData)
|
infoBytes, _ := json.Marshal(infoData)
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
writer.WriteHeader(http.StatusOK)
|
||||||
_, _ = w.Write(infoBytes)
|
_, _ = writer.Write(infoBytes)
|
||||||
|
|
||||||
return
|
return
|
||||||
case "metrics":
|
case "metrics":
|
||||||
handler, found := h.handlers[rInfo.Application]
|
handler, found := h.handlers[rInfo.Application]
|
||||||
if !found {
|
if !found {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
writer.WriteHeader(http.StatusBadRequest)
|
||||||
_, _ = w.Write([]byte("400 bad request - " + errInvalidApplication.Error()))
|
_, _ = writer.Write([]byte("400 bad request - " + errInvalidApplication.Error()))
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -185,16 +185,16 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
// Get data from handler.
|
// Get data from handler.
|
||||||
data := handler(rInfo)
|
data := handler(rInfo)
|
||||||
if data == "" {
|
if data == "" {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
writer.WriteHeader(http.StatusBadRequest)
|
||||||
_, _ = w.Write([]byte("400 bad request - " + errNoData.Error()))
|
_, _ = writer.Write([]byte("400 bad request - " + errNoData.Error()))
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
writer.WriteHeader(http.StatusOK)
|
||||||
_, _ = w.Write([]byte(data))
|
_, _ = writer.Write([]byte(data))
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
writer.WriteHeader(http.StatusBadRequest)
|
||||||
_, _ = w.Write([]byte("400 bad request - " + errInvalidPath.Error()))
|
_, _ = writer.Write([]byte("400 bad request - " + errInvalidPath.Error()))
|
||||||
}
|
}
|
||||||
|
@ -25,15 +25,16 @@ type HTTPServer struct {
|
|||||||
// NewHTTPServer creates HTTP server and executes preliminary initialization
|
// NewHTTPServer creates HTTP server and executes preliminary initialization
|
||||||
// (HTTP server structure initialized but it doesn't start).
|
// (HTTP server structure initialized but it doesn't start).
|
||||||
func NewHTTPServer(ctx context.Context, cfg *configuration.Config, logger *logger.Logger) (*HTTPServer, chan struct{}) {
|
func NewHTTPServer(ctx context.Context, cfg *configuration.Config, logger *logger.Logger) (*HTTPServer, chan struct{}) {
|
||||||
h := &HTTPServer{
|
// nolint:exhaustivestruct
|
||||||
|
httpServer := &HTTPServer{
|
||||||
config: cfg,
|
config: cfg,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
doneChan: make(chan struct{}),
|
doneChan: make(chan struct{}),
|
||||||
logger: logger,
|
logger: logger,
|
||||||
}
|
}
|
||||||
h.initialize()
|
httpServer.initialize()
|
||||||
|
|
||||||
return h, h.doneChan
|
return httpServer, httpServer.doneChan
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns request's context based on main context of application.
|
// Returns request's context based on main context of application.
|
||||||
|
@ -14,25 +14,27 @@ var ErrMetricNotFound = errors.New("metric not found")
|
|||||||
|
|
||||||
// Storage is an in-memory storage.
|
// Storage is an in-memory storage.
|
||||||
type Storage struct {
|
type Storage struct {
|
||||||
|
dataMutex sync.RWMutex
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
doneChan chan struct{}
|
doneChan chan struct{}
|
||||||
logger *logger.Logger
|
logger *logger.Logger
|
||||||
data map[string]schema.Metric
|
data map[string]schema.Metric
|
||||||
name string
|
name string
|
||||||
dataMutex sync.RWMutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStorage creates new in-memory storage to use.
|
// NewStorage creates new in-memory storage to use.
|
||||||
func NewStorage(ctx context.Context, name string, logger *logger.Logger) (*Storage, chan struct{}) {
|
func NewStorage(ctx context.Context, name string, logger *logger.Logger) (*Storage, chan struct{}) {
|
||||||
s := &Storage{
|
// nolint:exhaustivestruct
|
||||||
|
storage := &Storage{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
doneChan: make(chan struct{}),
|
doneChan: make(chan struct{}),
|
||||||
logger: logger,
|
logger: logger,
|
||||||
name: name,
|
name: name,
|
||||||
|
data: make(map[string]schema.Metric),
|
||||||
}
|
}
|
||||||
s.initialize()
|
storage.initialize()
|
||||||
|
|
||||||
return s, s.doneChan
|
return storage, storage.doneChan
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns data from storage by key.
|
// Get returns data from storage by key.
|
||||||
|
@ -23,13 +23,14 @@ type Client struct {
|
|||||||
|
|
||||||
// NewClient creates new Metricator client.
|
// NewClient creates new Metricator client.
|
||||||
func NewClient(config *Config, logger *logger.Logger) *Client {
|
func NewClient(config *Config, logger *logger.Logger) *Client {
|
||||||
c := &Client{
|
// nolint:exhaustivestruct
|
||||||
|
client := &Client{
|
||||||
config: config,
|
config: config,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
}
|
}
|
||||||
c.initialize()
|
client.initialize()
|
||||||
|
|
||||||
return c
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
// Executes request and parses it's contents.
|
// Executes request and parses it's contents.
|
||||||
|
@ -18,7 +18,7 @@ type Metric struct {
|
|||||||
|
|
||||||
// NewMetric creates new structure for storing single metric data.
|
// NewMetric creates new structure for storing single metric data.
|
||||||
func NewMetric(name, mType, description string, params []string) Metric {
|
func NewMetric(name, mType, description string, params []string) Metric {
|
||||||
m := Metric{
|
metric := Metric{
|
||||||
BaseName: name,
|
BaseName: name,
|
||||||
Name: name,
|
Name: name,
|
||||||
Description: description,
|
Description: description,
|
||||||
@ -27,7 +27,7 @@ func NewMetric(name, mType, description string, params []string) Metric {
|
|||||||
Value: "",
|
Value: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
return m
|
return metric
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetValue returns metric's value.
|
// GetValue returns metric's value.
|
||||||
|
Loading…
Reference in New Issue
Block a user