2020-11-29 05:44:21 +05:00
|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
2021-06-04 13:50:12 +05:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-11-29 05:44:21 +05:00
|
|
|
"strings"
|
2020-12-23 13:27:17 +05:00
|
|
|
|
2020-12-24 23:06:13 +05:00
|
|
|
"go.dev.pztrn.name/metricator/pkg/schema"
|
2020-11-29 05:44:21 +05:00
|
|
|
)
|
|
|
|
|
2021-06-04 13:50:12 +05:00
|
|
|
// Parses io.Reader passed and returns a map suitable for pushing into storage.
|
|
|
|
func (a *Application) parse(r io.Reader) (map[string]schema.Metric, error) {
|
2020-12-24 23:06:13 +05:00
|
|
|
data := make(map[string]schema.Metric)
|
2020-11-29 05:44:21 +05:00
|
|
|
|
2021-06-04 13:50:12 +05:00
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
|
|
|
|
// Skip empty lines.
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
2020-11-29 05:44:21 +05:00
|
|
|
|
|
|
|
// 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
|
|
|
)
|
|
|
|
|
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 22:12:25 +05:00
|
|
|
a.logger.Debugln("Got metric name:", name)
|
2020-12-23 19:31:09 +05:00
|
|
|
|
2020-12-23 15:40:48 +05:00
|
|
|
metric, found := data[name]
|
|
|
|
if !found {
|
2020-12-23 22:12:25 +05:00
|
|
|
a.logger.Debugln("Metric wasn't yet created, creating new structure")
|
|
|
|
|
2020-12-24 23:06:13 +05:00
|
|
|
metric = schema.NewMetric(name, "", "", nil)
|
2020-12-23 15:40:48 +05:00
|
|
|
}
|
|
|
|
|
2020-12-23 22:12:25 +05:00
|
|
|
a.logger.Debugf("Got metric to use: %+v\n", metric)
|
|
|
|
|
2020-12-23 15:40:48 +05:00
|
|
|
// 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-12-23 22:12:25 +05:00
|
|
|
newMetric.Name = newMetric.BaseName
|
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
|
|
|
|
2020-12-23 22:12:25 +05:00
|
|
|
newMetric.Params = params
|
|
|
|
|
2020-12-23 15:40:48 +05:00
|
|
|
metric = newMetric
|
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
|
|
|
|
2021-06-04 13:50:12 +05:00
|
|
|
data[metric.Name] = metric
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return nil, fmt.Errorf("wasn't able to scan input: %w", err)
|
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
|
|
|
|
2021-06-04 13:50:12 +05:00
|
|
|
return data, nil
|
2020-11-29 05:49:45 +05:00
|
|
|
}
|
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
|
|
|
|
2021-11-21 14:45:09 +05:00
|
|
|
for _, runeChar := range valuesString {
|
|
|
|
if paramValueFinished && string(runeChar) == "," {
|
2020-11-29 05:49:45 +05:00
|
|
|
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 {
|
2021-11-21 14:45:09 +05:00
|
|
|
if string(runeChar) != "=" {
|
|
|
|
paramName += string(runeChar)
|
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 {
|
2021-11-21 14:45:09 +05:00
|
|
|
if string(runeChar) == "\"" && !paramValueStarted {
|
2020-11-29 05:49:45 +05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-21 14:45:09 +05:00
|
|
|
if paramValueStarted && string(runeChar) != "\"" {
|
|
|
|
paramValue += string(runeChar)
|
2020-11-29 05:49:45 +05:00
|
|
|
|
|
|
|
continue
|
2020-11-29 05:44:21 +05:00
|
|
|
}
|
|
|
|
|
2021-11-21 14:45:09 +05:00
|
|
|
if paramValueStarted && string(runeChar) == "\"" {
|
2020-11-29 05:49:45 +05:00
|
|
|
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
|
|
|
}
|