forked from trpc-ecosystem/go-metrics-prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
91 lines (77 loc) · 2.18 KB
/
plugin.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package prometheus
import (
"strings"
"trpc.group/trpc-go/trpc-go/filter"
"trpc.group/trpc-go/trpc-go/log"
"trpc.group/trpc-go/trpc-go/plugin"
)
const (
pluginType = "metrics"
pluginName = "prometheus"
)
func init() {
//register plugin.
plugin.Register(pluginName, &Plugin{})
//register filter.
filter.Register(pluginName, ServerFilter, ClientFilter)
}
// Config config struct.
type Config struct {
IP string `yaml:"ip"` //metrics monitoring address.
Port int32 `yaml:"port"` //metrics listens to the port.
Path string `yaml:"path"` //metrics path.
Namespace string `yaml:"namespace"` //formal or test.
Subsystem string `yaml:"subsystem"` //default trpc.
RawMode bool `yaml:"rawmode"` //by default, the special character in metrics will be converted.
EnablePush bool `yaml:"enablepush"` //push is not enabled by default.
Password string `yaml:"password"` //account Password.
Gateway string `yaml:"gateway"` //push gateway address.
PushInterval uint32 `yaml:"pushinterval"` //push interval,default 1s.
Job string `yaml:"job"` //reported task name.
}
// Default set default values
func (c Config) Default() *Config {
return &Config{
IP: "127.0.0.1",
Port: 8080,
Path: "/metrics",
Namespace: "Development",
Subsystem: "trpc",
RawMode: false,
EnablePush: false,
Gateway: "",
PushInterval: 1,
Job: "",
}
}
// Plugin plugin obj
type Plugin struct {
}
// Type plugin type
func (p *Plugin) Type() string {
return pluginType
}
// Setup init plugin
func (p *Plugin) Setup(name string, decoder plugin.Decoder) error {
cfg := Config{}.Default()
err := decoder.Decode(cfg)
if err != nil {
log.Errorf("trpc-metrics-prometheus:conf Decode error:%v", err)
return err
}
go func() {
err := initMetrics(cfg.IP, cfg.Port, cfg.Path)
if err != nil {
log.Errorf("trpc-metrics-prometheus:running:%v", err)
}
}()
initSink(cfg)
return nil
}
func basicAuthForPasswordOption(s string) (username, password string) {
splits := strings.Split(s, ":")
if len(splits) < 2 {
return
}
return splits[0], splits[1]
}