Skip to content

Commit

Permalink
feat: Support zstd compression (prometheus#1496)
Browse files Browse the repository at this point in the history
* feat: Support zstd encoding

This allows endpoints to respond with zstd compressed metric data, if
the requester supports it.

I have imported a content-encoding parser from
https://github.com/golang/gddo which is an archived repository to
support different content-encoding headers.

Signed-off-by: Manuel Rüger <[email protected]>

* Update prometheus/promhttp/http.go

Co-authored-by: Bartlomiej Plotka <[email protected]>
Signed-off-by: Manuel Rüger <[email protected]>

* Update prometheus/promhttp/http.go

Co-authored-by: Bartlomiej Plotka <[email protected]>
Signed-off-by: Manuel Rüger <[email protected]>

* Update prometheus/promhttp/http.go

Co-authored-by: Bartlomiej Plotka <[email protected]>
Signed-off-by: Manuel Rüger <[email protected]>

* Integrate review comments

* String typed enum

Signed-off-by: Manuel Rüger <[email protected]>

* Test with gzip compression

Signed-off-by: Manuel Rüger <[email protected]>

* Update prometheus/promhttp/http.go

Co-authored-by: Bartlomiej Plotka <[email protected]>
Signed-off-by: Manuel Rüger <[email protected]>

* Reorder error handling

Signed-off-by: Manuel Rüger <[email protected]>

* Apply suggestions from code review

Co-authored-by: Bartlomiej Plotka <[email protected]>
Signed-off-by: Manuel Rüger <[email protected]>

* Include review suggestions

Signed-off-by: Manuel Rüger <[email protected]>

---------

Signed-off-by: Manuel Rüger <[email protected]>
Co-authored-by: Bartlomiej Plotka <[email protected]>
Signed-off-by: Steven Swartz <[email protected]>
  • Loading branch information
2 people authored and Steven Swartz committed Jun 29, 2024
1 parent 63819e2 commit 09746c5
Show file tree
Hide file tree
Showing 10 changed files with 697 additions and 29 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ go 1.20
require (
github.com/beorn7/perks v1.0.1
github.com/cespare/xxhash/v2 v2.3.0
github.com/google/go-cmp v0.6.0
github.com/json-iterator/go v1.1.12
github.com/klauspost/compress v1.17.8
github.com/prometheus/client_model v0.6.1
github.com/prometheus/common v0.53.0
github.com/prometheus/procfs v0.15.1
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down
27 changes: 27 additions & 0 deletions internal/github.com/golang/gddo/LICENSE
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.
1 change: 1 addition & 0 deletions internal/github.com/golang/gddo/README.md
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 internal/github.com/golang/gddo/httputil/header/header.go
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 internal/github.com/golang/gddo/httputil/header/header_test.go
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)
}
}
}
36 changes: 36 additions & 0 deletions internal/github.com/golang/gddo/httputil/negotiate.go
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 {
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 internal/github.com/golang/gddo/httputil/negotiate_test.go
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)
}
}
}
Loading

0 comments on commit 09746c5

Please sign in to comment.