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

Add fail_if_body_too_large http probe option #1276

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

hashworks
Copy link

This can be used in conjunction with body_size_limit when it is expected that endpoints return a body larger than the specified limit, e.g. streaming endpoints that potentially return infinite data.

@hashworks hashworks force-pushed the feature/fail_if_body_too_large branch from 81b9317 to 1e0ff2b Compare August 6, 2024 09:40
@@ -550,7 +550,7 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr

if !requestErrored {
_, err = io.Copy(io.Discard, byteCounter)
if err != nil {
if err != nil && (errors.Is(err, &http.MaxBytesError{}) || httpConfig.GetFailIfBodyTooLarge()) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check if this is okay or if I should move this to another if so it's clearer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer another if for clarity

@@ -450,6 +451,13 @@ func (s *HeaderMatch) UnmarshalYAML(unmarshal func(interface{}) error) error {
return nil
}

func (h HTTPProbe) GetFailIfBodyTooLarge() bool {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively I could do the nil check in UnmarshalYAML, but then it could panic if someone changes the variable under the hood later on.

Copy link
Member

@electron0zero electron0zero Dec 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To preserve the current behaviour, FailIfBodyTooLarge should be set to true by default.

you should be able to set it to true by default in DefaultHTTPProbe at https://github.com/prometheus/blackbox_exporter/blob/master/config/config.go#L51

if we make this bool instead of *bool, we do't have to worry about nil, and can remove this whole function.

This can be used in conjunction with `body_size_limit` when it is
expected that endpoints return a body larger than the specified limit,
e.g. streaming endpoints that potentially return infinite data.

Signed-off-by: Justin Kromlinger <[email protected]>
@hashworks hashworks force-pushed the feature/fail_if_body_too_large branch from 1e0ff2b to 2d225ab Compare November 1, 2024 13:36
@hashworks
Copy link
Author

Do you want any changes here? Or is this a feature you don't want merged?

@hashworks hashworks force-pushed the feature/fail_if_body_too_large branch from 2a3f154 to 1512b95 Compare November 1, 2024 14:03
@electron0zero
Copy link
Member

can you share more on the use-case? and what problem this flag will solve?

once I better understand the "why?" behind the change, I will find some time to review.

Signed-off-by: Justin Kromlinger <[email protected]>
@hashworks
Copy link
Author

hashworks commented Dec 31, 2024

can you share more on the use-case? and what problem this flag will solve?

At work, we need to probe endpoints that endlessly stream data. Without this flag every probe will fail after some time, since Blackbox Exporter expects the data stream to finish otherwise. We want to measure successful requests and "time to X bytes".

}))
defer ts.Close()

for _, failIfBodyTooLarge := range []bool{true, false} {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we turn into a proper table test with config, and the expectation, see TestFailIfNotSSLLogMsg for reference

@@ -211,6 +211,7 @@ type HTTPProbe struct {
NoFollowRedirects *bool `yaml:"no_follow_redirects,omitempty"`
FailIfSSL bool `yaml:"fail_if_ssl,omitempty"`
FailIfNotSSL bool `yaml:"fail_if_not_ssl,omitempty"`
FailIfBodyTooLarge *bool `yaml:"fail_if_body_too_large,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to use *bool here instead of just bool like FailIfNotSSL??

@@ -450,6 +451,13 @@ func (s *HeaderMatch) UnmarshalYAML(unmarshal func(interface{}) error) error {
return nil
}

func (h HTTPProbe) GetFailIfBodyTooLarge() bool {
Copy link
Member

@electron0zero electron0zero Dec 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To preserve the current behaviour, FailIfBodyTooLarge should be set to true by default.

you should be able to set it to true by default in DefaultHTTPProbe at https://github.com/prometheus/blackbox_exporter/blob/master/config/config.go#L51

if we make this bool instead of *bool, we do't have to worry about nil, and can remove this whole function.

@@ -550,7 +550,7 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr

if !requestErrored {
_, err = io.Copy(io.Discard, byteCounter)
if err != nil {
if err != nil && (errors.Is(err, &http.MaxBytesError{}) || httpConfig.GetFailIfBodyTooLarge()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer another if for clarity

@electron0zero
Copy link
Member

please rebase to fix the failing CI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants