@@ -41,11 +41,11 @@ import (
4141 "sync"
4242 "time"
4343
44- "github.com/klauspost/compress/zstd"
4544 "github.com/prometheus/common/expfmt"
4645
4746 "github.com/prometheus/client_golang/internal/github.com/golang/gddo/httputil"
4847 "github.com/prometheus/client_golang/prometheus"
48+ "github.com/prometheus/client_golang/prometheus/promhttp/internal"
4949)
5050
5151const (
@@ -65,7 +65,13 @@ const (
6565 Zstd Compression = "zstd"
6666)
6767
68- var defaultCompressionFormats = []Compression {Identity , Gzip , Zstd }
68+ func defaultCompressionFormats () []Compression {
69+ if internal .NewZstdWriter != nil {
70+ return []Compression {Identity , Gzip , Zstd }
71+ } else {
72+ return []Compression {Identity , Gzip }
73+ }
74+ }
6975
7076var gzipPool = sync.Pool {
7177 New : func () interface {} {
@@ -138,7 +144,7 @@ func HandlerForTransactional(reg prometheus.TransactionalGatherer, opts HandlerO
138144 // Select compression formats to offer based on default or user choice.
139145 var compressions []string
140146 if ! opts .DisableCompression {
141- offers := defaultCompressionFormats
147+ offers := defaultCompressionFormats ()
142148 if len (opts .OfferedCompressions ) > 0 {
143149 offers = opts .OfferedCompressions
144150 }
@@ -466,14 +472,12 @@ func negotiateEncodingWriter(r *http.Request, rw io.Writer, compressions []strin
466472
467473 switch selected {
468474 case "zstd" :
469- // TODO(mrueg): Replace klauspost/compress with stdlib implementation once https://github.com/golang/go/issues/62513 is implemented.
470- z , err := zstd .NewWriter (rw , zstd .WithEncoderLevel (zstd .SpeedFastest ))
471- if err != nil {
472- return nil , "" , func () {}, err
475+ if internal .NewZstdWriter == nil {
476+ // The content encoding was not implemented yet.
477+ return nil , "" , func () {}, fmt .Errorf ("content compression format not recognized: %s. Valid formats are: %s" , selected , defaultCompressionFormats ())
473478 }
474-
475- z .Reset (rw )
476- return z , selected , func () { _ = z .Close () }, nil
479+ writer , closeWriter , err := internal .NewZstdWriter (rw )
480+ return writer , selected , closeWriter , err
477481 case "gzip" :
478482 gz := gzipPool .Get ().(* gzip.Writer )
479483 gz .Reset (rw )
@@ -483,6 +487,6 @@ func negotiateEncodingWriter(r *http.Request, rw io.Writer, compressions []strin
483487 return rw , selected , func () {}, nil
484488 default :
485489 // The content encoding was not implemented yet.
486- return nil , "" , func () {}, fmt .Errorf ("content compression format not recognized: %s. Valid formats are: %s" , selected , defaultCompressionFormats )
490+ return nil , "" , func () {}, fmt .Errorf ("content compression format not recognized: %s. Valid formats are: %s" , selected , defaultCompressionFormats () )
487491 }
488492}
0 commit comments