Fix logger config and parser bugs; use scanner instead of reading request body at once

This commit is contained in:
Andrey Shcherbinin 2021-06-04 08:50:12 +00:00 committed by Stanislav N. aka pztrn
parent fb34d0d53e
commit a252681b26
4 changed files with 26 additions and 18 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
._bin ._bin
.vscode .vscode
metricator.yaml metricator.yaml
.idea

View File

@ -1,7 +1,6 @@
package application package application
import ( import (
"io/ioutil"
"net/http" "net/http"
"time" "time"
) )
@ -44,15 +43,13 @@ func (a *Application) fetch() {
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) data, err := a.parse(resp.Body)
if err != nil { if err != nil {
a.logger.Infoln("Failed to read response body for", a.name, "metrics:", err.Error()) a.logger.Infoln("Failed to parse response body for", a.name, "metrics:", err.Error())
return return
} }
data := a.parse(string(body))
a.storage.Put(data) a.storage.Put(data)
a.fetchIsRunningMutex.Lock() a.fetchIsRunningMutex.Lock()

View File

@ -1,19 +1,27 @@
package application package application
import ( import (
"bufio"
"fmt"
"io"
"strings" "strings"
"go.dev.pztrn.name/metricator/pkg/schema" "go.dev.pztrn.name/metricator/pkg/schema"
) )
// Parses passed body and returns a map suitable for pushing into storage. // Parses io.Reader passed and returns a map suitable for pushing into storage.
func (a *Application) parse(body string) map[string]schema.Metric { func (a *Application) parse(r io.Reader) (map[string]schema.Metric, error) {
data := make(map[string]schema.Metric) data := make(map[string]schema.Metric)
// ToDo: switch to bytes buffer and maybe do not read body in caller? scanner := bufio.NewScanner(r)
splittedBody := strings.Split(body, "\n") for scanner.Scan() {
line := scanner.Text()
// Skip empty lines.
if line == "" {
continue
}
for _, line := range splittedBody {
// Prometheus line contains metric name and metric parameters defined // Prometheus line contains metric name and metric parameters defined
// in "{}". // in "{}".
var ( var (
@ -21,11 +29,6 @@ func (a *Application) parse(body string) map[string]schema.Metric {
params []string params []string
) )
// Skip empty lines.
if line == "" {
continue
}
a.logger.Debugln("Analyzing line:", line) a.logger.Debugln("Analyzing line:", line)
name = a.getMetricName(line) name = a.getMetricName(line)
@ -80,19 +83,22 @@ func (a *Application) parse(body string) map[string]schema.Metric {
newMetric.Params = params newMetric.Params = params
metric = newMetric metric = newMetric
data[metric.Name] = metric
} }
metric.Value = a.getMetricValue(line) metric.Value = a.getMetricValue(line)
a.logger.Debugf("Got metric: %+v\n", metric) a.logger.Debugf("Got metric: %+v\n", metric)
data[name] = metric data[metric.Name] = metric
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("wasn't able to scan input: %w", err)
} }
a.logger.Debugf("Data parsed: %+v\n", data) a.logger.Debugf("Data parsed: %+v\n", data)
return data return data, nil
} }
// Gets metric description from passed line. // Gets metric description from passed line.

View File

@ -9,6 +9,10 @@ type Logger struct {
// NewLogger creates new logging wrapper and returns it to caller. // NewLogger creates new logging wrapper and returns it to caller.
func NewLogger(config *Config) *Logger { func NewLogger(config *Config) *Logger {
if config == nil {
config = &Config{Debug: false}
}
l := &Logger{config: config} l := &Logger{config: config}
return l return l