Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/metrics/instance/marshal.go
4094 views
1
package instance
2
3
import (
4
"bytes"
5
"io"
6
7
config_util "github.com/prometheus/common/config"
8
"gopkg.in/yaml.v2"
9
)
10
11
// UnmarshalConfig unmarshals an instance config from a reader based on a
12
// provided content type.
13
func UnmarshalConfig(r io.Reader) (*Config, error) {
14
var cfg Config
15
dec := yaml.NewDecoder(r)
16
dec.SetStrict(true)
17
err := dec.Decode(&cfg)
18
return &cfg, err
19
}
20
21
// MarshalConfig marshals an instance config based on a provided content type.
22
func MarshalConfig(c *Config, scrubSecrets bool) ([]byte, error) {
23
var buf bytes.Buffer
24
err := MarshalConfigToWriter(c, &buf, scrubSecrets)
25
return buf.Bytes(), err
26
}
27
28
// MarshalConfigToWriter marshals a config to an io.Writer.
29
func MarshalConfigToWriter(c *Config, w io.Writer, scrubSecrets bool) error {
30
enc := yaml.NewEncoder(w)
31
32
// If we're not sanitizing the marshaled config, we want to add in an
33
// encoding hook to ignore how Secrets marshal (i.e., scrubbing the value
34
// and replacing it with <secret>).
35
if !scrubSecrets {
36
enc.SetHook(func(in interface{}) (ok bool, out interface{}, err error) {
37
switch v := in.(type) {
38
case config_util.Secret:
39
return true, string(v), nil
40
case *config_util.URL:
41
return true, v.String(), nil
42
default:
43
return false, nil, nil
44
}
45
})
46
}
47
48
type plain Config
49
return enc.Encode((*plain)(c))
50
}
51
52