-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Set OpenMetrics content header #1974
Conversation
This issue is currently awaiting triage. If kube-state-metrics contributors determine this is a relevant issue, they will accept it by applying the The Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
Can we really support OpenMetrics without changing the output of kube-state-metrics? The OpenMetrics format is slighly different from the Prometheus one that we currently hardcode |
@dgrisonnet good idea to check it again. https://www.robustperception.io/checking-openmetrics-output-is-valid/ I used the check above and added a Edit: |
b0b4b7b
to
47c7427
Compare
@@ -183,7 +183,7 @@ func (m *MetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |||
resHeader := w.Header() | |||
var writer io.Writer = w | |||
|
|||
resHeader.Set("Content-Type", `text/plain; version=`+"0.0.4") | |||
resHeader.Set("Content-Type", `application/openmetrics-text; version=`+"0.0.1"+`; charset=utf-8`) |
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.
I wonder if we should really do that by default. I would only expect us to modify the content type when an OpenMetrics
option is enabled. Otherwise we could break existing proxies only allowing text/plain
content type.
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.
moved it to the main thread, since this change is outdated.
I've changed the implementation to use the expfmt negotiation. You can test it with: curl -v --header "Accept:text/plain" Be aware that this can still generate invalid prometheus format (see #1973) in case the client requests application/text and info/stateset metrics are requested. This might be more difficult to solve as we would need to go through all metrics before writing them and replace the metric type based on the client's request. |
Once encountered some issues with |
I would like to avoid adding that if possible as the client already has a way to negotiate by presenting the txtFmt Accept Header. |
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.
Nice one. I was gonna suggest to use expfmt negociation but you already made that change.
We should add a simple e2e test as a follow-up.
/lgtm
/approve
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: dgrisonnet, mrueg The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
I just thought I'd mention this issue prometheus/prometheus#12121 as I think it may be related to this PR. |
Could you help test the issue will be gone without this PR? cc @Joseph-Irving |
Or could provide steps to reproduce this issue? Thanks! |
So I can reproduce this just on minikube with the standard helm prometheus, chart https://github.com/prometheus-community/helm-charts/tree/main/charts/prometheus I ran
This deploys kube-state-metrics version: This Prometheus fails to scrape Kube-state-metrics with the error: As I mentioned in the Prom issue prometheus/prometheus#12121 (comment) when this feature is turned on it changes the headers for Prometheus scrapes. |
So I think the issue, is that when you enable Native Histograms Prometheus is sending headers with a content type of:
Which we're then mirroring in our headers: resHeader.Set("Content-Type", string(contentType)) But we're not actually changing our response so we're replying with invalid protobuf hence the error from Prometheus. I did this as a simple workaround and it fixed the issue: if contentType == expfmt.FmtProtoDelim {
resHeader.Set("Content-Type", `text/plain; version=`+"0.0.4")
} else {
resHeader.Set("Content-Type", string(contentType))
} However I'm not sure this is the right approach, is it intended that kube-state-metrics always responds with content headers that match the request? Or was this just a change for Open Metrics? In which case perhaps the header should only get changed if the |
I can create a new issue for this if desired btw, sorry for hijacking this old PR |
Yes, could you create a new issue? Thx! |
Ok I've opened a new issue #2022 |
What this PR does / why we need it:
This change will expose metrics in openmetrics' format (basically just adding the right content header), as otherwise info and stateset type metrics won't be recognized by prometheus.
See: https://github.com/prometheus/common/blob/main/expfmt/encode.go#L86
See: #1973
How does this change affect the cardinality of KSM: (increases, decreases or does not change cardinality)
None
Which issue(s) this PR fixes (optional, in
fixes #<issue number>(, fixes #<issue_number>, ...)
format, will close the issue(s) when PR gets merged):Fixes #1973