echo-contrib icon indicating copy to clipboard operation
echo-contrib copied to clipboard

Compression of prometheus metrics without Transfer-Encoding header

Open abh opened this issue 4 years ago • 3 comments

When using the prometheus middleware AND the Gzip middleware I get a compressed response for /metrics without the Transfer-Encoding header.

abh avatar Nov 10 '21 06:11 abh

oh, wait -- there's a Content-Encoding header (but all the same, curl doesn't seem to recognize that the content is compressed)

abh avatar Nov 10 '21 06:11 abh

Does your request contains necessary headers? Compression is applied only when there is Accept-Encoding present

https://github.com/labstack/echo/blob/d604704563de63e42a352ffc51b6d633d9d595e3/middleware/compress.go#L72

aldas avatar Nov 10 '21 06:11 aldas

I had a similar issue where when using both gzip and prometheus I got a gzipped response on the /metrics endpoint, the solution that worked for me was that I added a skipper to the gzip handler.

return middleware.GzipWithConfig(middleware.GzipConfig{
    Level: 5,
    Skipper: func(c echo.Context) bool {
        return strings.HasPrefix(c.Path(), "/metrics")
    },
})

martonkaufmann avatar Jan 17 '22 12:01 martonkaufmann

This should probably be closed.

At least I can not recreate it.

package main

import (
	"errors"
	"github.com/labstack/echo-contrib/echoprometheus"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"log"
	"net/http"
)

func main() {
	e := echo.New()

	e.Use(middleware.Gzip())
	e.Use(echoprometheus.NewMiddleware("myapp"))

	e.GET("/metrics", echoprometheus.NewHandler())

	if err := e.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {
		log.Fatal(err)
	}
}

output:

x@x:~/code$ curl -v http://localhost:8080/metrics
*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /metrics HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.85.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: text/plain; version=0.0.4; charset=utf-8
< Vary: Accept-Encoding
< Date: Tue, 23 May 2023 20:45:50 GMT
< Transfer-Encoding: chunked
< 
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
...
...

seems to work with curl

aldas avatar May 23 '23 20:05 aldas

This should probably be closed. ... At least I can not recreate it.

I think if you used something like curl -v -s -H 'Accept-Encoding: gzip,deflate' http://localhost:8080/metrics you will see the issue

gabrie30 avatar Oct 26 '23 21:10 gabrie30

I think this was done with https://github.com/labstack/echo-contrib/pull/97

aldas avatar Oct 27 '23 06:10 aldas