Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(inputs.prometheus): Add option to limit body length #14661

Merged
merged 6 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions plugins/inputs/prometheus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## Default is 60 seconds.
# pod_scrape_interval = 60

## Content length limit
## When set, telegraf will drop responses with length larger than the
## configured value in bytes.
# content_length_limit = 0

## Restricts Kubernetes monitoring to a single namespace
## ex: monitor_kubernetes_pods_namespace = "default"
# monitor_kubernetes_pods_namespace = ""
Expand Down
26 changes: 22 additions & 4 deletions plugins/inputs/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ type Prometheus struct {

HTTPHeaders map[string]string `toml:"http_headers"`

ResponseTimeout config.Duration `toml:"response_timeout" deprecated:"1.26.0;use 'timeout' instead"`
ResponseTimeout config.Duration `toml:"response_timeout" deprecated:"1.26.0;use 'timeout' instead"`
ContentLengthLimit int64 `toml:"content_length_limit"`
srebhan marked this conversation as resolved.
Show resolved Hide resolved

MetricVersion int `toml:"metric_version"`

Expand Down Expand Up @@ -437,9 +438,26 @@ func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) error
return fmt.Errorf("%q returned HTTP status %q", u.URL, resp.Status)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error reading body: %w", err)
var body []byte
if p.ContentLengthLimit != 0 {
// To determine whether io.ReadAll() ended due to EOF or reached the specified limit,
// read up to the specified limit plus one extra byte, and then make a decision based
// on the length of the result.
lr := io.LimitReader(resp.Body, p.ContentLengthLimit+1)
srebhan marked this conversation as resolved.
Show resolved Hide resolved

body, err = io.ReadAll(lr)
if err != nil {
return fmt.Errorf("error reading body: %w", err)
}
if int64(len(body)) > p.ContentLengthLimit {
srebhan marked this conversation as resolved.
Show resolved Hide resolved
p.Log.Infof("skipping %s: content length exceeded maximum body size (%d)", u.URL, p.ContentLengthLimit)
return nil
}
} else {
body, err = io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error reading body: %w", err)
}
}

// Parse the metrics
Expand Down
21 changes: 21 additions & 0 deletions plugins/inputs/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,27 @@ func TestPrometheusGeneratesMetricsSlowEndpointHitTheTimeoutNewConfigParameter(t
require.ErrorContains(t, err, "error making HTTP request to \""+ts.URL+"/metrics\"")
}

func TestPrometheusContentLengthLimit(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",
ContentLengthLimit: 1,
}
err := p.Init()
require.NoError(t, err)
hhiroshell marked this conversation as resolved.
Show resolved Hide resolved

var acc testutil.Accumulator
require.NoError(t, p.Gather())
require.Empty(t, acc.Metrics)
}

func TestPrometheusGeneratesSummaryMetricsV2(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprintln(w, sampleSummaryTextFormat)
Expand Down
5 changes: 5 additions & 0 deletions plugins/inputs/prometheus/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
## Default is 60 seconds.
# pod_scrape_interval = 60

## Content length limit
## When set, telegraf will drop responses with length larger than the
## configured value in bytes.
# content_length_limit = 0
srebhan marked this conversation as resolved.
Show resolved Hide resolved

## Restricts Kubernetes monitoring to a single namespace
## ex: monitor_kubernetes_pods_namespace = "default"
# monitor_kubernetes_pods_namespace = ""
Expand Down
Loading