-
-
Notifications
You must be signed in to change notification settings - Fork 588
/
prober.go
76 lines (62 loc) · 2.25 KB
/
prober.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package observer
import (
"fmt"
"time"
"github.com/letsencrypt/boulder/cmd"
)
var (
// Registry is the global mapping of all `Configurer` types. Types
// are added to this mapping on import by including a call to
// `Register` in their `init` function
Registry = make(map[string]Configurer)
)
// Prober is the interface for `Prober` types
type Prober interface {
// Name returns a name that uniquely identifies the monitor that
// configured this `Prober`. Used for metrics and logging
Name() string
// Kind returns a name that uniquely identifies the `Kind` of
// `Prober`. Used for metrics and logging
Kind() string
// Probe attempts the configured request or query
Probe(time.Duration) (bool, time.Duration)
}
// Configurer is the interface for `Configurer` types
type Configurer interface {
// UnmarshalSettings unmarshals YAML as bytes to the fields of the
// bound struct
UnmarshalSettings([]byte) (Configurer, error)
// Validate ensures that the unmarshalled settings are valid and
// returns errors appropriate for operator consumption when this is
// not the case
Validate() error
// AsProbe should be called last and return a `Prober` object to be
// used by an `observer.monitor`
AsProbe() Prober
}
// Settings is exported as a temporary receiver for the `settings` field
// of the yaml config. `Settings` is always marshaled back to bytes and
// then unmarshalled into the `Configurer` specified by the `kind` field
// of the `MonConf`
type Settings map[string]interface{}
// GetProbeConf returns the probe configurer specified by name from
// `observer.Registry`
func GetProbeConf(kind string, s Settings) (Configurer, error) {
if _, ok := Registry[kind]; ok {
return Registry[kind], nil
}
return nil, fmt.Errorf("%s is not a registered probe type", kind)
}
// Register is called by every `Configurer` `init` function to add the
// caller to the global `observer.Registry` map. If the caller attempts
// to add a `Configurer` to the registry using the same name as a prior
// `Configurer` the call will exit with an error
func Register(kind string, c Configurer) {
if _, ok := Registry[kind]; ok {
cmd.FailOnError(
fmt.Errorf(
"configurer: %s has already been added", kind),
"Error while initializing probes")
}
Registry[kind] = c
}