-
Notifications
You must be signed in to change notification settings - Fork 1.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
Double compression when using v1.20 in combination with gorilla/mux CompressHandler #1595
Comments
Hey @Mattie112! I'm here to help you with any bugs, questions, or contributions you have. Let's tackle this issue together! The issue you're encountering with double compression when using In version 1.20.1, the Prometheus client library uses the Here's a relevant code snippet from the Prometheus client library: handler := InstrumentMetricHandler(reg, HandlerForTransactional(mReg, HandlerOpts{DisableCompression: false})) To avoid this double compression, you can either disable the compression in the Prometheus client library or avoid using
By implementing one of these solutions, you should be able to avoid the double compression issue and have the |
Just to confirm:
Has no effect for me, it is still double compressed. And the 2nd option for as far as I know is not possible. I can only add the ConpressHandler to the entire server. (cool AI service btw!) |
We're giving it a try, looks useful as a quick first responder :) It seems like desubot was going in the right direction. I tried to reproduce the problem and confirmed! I think we have a bug related to #1496, although not clear to me what the bug is at first glance. When compression is disabled, the client defaults to no compression[1][2]. |
I was able to spin up two webservers, one compressed and another without compression with the following snippet package main
import (
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
func main() {
// Create non-global registry.
reg := prometheus.NewRegistry()
// Add go runtime metrics and process collectors.
reg.MustRegister(
collectors.NewGoCollector(),
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
)
// Expose handlers in separate goroutine.
go func() {
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg, DisableCompression: true}))
log.Fatal(http.ListenAndServe(":8080", nil))
}()
go func() {
router := mux.NewRouter()
router.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}))
log.Fatal(http.ListenAndServe(":8081", handlers.CompressHandler(router)))
}()
select {}
} |
Wow, desubot just erased his second comment after I said it was not a good comment 🤯 |
Thanks for report and investigation!
I investigated a bit and it looks like the response is NOT double compressed, it works fine. Specifically gorilla/handlers#157 helps. The only problem is that with the new promhttp version we This works incorrectly with gorilla compression layer, which compresses to gzip, but does NOT update response content encoding, which results in:
..and then you see browser or clients weird behaviour. Removing
FixThis is actually our bug, not gorilla lib, because they cannot set headers AFTER response is being written (https://github.com/gorilla/handlers/blob/main/compress.go#L118). As a fix we need to:
@ArthurSens would like to do the fix? 🤗 |
Yeah, I mean the first post was pretty OK. It was one thing that I did not test yet (although I was quite certain that it was a bug somewhere). But I think it is a nice addition. I did see another response indeed (even though I did not tag the bot). But now I don't see it. Perhaps it took your negative response as an indicator to delete the post? That is a bit strange perhaps?
That indeed is an option but that would require me to make a code change to all my services (and we have quite a few) :)
Thank you for your thorough investigation / explanation. Looking forward for a fix. I am really happy that I don't need to make code-changes to all our microservices :) |
Thanks for the investigation @bwplotka and apologies for introducing an unintended bug here. Reading through RFC2616, it seems like we should not set
|
Fixes: prometheus#1595 Signed-off-by: Manuel Rüger <[email protected]>
Fixes: prometheus#1595 Signed-off-by: Manuel Rüger <[email protected]>
Hi there, Just to confirm: this new release fixed the issue for us. Many thanks for the quick response and fix! |
To start: I am not exactly sure if this issue belongs here, at https://github.com/gorilla/mux or at https://github.com/gorilla/handlers. I figured to start here as it possible other people are also approaching it from this angle.
The problem:
With v1.19 I could access the
/metrics
endpoint in my browser and it would work. Now with v1.20(.1) I get to see gzipped content.I have traced it back to the usage of using the mux CompressHandler. So why am I reporting it here then? Because it occurs after a new release here so perhaps the bug is somewhere else but someone here has a clue.
Minimal exxample:
main.go
In your
go.mod
:(Tested with go 1.21 and 1.23)
If you open the
/metrics
in your browser you will get gzipped output. If you then roll back to v1.19 it works as intended.I did found this (old) issue on the mux repo: gorilla/handlers#153 so it has been fixed in the past. Does anyone have a clue if the changes made could affect this somehow?
The text was updated successfully, but these errors were encountered: