Path: blob/main/pkg/metrics/instance/configstore/codec.go
5317 views
package configstore12import (3"bytes"4"compress/gzip"5"fmt"6"io"7"strings"89"github.com/grafana/dskit/kv/codec"10)1112// GetCodec returns the codec for encoding and decoding instance.Configs13// in the Remote store.14func GetCodec() codec.Codec {15return &yamlCodec{}16}1718type yamlCodec struct{}1920func (*yamlCodec) Decode(bb []byte) (interface{}, error) {21// Decode is called by kv.Clients with an empty slice when a22// key is deleted. We should stop early here and don't return23// an error so the deletion event propagates to watchers.24if len(bb) == 0 {25return nil, nil26}2728r, err := gzip.NewReader(bytes.NewReader(bb))29if err != nil {30return nil, err31}3233var sb strings.Builder34if _, err := io.Copy(&sb, r); err != nil {35return nil, err36}37return sb.String(), nil38}3940func (*yamlCodec) Encode(v interface{}) ([]byte, error) {41var buf bytes.Buffer4243var cfg string4445switch v := v.(type) {46case string:47cfg = v48default:49panic(fmt.Sprintf("unexpected type %T passed to yamlCodec.Encode", v))50}5152w := gzip.NewWriter(&buf)5354if _, err := io.Copy(w, strings.NewReader(cfg)); err != nil {55return nil, err56}5758w.Close()59return buf.Bytes(), nil60}6162func (*yamlCodec) CodecID() string {63return "agentConfig/yaml"64}656667