github.com/letsencrypt/boulder@v0.20251208.0/observer/mon_conf.go (about) 1 package observer 2 3 import ( 4 "errors" 5 "time" 6 7 "github.com/prometheus/client_golang/prometheus" 8 "gopkg.in/yaml.v3" 9 10 "github.com/letsencrypt/boulder/config" 11 "github.com/letsencrypt/boulder/observer/probers" 12 ) 13 14 // MonConf is exported to receive YAML configuration in `ObsConf`. 15 type MonConf struct { 16 Period config.Duration `yaml:"period"` 17 Kind string `yaml:"kind" validate:"required,oneof=DNS HTTP CRL TLS TCP"` 18 Settings probers.Settings `yaml:"settings" validate:"min=1,dive"` 19 } 20 21 // validatePeriod ensures the received `Period` field is at least 1µs. 22 func (c *MonConf) validatePeriod() error { 23 if c.Period.Duration < 1*time.Microsecond { 24 return errors.New("period must be at least 1µs") 25 } 26 return nil 27 } 28 29 // unmarshalConfigurer constructs a `Configurer` by marshaling the 30 // value of the `Settings` field back to bytes, then passing it to the 31 // `UnmarshalSettings` method of the `Configurer` type specified by the 32 // `Kind` field. 33 func (c MonConf) unmarshalConfigurer() (probers.Configurer, error) { 34 configurer, err := probers.GetConfigurer(c.Kind) 35 if err != nil { 36 return nil, err 37 } 38 settings, _ := yaml.Marshal(c.Settings) 39 configurer, err = configurer.UnmarshalSettings(settings) 40 if err != nil { 41 return nil, err 42 } 43 return configurer, nil 44 } 45 46 // makeMonitor constructs a `monitor` object from the contents of the 47 // bound `MonConf`. If the `MonConf` cannot be validated, an error 48 // appropriate for end-user consumption is returned instead. 49 func (c MonConf) makeMonitor(collectors map[string]prometheus.Collector) (*monitor, error) { 50 err := c.validatePeriod() 51 if err != nil { 52 return nil, err 53 } 54 probeConf, err := c.unmarshalConfigurer() 55 if err != nil { 56 return nil, err 57 } 58 prober, err := probeConf.MakeProber(collectors) 59 if err != nil { 60 return nil, err 61 } 62 return &monitor{c.Period.Duration, prober}, nil 63 }