Skip to content

Conversation

devamanv
Copy link
Contributor

@devamanv devamanv commented Oct 14, 2025

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

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

Disruptive User Impact

Author's Checklist

  • [ ]

How to test this PR locally

Related issues

@devamanv devamanv self-assigned this Oct 14, 2025
@botelastic botelastic bot added the needs_team Indicates that the issue/PR needs a Team:* label label Oct 14, 2025
Copy link
Contributor

🤖 GitHub comments

Expand to view the GitHub comments

Just comment with:

  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)

Copy link
Contributor

mergify bot commented Oct 14, 2025

This pull request does not have a backport label.
If this is a bug or security fix, could you label this PR @devamanv? 🙏.
For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-8./d is the label to automatically backport to the 8./d branch. /d is the digit
  • backport-active-all is the label that automatically backports to all active branches.
  • backport-active-8 is the label that automatically backports to all active minor branches for the 8 major.
  • backport-active-9 is the label that automatically backports to all active minor branches for the 9 major.

@devamanv devamanv marked this pull request as ready for review October 14, 2025 12:56
@devamanv devamanv requested a review from a team as a code owner October 14, 2025 12:56
@pierrehilbert pierrehilbert added the Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team label Oct 14, 2025
@botelastic botelastic bot removed the needs_team Indicates that the issue/PR needs a Team:* label label Oct 14, 2025
@elasticmachine
Copy link
Collaborator

Pinging @elastic/elastic-agent-data-plane (Team:Elastic-Agent-Data-Plane)

@pierrehilbert pierrehilbert requested review from rdner and removed request for andrzej-stencel October 14, 2025 13:02
}
require.ElementsMatch(t, expected, result)
}

Copy link
Member

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.

Copy link
Contributor

@tommyers-elastic tommyers-elastic left a 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

@tommyers-elastic
Copy link
Contributor

tommyers-elastic commented Oct 14, 2025

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.

@ishleenk17
Copy link
Member

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 Content-Type: text/html; charset=utf-8 header. Although that would come into invalid type as per Prometheus header support. But surely we can add it.

@devamanv
Copy link
Contributor Author

devamanv commented Oct 15, 2025

@lalit-satapathy had some additional cases he identified that would make good test inputs.

@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},
Copy link
Member

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

@lalit-satapathy
Copy link
Contributor

Also please add this case:

Content-Type: text/plain; version=1.0.0; charset=utf-8; charset=utf-8

@devamanv
Copy link
Contributor Author

could someone from @elastic/elastic-agent-data-plane please review this as well?

Copy link
Member

@rdner rdner left a 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 {
Copy link
Member

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.

Copy link
Member

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:

https://github.com/prometheus/prometheus/blob/fd421dc3b1cf542108e1328623e38b745cbd3f49/model/textparse/interface.go#L115

https://github.com/prometheus/prometheus/blob/fd421dc3b1cf542108e1328623e38b745cbd3f49/model/textparse/interface.go#L164-L176

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.

Copy link
Contributor

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)

Copy link
Member

@ishleenk17 ishleenk17 Oct 15, 2025

Choose a reason for hiding this comment

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

@devamanv

+   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

Copy link
Member

@rdner rdner Oct 15, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants