2020-01-24 18:10:03 +00:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
"github.com/valyala/fastjson/fastfloat"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Rows contains parsed Prometheus rows.
|
|
|
|
type Rows struct {
|
|
|
|
Rows []Row
|
|
|
|
|
|
|
|
tagsPool []Tag
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset resets rs.
|
|
|
|
func (rs *Rows) Reset() {
|
|
|
|
// Reset items, so they can be GC'ed
|
|
|
|
|
|
|
|
for i := range rs.Rows {
|
|
|
|
rs.Rows[i].reset()
|
|
|
|
}
|
|
|
|
rs.Rows = rs.Rows[:0]
|
|
|
|
|
|
|
|
for i := range rs.tagsPool {
|
|
|
|
rs.tagsPool[i].reset()
|
|
|
|
}
|
|
|
|
rs.tagsPool = rs.tagsPool[:0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals Prometheus exposition text rows from s.
|
|
|
|
//
|
|
|
|
// See https://github.com/prometheus/docs/blob/master/content/docs/instrumenting/exposition_formats.md#text-format-details
|
|
|
|
//
|
2020-08-11 14:04:39 +00:00
|
|
|
// s shouldn't be modified while rs is in use.
|
2020-01-24 18:10:03 +00:00
|
|
|
func (rs *Rows) Unmarshal(s string) {
|
2020-02-23 11:35:47 +00:00
|
|
|
rs.UnmarshalWithErrLogger(s, stdErrLogger)
|
|
|
|
}
|
|
|
|
|
|
|
|
func stdErrLogger(s string) {
|
|
|
|
logger.ErrorfSkipframes(1, "%s", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalWithErrLogger unmarshal Prometheus exposition text rows from s.
|
|
|
|
//
|
|
|
|
// It calls errLogger for logging parsing errors.
|
2020-08-11 14:04:39 +00:00
|
|
|
//
|
|
|
|
// s shouldn't be modified while rs is in use.
|
2020-02-23 11:35:47 +00:00
|
|
|
func (rs *Rows) UnmarshalWithErrLogger(s string, errLogger func(s string)) {
|
2020-01-24 18:10:03 +00:00
|
|
|
noEscapes := strings.IndexByte(s, '\\') < 0
|
2020-02-23 11:35:47 +00:00
|
|
|
rs.Rows, rs.tagsPool = unmarshalRows(rs.Rows[:0], s, rs.tagsPool[:0], noEscapes, errLogger)
|
2020-01-24 18:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Row is a single Prometheus row.
|
|
|
|
type Row struct {
|
|
|
|
Metric string
|
|
|
|
Tags []Tag
|
|
|
|
Value float64
|
|
|
|
Timestamp int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Row) reset() {
|
|
|
|
r.Metric = ""
|
|
|
|
r.Tags = nil
|
|
|
|
r.Value = 0
|
|
|
|
r.Timestamp = 0
|
|
|
|
}
|
|
|
|
|
2020-11-24 10:26:17 +00:00
|
|
|
func skipTrailingComment(s string) string {
|
|
|
|
n := strings.IndexByte(s, '#')
|
|
|
|
if n < 0 {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return s[:n]
|
|
|
|
}
|
|
|
|
|
2020-01-24 18:10:03 +00:00
|
|
|
func skipLeadingWhitespace(s string) string {
|
|
|
|
// Prometheus treats ' ' and '\t' as whitespace
|
|
|
|
// according to https://github.com/prometheus/docs/blob/master/content/docs/instrumenting/exposition_formats.md#text-format-details
|
|
|
|
for len(s) > 0 && (s[0] == ' ' || s[0] == '\t') {
|
|
|
|
s = s[1:]
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func skipTrailingWhitespace(s string) string {
|
|
|
|
// Prometheus treats ' ' and '\t' as whitespace
|
|
|
|
// according to https://github.com/prometheus/docs/blob/master/content/docs/instrumenting/exposition_formats.md#text-format-details
|
|
|
|
for len(s) > 0 && (s[len(s)-1] == ' ' || s[len(s)-1] == '\t') {
|
|
|
|
s = s[:len(s)-1]
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func nextWhitespace(s string) int {
|
|
|
|
n := strings.IndexByte(s, ' ')
|
|
|
|
if n < 0 {
|
|
|
|
return strings.IndexByte(s, '\t')
|
|
|
|
}
|
|
|
|
n1 := strings.IndexByte(s, '\t')
|
|
|
|
if n1 < 0 || n1 > n {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
return n1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Row) unmarshal(s string, tagsPool []Tag, noEscapes bool) ([]Tag, error) {
|
|
|
|
r.reset()
|
|
|
|
s = skipLeadingWhitespace(s)
|
|
|
|
n := strings.IndexByte(s, '{')
|
|
|
|
if n >= 0 {
|
|
|
|
// Tags found. Parse them.
|
|
|
|
r.Metric = skipTrailingWhitespace(s[:n])
|
|
|
|
s = s[n+1:]
|
|
|
|
tagsStart := len(tagsPool)
|
|
|
|
var err error
|
|
|
|
s, tagsPool, err = unmarshalTags(tagsPool, s, noEscapes)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return tagsPool, fmt.Errorf("cannot unmarshal tags: %w", err)
|
2020-01-24 18:10:03 +00:00
|
|
|
}
|
|
|
|
if len(s) > 0 && s[0] == ' ' {
|
|
|
|
// Fast path - skip whitespace.
|
|
|
|
s = s[1:]
|
|
|
|
}
|
|
|
|
tags := tagsPool[tagsStart:]
|
|
|
|
r.Tags = tags[:len(tags):len(tags)]
|
|
|
|
} else {
|
|
|
|
// Tags weren't found. Search for value after whitespace
|
|
|
|
n = nextWhitespace(s)
|
|
|
|
if n < 0 {
|
|
|
|
return tagsPool, fmt.Errorf("missing value")
|
|
|
|
}
|
|
|
|
r.Metric = s[:n]
|
|
|
|
s = s[n+1:]
|
|
|
|
}
|
|
|
|
if len(r.Metric) == 0 {
|
|
|
|
return tagsPool, fmt.Errorf("metric cannot be empty")
|
|
|
|
}
|
|
|
|
s = skipLeadingWhitespace(s)
|
2020-11-24 10:26:17 +00:00
|
|
|
s = skipTrailingComment(s)
|
2020-01-24 18:10:03 +00:00
|
|
|
if len(s) == 0 {
|
|
|
|
return tagsPool, fmt.Errorf("value cannot be empty")
|
|
|
|
}
|
|
|
|
n = nextWhitespace(s)
|
|
|
|
if n < 0 {
|
|
|
|
// There is no timestamp.
|
2020-09-15 23:03:35 +00:00
|
|
|
v, err := fastfloat.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return tagsPool, fmt.Errorf("cannot parse value %q: %w", s, err)
|
|
|
|
}
|
|
|
|
r.Value = v
|
2020-01-24 18:10:03 +00:00
|
|
|
return tagsPool, nil
|
|
|
|
}
|
2020-11-24 17:00:55 +00:00
|
|
|
// There is a timestamp.
|
2020-09-15 23:03:35 +00:00
|
|
|
v, err := fastfloat.Parse(s[:n])
|
|
|
|
if err != nil {
|
|
|
|
return tagsPool, fmt.Errorf("cannot parse value %q: %w", s[:n], err)
|
|
|
|
}
|
2020-11-24 10:26:17 +00:00
|
|
|
r.Value = v
|
2020-01-24 18:10:03 +00:00
|
|
|
s = skipLeadingWhitespace(s[n+1:])
|
2020-11-24 10:26:17 +00:00
|
|
|
if len(s) == 0 {
|
|
|
|
// There is no timestamp - just a whitespace after the value.
|
|
|
|
return tagsPool, nil
|
|
|
|
}
|
2020-12-16 21:36:20 +00:00
|
|
|
// There are some whitespaces after timestamp
|
|
|
|
s = skipTrailingWhitespace(s)
|
2020-11-27 12:53:27 +00:00
|
|
|
ts, err := fastfloat.Parse(s)
|
2020-09-15 23:03:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return tagsPool, fmt.Errorf("cannot parse timestamp %q: %w", s, err)
|
|
|
|
}
|
2020-11-27 12:53:27 +00:00
|
|
|
if ts >= -1<<31 && ts < 1<<31 {
|
|
|
|
// This looks like OpenMetrics timestamp in Unix seconds.
|
|
|
|
// Convert it to milliseconds.
|
|
|
|
//
|
|
|
|
// See https://github.com/OpenObservability/OpenMetrics/blob/master/specification/OpenMetrics.md#timestamps
|
|
|
|
ts *= 1000
|
|
|
|
}
|
|
|
|
r.Timestamp = int64(ts)
|
2020-01-24 18:10:03 +00:00
|
|
|
return tagsPool, nil
|
|
|
|
}
|
|
|
|
|
2020-07-14 09:02:25 +00:00
|
|
|
var rowsReadScrape = metrics.NewCounter(`vm_protoparser_rows_read_total{type="promscrape"}`)
|
|
|
|
|
2020-02-23 11:35:47 +00:00
|
|
|
func unmarshalRows(dst []Row, s string, tagsPool []Tag, noEscapes bool, errLogger func(s string)) ([]Row, []Tag) {
|
2020-07-14 09:15:25 +00:00
|
|
|
dstLen := len(dst)
|
2020-01-24 18:10:03 +00:00
|
|
|
for len(s) > 0 {
|
|
|
|
n := strings.IndexByte(s, '\n')
|
|
|
|
if n < 0 {
|
|
|
|
// The last line.
|
2020-07-14 09:02:25 +00:00
|
|
|
dst, tagsPool = unmarshalRow(dst, s, tagsPool, noEscapes, errLogger)
|
|
|
|
break
|
2020-01-24 18:10:03 +00:00
|
|
|
}
|
2020-07-14 09:15:25 +00:00
|
|
|
dst, tagsPool = unmarshalRow(dst, s[:n], tagsPool, noEscapes, errLogger)
|
|
|
|
s = s[n+1:]
|
2020-01-24 18:10:03 +00:00
|
|
|
}
|
2020-07-14 09:15:25 +00:00
|
|
|
rowsReadScrape.Add(len(dst) - dstLen)
|
2020-01-24 18:10:03 +00:00
|
|
|
return dst, tagsPool
|
|
|
|
}
|
|
|
|
|
2020-02-23 11:35:47 +00:00
|
|
|
func unmarshalRow(dst []Row, s string, tagsPool []Tag, noEscapes bool, errLogger func(s string)) ([]Row, []Tag) {
|
2020-01-24 18:10:03 +00:00
|
|
|
if len(s) > 0 && s[len(s)-1] == '\r' {
|
|
|
|
s = s[:len(s)-1]
|
|
|
|
}
|
|
|
|
s = skipLeadingWhitespace(s)
|
|
|
|
if len(s) == 0 {
|
|
|
|
// Skip empty line
|
|
|
|
return dst, tagsPool
|
|
|
|
}
|
|
|
|
if s[0] == '#' {
|
|
|
|
// Skip comment
|
|
|
|
return dst, tagsPool
|
|
|
|
}
|
|
|
|
if cap(dst) > len(dst) {
|
|
|
|
dst = dst[:len(dst)+1]
|
|
|
|
} else {
|
|
|
|
dst = append(dst, Row{})
|
|
|
|
}
|
|
|
|
r := &dst[len(dst)-1]
|
|
|
|
var err error
|
|
|
|
tagsPool, err = r.unmarshal(s, tagsPool, noEscapes)
|
|
|
|
if err != nil {
|
|
|
|
dst = dst[:len(dst)-1]
|
2020-02-23 11:35:47 +00:00
|
|
|
msg := fmt.Sprintf("cannot unmarshal Prometheus line %q: %s", s, err)
|
|
|
|
errLogger(msg)
|
2020-01-24 18:10:03 +00:00
|
|
|
invalidLines.Inc()
|
|
|
|
}
|
|
|
|
return dst, tagsPool
|
|
|
|
}
|
|
|
|
|
|
|
|
var invalidLines = metrics.NewCounter(`vm_rows_invalid_total{type="prometheus"}`)
|
|
|
|
|
|
|
|
func unmarshalTags(dst []Tag, s string, noEscapes bool) (string, []Tag, error) {
|
|
|
|
for {
|
2020-03-02 20:21:50 +00:00
|
|
|
s = skipLeadingWhitespace(s)
|
|
|
|
if len(s) > 0 && s[0] == '}' {
|
|
|
|
// End of tags found.
|
|
|
|
return s[1:], dst, nil
|
|
|
|
}
|
2020-01-24 18:10:03 +00:00
|
|
|
n := strings.IndexByte(s, '=')
|
|
|
|
if n < 0 {
|
|
|
|
return s, dst, fmt.Errorf("missing value for tag %q", s)
|
|
|
|
}
|
|
|
|
key := skipTrailingWhitespace(s[:n])
|
|
|
|
s = skipLeadingWhitespace(s[n+1:])
|
|
|
|
if len(s) == 0 || s[0] != '"' {
|
|
|
|
return s, dst, fmt.Errorf("expecting quoted value for tag %q; got %q", key, s)
|
|
|
|
}
|
|
|
|
value := s[1:]
|
|
|
|
if noEscapes {
|
|
|
|
// Fast path - the line has no escape chars
|
|
|
|
n = strings.IndexByte(value, '"')
|
|
|
|
if n < 0 {
|
|
|
|
return s, dst, fmt.Errorf("missing closing quote for tag value %q", s)
|
|
|
|
}
|
|
|
|
s = value[n+1:]
|
|
|
|
value = value[:n]
|
|
|
|
} else {
|
|
|
|
// Slow path - the line contains escape chars
|
|
|
|
n = findClosingQuote(s)
|
|
|
|
if n < 0 {
|
|
|
|
return s, dst, fmt.Errorf("missing closing quote for tag value %q", s)
|
|
|
|
}
|
2021-03-02 11:20:22 +00:00
|
|
|
value = unescapeValue(s[1:n])
|
2020-01-24 18:10:03 +00:00
|
|
|
s = s[n+1:]
|
|
|
|
}
|
2020-05-03 13:51:03 +00:00
|
|
|
if len(key) > 0 {
|
2020-05-03 13:41:33 +00:00
|
|
|
// Allow empty values (len(value)==0) - see https://github.com/VictoriaMetrics/VictoriaMetrics/issues/453
|
2020-01-24 18:10:03 +00:00
|
|
|
if cap(dst) > len(dst) {
|
|
|
|
dst = dst[:len(dst)+1]
|
|
|
|
} else {
|
|
|
|
dst = append(dst, Tag{})
|
|
|
|
}
|
|
|
|
tag := &dst[len(dst)-1]
|
|
|
|
tag.Key = key
|
|
|
|
tag.Value = value
|
|
|
|
}
|
|
|
|
s = skipLeadingWhitespace(s)
|
|
|
|
if len(s) > 0 && s[0] == '}' {
|
|
|
|
// End of tags found.
|
|
|
|
return s[1:], dst, nil
|
|
|
|
}
|
|
|
|
if len(s) == 0 || s[0] != ',' {
|
|
|
|
return s, dst, fmt.Errorf("missing comma after tag %s=%q", key, value)
|
|
|
|
}
|
2020-03-02 20:21:50 +00:00
|
|
|
s = s[1:]
|
2020-01-24 18:10:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tag is a Prometheus tag.
|
|
|
|
type Tag struct {
|
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tag) reset() {
|
|
|
|
t.Key = ""
|
|
|
|
t.Value = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func findClosingQuote(s string) int {
|
|
|
|
if len(s) == 0 || s[0] != '"' {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
off := 1
|
|
|
|
s = s[1:]
|
|
|
|
for {
|
|
|
|
n := strings.IndexByte(s, '"')
|
|
|
|
if n < 0 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if prevBackslashesCount(s[:n])%2 == 0 {
|
|
|
|
return off + n
|
|
|
|
}
|
|
|
|
off += n + 1
|
|
|
|
s = s[n+1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 11:20:22 +00:00
|
|
|
func unescapeValue(s string) string {
|
2020-01-24 18:10:03 +00:00
|
|
|
n := strings.IndexByte(s, '\\')
|
|
|
|
if n < 0 {
|
|
|
|
// Fast path - nothing to unescape
|
2021-03-02 11:20:22 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
b := make([]byte, 0, len(s))
|
|
|
|
for {
|
|
|
|
b = append(b, s[:n]...)
|
|
|
|
s = s[n+1:]
|
|
|
|
if len(s) == 0 {
|
|
|
|
b = append(b, '\\')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// label_value can be any sequence of UTF-8 characters, but the backslash (\), double-quote ("),
|
|
|
|
// and line feed (\n) characters have to be escaped as \\, \", and \n, respectively.
|
|
|
|
// See https://github.com/prometheus/docs/blob/master/content/docs/instrumenting/exposition_formats.md
|
|
|
|
switch s[0] {
|
|
|
|
case '\\':
|
|
|
|
b = append(b, '\\')
|
|
|
|
case '"':
|
|
|
|
b = append(b, '"')
|
|
|
|
case 'n':
|
|
|
|
b = append(b, '\n')
|
|
|
|
default:
|
|
|
|
b = append(b, '\\', s[0])
|
|
|
|
}
|
|
|
|
s = s[1:]
|
|
|
|
n = strings.IndexByte(s, '\\')
|
|
|
|
if n < 0 {
|
|
|
|
b = append(b, s...)
|
|
|
|
break
|
|
|
|
}
|
2020-01-24 18:10:03 +00:00
|
|
|
}
|
2021-03-02 11:20:22 +00:00
|
|
|
return string(b)
|
2020-01-24 18:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func prevBackslashesCount(s string) int {
|
|
|
|
n := 0
|
|
|
|
for len(s) > 0 && s[len(s)-1] == '\\' {
|
|
|
|
n++
|
|
|
|
s = s[:len(s)-1]
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|