Little refactor for Prometheus metrics parser.
This commit is contained in:
parent
5b1094c258
commit
cb5fdd4d8e
@ -39,11 +39,27 @@ func (a *Application) parse(body string) map[string]string {
|
||||
} else {
|
||||
value = strings.Split(line, " ")[1]
|
||||
name = strings.Split(line, "{")[0]
|
||||
params = a.getParametersForPrometheusMetric(line)
|
||||
|
||||
// Parse params into "name:value" string.
|
||||
for _, param := range params {
|
||||
name += "/" + param
|
||||
}
|
||||
}
|
||||
|
||||
data[name] = value
|
||||
}
|
||||
|
||||
log.Printf("Data parsed: %+v\n", data)
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
// Parses passed line and returns a slice of strings with parameters parsed.
|
||||
func (a *Application) getParametersForPrometheusMetric(line string) []string {
|
||||
valuesString := strings.Split(strings.Split(line, "{")[1], "}")[0]
|
||||
|
||||
var (
|
||||
params []string
|
||||
paramName, paramValue string
|
||||
paramNameFinished, paramValueStarted, paramValueFinished bool
|
||||
)
|
||||
@ -56,6 +72,10 @@ func (a *Application) parse(body string) map[string]string {
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
// Sometimes nestif causes questions, like here. Is code below is
|
||||
// "deply nested"? I think not. So:
|
||||
// nolint:nestif
|
||||
if !paramNameFinished {
|
||||
if string(r) != "=" {
|
||||
paramName += string(r)
|
||||
@ -91,15 +111,5 @@ func (a *Application) parse(body string) map[string]string {
|
||||
params = append(params, paramName+":"+paramValue)
|
||||
}
|
||||
|
||||
for _, param := range params {
|
||||
name += "/" + param
|
||||
}
|
||||
}
|
||||
|
||||
data[name] = value
|
||||
}
|
||||
|
||||
log.Printf("Data parsed: %+v\n", data)
|
||||
|
||||
return data
|
||||
return params
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user