From 9e88381085051447e061ddec1c2f5fa961b5d87e Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Fri, 5 Jan 2024 17:23:38 +0100 Subject: [PATCH] chore(parsers.prometheus): Migrate tests to test-cases (#14527) --- metric.go | 3 + metric/metric.go | 4 + plugins/inputs/prometheus/prometheus_test.go | 1 - plugins/parsers/prometheus/common.go | 4 +- plugins/parsers/prometheus/parser_test.go | 936 +++--------------- plugins/parsers/prometheus/parser_v1.go | 2 +- .../testcases/benchmark/expected_v1.out | 2 + .../testcases/benchmark/expected_v2.out | 2 + .../prometheus/testcases/benchmark/input.txt | 7 + .../testcases/benchmark/telegraf.conf | 4 + .../testcases/default_tags/expected_v1.out | 1 + .../testcases/default_tags/expected_v2.out | 1 + .../testcases/default_tags/input.txt | 3 + .../testcases/default_tags/telegraf.conf | 7 + .../histogram_inf_bucket/expected_v1.out | 1 + .../histogram_inf_bucket/expected_v2.out | 9 + .../testcases/histogram_inf_bucket/input.bin | Bin 0 -> 267 bytes .../histogram_inf_bucket/telegraf.conf | 6 + .../ignore_timestamp/expected_v1.out | 1 + .../ignore_timestamp/expected_v2.out | 1 + .../testcases/ignore_timestamp/input.txt | 2 + .../testcases/ignore_timestamp/telegraf.conf | 4 + .../metric_with_timestamp/expected_v1.out | 1 + .../metric_with_timestamp/expected_v2.out | 1 + .../testcases/metric_with_timestamp/input.txt | 2 + .../metric_with_timestamp/telegraf.conf | 4 + .../testcases/protobuf/expected_v1.out | 6 + .../testcases/protobuf/expected_v2.out | 6 + .../prometheus/testcases/protobuf/input.bin | Bin 0 -> 414 bytes .../testcases/protobuf/telegraf.conf | 6 + .../testcases/valid_counter/expected_v1.out | 1 + .../testcases/valid_counter/expected_v2.out | 1 + .../testcases/valid_counter/input.txt | 3 + .../testcases/valid_counter/telegraf.conf | 4 + .../testcases/valid_gauge/expected_v1.out | 1 + .../testcases/valid_gauge/expected_v2.out | 1 + .../testcases/valid_gauge/input.txt | 3 + .../testcases/valid_gauge/telegraf.conf | 4 + .../testcases/valid_histogram/expected_v1.out | 1 + .../testcases/valid_histogram/expected_v2.out | 9 + .../testcases/valid_histogram/input.txt | 12 + .../testcases/valid_histogram/telegraf.conf | 4 + .../testcases/valid_summary/expected_v1.out | 1 + .../testcases/valid_summary/expected_v2.out | 4 + .../testcases/valid_summary/input.txt | 7 + .../testcases/valid_summary/telegraf.conf | 4 + testutil/plugin_input/plugin.go | 177 ++++ testutil/plugin_input/sample.conf | 15 + 48 files changed, 490 insertions(+), 789 deletions(-) create mode 100644 plugins/parsers/prometheus/testcases/benchmark/expected_v1.out create mode 100644 plugins/parsers/prometheus/testcases/benchmark/expected_v2.out create mode 100644 plugins/parsers/prometheus/testcases/benchmark/input.txt create mode 100644 plugins/parsers/prometheus/testcases/benchmark/telegraf.conf create mode 100644 plugins/parsers/prometheus/testcases/default_tags/expected_v1.out create mode 100644 plugins/parsers/prometheus/testcases/default_tags/expected_v2.out create mode 100644 plugins/parsers/prometheus/testcases/default_tags/input.txt create mode 100644 plugins/parsers/prometheus/testcases/default_tags/telegraf.conf create mode 100644 plugins/parsers/prometheus/testcases/histogram_inf_bucket/expected_v1.out create mode 100644 plugins/parsers/prometheus/testcases/histogram_inf_bucket/expected_v2.out create mode 100644 plugins/parsers/prometheus/testcases/histogram_inf_bucket/input.bin create mode 100644 plugins/parsers/prometheus/testcases/histogram_inf_bucket/telegraf.conf create mode 100644 plugins/parsers/prometheus/testcases/ignore_timestamp/expected_v1.out create mode 100644 plugins/parsers/prometheus/testcases/ignore_timestamp/expected_v2.out create mode 100644 plugins/parsers/prometheus/testcases/ignore_timestamp/input.txt create mode 100644 plugins/parsers/prometheus/testcases/ignore_timestamp/telegraf.conf create mode 100644 plugins/parsers/prometheus/testcases/metric_with_timestamp/expected_v1.out create mode 100644 plugins/parsers/prometheus/testcases/metric_with_timestamp/expected_v2.out create mode 100644 plugins/parsers/prometheus/testcases/metric_with_timestamp/input.txt create mode 100644 plugins/parsers/prometheus/testcases/metric_with_timestamp/telegraf.conf create mode 100644 plugins/parsers/prometheus/testcases/protobuf/expected_v1.out create mode 100644 plugins/parsers/prometheus/testcases/protobuf/expected_v2.out create mode 100644 plugins/parsers/prometheus/testcases/protobuf/input.bin create mode 100644 plugins/parsers/prometheus/testcases/protobuf/telegraf.conf create mode 100644 plugins/parsers/prometheus/testcases/valid_counter/expected_v1.out create mode 100644 plugins/parsers/prometheus/testcases/valid_counter/expected_v2.out create mode 100644 plugins/parsers/prometheus/testcases/valid_counter/input.txt create mode 100644 plugins/parsers/prometheus/testcases/valid_counter/telegraf.conf create mode 100644 plugins/parsers/prometheus/testcases/valid_gauge/expected_v1.out create mode 100644 plugins/parsers/prometheus/testcases/valid_gauge/expected_v2.out create mode 100644 plugins/parsers/prometheus/testcases/valid_gauge/input.txt create mode 100644 plugins/parsers/prometheus/testcases/valid_gauge/telegraf.conf create mode 100644 plugins/parsers/prometheus/testcases/valid_histogram/expected_v1.out create mode 100644 plugins/parsers/prometheus/testcases/valid_histogram/expected_v2.out create mode 100644 plugins/parsers/prometheus/testcases/valid_histogram/input.txt create mode 100644 plugins/parsers/prometheus/testcases/valid_histogram/telegraf.conf create mode 100644 plugins/parsers/prometheus/testcases/valid_summary/expected_v1.out create mode 100644 plugins/parsers/prometheus/testcases/valid_summary/expected_v2.out create mode 100644 plugins/parsers/prometheus/testcases/valid_summary/input.txt create mode 100644 plugins/parsers/prometheus/testcases/valid_summary/telegraf.conf create mode 100644 testutil/plugin_input/plugin.go create mode 100644 testutil/plugin_input/sample.conf diff --git a/metric.go b/metric.go index cc9235c4b..2cf3d6a06 100644 --- a/metric.go +++ b/metric.go @@ -106,6 +106,9 @@ type Metric interface { // SetTime sets the timestamp of the Metric. SetTime(t time.Time) + // SetType sets the value-type of the Metric. + SetType(t ValueType) + // HashID returns an unique identifier for the series. HashID() uint64 diff --git a/metric/metric.go b/metric/metric.go index de03edf9b..e2b18f52c 100644 --- a/metric/metric.go +++ b/metric/metric.go @@ -241,6 +241,10 @@ func (m *metric) SetTime(t time.Time) { m.tm = t } +func (m *metric) SetType(t telegraf.ValueType) { + m.tp = t +} + func (m *metric) Copy() telegraf.Metric { m2 := &metric{ name: m.name, diff --git a/plugins/inputs/prometheus/prometheus_test.go b/plugins/inputs/prometheus/prometheus_test.go index b28dfb1ea..1631f2692 100644 --- a/plugins/inputs/prometheus/prometheus_test.go +++ b/plugins/inputs/prometheus/prometheus_test.go @@ -80,7 +80,6 @@ func TestPrometheusGeneratesMetrics(t *testing.T) { func TestPrometheusCustomHeader(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Println(r.Header.Get("accept")) switch r.Header.Get("accept") { case "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited;q=0.7,text/plain;version=0.0.4;q=0.3": _, err := fmt.Fprintln(w, "proto 15 1490802540000") diff --git a/plugins/parsers/prometheus/common.go b/plugins/parsers/prometheus/common.go index 91b61da26..529e0c31b 100644 --- a/plugins/parsers/prometheus/common.go +++ b/plugins/parsers/prometheus/common.go @@ -28,7 +28,9 @@ func GetTagsFromLabels(m *dto.Metric, defaultTags map[string]string) map[string] } for _, label := range m.Label { - result[label.GetName()] = label.GetValue() + if v := label.GetValue(); v != "" { + result[label.GetName()] = v + } } return result diff --git a/plugins/parsers/prometheus/parser_test.go b/plugins/parsers/prometheus/parser_test.go index b3f5685dd..e8e7d4f49 100644 --- a/plugins/parsers/prometheus/parser_test.go +++ b/plugins/parsers/prometheus/parser_test.go @@ -1,812 +1,176 @@ package prometheus import ( - "encoding/json" - "fmt" - "io" "net/http" - "net/http/httptest" + "os" + "path/filepath" "testing" - "time" - dto "github.com/prometheus/client_model/go" + "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/require" - "github.com/influxdata/telegraf" - "github.com/influxdata/telegraf/metric" + "github.com/influxdata/telegraf/config" + "github.com/influxdata/telegraf/models" "github.com/influxdata/telegraf/testutil" + test "github.com/influxdata/telegraf/testutil/plugin_input" ) -const ( - validUniqueGauge = ` - # HELP cadvisor_version_info A metric with a constant '1' value labeled by kernel version, OS version, docker version, cadvisor version & cadvisor revision. - # TYPE cadvisor_version_info gauge - cadvisor_version_info{cadvisorRevision="",cadvisorVersion="",dockerVersion="1.8.2",` + - `kernelVersion="3.10.0-229.20.1.el7.x86_64",osVersion="CentOS Linux 7 (Core)"} 1 - ` +func TestCases(t *testing.T) { + // Get all directories in testcases + folders, err := os.ReadDir("testcases") + require.NoError(t, err) + // Make sure testdata contains data + require.NotEmpty(t, folders) - validUniqueCounter = `# HELP get_token_fail_count Counter of failed Token() requests to the alternate token source - # TYPE get_token_fail_count counter - get_token_fail_count 0 - ` + for _, f := range folders { + fname := f.Name() + testdataPath := filepath.Join("testcases", fname) + configFilename := filepath.Join(testdataPath, "telegraf.conf") - validUniqueSummary = `# HELP http_request_duration_microseconds The HTTP request latencies in microseconds. - # TYPE http_request_duration_microseconds summary - http_request_duration_microseconds{handler="prometheus",quantile="0.5"} 552048.506 - http_request_duration_microseconds{handler="prometheus",quantile="0.9"} 5.876804288e+06 - http_request_duration_microseconds{handler="prometheus",quantile="0.99"} 5.876804288e+06 - http_request_duration_microseconds_sum{handler="prometheus"} 1.8909097205e+07 - http_request_duration_microseconds_count{handler="prometheus"} 9 - ` + // Run tests as metric version 1 + t.Run(fname+"_v1", func(t *testing.T) { + // Load the configuration + cfg := config.NewConfig() + require.NoError(t, cfg.LoadConfig(configFilename)) + require.Len(t, cfg.Inputs, 1) - validUniqueHistogram = `# HELP apiserver_request_latencies Response latency distribution in microseconds for each verb, resource and client. - # TYPE apiserver_request_latencies histogram - apiserver_request_latencies_bucket{resource="bindings",verb="POST",le="125000"} 1994 - apiserver_request_latencies_bucket{resource="bindings",verb="POST",le="250000"} 1997 - apiserver_request_latencies_bucket{resource="bindings",verb="POST",le="500000"} 2000 - apiserver_request_latencies_bucket{resource="bindings",verb="POST",le="1e+06"} 2005 - apiserver_request_latencies_bucket{resource="bindings",verb="POST",le="2e+06"} 2012 - apiserver_request_latencies_bucket{resource="bindings",verb="POST",le="4e+06"} 2017 - apiserver_request_latencies_bucket{resource="bindings",verb="POST",le="8e+06"} 2024 - apiserver_request_latencies_bucket{resource="bindings",verb="POST",le="+Inf"} 2025 - apiserver_request_latencies_sum{resource="bindings",verb="POST"} 1.02726334e+08 - apiserver_request_latencies_count{resource="bindings",verb="POST"} 2025 - ` - validUniqueHistogramJSON = `{ - "name": "apiserver_request_latencies", - "help": "Response latency distribution in microseconds for each verb, resource and client.", - "type": "HISTOGRAM", - "metric": [ - { - "label": [ - {"name": "resource", "value": "bindings"}, - {"name": "verb", "value": "POST"} - ], - "histogram": { - "sample_count": 2025, - "sample_sum": 1.02726334e+08, - "bucket": [ - {"cumulative_count": 1994,"upper_bound": 125000}, - {"cumulative_count": 1997,"upper_bound": 250000}, - {"cumulative_count": 2000,"upper_bound": 500000}, - {"cumulative_count": 2005,"upper_bound": 1e+06}, - {"cumulative_count": 2012,"upper_bound": 2e+06}, - {"cumulative_count": 2017,"upper_bound": 4e+06}, - {"cumulative_count": 2024,"upper_bound": 8e+06} - ] + // Tune plugin + plugin := cfg.Inputs[0].Input.(*test.Plugin) + plugin.Path = testdataPath + plugin.UseTypeTag = "_type" + plugin.ExpectedFilename = "expected_v1.out" + + parser := plugin.Parser.(*models.RunningParser).Parser.(*Parser) + parser.MetricVersion = 1 + if raw, found := plugin.AdditionalParams["headers"]; found { + headers, ok := raw.(map[string]interface{}) + require.Truef(t, ok, "unknown header type %T", raw) + parser.Header = make(http.Header) + for k, rv := range headers { + v, ok := rv.(string) + require.Truef(t, ok, "unknown header value type %T for %q", raw, k) + parser.Header.Add(k, v) } } - ] - }` -) + require.NoError(t, plugin.Init()) -func TestParsingValidGauge(t *testing.T) { - expected := []telegraf.Metric{ - testutil.MustMetric( - "prometheus", - map[string]string{ - "osVersion": "CentOS Linux 7 (Core)", - "cadvisorRevision": "", - "cadvisorVersion": "", - "dockerVersion": "1.8.2", - "kernelVersion": "3.10.0-229.20.1.el7.x86_64", - }, - map[string]interface{}{ - "cadvisor_version_info": float64(1), - }, - time.Unix(0, 0), - telegraf.Gauge, - ), + // Gather data and check errors + var acc testutil.Accumulator + err := plugin.Gather(&acc) + switch len(plugin.ExpectedErrors) { + case 0: + require.NoError(t, err) + case 1: + require.ErrorContains(t, err, plugin.ExpectedErrors[0]) + default: + require.Contains(t, plugin.ExpectedErrors, err.Error()) + } + + // Determine checking options + options := []cmp.Option{ + testutil.SortMetrics(), + } + if plugin.ShouldIgnoreTimestamp || parser.IgnoreTimestamp { + options = append(options, testutil.IgnoreTime()) + } + + // Check the resulting metrics + actual := acc.GetTelegrafMetrics() + testutil.RequireMetricsEqual(t, plugin.Expected, actual, options...) + + // Special checks + if parser.IgnoreTimestamp { + t.Log("testing ignore-timestamp case") + for i, m := range actual { + expected := plugin.Expected[i] + require.Greaterf(t, m.Time(), expected.Time(), "metric time not after prometheus value in %d", i) + } + } + }) + + // Run tests as metric version 2 + t.Run(fname+"_v2", func(t *testing.T) { + // Load the configuration + cfg := config.NewConfig() + require.NoError(t, cfg.LoadConfig(configFilename)) + require.Len(t, cfg.Inputs, 1) + + // Tune plugin + plugin := cfg.Inputs[0].Input.(*test.Plugin) + plugin.Path = testdataPath + plugin.UseTypeTag = "_type" + plugin.ExpectedFilename = "expected_v2.out" + + parser := plugin.Parser.(*models.RunningParser).Parser.(*Parser) + parser.MetricVersion = 2 + if raw, found := plugin.AdditionalParams["headers"]; found { + headers, ok := raw.(map[string]interface{}) + require.Truef(t, ok, "unknown header type %T", raw) + parser.Header = make(http.Header) + for k, rv := range headers { + v, ok := rv.(string) + require.Truef(t, ok, "unknown header value type %T for %q", raw, k) + parser.Header.Add(k, v) + } + } + require.NoError(t, plugin.Init()) + + // Gather data and check errors + var acc testutil.Accumulator + err := plugin.Gather(&acc) + switch len(plugin.ExpectedErrors) { + case 0: + require.NoError(t, err) + case 1: + require.ErrorContains(t, err, plugin.ExpectedErrors[0]) + default: + require.Contains(t, plugin.ExpectedErrors, err.Error()) + } + + // Determine checking options + options := []cmp.Option{ + testutil.SortMetrics(), + } + if plugin.ShouldIgnoreTimestamp || parser.IgnoreTimestamp { + options = append(options, testutil.IgnoreTime()) + } + + // Check the resulting metrics + actual := acc.GetTelegrafMetrics() + testutil.RequireMetricsEqual(t, plugin.Expected, actual, options...) + + // Special checks + if parser.IgnoreTimestamp { + t.Log("testing ignore-timestamp case") + for i, m := range actual { + expected := plugin.Expected[i] + require.Greaterf(t, m.Time(), expected.Time(), "metric time not after prometheus value in %d", i) + } + } + }) } - - metrics, err := parse([]byte(validUniqueGauge)) - - require.NoError(t, err) - require.Len(t, metrics, 1) - testutil.RequireMetricsEqual(t, expected, metrics, testutil.IgnoreTime(), testutil.SortMetrics()) } -func TestParsingValidCounter(t *testing.T) { - expected := []telegraf.Metric{ - testutil.MustMetric( - "prometheus", - map[string]string{}, - map[string]interface{}{ - "get_token_fail_count": float64(0), - }, - time.Unix(0, 0), - telegraf.Counter, - ), - } - - metrics, err := parse([]byte(validUniqueCounter)) - - require.NoError(t, err) - require.Len(t, metrics, 1) - testutil.RequireMetricsEqual(t, expected, metrics, testutil.IgnoreTime(), testutil.SortMetrics()) -} - -func TestParsingValidSummary(t *testing.T) { - expected := []telegraf.Metric{ - testutil.MustMetric( - "prometheus", - map[string]string{ - "handler": "prometheus", - }, - map[string]interface{}{ - "http_request_duration_microseconds_sum": float64(1.8909097205e+07), - "http_request_duration_microseconds_count": float64(9.0), - }, - time.Unix(0, 0), - telegraf.Summary, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "handler": "prometheus", - "quantile": "0.5", - }, - map[string]interface{}{ - "http_request_duration_microseconds": float64(552048.506), - }, - time.Unix(0, 0), - telegraf.Summary, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "handler": "prometheus", - "quantile": "0.9", - }, - map[string]interface{}{ - "http_request_duration_microseconds": float64(5.876804288e+06), - }, - time.Unix(0, 0), - telegraf.Summary, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "handler": "prometheus", - "quantile": "0.99", - }, - map[string]interface{}{ - "http_request_duration_microseconds": float64(5.876804288e+6), - }, - time.Unix(0, 0), - telegraf.Summary, - ), - } - - metrics, err := parse([]byte(validUniqueSummary)) - - require.NoError(t, err) - require.Len(t, metrics, 4) - testutil.RequireMetricsEqual(t, expected, metrics, testutil.IgnoreTime(), testutil.SortMetrics()) -} - -func TestParsingValidHistogram(t *testing.T) { - expected := []telegraf.Metric{ - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - }, - map[string]interface{}{ - "apiserver_request_latencies_count": float64(2025.0), - "apiserver_request_latencies_sum": float64(1.02726334e+08), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - "le": "125000", - }, - map[string]interface{}{ - "apiserver_request_latencies_bucket": float64(1994.0), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - "le": "250000", - }, - map[string]interface{}{ - "apiserver_request_latencies_bucket": float64(1997.0), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - "le": "500000", - }, - map[string]interface{}{ - "apiserver_request_latencies_bucket": float64(2000.0), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - "le": "1e+06", - }, - map[string]interface{}{ - "apiserver_request_latencies_bucket": float64(2005.0), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - "le": "2e+06", - }, - map[string]interface{}{ - "apiserver_request_latencies_bucket": float64(2012.0), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - "le": "4e+06", - }, - map[string]interface{}{ - "apiserver_request_latencies_bucket": float64(2017.0), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - "le": "8e+06", - }, - map[string]interface{}{ - "apiserver_request_latencies_bucket": float64(2024.0), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - "le": "+Inf", - }, - map[string]interface{}{ - "apiserver_request_latencies_bucket": float64(2025.0), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - } - - metrics, err := parse([]byte(validUniqueHistogram)) - - require.NoError(t, err) - require.Len(t, metrics, 9) - testutil.RequireMetricsEqual(t, expected, metrics, testutil.IgnoreTime(), testutil.SortMetrics()) -} - -func TestDefautTags(t *testing.T) { - expected := []telegraf.Metric{ - testutil.MustMetric( - "prometheus", - map[string]string{ - "osVersion": "CentOS Linux 7 (Core)", - "cadvisorRevision": "", - "cadvisorVersion": "", - "dockerVersion": "1.8.2", - "kernelVersion": "3.10.0-229.20.1.el7.x86_64", - "defaultTag": "defaultTagValue", - }, - map[string]interface{}{ - "cadvisor_version_info": float64(1), - }, - time.Unix(0, 0), - telegraf.Gauge, - ), - } - - parser := Parser{ - DefaultTags: map[string]string{ - "defaultTag": "defaultTagValue", - "dockerVersion": "to_be_overridden", - }, - } - metrics, err := parser.Parse([]byte(validUniqueGauge)) - - require.NoError(t, err) - require.Len(t, metrics, 1) - testutil.RequireMetricsEqual(t, expected, metrics, testutil.IgnoreTime(), testutil.SortMetrics()) -} - -func TestMetricsWithTimestamp(t *testing.T) { - testTime := time.Date(2020, time.October, 4, 17, 0, 0, 0, time.UTC) - testTimeUnix := testTime.UnixNano() / int64(time.Millisecond) - metricsWithTimestamps := fmt.Sprintf(` -# TYPE test_counter counter -test_counter{label="test"} 1 %d -`, testTimeUnix) - expected := []telegraf.Metric{ - testutil.MustMetric( - "prometheus", - map[string]string{ - "label": "test", - }, - map[string]interface{}{ - "test_counter": float64(1.0), - }, - testTime, - telegraf.Counter, - ), - } - - metrics, _ := parse([]byte(metricsWithTimestamps)) - - testutil.RequireMetricsEqual(t, expected, metrics, testutil.SortMetrics()) -} - -func TestMetricsWithoutIgnoreTimestamp(t *testing.T) { - testTime := time.Date(2020, time.October, 4, 17, 0, 0, 0, time.UTC) - testTimeUnix := testTime.UnixNano() / int64(time.Millisecond) - metricsWithTimestamps := fmt.Sprintf(` -# TYPE test_counter counter -test_counter{label="test"} 1 %d -`, testTimeUnix) - expected := testutil.MustMetric( - "prometheus", - map[string]string{ - "label": "test", - }, - map[string]interface{}{ - "test_counter": float64(1.0), - }, - testTime, - telegraf.Counter, - ) - - parser := Parser{IgnoreTimestamp: true} - m, _ := parser.ParseLine(metricsWithTimestamps) - - testutil.RequireMetricEqual(t, expected, m, testutil.IgnoreTime(), testutil.SortMetrics()) - require.WithinDuration(t, time.Now(), m.Time(), 5*time.Second) -} - -func parse(buf []byte) ([]telegraf.Metric, error) { - parser := Parser{} - return parser.Parse(buf) -} - -func TestParserProtobufHeader(t *testing.T) { - var uClient = &http.Client{ - Transport: &http.Transport{ - DisableKeepAlives: true, - }, - } - expected := []telegraf.Metric{ - testutil.MustMetric( - "prometheus", - map[string]string{ - "host": "omsk", - }, - map[string]interface{}{ - "swap_free": 9.77911808e+08, - }, - time.Unix(0, 0), - 2, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "host": "omsk", - }, - map[string]interface{}{ - "swap_in": 2.031616e+06, - }, - time.Unix(0, 0), - 1, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "host": "omsk", - }, - map[string]interface{}{ - "swap_out": 1.579008e+07, - }, - time.Unix(0, 0), - 1, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "host": "omsk", - }, - map[string]interface{}{ - "swap_total": 9.93185792e+08, - }, - time.Unix(0, 0), - 2, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "host": "omsk", - }, - map[string]interface{}{ - "swap_used": 1.5273984e+07, - }, - time.Unix(0, 0), - 2, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "host": "omsk", - }, - map[string]interface{}{ - "swap_used_percent": 1.5378778193395661, - }, - time.Unix(0, 0), - 2, - ), - } - sampleProtoBufData := []uint8{67, 10, 9, 115, 119, 97, 112, 95, 102, 114, 101, 101, 18, 25, 84, 101, 108, 101, 103, 114, 97, 102, 32, 99, 111, 108, 108, - 101, 99, 116, 101, 100, 32, 109, 101, 116, 114, 105, 99, 24, 1, 34, 25, 10, 12, 10, 4, 104, 111, 115, 116, 18, 4, 111, 109, 115, 107, 18, 9, 9, 0, 0, - 0, 0, 224, 36, 205, 65, 65, 10, 7, 115, 119, 97, 112, 95, 105, 110, 18, 25, 84, 101, 108, 101, 103, 114, 97, 102, 32, 99, 111, 108, 108, 101, 99, 116, - 101, 100, 32, 109, 101, 116, 114, 105, 99, 24, 0, 34, 25, 10, 12, 10, 4, 104, 111, 115, 116, 18, 4, 111, 109, 115, 107, 26, 9, 9, 0, 0, 0, 0, 0, 0, - 63, 65, 66, 10, 8, 115, 119, 97, 112, 95, 111, 117, 116, 18, 25, 84, 101, 108, 101, 103, 114, 97, 102, 32, 99, 111, 108, 108, 101, 99, 116, 101, 100, - 32, 109, 101, 116, 114, 105, 99, 24, 0, 34, 25, 10, 12, 10, 4, 104, 111, 115, 116, 18, 4, 111, 109, 115, 107, 26, 9, 9, 0, 0, 0, 0, 0, 30, 110, 65, - 68, 10, 10, 115, 119, 97, 112, 95, 116, 111, 116, 97, 108, 18, 25, 84, 101, 108, 101, 103, 114, 97, 102, 32, 99, 111, 108, 108, 101, 99, 116, 101, - 100, 32, 109, 101, 116, 114, 105, 99, 24, 1, 34, 25, 10, 12, 10, 4, 104, 111, 115, 116, 18, 4, 111, 109, 115, 107, 18, 9, 9, 0, 0, 0, 0, 104, 153, - 205, 65, 67, 10, 9, 115, 119, 97, 112, 95, 117, 115, 101, 100, 18, 25, 84, 101, 108, 101, 103, 114, 97, 102, 32, 99, 111, 108, 108, 101, 99, 116, - 101, 100, 32, 109, 101, 116, 114, 105, 99, 24, 1, 34, 25, 10, 12, 10, 4, 104, 111, 115, 116, 18, 4, 111, 109, 115, 107, 18, 9, 9, 0, 0, 0, 0, 0, 34, - 109, 65, 75, 10, 17, 115, 119, 97, 112, 95, 117, 115, 101, 100, 95, 112, 101, 114, 99, 101, 110, 116, 18, 25, 84, 101, 108, 101, 103, 114, 97, 102, - 32, 99, 111, 108, 108, 101, 99, 116, 101, 100, 32, 109, 101, 116, 114, 105, 99, 24, 1, 34, 25, 10, 12, 10, 4, 104, 111, 115, 116, 18, 4, 111, 109, - 115, 107, 18, 9, 9, 109, 234, 180, 197, 37, 155, 248, 63} - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited") - _, err := w.Write(sampleProtoBufData) - require.NoError(t, err) - })) - defer ts.Close() - req, err := http.NewRequest("GET", ts.URL, nil) - if err != nil { - t.Fatalf("unable to create new request %q: %s", ts.URL, err) - } - var resp *http.Response - resp, err = uClient.Do(req) - if err != nil { - t.Fatalf("error making HTTP request to %q: %s", ts.URL, err) - } - defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - if err != nil { - t.Fatalf("error reading body: %s", err) - } - parser := Parser{Header: resp.Header} - metrics, err := parser.Parse(body) - if err != nil { - t.Fatalf("error reading metrics for %q: %s", ts.URL, err) - } - testutil.RequireMetricsEqual(t, expected, metrics, testutil.IgnoreTime(), testutil.SortMetrics()) -} - -func TestHistogramInfBucketPresence(t *testing.T) { - expected := []telegraf.Metric{ - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - }, - map[string]interface{}{ - "apiserver_request_latencies_count": float64(2025.0), - "apiserver_request_latencies_sum": float64(1.02726334e+08), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - "le": "125000", - }, - map[string]interface{}{ - "apiserver_request_latencies_bucket": float64(1994.0), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - "le": "250000", - }, - map[string]interface{}{ - "apiserver_request_latencies_bucket": float64(1997.0), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - "le": "500000", - }, - map[string]interface{}{ - "apiserver_request_latencies_bucket": float64(2000.0), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - "le": "1e+06", - }, - map[string]interface{}{ - "apiserver_request_latencies_bucket": float64(2005.0), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - "le": "2e+06", - }, - map[string]interface{}{ - "apiserver_request_latencies_bucket": float64(2012.0), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - "le": "4e+06", - }, - map[string]interface{}{ - "apiserver_request_latencies_bucket": float64(2017.0), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - "le": "8e+06", - }, - map[string]interface{}{ - "apiserver_request_latencies_bucket": float64(2024.0), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - testutil.MustMetric( - "prometheus", - map[string]string{ - "verb": "POST", - "resource": "bindings", - "le": "+Inf", - }, - map[string]interface{}{ - "apiserver_request_latencies_bucket": float64(2025.0), - }, - time.Unix(0, 0), - telegraf.Histogram, - ), - } - - var metricFamily dto.MetricFamily - err := json.Unmarshal([]byte(validUniqueHistogramJSON), &metricFamily) - require.NoError(t, err) - - m := metricFamily.Metric[0] - tags := GetTagsFromLabels(m, map[string]string{}) - metrics := makeBucketsV2(m, tags, *metricFamily.Name, metricFamily.GetType(), time.Now()) - - testutil.RequireMetricsEqual(t, expected, metrics, testutil.IgnoreTime(), testutil.SortMetrics()) -} - -func TestParseValidPrometheusV1(t *testing.T) { - // Gauge value +func BenchmarkParsingMetricVersion1(b *testing.B) { plugin := &Parser{MetricVersion: 1} - metrics, err := plugin.Parse([]byte(validUniqueGauge)) - require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "cadvisor_version_info", metrics[0].Name()) - require.Equal(t, map[string]interface{}{ - "gauge": float64(1), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{ - "osVersion": "CentOS Linux 7 (Core)", - "cadvisorRevision": "", - "cadvisorVersion": "", - "dockerVersion": "1.8.2", - "kernelVersion": "3.10.0-229.20.1.el7.x86_64", - }, metrics[0].Tags()) - // Counter value - plugin = &Parser{MetricVersion: 1} - metrics, err = plugin.Parse([]byte(validUniqueCounter)) - require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "get_token_fail_count", metrics[0].Name()) - require.Equal(t, map[string]interface{}{ - "counter": float64(0), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{}, metrics[0].Tags()) - - // Summary data - plugin = &Parser{MetricVersion: 1} - metrics, err = plugin.Parse([]byte(validUniqueSummary)) - require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "http_request_duration_microseconds", metrics[0].Name()) - require.Equal(t, map[string]interface{}{ - "0.5": 552048.506, - "0.9": 5.876804288e+06, - "0.99": 5.876804288e+06, - "count": 9.0, - "sum": 1.8909097205e+07, - }, metrics[0].Fields()) - require.Equal(t, map[string]string{"handler": "prometheus"}, metrics[0].Tags()) - - // histogram data - plugin = &Parser{MetricVersion: 1} - metrics, err = plugin.Parse([]byte(validUniqueHistogram)) - require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "apiserver_request_latencies", metrics[0].Name()) - require.Equal(t, map[string]interface{}{ - "500000": 2000.0, - "count": 2025.0, - "sum": 1.02726334e+08, - "250000": 1997.0, - "2e+06": 2012.0, - "4e+06": 2017.0, - "8e+06": 2024.0, - "+Inf": 2025.0, - "125000": 1994.0, - "1e+06": 2005.0, - }, metrics[0].Fields()) - require.Equal(t, - map[string]string{"verb": "POST", "resource": "bindings"}, - metrics[0].Tags()) -} - -func TestMetricsWithTimestampV1(t *testing.T) { - testTime := time.Date(2020, time.October, 4, 17, 0, 0, 0, time.UTC) - testTimeUnix := testTime.UnixNano() / int64(time.Millisecond) - metricsWithTimestamps := fmt.Sprintf(` -# TYPE test_counter counter -test_counter{label="test"} 1 %d -`, testTimeUnix) - - // IgnoreTimestamp is false - plugin := &Parser{MetricVersion: 1} - metrics, err := plugin.Parse([]byte(metricsWithTimestamps)) - require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "test_counter", metrics[0].Name()) - require.Equal(t, map[string]interface{}{ - "counter": float64(1), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{ - "label": "test", - }, metrics[0].Tags()) - require.Equal(t, testTime, metrics[0].Time().UTC()) - - // IgnoreTimestamp is true - plugin = &Parser{MetricVersion: 1, IgnoreTimestamp: true} - metrics, err = plugin.Parse([]byte(metricsWithTimestamps)) - require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "test_counter", metrics[0].Name()) - require.Equal(t, map[string]interface{}{ - "counter": float64(1), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{ - "label": "test", - }, metrics[0].Tags()) - require.WithinDuration(t, time.Now(), metrics[0].Time().UTC(), 5*time.Second) -} - -const benchmarkData = ` -# HELP benchmark_a Test metric for benchmarking -# TYPE benchmark_a gauge -benchmark_a{source="myhost",tags_platform="python",tags_sdkver="3.11.5"} 5 1653643420000 - -# HELP benchmark_b Test metric for benchmarking -# TYPE benchmark_b gauge -benchmark_b{source="myhost",tags_platform="python",tags_sdkver="3.11.4"} 4 1653643420000 -` - -func TestBenchmarkData(t *testing.T) { - plugin := &Parser{ - IgnoreTimestamp: false, - } - - expected := []telegraf.Metric{ - metric.New( - "prometheus", - map[string]string{ - "source": "myhost", - "tags_platform": "python", - "tags_sdkver": "3.11.5", - }, - map[string]interface{}{ - "benchmark_a": 5.0, - }, - time.Unix(1653643420, 0), - telegraf.Gauge, - ), - metric.New( - "prometheus", - map[string]string{ - "source": "myhost", - "tags_platform": "python", - "tags_sdkver": "3.11.4", - }, - map[string]interface{}{ - "benchmark_b": 4.0, - }, - time.Unix(1653643420, 0), - telegraf.Gauge, - ), - } - - actual, err := plugin.Parse([]byte(benchmarkData)) - require.NoError(t, err) - testutil.RequireMetricsEqual(t, expected, actual, testutil.SortMetrics()) -} - -func BenchmarkParsing(b *testing.B) { - plugin := &Parser{} + benchmarkData, err := os.ReadFile(filepath.FromSlash("testcases/benchmark/input.txt")) + require.NoError(b, err) + require.NotEmpty(b, benchmarkData) for n := 0; n < b.N; n++ { - _, _ = plugin.Parse([]byte(benchmarkData)) + _, _ = plugin.Parse(benchmarkData) + } +} + +func BenchmarkParsingMetricVersion2(b *testing.B) { + plugin := &Parser{MetricVersion: 2} + + benchmarkData, err := os.ReadFile(filepath.FromSlash("testcases/benchmark/input.txt")) + require.NoError(b, err) + require.NotEmpty(b, benchmarkData) + + for n := 0; n < b.N; n++ { + _, _ = plugin.Parse(benchmarkData) } } diff --git a/plugins/parsers/prometheus/parser_v1.go b/plugins/parsers/prometheus/parser_v1.go index a3fe30fd1..19932f8ee 100644 --- a/plugins/parsers/prometheus/parser_v1.go +++ b/plugins/parsers/prometheus/parser_v1.go @@ -55,7 +55,7 @@ func (p *Parser) parseV1(buf []byte) ([]telegraf.Metric, error) { for metricName, mf := range metricFamilies { for _, m := range mf.Metric { // reading tags - tags := GetTagsFromLabels(m, nil) + tags := GetTagsFromLabels(m, p.DefaultTags) // reading fields var fields map[string]interface{} diff --git a/plugins/parsers/prometheus/testcases/benchmark/expected_v1.out b/plugins/parsers/prometheus/testcases/benchmark/expected_v1.out new file mode 100644 index 000000000..5233da647 --- /dev/null +++ b/plugins/parsers/prometheus/testcases/benchmark/expected_v1.out @@ -0,0 +1,2 @@ +benchmark_a,_type=gauge,source=myhost,tags_platform=python,tags_sdkver=3.11.5 gauge=5 1653643420000000000 +benchmark_b,_type=gauge,source=myhost,tags_platform=python,tags_sdkver=3.11.4 gauge=4 1653643420000000000 diff --git a/plugins/parsers/prometheus/testcases/benchmark/expected_v2.out b/plugins/parsers/prometheus/testcases/benchmark/expected_v2.out new file mode 100644 index 000000000..618c77ec3 --- /dev/null +++ b/plugins/parsers/prometheus/testcases/benchmark/expected_v2.out @@ -0,0 +1,2 @@ +prometheus,_type=gauge,source=myhost,tags_platform=python,tags_sdkver=3.11.5 benchmark_a=5 1653643420000000000 +prometheus,_type=gauge,source=myhost,tags_platform=python,tags_sdkver=3.11.4 benchmark_b=4 1653643420000000000 \ No newline at end of file diff --git a/plugins/parsers/prometheus/testcases/benchmark/input.txt b/plugins/parsers/prometheus/testcases/benchmark/input.txt new file mode 100644 index 000000000..7dd64029f --- /dev/null +++ b/plugins/parsers/prometheus/testcases/benchmark/input.txt @@ -0,0 +1,7 @@ +# HELP benchmark_a Test metric for benchmarking +# TYPE benchmark_a gauge +benchmark_a{source="myhost",tags_platform="python",tags_sdkver="3.11.5"} 5 1653643420000 + +# HELP benchmark_b Test metric for benchmarking +# TYPE benchmark_b gauge +benchmark_b{source="myhost",tags_platform="python",tags_sdkver="3.11.4"} 4 1653643420000 diff --git a/plugins/parsers/prometheus/testcases/benchmark/telegraf.conf b/plugins/parsers/prometheus/testcases/benchmark/telegraf.conf new file mode 100644 index 000000000..08020ca58 --- /dev/null +++ b/plugins/parsers/prometheus/testcases/benchmark/telegraf.conf @@ -0,0 +1,4 @@ +[[inputs.test]] + files = ["input.txt"] + data_format = "prometheus" + diff --git a/plugins/parsers/prometheus/testcases/default_tags/expected_v1.out b/plugins/parsers/prometheus/testcases/default_tags/expected_v1.out new file mode 100644 index 000000000..5cec43a2b --- /dev/null +++ b/plugins/parsers/prometheus/testcases/default_tags/expected_v1.out @@ -0,0 +1 @@ +cadvisor_version_info,_type=gauge,dockerVersion=1.8.2,kernelVersion=3.10.0-229.20.1.el7.x86_64,osVersion=CentOS\ Linux\ 7\ (Core),defaultTag=defaultTagValue gauge=1 diff --git a/plugins/parsers/prometheus/testcases/default_tags/expected_v2.out b/plugins/parsers/prometheus/testcases/default_tags/expected_v2.out new file mode 100644 index 000000000..809193beb --- /dev/null +++ b/plugins/parsers/prometheus/testcases/default_tags/expected_v2.out @@ -0,0 +1 @@ +prometheus,_type=gauge,dockerVersion=1.8.2,kernelVersion=3.10.0-229.20.1.el7.x86_64,osVersion=CentOS\ Linux\ 7\ (Core),defaultTag=defaultTagValue cadvisor_version_info=1 diff --git a/plugins/parsers/prometheus/testcases/default_tags/input.txt b/plugins/parsers/prometheus/testcases/default_tags/input.txt new file mode 100644 index 000000000..964b1df2c --- /dev/null +++ b/plugins/parsers/prometheus/testcases/default_tags/input.txt @@ -0,0 +1,3 @@ +# HELP cadvisor_version_info A metric with a constant '1' value labeled by kernel version, OS version, docker version, cadvisor version & cadvisor revision. +# TYPE cadvisor_version_info gauge +cadvisor_version_info{cadvisorRevision="",cadvisorVersion="",dockerVersion="1.8.2",kernelVersion="3.10.0-229.20.1.el7.x86_64",osVersion="CentOS Linux 7 (Core)"} 1 diff --git a/plugins/parsers/prometheus/testcases/default_tags/telegraf.conf b/plugins/parsers/prometheus/testcases/default_tags/telegraf.conf new file mode 100644 index 000000000..c9f89e10e --- /dev/null +++ b/plugins/parsers/prometheus/testcases/default_tags/telegraf.conf @@ -0,0 +1,7 @@ +[[inputs.test]] + files = ["input.txt"] + data_format = "prometheus" + + [inputs.test.default_tag_defs] + defaultTag = "defaultTagValue" + dockerVersion = "to_be_overridden" \ No newline at end of file diff --git a/plugins/parsers/prometheus/testcases/histogram_inf_bucket/expected_v1.out b/plugins/parsers/prometheus/testcases/histogram_inf_bucket/expected_v1.out new file mode 100644 index 000000000..d5e9e0bc0 --- /dev/null +++ b/plugins/parsers/prometheus/testcases/histogram_inf_bucket/expected_v1.out @@ -0,0 +1 @@ +apiserver_request_latencies,_type=histogram,resource=bindings,verb=POST 125000=1994,1e+06=2005,250000=1997,2e+06=2012,4e+06=2017,500000=2000,8e+06=2024,count=2025,sum=102726334 diff --git a/plugins/parsers/prometheus/testcases/histogram_inf_bucket/expected_v2.out b/plugins/parsers/prometheus/testcases/histogram_inf_bucket/expected_v2.out new file mode 100644 index 000000000..651859c89 --- /dev/null +++ b/plugins/parsers/prometheus/testcases/histogram_inf_bucket/expected_v2.out @@ -0,0 +1,9 @@ +prometheus,_type=histogram,resource=bindings,verb=POST apiserver_request_latencies_count=2025,apiserver_request_latencies_sum=102726334 +prometheus,_type=histogram,le=125000,resource=bindings,verb=POST apiserver_request_latencies_bucket=1994 +prometheus,_type=histogram,le=250000,resource=bindings,verb=POST apiserver_request_latencies_bucket=1997 +prometheus,_type=histogram,le=500000,resource=bindings,verb=POST apiserver_request_latencies_bucket=2000 +prometheus,_type=histogram,le=1e+06,resource=bindings,verb=POST apiserver_request_latencies_bucket=2005 +prometheus,_type=histogram,le=2e+06,resource=bindings,verb=POST apiserver_request_latencies_bucket=2012 +prometheus,_type=histogram,le=4e+06,resource=bindings,verb=POST apiserver_request_latencies_bucket=2017 +prometheus,_type=histogram,le=8e+06,resource=bindings,verb=POST apiserver_request_latencies_bucket=2024 +prometheus,_type=histogram,le=+Inf,resource=bindings,verb=POST apiserver_request_latencies_bucket=2025 diff --git a/plugins/parsers/prometheus/testcases/histogram_inf_bucket/input.bin b/plugins/parsers/prometheus/testcases/histogram_inf_bucket/input.bin new file mode 100644 index 0000000000000000000000000000000000000000..afcca2eaf8f67609e8e8c050ea1269945401cd70 GIT binary patch literal 267 zcmeBV;*w4*$Sh7RDoZVjFG?*eO)V~o&q*vv%}dTqEfxw4N-ZwP&nr$gL9*$FB9z#RRKL;T1ER4qolDh!o$$@xRVLUw$?+%P-2jV@1@%%u%7cgF&BLJ(C BP51x+ literal 0 HcmV?d00001 diff --git a/plugins/parsers/prometheus/testcases/histogram_inf_bucket/telegraf.conf b/plugins/parsers/prometheus/testcases/histogram_inf_bucket/telegraf.conf new file mode 100644 index 000000000..55d11c72f --- /dev/null +++ b/plugins/parsers/prometheus/testcases/histogram_inf_bucket/telegraf.conf @@ -0,0 +1,6 @@ +[[inputs.test]] + files = ["input.bin"] + data_format = "prometheus" + + [inputs.test.additional_params] + headers = {Content-Type = "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited"} diff --git a/plugins/parsers/prometheus/testcases/ignore_timestamp/expected_v1.out b/plugins/parsers/prometheus/testcases/ignore_timestamp/expected_v1.out new file mode 100644 index 000000000..e5435eae0 --- /dev/null +++ b/plugins/parsers/prometheus/testcases/ignore_timestamp/expected_v1.out @@ -0,0 +1 @@ +test_counter,_type=counter,label=test counter=1 1601830800000000000 diff --git a/plugins/parsers/prometheus/testcases/ignore_timestamp/expected_v2.out b/plugins/parsers/prometheus/testcases/ignore_timestamp/expected_v2.out new file mode 100644 index 000000000..a4d8b9f65 --- /dev/null +++ b/plugins/parsers/prometheus/testcases/ignore_timestamp/expected_v2.out @@ -0,0 +1 @@ +prometheus,_type=counter,label=test test_counter=1 1601830800000000000 diff --git a/plugins/parsers/prometheus/testcases/ignore_timestamp/input.txt b/plugins/parsers/prometheus/testcases/ignore_timestamp/input.txt new file mode 100644 index 000000000..11fd28670 --- /dev/null +++ b/plugins/parsers/prometheus/testcases/ignore_timestamp/input.txt @@ -0,0 +1,2 @@ +# TYPE test_counter counter +test_counter{label="test"} 1 1601830800000 diff --git a/plugins/parsers/prometheus/testcases/ignore_timestamp/telegraf.conf b/plugins/parsers/prometheus/testcases/ignore_timestamp/telegraf.conf new file mode 100644 index 000000000..97c3e9907 --- /dev/null +++ b/plugins/parsers/prometheus/testcases/ignore_timestamp/telegraf.conf @@ -0,0 +1,4 @@ +[[inputs.test]] + files = ["input.txt"] + data_format = "prometheus" + prometheus_ignore_timestamp = true diff --git a/plugins/parsers/prometheus/testcases/metric_with_timestamp/expected_v1.out b/plugins/parsers/prometheus/testcases/metric_with_timestamp/expected_v1.out new file mode 100644 index 000000000..e5435eae0 --- /dev/null +++ b/plugins/parsers/prometheus/testcases/metric_with_timestamp/expected_v1.out @@ -0,0 +1 @@ +test_counter,_type=counter,label=test counter=1 1601830800000000000 diff --git a/plugins/parsers/prometheus/testcases/metric_with_timestamp/expected_v2.out b/plugins/parsers/prometheus/testcases/metric_with_timestamp/expected_v2.out new file mode 100644 index 000000000..a4d8b9f65 --- /dev/null +++ b/plugins/parsers/prometheus/testcases/metric_with_timestamp/expected_v2.out @@ -0,0 +1 @@ +prometheus,_type=counter,label=test test_counter=1 1601830800000000000 diff --git a/plugins/parsers/prometheus/testcases/metric_with_timestamp/input.txt b/plugins/parsers/prometheus/testcases/metric_with_timestamp/input.txt new file mode 100644 index 000000000..11fd28670 --- /dev/null +++ b/plugins/parsers/prometheus/testcases/metric_with_timestamp/input.txt @@ -0,0 +1,2 @@ +# TYPE test_counter counter +test_counter{label="test"} 1 1601830800000 diff --git a/plugins/parsers/prometheus/testcases/metric_with_timestamp/telegraf.conf b/plugins/parsers/prometheus/testcases/metric_with_timestamp/telegraf.conf new file mode 100644 index 000000000..08020ca58 --- /dev/null +++ b/plugins/parsers/prometheus/testcases/metric_with_timestamp/telegraf.conf @@ -0,0 +1,4 @@ +[[inputs.test]] + files = ["input.txt"] + data_format = "prometheus" + diff --git a/plugins/parsers/prometheus/testcases/protobuf/expected_v1.out b/plugins/parsers/prometheus/testcases/protobuf/expected_v1.out new file mode 100644 index 000000000..e95b3d22c --- /dev/null +++ b/plugins/parsers/prometheus/testcases/protobuf/expected_v1.out @@ -0,0 +1,6 @@ +swap_free,_type=gauge,host=omsk gauge=977911808 +swap_in,_type=counter,host=omsk counter=2031616 +swap_out,_type=counter,host=omsk counter=15790080 +swap_total,_type=gauge,host=omsk gauge=993185792 +swap_used,_type=gauge,host=omsk gauge=15273984 +swap_used_percent,_type=gauge,host=omsk gauge=1.5378778193395661 diff --git a/plugins/parsers/prometheus/testcases/protobuf/expected_v2.out b/plugins/parsers/prometheus/testcases/protobuf/expected_v2.out new file mode 100644 index 000000000..058d38a2b --- /dev/null +++ b/plugins/parsers/prometheus/testcases/protobuf/expected_v2.out @@ -0,0 +1,6 @@ +prometheus,_type=gauge,host=omsk swap_used_percent=1.5378778193395661 +prometheus,_type=gauge,host=omsk swap_free=977911808 +prometheus,_type=counter,host=omsk swap_in=2031616 +prometheus,_type=counter,host=omsk swap_out=15790080 +prometheus,_type=gauge,host=omsk swap_total=993185792 +prometheus,_type=gauge,host=omsk swap_used=15273984 \ No newline at end of file diff --git a/plugins/parsers/prometheus/testcases/protobuf/input.bin b/plugins/parsers/prometheus/testcases/protobuf/input.bin new file mode 100644 index 0000000000000000000000000000000000000000..d9fa9fe14667b9f6e087b3c8997b960e6afa1463 GIT binary patch literal 414 zcmZ?f;w&ytEQn7lN=+4#3`xyNO)pAJQ%KIw$w^HvNlj76O)V+POqO6&lH}syV#&xa zE)inM&n?at;^brif(I&R9UZyY!DeLU;WL2&-2^F!2@DMOj!s-0U=#97OUN=pF3-`0 ziwkT-Nq$LU4n9M0`6OfJSx0ARXp|PGrjTO>gHo=eH 0 { + p.Parser.SetDefaultTags(p.DefaultTags) + } +} + +// Register the plugin +func init() { + inputs.Add("test", func() telegraf.Input { + return &Plugin{} + }) +} diff --git a/testutil/plugin_input/sample.conf b/testutil/plugin_input/sample.conf new file mode 100644 index 000000000..600a014fc --- /dev/null +++ b/testutil/plugin_input/sample.conf @@ -0,0 +1,15 @@ +# Plugin for testing purposes +[[inputs.test]] + ## Input files for reading + files = [] + + ## Parser settings as needed + # data_format = "influx" + + ## Tag setting used as "default tags" + # [inputs.test.default_tag_defs] + # testtag = somevalue + + ## Additional parameters that can be used in the testing functions" + # [inputs.test.additional_params] + # dump_data = true