-
Notifications
You must be signed in to change notification settings - Fork 5k
[prometheus] Update the error check to handle invalid and empty content header #47085
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
base: main
Are you sure you want to change the base?
Conversation
🤖 GitHub commentsExpand to view the GitHub comments
Just comment with:
|
This pull request does not have a backport label.
To fixup this pull request, you need to add the backport labels for the needed
|
Pinging @elastic/elastic-agent-data-plane (Team:Elastic-Agent-Data-Plane) |
} | ||
require.ElementsMatch(t, expected, result) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have UT's for no header, invalid header now.
Lets have one for valid header too.
Just to have all the coverage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM thanks. we are missing the changelog though
and i agree with ishleen on the unit tests - let's add as many test cases as we can think of @lalit-satapathy had some additional cases he identified that would make good test inputs. |
Yeah, Lalit had one for |
@tommyers-elastic I have added tests for these additional cases here. Could you please review it again? |
{"html", "text/html; charset=utf-8", false}, | ||
{"empty_content_type", "", false}, | ||
{"openmetrics", "application/openmetrics-text", false}, | ||
{"octet_stream", "application/octet-stream", false}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets add one for protobuf too: Should behave the same way
Content-Type: application/vnd.google.protobuf
Also please add this case:
|
could someone from @elastic/elastic-agent-data-plane please review this as well? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One question to clarify.
// This check allows to continue where the content type is blank but the parser is non-nil. Returns error on all other cases. | ||
if err != nil && !strings.Contains(err.Error(), "non-compliant scrape target sending blank Content-Type, using fallback_scrape_protocol") { | ||
// This check allows to continue where the content type is blank/invalid but the parser is non-nil. Returns error on all other cases. | ||
if parser == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How confident are we that textparse.New
will always return err != nil
when parser == nil
?
Looks risky to me, I assume that consumers of this function would not expect return nil, nil
from here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it all relies on the fact that these 2 functions are in sync:
I think it's totally possible that at some point someone adds a new content type support to the extractMediaType
and then forgets to update the switch in the New
function and then the default
switch case returns nil, nil
.
In this case we also return nil, nil
from this condition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a weird quirk of this function that it returns valid parsers when err != nil.
there's not really a great way to handle it here. since we know valid parsers get returned with errors, we can't rely on the error to gate if we should fail. the only way is to check the parser.
i think what's here is the best we can do. if the function ends up returning nil, nil
, it's still an error condition for us since there's no parser.
maybe we wrap the error that we return here to clarify this?
return nil, fmt.Errorf("nil parser returned from textparse.New: %w", err)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+ if parser == nil {
+ if err != nil {
+ return nil, err
+ }
+ return nil, fmt.Errorf("no parser returned for contentType %q (err = %v)", contentType, err)
+ }
Lets add this patch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return nil, fmt.Errorf("no parser returned for contentType %q (err = %v)", contentType, err)
@ishleenk17 err
is always nil
on this line. No need to include it into the string.
Proposed commit message
The PR contains changes to improve the handling the issue that would cause Metricbeat Prometheus module to not fetch any metrics in case of invalid and empty content type header.
Note: Previously it was relying on a particular upstream change. This replaces it with better handling.
Checklist
CHANGELOG.next.asciidoc
orCHANGELOG-developer.next.asciidoc
.Disruptive User Impact
Author's Checklist
How to test this PR locally
Related issues