2020-11-29 05:44:21 +05:00
|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2020-12-23 13:27:17 +05:00
|
|
|
|
|
|
|
"go.dev.pztrn.name/metricator/internal/models"
|
2020-11-29 05:44:21 +05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Parses passed body and returns a map suitable for pushing into storage.
|
2020-12-23 13:27:17 +05:00
|
|
|
func (a *Application) parse(body string) map[string]models.Metric {
|
|
|
|
data := make(map[string]models.Metric)
|
2020-11-29 05:44:21 +05:00
|
|
|
|
|
|
|
// ToDo: switch to bytes buffer and maybe do not read body in caller?
|
|
|
|
splittedBody := strings.Split(body, "\n")
|
|
|
|
|
|
|
|
for _, line := range splittedBody {
|
|
|
|
// Prometheus line contains metric name and metric parameters defined
|
|
|
|
// in "{}".
|
|
|
|
var (
|
2020-12-23 15:40:48 +05:00
|
|
|
name string
|
|
|
|
params []string
|
2020-11-29 05:44:21 +05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Skip empty lines.
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:28:57 +05:00
|
|
|
a.logger.Debugln("Analyzing line:", line)
|
2020-12-23 15:40:48 +05:00
|
|
|
|
|
|
|
name = a.getMetricName(line)
|
2020-12-23 19:31:09 +05:00
|
|
|
|
2020-12-23 15:40:48 +05:00
|
|
|
metric, found := data[name]
|
|
|
|
if !found {
|
|
|
|
metric = models.NewMetric(name, "", "", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If line is commented - then we have something about metric's description
|
|
|
|
// or type. It should be handled in special way - these metric will became
|
|
|
|
// "pseudometric" which will be used as template for next iterations. For
|
|
|
|
// example if HELP line was parsed first, then TYPE line will be parsed and
|
|
|
|
// data will be added to already existing metric. If next line which should
|
|
|
|
// represent metric itself contains parameters (e.g. "{instance='host1'}")
|
|
|
|
// then next code block will COPY populated with HELP and TYPE pesudometric
|
|
|
|
// and put it's value as new metric with different name (metric/instance:host1
|
|
|
|
// in this example).
|
2020-11-29 05:44:21 +05:00
|
|
|
if strings.HasPrefix(line, "#") {
|
2020-12-23 15:40:48 +05:00
|
|
|
switch strings.Split(line, " ")[1] {
|
|
|
|
case "HELP":
|
|
|
|
metric.Description = a.getMetricDescription(line)
|
|
|
|
case "TYPE":
|
|
|
|
metric.Type = a.getMetricType(line)
|
|
|
|
}
|
|
|
|
|
|
|
|
data[name] = metric
|
|
|
|
|
2020-12-23 19:31:09 +05:00
|
|
|
// According to docs HELP and TYPE lines should be printed before actual metric. Do not even
|
2020-12-23 15:40:48 +05:00
|
|
|
// report bugs regarding that!
|
2020-12-23 19:31:09 +05:00
|
|
|
// Docs: https://github.com/Showmax/prometheus-docs/blob/master/content/docs/instrumenting/exposition_formats.md
|
2020-11-29 05:44:21 +05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-12-23 15:40:48 +05:00
|
|
|
// Parametrized metrics should have own piece of love - we should
|
|
|
|
// add parameters to metric's name. This would also require metrics
|
|
|
|
// structure copying.
|
|
|
|
if strings.Contains(line, "{") {
|
|
|
|
newMetric := metric
|
2020-11-29 05:44:21 +05:00
|
|
|
|
2020-11-29 05:49:45 +05:00
|
|
|
params = a.getParametersForPrometheusMetric(line)
|
|
|
|
for _, param := range params {
|
2020-12-23 15:40:48 +05:00
|
|
|
newMetric.Name += "/" + param
|
2020-11-29 05:49:45 +05:00
|
|
|
}
|
2020-12-23 15:40:48 +05:00
|
|
|
|
|
|
|
metric = newMetric
|
|
|
|
data[metric.Name] = metric
|
2020-11-29 05:49:45 +05:00
|
|
|
}
|
2020-11-29 05:44:21 +05:00
|
|
|
|
2020-12-23 15:40:48 +05:00
|
|
|
metric.Value = a.getMetricValue(line)
|
|
|
|
|
2020-12-23 19:31:09 +05:00
|
|
|
a.logger.Debugf("Got metric: %+v\n", metric)
|
2020-12-23 13:27:17 +05:00
|
|
|
|
|
|
|
data[name] = metric
|
2020-11-29 05:49:45 +05:00
|
|
|
}
|
2020-11-29 05:44:21 +05:00
|
|
|
|
2020-12-23 19:31:09 +05:00
|
|
|
a.logger.Debugf("Data parsed: %+v\n", data)
|
2020-11-29 05:44:21 +05:00
|
|
|
|
2020-11-29 05:49:45 +05:00
|
|
|
return data
|
|
|
|
}
|
2020-11-29 05:44:21 +05:00
|
|
|
|
2020-12-23 15:40:48 +05:00
|
|
|
// Gets metric description from passed line.
|
|
|
|
func (a *Application) getMetricDescription(line string) string {
|
|
|
|
return strings.Join(strings.Split(line, " ")[3:], " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets metric name from passed line.
|
|
|
|
func (a *Application) getMetricName(line string) string {
|
|
|
|
var metricNameData string
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, "#") {
|
|
|
|
metricNameData = strings.Split(line, " ")[2]
|
|
|
|
} else {
|
|
|
|
metricNameData = strings.Split(line, " ")[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Split(metricNameData, "{")[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets metric type from passed line.
|
|
|
|
func (a *Application) getMetricType(line string) string {
|
|
|
|
return strings.Split(line, " ")[3]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets metric value from passed line.
|
|
|
|
func (a *Application) getMetricValue(line string) string {
|
|
|
|
if strings.Contains(line, "}") {
|
|
|
|
return strings.Split(line, "} ")[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Split(line, " ")[1]
|
|
|
|
}
|
|
|
|
|
2020-11-29 05:49:45 +05:00
|
|
|
// 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]
|
2020-11-29 05:44:21 +05:00
|
|
|
|
2020-11-29 05:49:45 +05:00
|
|
|
var (
|
|
|
|
params []string
|
|
|
|
paramName, paramValue string
|
|
|
|
paramNameFinished, paramValueStarted, paramValueFinished bool
|
|
|
|
)
|
2020-11-29 05:44:21 +05:00
|
|
|
|
2020-11-29 05:49:45 +05:00
|
|
|
for _, r := range valuesString {
|
|
|
|
if paramValueFinished && string(r) == "," {
|
|
|
|
params = append(params, paramName+":"+paramValue)
|
|
|
|
paramName, paramValue = "", ""
|
|
|
|
paramNameFinished, paramValueStarted, paramValueFinished = false, false, false
|
2020-11-29 05:44:21 +05:00
|
|
|
|
2020-11-29 05:49:45 +05:00
|
|
|
continue
|
|
|
|
}
|
2020-11-29 05:44:21 +05:00
|
|
|
|
2020-11-29 05:49:45 +05:00
|
|
|
// Sometimes nestif causes questions, like here. Is code below is
|
2020-12-23 19:31:09 +05:00
|
|
|
// "deeply nested"? I think not. So:
|
2020-11-29 05:49:45 +05:00
|
|
|
// nolint:nestif
|
|
|
|
if !paramNameFinished {
|
|
|
|
if string(r) != "=" {
|
|
|
|
paramName += string(r)
|
2020-11-29 05:44:21 +05:00
|
|
|
|
2020-11-29 05:49:45 +05:00
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
paramNameFinished = true
|
2020-11-29 05:44:21 +05:00
|
|
|
|
2020-11-29 05:49:45 +05:00
|
|
|
continue
|
2020-11-29 05:44:21 +05:00
|
|
|
}
|
2020-11-29 05:49:45 +05:00
|
|
|
} else {
|
|
|
|
if string(r) == "\"" && !paramValueStarted {
|
|
|
|
paramValueStarted = true
|
2020-11-29 05:44:21 +05:00
|
|
|
|
2020-11-29 05:49:45 +05:00
|
|
|
continue
|
2020-11-29 05:44:21 +05:00
|
|
|
}
|
|
|
|
|
2020-11-29 05:49:45 +05:00
|
|
|
if paramValueStarted && string(r) != "\"" {
|
|
|
|
paramValue += string(r)
|
|
|
|
|
|
|
|
continue
|
2020-11-29 05:44:21 +05:00
|
|
|
}
|
|
|
|
|
2020-11-29 05:49:45 +05:00
|
|
|
if paramValueStarted && string(r) == "\"" {
|
|
|
|
paramValueFinished = true
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2020-11-29 05:44:21 +05:00
|
|
|
}
|
|
|
|
|
2020-11-29 05:49:45 +05:00
|
|
|
if paramName != "" && paramValue != "" {
|
|
|
|
params = append(params, paramName+":"+paramValue)
|
|
|
|
}
|
2020-11-29 05:44:21 +05:00
|
|
|
|
2020-11-29 05:49:45 +05:00
|
|
|
return params
|
2020-11-29 05:44:21 +05:00
|
|
|
}
|