Little refactor for Prometheus metrics parser.

This commit is contained in:
Stanislav Nikitin 2020-11-29 05:49:45 +05:00
parent 5b1094c258
commit cb5fdd4d8e
Signed by: pztrn
GPG Key ID: 1E944A0F0568B550

View File

@ -39,11 +39,27 @@ func (a *Application) parse(body string) map[string]string {
} else { } else {
value = strings.Split(line, " ")[1] value = strings.Split(line, " ")[1]
name = strings.Split(line, "{")[0] 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] valuesString := strings.Split(strings.Split(line, "{")[1], "}")[0]
var ( var (
params []string
paramName, paramValue string paramName, paramValue string
paramNameFinished, paramValueStarted, paramValueFinished bool paramNameFinished, paramValueStarted, paramValueFinished bool
) )
@ -56,6 +72,10 @@ func (a *Application) parse(body string) map[string]string {
continue continue
} }
// Sometimes nestif causes questions, like here. Is code below is
// "deply nested"? I think not. So:
// nolint:nestif
if !paramNameFinished { if !paramNameFinished {
if string(r) != "=" { if string(r) != "=" {
paramName += string(r) paramName += string(r)
@ -91,15 +111,5 @@ func (a *Application) parse(body string) map[string]string {
params = append(params, paramName+":"+paramValue) params = append(params, paramName+":"+paramValue)
} }
for _, param := range params { return params
name += "/" + param
}
}
data[name] = value
}
log.Printf("Data parsed: %+v\n", data)
return data
} }