| 1 |
// Copyright 2025 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package zstd activates support for zstd compression. |
| 15 |
// To enable zstd compression support, import like this: |
| 16 |
// |
| 17 |
// import ( |
| 18 |
// _ "github.com/prometheus/client_golang/prometheus/promhttp/zstd" |
| 19 |
// ) |
| 20 |
// |
| 21 |
// This support is currently implemented via the github.com/klauspost/compress library, |
| 22 |
// so importing this package requires linking and building that library. |
| 23 |
// Once stdlib support is added to the Go standard library (https://github.com/golang/go/issues/62513), |
| 24 |
// this package is expected to become a no-op, and zstd support will be enabled by default. |
| 25 |
package zstd |
| 26 |
|
| 27 |
import ( |
| 28 |
"io" |
| 29 |
|
| 30 |
"github.com/klauspost/compress/zstd" |
| 31 |
|
| 32 |
"github.com/prometheus/client_golang/prometheus/promhttp/internal" |
| 33 |
) |
| 34 |
|
| 35 |
func init() { |
| 36 |
// Enable zstd support |
| 37 |
internal.NewZstdWriter = func(rw io.Writer) (_ io.Writer, closeWriter func(), _ error) { |
| 38 |
// TODO(mrueg): Replace klauspost/compress with stdlib implementation once https://github.com/golang/go/issues/62513 is implemented, and move this package to be a no-op / backfill for older go versions. |
| 39 |
z, err := zstd.NewWriter(rw, zstd.WithEncoderLevel(zstd.SpeedFastest)) |
| 40 |
if err != nil { |
| 41 |
return nil, func() {}, err |
| 42 |
} |
| 43 |
|
| 44 |
z.Reset(rw) |
| 45 |
return z, func() { _ = z.Close() }, nil |
| 46 |
} |
| 47 |
} |