-
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
feat: Support zstd compression #1496
Merged
+697
−29
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5f5f396
feat: Support zstd encoding
mrueg 6ad6d34
Update prometheus/promhttp/http.go
mrueg b0aadb6
Update prometheus/promhttp/http.go
mrueg 07c2ea2
Update prometheus/promhttp/http.go
mrueg bf0f3dc
Integrate review comments
mrueg c40f12a
Test with gzip compression
mrueg f0c8656
Update prometheus/promhttp/http.go
mrueg 9fea575
Reorder error handling
mrueg 140db7a
Apply suggestions from code review
mrueg 00ce2e8
Include review suggestions
mrueg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Copyright (c) 2013 The Go Authors. All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are | ||
met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following disclaimer | ||
in the documentation and/or other materials provided with the | ||
distribution. | ||
* Neither the name of Google Inc. nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This source code is a stripped down version from the archived repository https://github.com/golang/gddo and licensed under BSD. |
145 changes: 145 additions & 0 deletions
145
internal/github.com/golang/gddo/httputil/header/header.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// Copyright 2013 The Go Authors. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file or at | ||
// https://developers.google.com/open-source/licenses/bsd. | ||
|
||
// Package header provides functions for parsing HTTP headers. | ||
package header | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// Octet types from RFC 2616. | ||
var octetTypes [256]octetType | ||
|
||
type octetType byte | ||
|
||
const ( | ||
isToken octetType = 1 << iota | ||
isSpace | ||
) | ||
|
||
func init() { | ||
// OCTET = <any 8-bit sequence of data> | ||
// CHAR = <any US-ASCII character (octets 0 - 127)> | ||
// CTL = <any US-ASCII control character (octets 0 - 31) and DEL (127)> | ||
// CR = <US-ASCII CR, carriage return (13)> | ||
// LF = <US-ASCII LF, linefeed (10)> | ||
// SP = <US-ASCII SP, space (32)> | ||
// HT = <US-ASCII HT, horizontal-tab (9)> | ||
// <"> = <US-ASCII double-quote mark (34)> | ||
// CRLF = CR LF | ||
// LWS = [CRLF] 1*( SP | HT ) | ||
// TEXT = <any OCTET except CTLs, but including LWS> | ||
// separators = "(" | ")" | "<" | ">" | "@" | "," | ";" | ":" | "\" | <"> | ||
// | "/" | "[" | "]" | "?" | "=" | "{" | "}" | SP | HT | ||
// token = 1*<any CHAR except CTLs or separators> | ||
// qdtext = <any TEXT except <">> | ||
|
||
for c := 0; c < 256; c++ { | ||
var t octetType | ||
isCtl := c <= 31 || c == 127 | ||
isChar := 0 <= c && c <= 127 | ||
isSeparator := strings.ContainsRune(" \t\"(),/:;<=>?@[]\\{}", rune(c)) | ||
if strings.ContainsRune(" \t\r\n", rune(c)) { | ||
t |= isSpace | ||
} | ||
if isChar && !isCtl && !isSeparator { | ||
t |= isToken | ||
} | ||
octetTypes[c] = t | ||
} | ||
} | ||
|
||
// AcceptSpec describes an Accept* header. | ||
type AcceptSpec struct { | ||
Value string | ||
Q float64 | ||
} | ||
|
||
// ParseAccept parses Accept* headers. | ||
func ParseAccept(header http.Header, key string) (specs []AcceptSpec) { | ||
loop: | ||
for _, s := range header[key] { | ||
for { | ||
var spec AcceptSpec | ||
spec.Value, s = expectTokenSlash(s) | ||
if spec.Value == "" { | ||
continue loop | ||
} | ||
spec.Q = 1.0 | ||
s = skipSpace(s) | ||
if strings.HasPrefix(s, ";") { | ||
s = skipSpace(s[1:]) | ||
if !strings.HasPrefix(s, "q=") { | ||
continue loop | ||
} | ||
spec.Q, s = expectQuality(s[2:]) | ||
if spec.Q < 0.0 { | ||
continue loop | ||
} | ||
} | ||
specs = append(specs, spec) | ||
s = skipSpace(s) | ||
if !strings.HasPrefix(s, ",") { | ||
continue loop | ||
} | ||
s = skipSpace(s[1:]) | ||
} | ||
} | ||
return | ||
} | ||
|
||
func skipSpace(s string) (rest string) { | ||
i := 0 | ||
for ; i < len(s); i++ { | ||
if octetTypes[s[i]]&isSpace == 0 { | ||
break | ||
} | ||
} | ||
return s[i:] | ||
} | ||
|
||
func expectTokenSlash(s string) (token, rest string) { | ||
i := 0 | ||
for ; i < len(s); i++ { | ||
b := s[i] | ||
if (octetTypes[b]&isToken == 0) && b != '/' { | ||
break | ||
} | ||
} | ||
return s[:i], s[i:] | ||
} | ||
|
||
func expectQuality(s string) (q float64, rest string) { | ||
switch { | ||
case len(s) == 0: | ||
return -1, "" | ||
case s[0] == '0': | ||
q = 0 | ||
case s[0] == '1': | ||
q = 1 | ||
default: | ||
return -1, "" | ||
} | ||
s = s[1:] | ||
if !strings.HasPrefix(s, ".") { | ||
return q, s | ||
} | ||
s = s[1:] | ||
i := 0 | ||
n := 0 | ||
d := 1 | ||
for ; i < len(s); i++ { | ||
b := s[i] | ||
if b < '0' || b > '9' { | ||
break | ||
} | ||
n = n*10 + int(b) - '0' | ||
d *= 10 | ||
} | ||
return q + float64(n)/float64(d), s[i:] | ||
} |
49 changes: 49 additions & 0 deletions
49
internal/github.com/golang/gddo/httputil/header/header_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2013 The Go Authors. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file or at | ||
// https://developers.google.com/open-source/licenses/bsd. | ||
|
||
package header | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
var parseAcceptTests = []struct { | ||
s string | ||
expected []AcceptSpec | ||
}{ | ||
{"text/html", []AcceptSpec{{"text/html", 1}}}, | ||
{"text/html; q=0", []AcceptSpec{{"text/html", 0}}}, | ||
{"text/html; q=0.0", []AcceptSpec{{"text/html", 0}}}, | ||
{"text/html; q=1", []AcceptSpec{{"text/html", 1}}}, | ||
{"text/html; q=1.0", []AcceptSpec{{"text/html", 1}}}, | ||
{"text/html; q=0.1", []AcceptSpec{{"text/html", 0.1}}}, | ||
{"text/html;q=0.1", []AcceptSpec{{"text/html", 0.1}}}, | ||
{"text/html, text/plain", []AcceptSpec{{"text/html", 1}, {"text/plain", 1}}}, | ||
{"text/html; q=0.1, text/plain", []AcceptSpec{{"text/html", 0.1}, {"text/plain", 1}}}, | ||
{"iso-8859-5, unicode-1-1;q=0.8,iso-8859-1", []AcceptSpec{{"iso-8859-5", 1}, {"unicode-1-1", 0.8}, {"iso-8859-1", 1}}}, | ||
{"iso-8859-1", []AcceptSpec{{"iso-8859-1", 1}}}, | ||
{"*", []AcceptSpec{{"*", 1}}}, | ||
{"da, en-gb;q=0.8, en;q=0.7", []AcceptSpec{{"da", 1}, {"en-gb", 0.8}, {"en", 0.7}}}, | ||
{"da, q, en-gb;q=0.8", []AcceptSpec{{"da", 1}, {"q", 1}, {"en-gb", 0.8}}}, | ||
{"image/png, image/*;q=0.5", []AcceptSpec{{"image/png", 1}, {"image/*", 0.5}}}, | ||
|
||
// bad cases | ||
{"value1; q=0.1.2", []AcceptSpec{{"value1", 0.1}}}, | ||
{"da, en-gb;q=foo", []AcceptSpec{{"da", 1}}}, | ||
} | ||
|
||
func TestParseAccept(t *testing.T) { | ||
for _, tt := range parseAcceptTests { | ||
header := http.Header{"Accept": {tt.s}} | ||
actual := ParseAccept(header, "Accept") | ||
if !cmp.Equal(actual, tt.expected) { | ||
t.Errorf("ParseAccept(h, %q)=%v, want %v", tt.s, actual, tt.expected) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2013 The Go Authors. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file or at | ||
// https://developers.google.com/open-source/licenses/bsd. | ||
|
||
package httputil | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/prometheus/client_golang/internal/github.com/golang/gddo/httputil/header" | ||
) | ||
|
||
// NegotiateContentEncoding returns the best offered content encoding for the | ||
// request's Accept-Encoding header. If two offers match with equal weight and | ||
// then the offer earlier in the list is preferred. If no offers are | ||
// acceptable, then "" is returned. | ||
func NegotiateContentEncoding(r *http.Request, offers []string) string { | ||
bwplotka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bestOffer := "identity" | ||
bestQ := -1.0 | ||
specs := header.ParseAccept(r.Header, "Accept-Encoding") | ||
for _, offer := range offers { | ||
for _, spec := range specs { | ||
if spec.Q > bestQ && | ||
(spec.Value == "*" || spec.Value == offer) { | ||
bestQ = spec.Q | ||
bestOffer = offer | ||
} | ||
} | ||
} | ||
if bestQ == 0 { | ||
bestOffer = "" | ||
} | ||
return bestOffer | ||
} |
40 changes: 40 additions & 0 deletions
40
internal/github.com/golang/gddo/httputil/negotiate_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2013 The Go Authors. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file or at | ||
// https://developers.google.com/open-source/licenses/bsd. | ||
|
||
package httputil | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
var negotiateContentEncodingTests = []struct { | ||
s string | ||
offers []string | ||
expect string | ||
}{ | ||
{"", []string{"identity", "gzip"}, "identity"}, | ||
{"*;q=0", []string{"identity", "gzip"}, ""}, | ||
{"gzip", []string{"identity", "gzip"}, "gzip"}, | ||
{"gzip,zstd", []string{"identity", "zstd"}, "zstd"}, | ||
{"zstd,gzip", []string{"gzip", "zstd"}, "gzip"}, | ||
{"gzip,zstd", []string{"gzip", "zstd"}, "gzip"}, | ||
{"gzip,zstd", []string{"zstd", "gzip"}, "zstd"}, | ||
{"gzip;q=0.1,zstd;q=0.5", []string{"gzip", "zstd"}, "zstd"}, | ||
{"gzip;q=1.0, identity; q=0.5, *;q=0", []string{"identity", "gzip"}, "gzip"}, | ||
{"gzip;q=1.0, identity; q=0.5, *;q=0", []string{"identity", "zstd"}, "identity"}, | ||
{"zstd", []string{"identity", "gzip"}, "identity"}, | ||
} | ||
|
||
func TestNegotiateContentEncoding(t *testing.T) { | ||
for _, tt := range negotiateContentEncodingTests { | ||
r := &http.Request{Header: http.Header{"Accept-Encoding": {tt.s}}} | ||
actual := NegotiateContentEncoding(r, tt.offers) | ||
if actual != tt.expect { | ||
t.Errorf("NegotiateContentEncoding(%q, %#v)=%q, want %q", tt.s, tt.offers, actual, tt.expect) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Came here from the release page. Most people don't care about zstd, but now every user will have one more dependency. Would be better to wait for golang/go#62513 and avoid extra dependencies.
Why not let stdlib's http package handle compression negotiation? It's kind of out of scope of this library (from my POV at least).
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.
It's fair point we could outsource compression + negotiation to separate libs, but we wanted default client_golang without extra deps to handle that automatically for everyone. That's some choice which was already made before by adding
gzip
.Fair point, hopefully we can remove it once zstd is in std. Luckly, this dep is trusted, proven and very minimal https://github.com/klauspost/compress/blob/master/go.mod - we could vendor that code, but we chosen to add one little dep -- we are open on feedback on how blocking this is for our users.
Is there std http flow for compression negotiation? I don't think so.
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.
Yes, it turns out it doesn't handle that (I didn't know). My point is that there are many things that can be improved and done that will improve how this library works (bigger picture). But this doesn't mean they should be part of this library. Imagine every library that provides e.g. http handlers also tried to take care of compression, compression negotiation, doing http/3, etc, etc, etc. Dependency sprawl quickly gets out of hand, things work differently, become incompatible. I think unix way is a better approach. Provide solution for one problem, limit the scope, reduce dependencies. But also make it possible for your consumers to solve those problems somehow (composition!). With other libraries or with stdlib or with custom code.
I wonder if it's possible to have zstd compression without this PR? Can I do it today with a previous version of this library? It seems it should be possible to have an http handler that wraps the handler of this library (or any other one) and implements compression. Perhaps this should be a separate generic library that anyone could use, contribute too. I found the chi middleware that does gzip compression. Sould be possible to have a zstd middleware? And then anyone who wants it can use it for all/any http handlers they have, not just for the metrics endpoint.
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.
May I ask what is the motivation for this? How many users of this library have asked for this feature?
If something can be done (even if easily), doesn't mean it should be done. My gut feeling is that it is cheaper (cpu/ram/network resources) to pass uncompressed metrics to a scraper that is running in the same local network (99% of people do this?). If things are running on Kubernetes, the scraper is quite likely on the same host (as a DaemonSet)! So it's "free", virtually/almost no network cost. In that case compression is a pure waste of CPU time. Shall we add some code that detects this situation and optimizes the compression away? 😁 Why not, just a little dependency on the Kubernetes client? Let's make the library handle this case too! Ok, I hope you see my point.
p.s. I'm very grateful to people who contribute and maintain great libraries like this one. Please don't take this personally.