2015-06-20 20:38:01 +08:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
|
|
import (
|
2023-01-30 22:52:54 +08:00
|
|
|
"errors"
|
2015-06-20 20:38:01 +08:00
|
|
|
"fmt"
|
2020-02-11 06:18:30 +08:00
|
|
|
"math"
|
2015-06-20 20:38:01 +08:00
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
2017-09-19 06:06:11 +08:00
|
|
|
"net/url"
|
2015-06-20 20:38:01 +08:00
|
|
|
"testing"
|
2017-03-30 06:04:29 +08:00
|
|
|
"time"
|
2015-06-20 20:38:01 +08:00
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-03-24 23:59:05 +08:00
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
2021-11-15 23:14:09 +08:00
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2023-10-25 00:45:17 +08:00
|
|
|
"github.com/influxdata/telegraf/config"
|
|
|
|
|
"github.com/influxdata/telegraf/metric"
|
2021-11-15 23:14:09 +08:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2015-06-20 20:38:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const sampleTextFormat = `# HELP go_gc_duration_seconds A summary of the GC invocation durations.
|
|
|
|
|
# TYPE go_gc_duration_seconds summary
|
|
|
|
|
go_gc_duration_seconds{quantile="0"} 0.00010425500000000001
|
|
|
|
|
go_gc_duration_seconds{quantile="0.25"} 0.000139108
|
|
|
|
|
go_gc_duration_seconds{quantile="0.5"} 0.00015749400000000002
|
|
|
|
|
go_gc_duration_seconds{quantile="0.75"} 0.000331463
|
|
|
|
|
go_gc_duration_seconds{quantile="1"} 0.000667154
|
|
|
|
|
go_gc_duration_seconds_sum 0.0018183950000000002
|
|
|
|
|
go_gc_duration_seconds_count 7
|
|
|
|
|
# HELP go_goroutines Number of goroutines that currently exist.
|
|
|
|
|
# TYPE go_goroutines gauge
|
|
|
|
|
go_goroutines 15
|
2017-03-30 06:04:29 +08:00
|
|
|
# HELP test_metric An untyped metric with a timestamp
|
|
|
|
|
# TYPE test_metric untyped
|
2022-03-22 00:11:25 +08:00
|
|
|
test_metric{label="value"} 1.0 1490802350000`
|
|
|
|
|
|
2019-11-21 12:53:57 +08:00
|
|
|
const sampleSummaryTextFormat = `# HELP go_gc_duration_seconds A summary of the GC invocation durations.
|
|
|
|
|
# TYPE go_gc_duration_seconds summary
|
|
|
|
|
go_gc_duration_seconds{quantile="0"} 0.00010425500000000001
|
|
|
|
|
go_gc_duration_seconds{quantile="0.25"} 0.000139108
|
|
|
|
|
go_gc_duration_seconds{quantile="0.5"} 0.00015749400000000002
|
|
|
|
|
go_gc_duration_seconds{quantile="0.75"} 0.000331463
|
|
|
|
|
go_gc_duration_seconds{quantile="1"} 0.000667154
|
|
|
|
|
go_gc_duration_seconds_sum 0.0018183950000000002
|
2022-03-22 00:11:25 +08:00
|
|
|
go_gc_duration_seconds_count 7`
|
|
|
|
|
|
2019-11-21 12:53:57 +08:00
|
|
|
const sampleGaugeTextFormat = `
|
|
|
|
|
# HELP go_goroutines Number of goroutines that currently exist.
|
|
|
|
|
# TYPE go_goroutines gauge
|
2022-03-22 00:11:25 +08:00
|
|
|
go_goroutines 15 1490802350000`
|
2015-06-20 20:38:01 +08:00
|
|
|
|
|
|
|
|
func TestPrometheusGeneratesMetrics(t *testing.T) {
|
2015-08-05 06:09:59 +08:00
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-04-09 00:43:39 +08:00
|
|
|
_, err := fmt.Fprintln(w, sampleTextFormat)
|
|
|
|
|
require.NoError(t, err)
|
2015-08-05 06:09:59 +08:00
|
|
|
}))
|
2015-06-20 20:38:01 +08:00
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
p := &Prometheus{
|
2019-11-21 12:53:57 +08:00
|
|
|
Log: testutil.Logger{},
|
|
|
|
|
URLs: []string{ts.URL},
|
|
|
|
|
URLTag: "url",
|
2015-06-20 20:38:01 +08:00
|
|
|
}
|
2022-09-14 01:48:13 +08:00
|
|
|
err := p.Init()
|
|
|
|
|
require.NoError(t, err)
|
2015-06-20 20:38:01 +08:00
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
2022-09-14 01:48:13 +08:00
|
|
|
err = acc.GatherError(p.Gather)
|
2015-06-20 20:38:01 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2021-11-15 23:14:09 +08:00
|
|
|
require.True(t, acc.HasFloatField("go_gc_duration_seconds", "count"))
|
|
|
|
|
require.True(t, acc.HasFloatField("go_goroutines", "gauge"))
|
|
|
|
|
require.True(t, acc.HasFloatField("test_metric", "value"))
|
|
|
|
|
require.True(t, acc.HasTimestamp("test_metric", time.Unix(1490802350, 0)))
|
|
|
|
|
require.False(t, acc.HasTag("test_metric", "address"))
|
2023-10-25 05:02:26 +08:00
|
|
|
require.Equal(t, acc.TagValue("test_metric", "url"), ts.URL+"/metrics")
|
2017-09-19 06:06:11 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-21 23:39:34 +08:00
|
|
|
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")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
case "text/plain":
|
|
|
|
|
_, err := fmt.Fprintln(w, "plain 42 1490802380000")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
default:
|
|
|
|
|
_, err := fmt.Fprintln(w, "other 44 1490802420000")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
headers map[string]string
|
|
|
|
|
expectedMeasurementName string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
"default",
|
|
|
|
|
map[string]string{},
|
|
|
|
|
"proto",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"plain text",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"accept": "text/plain",
|
|
|
|
|
},
|
|
|
|
|
"plain",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"other",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"accept": "fakeACCEPTitem",
|
|
|
|
|
},
|
|
|
|
|
"other",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
p := &Prometheus{
|
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
|
URLs: []string{ts.URL},
|
|
|
|
|
URLTag: "url",
|
|
|
|
|
HTTPHeaders: test.headers,
|
|
|
|
|
}
|
|
|
|
|
err := p.Init()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
require.NoError(t, acc.GatherError(p.Gather))
|
|
|
|
|
require.Equal(t, test.expectedMeasurementName, acc.Metrics[0].Measurement)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-19 06:06:11 +08:00
|
|
|
func TestPrometheusGeneratesMetricsWithHostNameTag(t *testing.T) {
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-04-09 00:43:39 +08:00
|
|
|
_, err := fmt.Fprintln(w, sampleTextFormat)
|
|
|
|
|
require.NoError(t, err)
|
2017-09-19 06:06:11 +08:00
|
|
|
}))
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
p := &Prometheus{
|
2019-09-24 06:39:50 +08:00
|
|
|
Log: testutil.Logger{},
|
2017-09-19 06:06:11 +08:00
|
|
|
KubernetesServices: []string{ts.URL},
|
2019-11-21 12:53:57 +08:00
|
|
|
URLTag: "url",
|
2017-09-19 06:06:11 +08:00
|
|
|
}
|
2022-09-14 01:48:13 +08:00
|
|
|
err := p.Init()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2017-09-19 06:06:11 +08:00
|
|
|
u, _ := url.Parse(ts.URL)
|
|
|
|
|
tsAddress := u.Hostname()
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
2022-09-14 01:48:13 +08:00
|
|
|
err = acc.GatherError(p.Gather)
|
2017-09-19 06:06:11 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2021-11-15 23:14:09 +08:00
|
|
|
require.True(t, acc.HasFloatField("go_gc_duration_seconds", "count"))
|
|
|
|
|
require.True(t, acc.HasFloatField("go_goroutines", "gauge"))
|
|
|
|
|
require.True(t, acc.HasFloatField("test_metric", "value"))
|
|
|
|
|
require.True(t, acc.HasTimestamp("test_metric", time.Unix(1490802350, 0)))
|
2023-10-25 05:02:26 +08:00
|
|
|
require.Equal(t, acc.TagValue("test_metric", "address"), tsAddress)
|
|
|
|
|
require.Equal(t, acc.TagValue("test_metric", "url"), ts.URL)
|
2017-09-19 06:06:11 +08:00
|
|
|
}
|
2017-03-30 06:04:29 +08:00
|
|
|
|
2023-06-06 21:28:45 +08:00
|
|
|
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())
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 02:06:12 +08:00
|
|
|
func TestPrometheusGeneratesMetricsAlthoughFirstDNSFailsIntegration(t *testing.T) {
|
2017-09-19 06:06:11 +08:00
|
|
|
if testing.Short() {
|
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-04-09 00:43:39 +08:00
|
|
|
_, err := fmt.Fprintln(w, sampleTextFormat)
|
|
|
|
|
require.NoError(t, err)
|
2017-09-19 06:06:11 +08:00
|
|
|
}))
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
p := &Prometheus{
|
2019-09-24 06:39:50 +08:00
|
|
|
Log: testutil.Logger{},
|
2018-02-06 03:16:00 +08:00
|
|
|
URLs: []string{ts.URL},
|
2017-09-19 06:06:11 +08:00
|
|
|
KubernetesServices: []string{"http://random.telegraf.local:88/metrics"},
|
|
|
|
|
}
|
2022-09-14 01:48:13 +08:00
|
|
|
err := p.Init()
|
|
|
|
|
require.NoError(t, err)
|
2017-09-19 06:06:11 +08:00
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
2022-09-14 01:48:13 +08:00
|
|
|
err = acc.GatherError(p.Gather)
|
2017-09-19 06:06:11 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2021-11-15 23:14:09 +08:00
|
|
|
require.True(t, acc.HasFloatField("go_gc_duration_seconds", "count"))
|
|
|
|
|
require.True(t, acc.HasFloatField("go_goroutines", "gauge"))
|
|
|
|
|
require.True(t, acc.HasFloatField("test_metric", "value"))
|
|
|
|
|
require.True(t, acc.HasTimestamp("test_metric", time.Unix(1490802350, 0)))
|
2015-06-20 20:38:01 +08:00
|
|
|
}
|
2019-11-21 12:53:57 +08:00
|
|
|
|
2023-01-30 22:52:54 +08:00
|
|
|
func TestPrometheusGeneratesMetricsSlowEndpoint(t *testing.T) {
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
time.Sleep(4 * time.Second)
|
|
|
|
|
_, err := fmt.Fprintln(w, sampleTextFormat)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}))
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
p := &Prometheus{
|
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
|
URLs: []string{ts.URL},
|
|
|
|
|
URLTag: "url",
|
|
|
|
|
ResponseTimeout: config.Duration(time.Second * 5),
|
|
|
|
|
}
|
|
|
|
|
err := p.Init()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
|
|
err = acc.GatherError(p.Gather)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
require.True(t, acc.HasFloatField("go_gc_duration_seconds", "count"))
|
|
|
|
|
require.True(t, acc.HasFloatField("go_goroutines", "gauge"))
|
|
|
|
|
require.True(t, acc.HasFloatField("test_metric", "value"))
|
|
|
|
|
require.True(t, acc.HasTimestamp("test_metric", time.Unix(1490802350, 0)))
|
|
|
|
|
require.False(t, acc.HasTag("test_metric", "address"))
|
2023-10-25 05:02:26 +08:00
|
|
|
require.Equal(t, acc.TagValue("test_metric", "url"), ts.URL+"/metrics")
|
2023-01-30 22:52:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPrometheusGeneratesMetricsSlowEndpointHitTheTimeout(t *testing.T) {
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
time.Sleep(6 * time.Second)
|
|
|
|
|
_, err := fmt.Fprintln(w, sampleTextFormat)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}))
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
p := &Prometheus{
|
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
|
URLs: []string{ts.URL},
|
|
|
|
|
URLTag: "url",
|
|
|
|
|
ResponseTimeout: config.Duration(time.Second * 5),
|
|
|
|
|
}
|
|
|
|
|
err := p.Init()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
|
|
err = acc.GatherError(p.Gather)
|
2023-03-02 05:19:38 +08:00
|
|
|
errMessage := fmt.Sprintf("error making HTTP request to \"%s/metrics\": Get \"%s/metrics\": "+
|
2023-01-30 22:52:54 +08:00
|
|
|
"context deadline exceeded (Client.Timeout exceeded while awaiting headers)", ts.URL, ts.URL)
|
|
|
|
|
errExpected := errors.New(errMessage)
|
|
|
|
|
require.Error(t, err)
|
2023-03-02 05:19:38 +08:00
|
|
|
require.Equal(t, errExpected.Error(), err.Error())
|
2023-01-30 22:52:54 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-10 18:44:42 +08:00
|
|
|
func TestPrometheusGeneratesMetricsSlowEndpointNewConfigParameter(t *testing.T) {
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
time.Sleep(4 * time.Second)
|
|
|
|
|
_, err := fmt.Fprintln(w, sampleTextFormat)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}))
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
p := &Prometheus{
|
2023-03-16 00:52:51 +08:00
|
|
|
Log: testutil.Logger{},
|
|
|
|
|
URLs: []string{ts.URL},
|
|
|
|
|
URLTag: "url",
|
2023-02-10 18:44:42 +08:00
|
|
|
}
|
|
|
|
|
err := p.Init()
|
|
|
|
|
require.NoError(t, err)
|
2023-03-16 00:52:51 +08:00
|
|
|
p.client.Timeout = time.Second * 5
|
2023-02-10 18:44:42 +08:00
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
|
|
err = acc.GatherError(p.Gather)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
require.True(t, acc.HasFloatField("go_gc_duration_seconds", "count"))
|
|
|
|
|
require.True(t, acc.HasFloatField("go_goroutines", "gauge"))
|
|
|
|
|
require.True(t, acc.HasFloatField("test_metric", "value"))
|
|
|
|
|
require.True(t, acc.HasTimestamp("test_metric", time.Unix(1490802350, 0)))
|
|
|
|
|
require.False(t, acc.HasTag("test_metric", "address"))
|
2023-10-25 05:02:26 +08:00
|
|
|
require.Equal(t, acc.TagValue("test_metric", "url"), ts.URL+"/metrics")
|
2023-02-10 18:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPrometheusGeneratesMetricsSlowEndpointHitTheTimeoutNewConfigParameter(t *testing.T) {
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
time.Sleep(6 * time.Second)
|
|
|
|
|
_, err := fmt.Fprintln(w, sampleTextFormat)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}))
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
p := &Prometheus{
|
2023-03-16 00:52:51 +08:00
|
|
|
Log: testutil.Logger{},
|
|
|
|
|
URLs: []string{ts.URL},
|
|
|
|
|
URLTag: "url",
|
2023-02-10 18:44:42 +08:00
|
|
|
}
|
|
|
|
|
err := p.Init()
|
|
|
|
|
require.NoError(t, err)
|
2023-03-16 00:52:51 +08:00
|
|
|
p.client.Timeout = time.Second * 5
|
2023-02-10 18:44:42 +08:00
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
|
|
err = acc.GatherError(p.Gather)
|
2023-03-02 05:19:38 +08:00
|
|
|
require.ErrorContains(t, err, "error making HTTP request to \""+ts.URL+"/metrics\"")
|
2023-02-10 18:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
2019-11-21 12:53:57 +08:00
|
|
|
func TestPrometheusGeneratesSummaryMetricsV2(t *testing.T) {
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-04-09 00:43:39 +08:00
|
|
|
_, err := fmt.Fprintln(w, sampleSummaryTextFormat)
|
|
|
|
|
require.NoError(t, err)
|
2019-11-21 12:53:57 +08:00
|
|
|
}))
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
p := &Prometheus{
|
2023-07-21 17:00:54 +08:00
|
|
|
Log: &testutil.Logger{},
|
2019-11-21 12:53:57 +08:00
|
|
|
URLs: []string{ts.URL},
|
|
|
|
|
URLTag: "url",
|
|
|
|
|
MetricVersion: 2,
|
|
|
|
|
}
|
2022-09-14 01:48:13 +08:00
|
|
|
err := p.Init()
|
|
|
|
|
require.NoError(t, err)
|
2019-11-21 12:53:57 +08:00
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
2022-09-14 01:48:13 +08:00
|
|
|
err = acc.GatherError(p.Gather)
|
2019-11-21 12:53:57 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2023-10-25 05:02:26 +08:00
|
|
|
require.Equal(t, acc.TagSetValue("prometheus", "quantile"), "0")
|
2021-11-15 23:14:09 +08:00
|
|
|
require.True(t, acc.HasFloatField("prometheus", "go_gc_duration_seconds_sum"))
|
|
|
|
|
require.True(t, acc.HasFloatField("prometheus", "go_gc_duration_seconds_count"))
|
2023-10-25 05:02:26 +08:00
|
|
|
require.Equal(t, acc.TagValue("prometheus", "url"), ts.URL+"/metrics")
|
2019-11-21 12:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
2020-02-11 06:18:30 +08:00
|
|
|
func TestSummaryMayContainNaN(t *testing.T) {
|
|
|
|
|
const data = `# HELP go_gc_duration_seconds A summary of the GC invocation durations.
|
|
|
|
|
# TYPE go_gc_duration_seconds summary
|
|
|
|
|
go_gc_duration_seconds{quantile="0"} NaN
|
|
|
|
|
go_gc_duration_seconds{quantile="1"} NaN
|
|
|
|
|
go_gc_duration_seconds_sum 42.0
|
2022-03-22 00:11:25 +08:00
|
|
|
go_gc_duration_seconds_count 42`
|
|
|
|
|
|
2020-02-11 06:18:30 +08:00
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-04-09 00:43:39 +08:00
|
|
|
_, err := fmt.Fprintln(w, data)
|
|
|
|
|
require.NoError(t, err)
|
2020-02-11 06:18:30 +08:00
|
|
|
}))
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
p := &Prometheus{
|
2023-07-21 17:00:54 +08:00
|
|
|
Log: &testutil.Logger{},
|
2020-02-11 06:18:30 +08:00
|
|
|
URLs: []string{ts.URL},
|
|
|
|
|
URLTag: "",
|
|
|
|
|
MetricVersion: 2,
|
|
|
|
|
}
|
2022-09-14 01:48:13 +08:00
|
|
|
err := p.Init()
|
|
|
|
|
require.NoError(t, err)
|
2020-02-11 06:18:30 +08:00
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
2022-09-14 01:48:13 +08:00
|
|
|
err = p.Gather(&acc)
|
2020-02-11 06:18:30 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
expected := []telegraf.Metric{
|
|
|
|
|
testutil.MustMetric(
|
|
|
|
|
"prometheus",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"quantile": "0",
|
|
|
|
|
},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"go_gc_duration_seconds": math.NaN(),
|
|
|
|
|
},
|
|
|
|
|
time.Unix(0, 0),
|
|
|
|
|
telegraf.Summary,
|
|
|
|
|
),
|
|
|
|
|
testutil.MustMetric(
|
|
|
|
|
"prometheus",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"quantile": "1",
|
|
|
|
|
},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"go_gc_duration_seconds": math.NaN(),
|
|
|
|
|
},
|
|
|
|
|
time.Unix(0, 0),
|
|
|
|
|
telegraf.Summary,
|
|
|
|
|
),
|
|
|
|
|
testutil.MustMetric(
|
|
|
|
|
"prometheus",
|
|
|
|
|
map[string]string{},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"go_gc_duration_seconds_sum": 42.0,
|
|
|
|
|
"go_gc_duration_seconds_count": 42.0,
|
|
|
|
|
},
|
|
|
|
|
time.Unix(0, 0),
|
|
|
|
|
telegraf.Summary,
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(),
|
|
|
|
|
testutil.IgnoreTime(), testutil.SortMetrics())
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-21 12:53:57 +08:00
|
|
|
func TestPrometheusGeneratesGaugeMetricsV2(t *testing.T) {
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-04-09 00:43:39 +08:00
|
|
|
_, err := fmt.Fprintln(w, sampleGaugeTextFormat)
|
|
|
|
|
require.NoError(t, err)
|
2019-11-21 12:53:57 +08:00
|
|
|
}))
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
p := &Prometheus{
|
2023-07-21 17:00:54 +08:00
|
|
|
Log: &testutil.Logger{},
|
2019-11-21 12:53:57 +08:00
|
|
|
URLs: []string{ts.URL},
|
|
|
|
|
URLTag: "url",
|
|
|
|
|
MetricVersion: 2,
|
|
|
|
|
}
|
2022-09-14 01:48:13 +08:00
|
|
|
err := p.Init()
|
|
|
|
|
require.NoError(t, err)
|
2019-11-21 12:53:57 +08:00
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
2022-09-14 01:48:13 +08:00
|
|
|
err = acc.GatherError(p.Gather)
|
2019-11-21 12:53:57 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2021-11-15 23:14:09 +08:00
|
|
|
require.True(t, acc.HasFloatField("prometheus", "go_goroutines"))
|
2023-10-25 05:02:26 +08:00
|
|
|
require.Equal(t, acc.TagValue("prometheus", "url"), ts.URL+"/metrics")
|
2021-11-15 23:14:09 +08:00
|
|
|
require.True(t, acc.HasTimestamp("prometheus", time.Unix(1490802350, 0)))
|
2019-11-21 12:53:57 +08:00
|
|
|
}
|
2021-03-09 00:00:56 +08:00
|
|
|
|
2021-10-06 05:11:46 +08:00
|
|
|
func TestPrometheusGeneratesMetricsWithIgnoreTimestamp(t *testing.T) {
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
_, err := fmt.Fprintln(w, sampleTextFormat)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}))
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
p := &Prometheus{
|
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
|
URLs: []string{ts.URL},
|
|
|
|
|
URLTag: "url",
|
|
|
|
|
IgnoreTimestamp: true,
|
|
|
|
|
}
|
2022-09-14 01:48:13 +08:00
|
|
|
err := p.Init()
|
|
|
|
|
require.NoError(t, err)
|
2021-10-06 05:11:46 +08:00
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
2022-09-14 01:48:13 +08:00
|
|
|
err = acc.GatherError(p.Gather)
|
2021-10-06 05:11:46 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
m, _ := acc.Get("test_metric")
|
2021-11-15 23:14:09 +08:00
|
|
|
require.WithinDuration(t, time.Now(), m.Time, 5*time.Second)
|
2021-10-06 05:11:46 +08:00
|
|
|
}
|
|
|
|
|
|
2021-03-09 00:00:56 +08:00
|
|
|
func TestUnsupportedFieldSelector(t *testing.T) {
|
|
|
|
|
fieldSelectorString := "spec.containerName=container"
|
|
|
|
|
prom := &Prometheus{Log: testutil.Logger{}, KubernetesFieldSelector: fieldSelectorString}
|
|
|
|
|
|
|
|
|
|
fieldSelector, _ := fields.ParseSelector(prom.KubernetesFieldSelector)
|
|
|
|
|
isValid, invalidSelector := fieldSelectorIsSupported(fieldSelector)
|
2023-10-25 00:45:17 +08:00
|
|
|
require.False(t, isValid)
|
2021-11-15 23:14:09 +08:00
|
|
|
require.Equal(t, "spec.containerName", invalidSelector)
|
2021-03-09 00:00:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInitConfigErrors(t *testing.T) {
|
|
|
|
|
p := &Prometheus{
|
|
|
|
|
MetricVersion: 2,
|
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
|
URLs: nil,
|
|
|
|
|
URLTag: "url",
|
|
|
|
|
MonitorPods: true,
|
|
|
|
|
PodScrapeScope: "node",
|
|
|
|
|
PodScrapeInterval: 60,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Both invalid IP addresses
|
2023-02-10 18:46:14 +08:00
|
|
|
t.Run("Both invalid IP addresses", func(t *testing.T) {
|
|
|
|
|
p.NodeIP = "10.240.0.0.0"
|
|
|
|
|
t.Setenv("NODE_IP", "10.000.0.0.0")
|
|
|
|
|
err := p.Init()
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
expectedMessage := "the node_ip config and the environment variable NODE_IP are not set or invalid; " +
|
|
|
|
|
"cannot get pod list for monitor_kubernetes_pods using node scrape scope"
|
|
|
|
|
require.Equal(t, expectedMessage, err.Error())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("Valid IP address", func(t *testing.T) {
|
|
|
|
|
t.Setenv("NODE_IP", "10.000.0.0")
|
|
|
|
|
|
|
|
|
|
p.KubernetesLabelSelector = "label0==label0, label0 in (=)"
|
|
|
|
|
err := p.Init()
|
|
|
|
|
expectedMessage := "error parsing the specified label selector(s): unable to parse requirement: found '=', expected: ',', ')' or identifier"
|
|
|
|
|
require.Error(t, err, expectedMessage)
|
|
|
|
|
p.KubernetesLabelSelector = "label0==label"
|
|
|
|
|
|
|
|
|
|
p.KubernetesFieldSelector = "field,"
|
|
|
|
|
err = p.Init()
|
|
|
|
|
expectedMessage = "error parsing the specified field selector(s): invalid selector: 'field,'; can't understand 'field'"
|
|
|
|
|
require.Error(t, err, expectedMessage)
|
|
|
|
|
|
|
|
|
|
p.KubernetesFieldSelector = "spec.containerNames=containerNames"
|
|
|
|
|
err = p.Init()
|
|
|
|
|
expectedMessage = "the field selector spec.containerNames is not supported for pods"
|
|
|
|
|
require.Error(t, err, expectedMessage)
|
|
|
|
|
})
|
2021-03-09 00:00:56 +08:00
|
|
|
}
|
2022-11-28 23:05:31 +08:00
|
|
|
|
|
|
|
|
func TestInitConfigSelectors(t *testing.T) {
|
|
|
|
|
p := &Prometheus{
|
|
|
|
|
MetricVersion: 2,
|
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
|
URLs: nil,
|
|
|
|
|
URLTag: "url",
|
|
|
|
|
MonitorPods: true,
|
|
|
|
|
MonitorKubernetesPodsMethod: MonitorMethodSettings,
|
|
|
|
|
PodScrapeInterval: 60,
|
|
|
|
|
KubernetesLabelSelector: "app=test",
|
|
|
|
|
KubernetesFieldSelector: "spec.nodeName=node-0",
|
|
|
|
|
}
|
|
|
|
|
err := p.Init()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
require.NotNil(t, p.podLabelSelector)
|
|
|
|
|
require.NotNil(t, p.podFieldSelector)
|
|
|
|
|
}
|