docs(inputs.prometheus): Add example for prometheus timestamps (#13362)

This commit is contained in:
Sven Rebhan 2023-06-06 15:28:45 +02:00 committed by GitHub
parent 37d82f741c
commit 6d1da80ebb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 0 deletions

View File

@ -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.

View File

@ -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")