Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/metrics/instance/configstore/codec.go
5317 views
1
package configstore
2
3
import (
4
"bytes"
5
"compress/gzip"
6
"fmt"
7
"io"
8
"strings"
9
10
"github.com/grafana/dskit/kv/codec"
11
)
12
13
// GetCodec returns the codec for encoding and decoding instance.Configs
14
// in the Remote store.
15
func GetCodec() codec.Codec {
16
return &yamlCodec{}
17
}
18
19
type yamlCodec struct{}
20
21
func (*yamlCodec) Decode(bb []byte) (interface{}, error) {
22
// Decode is called by kv.Clients with an empty slice when a
23
// key is deleted. We should stop early here and don't return
24
// an error so the deletion event propagates to watchers.
25
if len(bb) == 0 {
26
return nil, nil
27
}
28
29
r, err := gzip.NewReader(bytes.NewReader(bb))
30
if err != nil {
31
return nil, err
32
}
33
34
var sb strings.Builder
35
if _, err := io.Copy(&sb, r); err != nil {
36
return nil, err
37
}
38
return sb.String(), nil
39
}
40
41
func (*yamlCodec) Encode(v interface{}) ([]byte, error) {
42
var buf bytes.Buffer
43
44
var cfg string
45
46
switch v := v.(type) {
47
case string:
48
cfg = v
49
default:
50
panic(fmt.Sprintf("unexpected type %T passed to yamlCodec.Encode", v))
51
}
52
53
w := gzip.NewWriter(&buf)
54
55
if _, err := io.Copy(w, strings.NewReader(cfg)); err != nil {
56
return nil, err
57
}
58
59
w.Close()
60
return buf.Bytes(), nil
61
}
62
63
func (*yamlCodec) CodecID() string {
64
return "agentConfig/yaml"
65
}
66
67