2017-04-19 12:43:09 +00:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-01-14 15:39:04 +00:00
|
|
|
package textparse
|
|
|
|
|
|
|
|
import (
|
2017-01-15 12:55:53 +00:00
|
|
|
"io"
|
2017-01-14 15:39:04 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-01-15 12:55:53 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2024-10-07 11:17:44 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-22 09:00:08 +00:00
|
|
|
|
2021-11-08 14:23:17 +00:00
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
2017-01-14 15:39:04 +00:00
|
|
|
)
|
|
|
|
|
2018-10-04 13:52:03 +00:00
|
|
|
func TestPromParse(t *testing.T) {
|
2017-01-14 18:30:19 +00:00
|
|
|
input := `# HELP go_gc_duration_seconds A summary of the GC invocation durations.
|
2018-05-14 20:19:53 +00:00
|
|
|
# TYPE go_gc_duration_seconds summary
|
2017-01-14 18:30:19 +00:00
|
|
|
go_gc_duration_seconds{quantile="0"} 4.9351e-05
|
2017-05-22 09:15:40 +00:00
|
|
|
go_gc_duration_seconds{quantile="0.25",} 7.424100000000001e-05
|
2017-01-14 18:30:19 +00:00
|
|
|
go_gc_duration_seconds{quantile="0.5",a="b"} 8.3835e-05
|
2017-06-22 07:38:55 +00:00
|
|
|
go_gc_duration_seconds{quantile="0.8", a="b"} 8.3835e-05
|
|
|
|
go_gc_duration_seconds{ quantile="0.9", a="b"} 8.3835e-05
|
2024-10-15 14:23:27 +00:00
|
|
|
# HELP prometheus_http_request_duration_seconds Histogram of latencies for HTTP requests.
|
|
|
|
# TYPE prometheus_http_request_duration_seconds histogram
|
|
|
|
prometheus_http_request_duration_seconds_bucket{handler="/",le="1"} 423
|
|
|
|
prometheus_http_request_duration_seconds_bucket{handler="/",le="2"} 1423
|
|
|
|
prometheus_http_request_duration_seconds_bucket{handler="/",le="+Inf"} 1423
|
|
|
|
prometheus_http_request_duration_seconds_sum{handler="/"} 2000
|
|
|
|
prometheus_http_request_duration_seconds_count{handler="/"} 1423
|
2018-05-14 20:19:53 +00:00
|
|
|
# Hrandom comment starting with prefix of HELP
|
|
|
|
#
|
2019-03-16 10:10:07 +00:00
|
|
|
wind_speed{A="2",c="3"} 12345
|
2018-05-14 20:19:53 +00:00
|
|
|
# comment with escaped \n newline
|
|
|
|
# comment with escaped \ escape character
|
2018-08-01 12:29:58 +00:00
|
|
|
# HELP nohelp1
|
|
|
|
# HELP nohelp2
|
2017-06-22 07:38:55 +00:00
|
|
|
go_gc_duration_seconds{ quantile="1.0", a="b" } 8.3835e-05
|
|
|
|
go_gc_duration_seconds { quantile="1.0", a="b" } 8.3835e-05
|
2018-05-14 20:19:53 +00:00
|
|
|
go_gc_duration_seconds { quantile= "1.0", a= "b", } 8.3835e-05
|
2017-06-22 07:38:55 +00:00
|
|
|
go_gc_duration_seconds { quantile = "1.0", a = "b" } 8.3835e-05
|
2024-02-15 19:25:12 +00:00
|
|
|
go_gc_duration_seconds { quantile = "2.0" a = "b" } 8.3835e-05
|
2017-01-14 18:30:19 +00:00
|
|
|
go_gc_duration_seconds_count 99
|
2017-05-09 10:21:19 +00:00
|
|
|
some:aggregate:rate5m{a_b="c"} 1
|
2017-01-14 18:30:19 +00:00
|
|
|
# HELP go_goroutines Number of goroutines that currently exist.
|
|
|
|
# TYPE go_goroutines gauge
|
2017-07-18 10:35:41 +00:00
|
|
|
go_goroutines 33 123123
|
|
|
|
_metric_starting_with_underscore 1
|
2017-07-27 13:15:41 +00:00
|
|
|
testmetric{_label_starting_with_underscore="foo"} 1
|
2024-10-15 14:23:27 +00:00
|
|
|
testmetric{label="\"bar\""} 1
|
|
|
|
testmetric{le="10"} 1`
|
2018-06-01 08:22:32 +00:00
|
|
|
input += "\n# HELP metric foo\x00bar"
|
2017-05-24 12:52:46 +00:00
|
|
|
input += "\nnull_byte_metric{a=\"abc\x00\"} 1"
|
2017-04-27 15:02:07 +00:00
|
|
|
|
2024-10-07 11:17:44 +00:00
|
|
|
exp := []parsedEntry{
|
2017-01-14 18:30:19 +00:00
|
|
|
{
|
2018-05-14 20:19:53 +00:00
|
|
|
m: "go_gc_duration_seconds",
|
|
|
|
help: "A summary of the GC invocation durations.",
|
|
|
|
}, {
|
|
|
|
m: "go_gc_duration_seconds",
|
2023-11-22 14:39:21 +00:00
|
|
|
typ: model.MetricTypeSummary,
|
2018-05-14 20:19:53 +00:00
|
|
|
}, {
|
2017-01-14 18:30:19 +00:00
|
|
|
m: `go_gc_duration_seconds{quantile="0"}`,
|
|
|
|
v: 4.9351e-05,
|
2024-10-15 14:23:27 +00:00
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "0.0"),
|
2017-01-14 18:30:19 +00:00
|
|
|
}, {
|
2017-05-22 09:15:40 +00:00
|
|
|
m: `go_gc_duration_seconds{quantile="0.25",}`,
|
2017-01-14 18:30:19 +00:00
|
|
|
v: 7.424100000000001e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "0.25"),
|
|
|
|
}, {
|
|
|
|
m: `go_gc_duration_seconds{quantile="0.5",a="b"}`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "0.5", "a", "b"),
|
2017-06-22 07:38:55 +00:00
|
|
|
}, {
|
|
|
|
m: `go_gc_duration_seconds{quantile="0.8", a="b"}`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "0.8", "a", "b"),
|
|
|
|
}, {
|
|
|
|
m: `go_gc_duration_seconds{ quantile="0.9", a="b"}`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "0.9", "a", "b"),
|
2024-10-15 14:23:27 +00:00
|
|
|
}, {
|
|
|
|
m: "prometheus_http_request_duration_seconds",
|
|
|
|
help: "Histogram of latencies for HTTP requests.",
|
|
|
|
}, {
|
|
|
|
m: "prometheus_http_request_duration_seconds",
|
|
|
|
typ: model.MetricTypeHistogram,
|
|
|
|
}, {
|
|
|
|
m: `prometheus_http_request_duration_seconds_bucket{handler="/",le="1"}`,
|
|
|
|
v: 423,
|
|
|
|
lset: labels.FromStrings("__name__", "prometheus_http_request_duration_seconds_bucket", "handler", "/", "le", "1.0"),
|
|
|
|
}, {
|
|
|
|
m: `prometheus_http_request_duration_seconds_bucket{handler="/",le="2"}`,
|
|
|
|
v: 1423,
|
|
|
|
lset: labels.FromStrings("__name__", "prometheus_http_request_duration_seconds_bucket", "handler", "/", "le", "2.0"),
|
|
|
|
}, {
|
|
|
|
m: `prometheus_http_request_duration_seconds_bucket{handler="/",le="+Inf"}`,
|
|
|
|
v: 1423,
|
|
|
|
lset: labels.FromStrings("__name__", "prometheus_http_request_duration_seconds_bucket", "handler", "/", "le", "+Inf"),
|
|
|
|
}, {
|
|
|
|
m: `prometheus_http_request_duration_seconds_sum{handler="/"}`,
|
|
|
|
v: 2000,
|
|
|
|
lset: labels.FromStrings("__name__", "prometheus_http_request_duration_seconds_sum", "handler", "/"),
|
|
|
|
}, {
|
|
|
|
m: `prometheus_http_request_duration_seconds_count{handler="/"}`,
|
|
|
|
v: 1423,
|
|
|
|
lset: labels.FromStrings("__name__", "prometheus_http_request_duration_seconds_count", "handler", "/"),
|
2018-05-14 20:19:53 +00:00
|
|
|
}, {
|
|
|
|
comment: "# Hrandom comment starting with prefix of HELP",
|
|
|
|
}, {
|
|
|
|
comment: "#",
|
2019-03-16 10:10:07 +00:00
|
|
|
}, {
|
|
|
|
m: `wind_speed{A="2",c="3"}`,
|
|
|
|
v: 12345,
|
|
|
|
lset: labels.FromStrings("A", "2", "__name__", "wind_speed", "c", "3"),
|
2018-05-14 20:19:53 +00:00
|
|
|
}, {
|
|
|
|
comment: "# comment with escaped \\n newline",
|
|
|
|
}, {
|
|
|
|
comment: "# comment with escaped \\ escape character",
|
2018-08-01 12:29:58 +00:00
|
|
|
}, {
|
|
|
|
m: "nohelp1",
|
|
|
|
help: "",
|
|
|
|
}, {
|
|
|
|
m: "nohelp2",
|
|
|
|
help: "",
|
2017-06-22 07:38:55 +00:00
|
|
|
}, {
|
|
|
|
m: `go_gc_duration_seconds{ quantile="1.0", a="b" }`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "1.0", "a", "b"),
|
|
|
|
}, {
|
|
|
|
m: `go_gc_duration_seconds { quantile="1.0", a="b" }`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "1.0", "a", "b"),
|
|
|
|
}, {
|
2018-05-14 20:19:53 +00:00
|
|
|
m: `go_gc_duration_seconds { quantile= "1.0", a= "b", }`,
|
2017-06-22 07:38:55 +00:00
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "1.0", "a", "b"),
|
|
|
|
}, {
|
|
|
|
m: `go_gc_duration_seconds { quantile = "1.0", a = "b" }`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "1.0", "a", "b"),
|
2024-02-15 19:25:12 +00:00
|
|
|
}, {
|
2024-09-10 20:32:03 +00:00
|
|
|
// NOTE: Unlike OpenMetrics, PromParser allows spaces between label terms. This appears to be unintended and should probably be fixed.
|
2024-02-15 19:25:12 +00:00
|
|
|
m: `go_gc_duration_seconds { quantile = "2.0" a = "b" }`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds", "quantile", "2.0", "a", "b"),
|
2017-01-14 18:30:19 +00:00
|
|
|
}, {
|
|
|
|
m: `go_gc_duration_seconds_count`,
|
|
|
|
v: 99,
|
|
|
|
lset: labels.FromStrings("__name__", "go_gc_duration_seconds_count"),
|
2017-05-09 10:21:19 +00:00
|
|
|
}, {
|
|
|
|
m: `some:aggregate:rate5m{a_b="c"}`,
|
|
|
|
v: 1,
|
|
|
|
lset: labels.FromStrings("__name__", "some:aggregate:rate5m", "a_b", "c"),
|
2018-05-14 20:19:53 +00:00
|
|
|
}, {
|
|
|
|
m: "go_goroutines",
|
|
|
|
help: "Number of goroutines that currently exist.",
|
|
|
|
}, {
|
|
|
|
m: "go_goroutines",
|
2023-11-22 14:39:21 +00:00
|
|
|
typ: model.MetricTypeGauge,
|
2017-01-14 18:30:19 +00:00
|
|
|
}, {
|
|
|
|
m: `go_goroutines`,
|
|
|
|
v: 33,
|
2017-04-27 15:02:07 +00:00
|
|
|
t: int64p(123123),
|
2017-01-14 18:30:19 +00:00
|
|
|
lset: labels.FromStrings("__name__", "go_goroutines"),
|
2017-07-18 10:35:41 +00:00
|
|
|
}, {
|
|
|
|
m: "_metric_starting_with_underscore",
|
|
|
|
v: 1,
|
|
|
|
lset: labels.FromStrings("__name__", "_metric_starting_with_underscore"),
|
|
|
|
}, {
|
|
|
|
m: "testmetric{_label_starting_with_underscore=\"foo\"}",
|
|
|
|
v: 1,
|
|
|
|
lset: labels.FromStrings("__name__", "testmetric", "_label_starting_with_underscore", "foo"),
|
2017-07-27 13:15:41 +00:00
|
|
|
}, {
|
|
|
|
m: "testmetric{label=\"\\\"bar\\\"\"}",
|
|
|
|
v: 1,
|
|
|
|
lset: labels.FromStrings("__name__", "testmetric", "label", `"bar"`),
|
2024-10-15 14:23:27 +00:00
|
|
|
}, {
|
|
|
|
m: `testmetric{le="10"}`,
|
|
|
|
v: 1,
|
|
|
|
lset: labels.FromStrings("__name__", "testmetric", "le", "10"),
|
2018-06-01 08:22:32 +00:00
|
|
|
}, {
|
|
|
|
m: "metric",
|
|
|
|
help: "foo\x00bar",
|
2017-05-24 12:52:46 +00:00
|
|
|
}, {
|
|
|
|
m: "null_byte_metric{a=\"abc\x00\"}",
|
|
|
|
v: 1,
|
|
|
|
lset: labels.FromStrings("__name__", "null_byte_metric", "a", "abc\x00"),
|
2017-01-14 18:30:19 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-03-25 13:31:24 +00:00
|
|
|
p := NewPromParser([]byte(input), labels.NewSymbolTable())
|
2024-10-07 11:17:44 +00:00
|
|
|
got := testParse(t, p)
|
|
|
|
requireEntries(t, exp, got)
|
2017-01-14 18:30:19 +00:00
|
|
|
}
|
|
|
|
|
2024-02-15 19:25:12 +00:00
|
|
|
func TestUTF8PromParse(t *testing.T) {
|
|
|
|
oldValidationScheme := model.NameValidationScheme
|
|
|
|
model.NameValidationScheme = model.UTF8Validation
|
|
|
|
defer func() {
|
|
|
|
model.NameValidationScheme = oldValidationScheme
|
|
|
|
}()
|
|
|
|
|
|
|
|
input := `# HELP "go.gc_duration_seconds" A summary of the GC invocation durations.
|
|
|
|
# TYPE "go.gc_duration_seconds" summary
|
|
|
|
{"go.gc_duration_seconds",quantile="0"} 4.9351e-05
|
|
|
|
{"go.gc_duration_seconds",quantile="0.25",} 7.424100000000001e-05
|
|
|
|
{"go.gc_duration_seconds",quantile="0.5",a="b"} 8.3835e-05
|
|
|
|
{"go.gc_duration_seconds",quantile="0.8", a="b"} 8.3835e-05
|
|
|
|
{"go.gc_duration_seconds", quantile="0.9", a="b"} 8.3835e-05
|
|
|
|
{"go.gc_duration_seconds", quantile="1.0", a="b" } 8.3835e-05
|
|
|
|
{ "go.gc_duration_seconds", quantile="1.0", a="b" } 8.3835e-05
|
|
|
|
{ "go.gc_duration_seconds", quantile= "1.0", a= "b", } 8.3835e-05
|
|
|
|
{ "go.gc_duration_seconds", quantile = "1.0", a = "b" } 8.3835e-05
|
|
|
|
{"go.gc_duration_seconds_count"} 99
|
|
|
|
{"Heizölrückstoßabdämpfung 10€ metric with \"interesting\" {character\nchoices}","strange©™\n'quoted' \"name\""="6"} 10.0`
|
|
|
|
|
2024-10-07 11:17:44 +00:00
|
|
|
exp := []parsedEntry{
|
2024-02-15 19:25:12 +00:00
|
|
|
{
|
|
|
|
m: "go.gc_duration_seconds",
|
|
|
|
help: "A summary of the GC invocation durations.",
|
|
|
|
}, {
|
|
|
|
m: "go.gc_duration_seconds",
|
|
|
|
typ: model.MetricTypeSummary,
|
|
|
|
}, {
|
|
|
|
m: `{"go.gc_duration_seconds",quantile="0"}`,
|
|
|
|
v: 4.9351e-05,
|
2024-10-15 14:23:27 +00:00
|
|
|
lset: labels.FromStrings("__name__", "go.gc_duration_seconds", "quantile", "0.0"),
|
2024-02-15 19:25:12 +00:00
|
|
|
}, {
|
|
|
|
m: `{"go.gc_duration_seconds",quantile="0.25",}`,
|
|
|
|
v: 7.424100000000001e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go.gc_duration_seconds", "quantile", "0.25"),
|
|
|
|
}, {
|
|
|
|
m: `{"go.gc_duration_seconds",quantile="0.5",a="b"}`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go.gc_duration_seconds", "quantile", "0.5", "a", "b"),
|
|
|
|
}, {
|
|
|
|
m: `{"go.gc_duration_seconds",quantile="0.8", a="b"}`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go.gc_duration_seconds", "quantile", "0.8", "a", "b"),
|
|
|
|
}, {
|
|
|
|
m: `{"go.gc_duration_seconds", quantile="0.9", a="b"}`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go.gc_duration_seconds", "quantile", "0.9", "a", "b"),
|
|
|
|
}, {
|
|
|
|
m: `{"go.gc_duration_seconds", quantile="1.0", a="b" }`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go.gc_duration_seconds", "quantile", "1.0", "a", "b"),
|
|
|
|
}, {
|
|
|
|
m: `{ "go.gc_duration_seconds", quantile="1.0", a="b" }`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go.gc_duration_seconds", "quantile", "1.0", "a", "b"),
|
|
|
|
}, {
|
|
|
|
m: `{ "go.gc_duration_seconds", quantile= "1.0", a= "b", }`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go.gc_duration_seconds", "quantile", "1.0", "a", "b"),
|
|
|
|
}, {
|
|
|
|
m: `{ "go.gc_duration_seconds", quantile = "1.0", a = "b" }`,
|
|
|
|
v: 8.3835e-05,
|
|
|
|
lset: labels.FromStrings("__name__", "go.gc_duration_seconds", "quantile", "1.0", "a", "b"),
|
|
|
|
}, {
|
|
|
|
m: `{"go.gc_duration_seconds_count"}`,
|
|
|
|
v: 99,
|
|
|
|
lset: labels.FromStrings("__name__", "go.gc_duration_seconds_count"),
|
|
|
|
}, {
|
|
|
|
m: `{"Heizölrückstoßabdämpfung 10€ metric with \"interesting\" {character\nchoices}","strange©™\n'quoted' \"name\""="6"}`,
|
|
|
|
v: 10.0,
|
|
|
|
lset: labels.FromStrings("__name__", `Heizölrückstoßabdämpfung 10€ metric with "interesting" {character
|
|
|
|
choices}`, "strange©™\n'quoted' \"name\"", "6"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-03-25 13:31:24 +00:00
|
|
|
p := NewPromParser([]byte(input), labels.NewSymbolTable())
|
2024-10-07 11:17:44 +00:00
|
|
|
got := testParse(t, p)
|
|
|
|
requireEntries(t, exp, got)
|
2024-02-15 19:25:12 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 13:52:03 +00:00
|
|
|
func TestPromParseErrors(t *testing.T) {
|
2017-06-16 12:09:50 +00:00
|
|
|
cases := []struct {
|
|
|
|
input string
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: "a",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "expected value after metric, got \"\\n\" (\"INVALID\") while parsing: \"a\\n\"",
|
2017-06-16 12:09:50 +00:00
|
|
|
},
|
2017-06-19 15:46:50 +00:00
|
|
|
{
|
|
|
|
input: "a{b='c'} 1\n",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "expected label value, got \"'\" (\"INVALID\") while parsing: \"a{b='\"",
|
2017-06-19 15:46:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a{b=\n",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "expected label value, got \"\\n\" (\"INVALID\") while parsing: \"a{b=\\n\"",
|
2017-06-19 15:46:50 +00:00
|
|
|
},
|
2017-06-16 12:09:50 +00:00
|
|
|
{
|
|
|
|
input: "a{\xff=\"foo\"} 1\n",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "expected label name, got \"\\xff\" (\"INVALID\") while parsing: \"a{\\xff\"",
|
2017-06-16 12:09:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a{b=\"\xff\"} 1\n",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "invalid UTF-8 label value: \"\\\"\\xff\\\"\"",
|
2017-06-16 12:09:50 +00:00
|
|
|
},
|
2024-02-15 19:25:12 +00:00
|
|
|
{
|
|
|
|
input: `{"a", "b = "c"}`,
|
|
|
|
err: "expected equal, got \"c\\\"\" (\"LNAME\") while parsing: \"{\\\"a\\\", \\\"b = \\\"c\\\"\"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `{"a",b\nc="d"} 1`,
|
|
|
|
err: "expected equal, got \"\\\\\" (\"INVALID\") while parsing: \"{\\\"a\\\",b\\\\\"",
|
|
|
|
},
|
2017-06-16 12:09:50 +00:00
|
|
|
{
|
|
|
|
input: "a true\n",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "strconv.ParseFloat: parsing \"true\": invalid syntax while parsing: \"a true\"",
|
2017-06-16 12:09:50 +00:00
|
|
|
},
|
2017-07-07 08:29:38 +00:00
|
|
|
{
|
|
|
|
input: "something_weird{problem=\"",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "expected label value, got \"\\\"\\n\" (\"INVALID\") while parsing: \"something_weird{problem=\\\"\\n\"",
|
2017-07-07 08:29:38 +00:00
|
|
|
},
|
2018-02-04 16:37:46 +00:00
|
|
|
{
|
|
|
|
input: "empty_label_name{=\"\"} 0",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "expected label name, got \"=\\\"\" (\"EQUAL\") while parsing: \"empty_label_name{=\\\"\"",
|
2018-02-04 16:37:46 +00:00
|
|
|
},
|
2019-09-05 14:18:18 +00:00
|
|
|
{
|
|
|
|
input: "foo 1_2\n",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "unsupported character in float while parsing: \"foo 1_2\"",
|
2019-09-05 14:18:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "foo 0x1p-3\n",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "unsupported character in float while parsing: \"foo 0x1p-3\"",
|
2019-09-05 14:18:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "foo 0x1P-3\n",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "unsupported character in float while parsing: \"foo 0x1P-3\"",
|
2019-09-05 14:18:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "foo 0 1_2\n",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "expected next entry after timestamp, got \"_\" (\"INVALID\") while parsing: \"foo 0 1_\"",
|
2019-09-05 14:18:18 +00:00
|
|
|
},
|
2020-03-02 07:18:05 +00:00
|
|
|
{
|
|
|
|
input: `{a="ok"} 1`,
|
2024-02-15 19:25:12 +00:00
|
|
|
err: "metric name not set while parsing: \"{a=\\\"ok\\\"} 1\"",
|
2020-03-02 07:18:05 +00:00
|
|
|
},
|
2022-03-08 13:04:11 +00:00
|
|
|
{
|
|
|
|
input: "# TYPE #\n#EOF\n",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "expected metric name after TYPE, got \"#\" (\"INVALID\") while parsing: \"# TYPE #\"",
|
2022-03-08 13:04:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "# HELP #\n#EOF\n",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "expected metric name after HELP, got \"#\" (\"INVALID\") while parsing: \"# HELP #\"",
|
2022-03-08 13:04:11 +00:00
|
|
|
},
|
2017-06-16 12:09:50 +00:00
|
|
|
}
|
|
|
|
|
2018-05-14 20:19:53 +00:00
|
|
|
for i, c := range cases {
|
2023-03-25 13:31:24 +00:00
|
|
|
p := NewPromParser([]byte(c.input), labels.NewSymbolTable())
|
2018-05-14 20:19:53 +00:00
|
|
|
var err error
|
|
|
|
for err == nil {
|
|
|
|
_, err = p.Next()
|
2017-06-16 12:09:50 +00:00
|
|
|
}
|
2024-10-06 16:35:29 +00:00
|
|
|
require.EqualError(t, err, c.err, "test %d", i)
|
2017-06-16 12:09:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 13:52:03 +00:00
|
|
|
func TestPromNullByteHandling(t *testing.T) {
|
2017-07-07 08:29:38 +00:00
|
|
|
cases := []struct {
|
|
|
|
input string
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: "null_byte_metric{a=\"abc\x00\"} 1",
|
|
|
|
err: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a{b=\"\x00ss\"} 1\n",
|
|
|
|
err: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a{b=\"\x00\"} 1\n",
|
|
|
|
err: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a{b=\"\x00\"} 1\n",
|
|
|
|
err: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a{b=\x00\"ssss\"} 1\n",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "expected label value, got \"\\x00\" (\"INVALID\") while parsing: \"a{b=\\x00\"",
|
2017-07-07 08:29:38 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a{b=\"\x00",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "expected label value, got \"\\\"\\x00\\n\" (\"INVALID\") while parsing: \"a{b=\\\"\\x00\\n\"",
|
2017-07-07 08:29:38 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a{b\x00=\"hiih\"} 1",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "expected equal, got \"\\x00\" (\"INVALID\") while parsing: \"a{b\\x00\"",
|
2017-07-07 08:29:38 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a\x00{b=\"ddd\"} 1",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "expected value after metric, got \"\\x00\" (\"INVALID\") while parsing: \"a\\x00\"",
|
2022-12-07 09:46:14 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "a 0 1\x00",
|
2022-12-07 13:32:03 +00:00
|
|
|
err: "expected next entry after timestamp, got \"\\x00\" (\"INVALID\") while parsing: \"a 0 1\\x00\"",
|
2017-07-07 08:29:38 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:19:53 +00:00
|
|
|
for i, c := range cases {
|
2023-03-25 13:31:24 +00:00
|
|
|
p := NewPromParser([]byte(c.input), labels.NewSymbolTable())
|
2018-05-14 20:19:53 +00:00
|
|
|
var err error
|
|
|
|
for err == nil {
|
|
|
|
_, err = p.Next()
|
2017-07-07 08:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.err == "" {
|
2020-10-29 09:43:23 +00:00
|
|
|
require.Equal(t, io.EOF, err, "test %d", i)
|
2017-07-07 08:29:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-10-06 16:35:29 +00:00
|
|
|
require.EqualError(t, err, c.err, "test %d", i)
|
2017-07-07 08:29:38 +00:00
|
|
|
}
|
|
|
|
}
|