From 6d1da80ebb543b039f7572d4c539a37b535685cd Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Tue, 6 Jun 2023 15:28:45 +0200 Subject: [PATCH] docs(inputs.prometheus): Add example for prometheus timestamps (#13362) --- plugins/inputs/prometheus/README.md | 28 +++++++++++++++ plugins/inputs/prometheus/prometheus_test.go | 36 ++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/plugins/inputs/prometheus/README.md b/plugins/inputs/prometheus/README.md index b1d0ecf3f..5d6dfd69b 100644 --- a/plugins/inputs/prometheus/README.md +++ b/plugins/inputs/prometheus/README.md @@ -402,3 +402,31 @@ prometheus,cpu=cpu1,url=http://example.org:9273/metrics cpu_usage_user=5.8291457 prometheus,cpu=cpu2,url=http://example.org:9273/metrics cpu_usage_user=2.119071644805144 1505776751000000000 prometheus,cpu=cpu3,url=http://example.org:9273/metrics cpu_usage_user=1.5228426395944945 1505776751000000000 ``` + +### Output with timestamp included + +Below is an example of a Prometheus metric which includes a timestamp: + +```text +# TYPE test_counter counter +test_counter{label="test"} 1 1685443805885 +``` + +Telegraf will generate the following metric: + +```text +test_counter,address=127.0.0.1,label=test counter=1 1685443805885000000 +``` + +using the standard configuration + +```toml +[[inputs.prometheus]] + ## An array of urls to scrape metrics from. + urls = ["http://localhost:2019/metrics"] +``` + +**Please note:** Metrics generated by Prometheus endpoints are generated with +*millisecond precision*. The default Telegraf agent level precision setting +reduces this to seconds. Change the `precision` setting at agent or plugin level +to milliseconds or smaller to report metric timestamps with full precision. diff --git a/plugins/inputs/prometheus/prometheus_test.go b/plugins/inputs/prometheus/prometheus_test.go index 905ddbde7..d93721dab 100644 --- a/plugins/inputs/prometheus/prometheus_test.go +++ b/plugins/inputs/prometheus/prometheus_test.go @@ -11,6 +11,7 @@ import ( "time" "github.com/influxdata/telegraf/config" + "github.com/influxdata/telegraf/metric" "github.com/stretchr/testify/require" "k8s.io/apimachinery/pkg/fields" @@ -168,6 +169,41 @@ func TestPrometheusGeneratesMetricsWithHostNameTag(t *testing.T) { require.True(t, acc.TagValue("test_metric", "url") == ts.URL) } +func TestPrometheusWithTimestamp(t *testing.T) { + prommetric := `# HELP test_counter A sample test counter. +# TYPE test_counter counter +test_counter{label="test"} 1 1685443805885` + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, err := fmt.Fprintln(w, prommetric) + require.NoError(t, err) + })) + defer ts.Close() + + p := &Prometheus{ + Log: testutil.Logger{}, + KubernetesServices: []string{ts.URL}, + } + require.NoError(t, p.Init()) + + u, err := url.Parse(ts.URL) + require.NoError(t, err) + tsAddress := u.Hostname() + + expected := []telegraf.Metric{ + metric.New( + "test_counter", + map[string]string{"address": tsAddress, "label": "test"}, + map[string]interface{}{"counter": float64(1.0)}, + time.UnixMilli(1685443805885), + telegraf.Counter, + ), + } + + var acc testutil.Accumulator + require.NoError(t, acc.GatherError(p.Gather)) + testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics()) +} + func TestPrometheusGeneratesMetricsAlthoughFirstDNSFailsIntegration(t *testing.T) { if testing.Short() { t.Skip("Skipping integration test in short mode")