diff --git a/prometheus/promhttp/http.go b/prometheus/promhttp/http.go index 2e0b9a8..e598e66 100644 --- a/prometheus/promhttp/http.go +++ b/prometheus/promhttp/http.go @@ -203,8 +203,10 @@ func HandlerForTransactional(reg prometheus.TransactionalGatherer, opts HandlerO defer closeWriter() - rsp.Header().Set(contentEncodingHeader, encodingHeader) - + // Set Content-Encoding only when data is compressed + if encodingHeader != string(Identity) { + rsp.Header().Set(contentEncodingHeader, encodingHeader) + } enc := expfmt.NewEncoder(w, contentType) // handleError handles the error according to opts.ErrorHandling diff --git a/prometheus/promhttp/http_test.go b/prometheus/promhttp/http_test.go index 0ed8fe3..3ad2d1d 100644 --- a/prometheus/promhttp/http_test.go +++ b/prometheus/promhttp/http_test.go @@ -267,7 +267,7 @@ func TestInstrumentMetricHandler(t *testing.T) { t.Errorf("got HTTP status code %d, want %d", got, want) } - if got, want := writer.Header().Get(contentEncodingHeader), string(Identity); got != want { + if got, want := writer.Header().Get(contentEncodingHeader), ""; got != want { t.Errorf("got HTTP content encoding header %s, want %s", got, want) }